-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/KeyThrowingDictionary'
* feature/KeyThrowingDictionary: Update readme New KeyThrowingDictionary Missing unit tests for Dictionary indexer setter Fixed - IDictionary contract is to throw ArgumentNullException on null key
- Loading branch information
Showing
7 changed files
with
307 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace BoringHelpers.Collections | ||
{ | ||
public class KeyThrowingDictionary<TKey, TValue> : Dictionary<TKey, TValue> | ||
{ | ||
public new TValue this[TKey key] | ||
{ | ||
get | ||
{ | ||
if (this.TryGetValue(key, out var result)) return result; | ||
else | ||
{ | ||
//key cannot be null, TryGetValue throws ArgumentNullException | ||
throw new KeyNotFoundException($"Key `{key}` not found"); | ||
} | ||
} | ||
set | ||
{ | ||
try | ||
{ | ||
base[key] = value; | ||
} | ||
catch (KeyNotFoundException ex) | ||
{ | ||
throw new KeyNotFoundException($"Key `{key}` not found", ex); | ||
} | ||
} | ||
} | ||
|
||
public new void Add(TKey key, TValue value) | ||
{ | ||
try | ||
{ | ||
base.Add(key, value); | ||
} | ||
catch (ArgumentException ex) | ||
{ | ||
//key cannot be null, TryGetValue throws ArgumentNullException | ||
throw new ArgumentException($"Key `{key}` already exists", ex); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
173 changes: 173 additions & 0 deletions
173
BoringHelpersTests/Collections/KeyThrowingDictionary.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
using Xunit; | ||
using System.Collections.Generic; | ||
using BoringHelpers.Collections; | ||
using System; | ||
|
||
|
||
namespace BoringHelpersTests.Collections | ||
{ | ||
public class KeyThrowingDictionary | ||
{ | ||
[Theory] | ||
[InlineData("{6F9E54B5-6E93-44B3-8C0A-C2D0B86268A0}")] | ||
[InlineData("{4F7ECDAB-3BC9-49DF-8989-0B45C359455B}")] | ||
[InlineData("{241517B8-57B7-4242-9770-CFDB197B147A}")] | ||
public void Contains(string input) | ||
{ | ||
KeyThrowingDictionary<string, bool> dict = new KeyThrowingDictionary<string, bool>(); | ||
dict.Add(input, true); | ||
Assert.True(dict.ContainsKey(input)); | ||
} | ||
|
||
[Theory] | ||
[InlineData("{6F9E54B5-6E93-44B3-8C0A-C2D0B86268A0}")] | ||
[InlineData("{4F7ECDAB-3BC9-49DF-8989-0B45C359455B}")] | ||
[InlineData("{241517B8-57B7-4242-9770-CFDB197B147A}")] | ||
public void Contains_Dictionary(string input) | ||
{ | ||
Dictionary<string, bool> dict = new KeyThrowingDictionary<string, bool>(); | ||
dict.Add(input, true); | ||
Assert.True(dict.ContainsKey(input)); | ||
} | ||
|
||
[Theory] | ||
[InlineData("{6F9E54B5-6E93-44B3-8C0A-C2D0B86268A0}")] | ||
[InlineData("{4F7ECDAB-3BC9-49DF-8989-0B45C359455B}")] | ||
[InlineData("{241517B8-57B7-4242-9770-CFDB197B147A}")] | ||
public void Contains_IDictionary(string input) | ||
{ | ||
IDictionary<string, bool> dict = new KeyThrowingDictionary<string, bool>(); | ||
dict.Add(input, true); | ||
Assert.True(dict.ContainsKey(input)); | ||
} | ||
|
||
[Theory] | ||
[InlineData("{6F9E54B5-6E93-44B3-8C0A-C2D0B86268A0}")] | ||
[InlineData("{4F7ECDAB-3BC9-49DF-8989-0B45C359455B}")] | ||
[InlineData("{241517B8-57B7-4242-9770-CFDB197B147A}")] | ||
public void DoubleAdd(string input) | ||
{ | ||
KeyThrowingDictionary<string, bool> dict = new KeyThrowingDictionary<string, bool>(); | ||
dict.Add(input, true); | ||
Assert.Throws<ArgumentException>(() => dict.Add(input, true)); | ||
try | ||
{ | ||
dict.Add(input, true); | ||
Assert.True(false); | ||
} | ||
catch(ArgumentException ex) | ||
{ | ||
Assert.Contains(input.ToString(), ex.Message); | ||
return; | ||
} | ||
Assert.True(false); | ||
} | ||
|
||
[Theory] | ||
[InlineData("{6F9E54B5-6E93-44B3-8C0A-C2D0B86268A0}")] | ||
[InlineData("{4F7ECDAB-3BC9-49DF-8989-0B45C359455B}")] | ||
[InlineData("{241517B8-57B7-4242-9770-CFDB197B147A}")] | ||
public void DoubleAdd_Dictionary(string input) | ||
{ | ||
Dictionary<string, bool> dict = new KeyThrowingDictionary<string, bool>(); | ||
dict.Add(input, true); | ||
Assert.Throws<ArgumentException>(() => dict.Add(input, true)); | ||
try | ||
{ | ||
dict.Add(input, true); | ||
Assert.True(false); | ||
} | ||
catch (ArgumentException ex) | ||
{ | ||
Assert.Contains(input.ToString(), ex.Message); | ||
return; | ||
} | ||
Assert.True(false); | ||
} | ||
|
||
[Theory] | ||
[InlineData("{6F9E54B5-6E93-44B3-8C0A-C2D0B86268A0}")] | ||
[InlineData("{4F7ECDAB-3BC9-49DF-8989-0B45C359455B}")] | ||
[InlineData("{241517B8-57B7-4242-9770-CFDB197B147A}")] | ||
public void DoubleAdd_IDictionary(string input) | ||
{ | ||
IDictionary<string, bool> dict = new KeyThrowingDictionary<string, bool>(); | ||
dict.Add(input, true); | ||
Assert.Throws<ArgumentException>(() => dict.Add(input, true)); | ||
try | ||
{ | ||
dict.Add(input, true); | ||
Assert.True(false); | ||
} | ||
catch (ArgumentException ex) | ||
{ | ||
Assert.Contains(input.ToString(), ex.Message); | ||
return; | ||
} | ||
Assert.True(false); | ||
} | ||
|
||
[Theory] | ||
[InlineData("{6F9E54B5-6E93-44B3-8C0A-C2D0B86268A0}")] | ||
[InlineData("{4F7ECDAB-3BC9-49DF-8989-0B45C359455B}")] | ||
[InlineData("{241517B8-57B7-4242-9770-CFDB197B147A}")] | ||
public void Missing(string input) | ||
{ | ||
KeyThrowingDictionary<string, bool> dict = new KeyThrowingDictionary<string, bool>(); | ||
Assert.Throws<KeyNotFoundException>(() => dict[input]); | ||
try | ||
{ | ||
var discard = dict[input]; | ||
Assert.True(false); | ||
} | ||
catch (KeyNotFoundException ex) | ||
{ | ||
Assert.Contains(input.ToString(), ex.Message); | ||
return; | ||
} | ||
Assert.True(false); | ||
} | ||
|
||
[Theory] | ||
[InlineData("{6F9E54B5-6E93-44B3-8C0A-C2D0B86268A0}")] | ||
[InlineData("{4F7ECDAB-3BC9-49DF-8989-0B45C359455B}")] | ||
[InlineData("{241517B8-57B7-4242-9770-CFDB197B147A}")] | ||
public void Missing_Dictionary(string input) | ||
{ | ||
Dictionary<string, bool> dict = new KeyThrowingDictionary<string, bool>(); | ||
Assert.Throws<KeyNotFoundException>(() => dict[input]); | ||
try | ||
{ | ||
var discard = dict[input]; | ||
Assert.True(false); | ||
} | ||
catch (KeyNotFoundException ex) | ||
{ | ||
Assert.Contains(input.ToString(), ex.Message); | ||
return; | ||
} | ||
Assert.True(false); | ||
} | ||
|
||
[Theory] | ||
[InlineData("{6F9E54B5-6E93-44B3-8C0A-C2D0B86268A0}")] | ||
[InlineData("{4F7ECDAB-3BC9-49DF-8989-0B45C359455B}")] | ||
[InlineData("{241517B8-57B7-4242-9770-CFDB197B147A}")] | ||
public void Missing_IDictionary(string input) | ||
{ | ||
IDictionary<string, bool> dict = new KeyThrowingDictionary<string, bool>(); | ||
Assert.Throws<KeyNotFoundException>(() => dict[input]); | ||
try | ||
{ | ||
var discard = dict[input]; | ||
Assert.True(false); | ||
} | ||
catch (KeyNotFoundException ex) | ||
{ | ||
Assert.Contains(input.ToString(), ex.Message); | ||
return; | ||
} | ||
Assert.True(false); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters