Skip to content

Commit 0184c37

Browse files
authored
Add tests for null key handling in DictionaryExtensions (#498)
1 parent c4091c7 commit 0184c37

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

Utilities.Tests/Extensions/DictionaryExtensionsTests.cs

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,101 @@ public void AddMany_ShouldAddAllKeyValuePairs()
3333
dictionary.Should().ContainKey("two").WhoseValue.Should().Be(2);
3434
dictionary.Should().ContainKey("three").WhoseValue.Should().Be(3);
3535
}
36+
37+
[Test]
38+
public void AddMany_ShouldNotChangeDictionary_WhenEnumerableIsEmpty()
39+
{
40+
var dictionary = new Dictionary<string, int> { ["one"] = 1 };
41+
var enumerable = Array.Empty<(string, int)>();
42+
43+
dictionary.AddMany(enumerable);
44+
45+
dictionary.Should().HaveCount(1);
46+
dictionary.Should().ContainKey("one").WhoseValue.Should().Be(1);
47+
}
48+
49+
[Test]
50+
public void AddMany_ShouldThrowArgumentNullException_WhenDictionaryIsNull()
51+
{
52+
Dictionary<string, int> dictionary = null!;
53+
var enumerable = new[] { ("one", 1) };
54+
55+
var action = () => dictionary.AddMany(enumerable);
56+
57+
action.Should().Throw<NullReferenceException>();
58+
}
59+
60+
[Test]
61+
public void AddMany_ShouldThrowArgumentNullException_WhenEnumerableIsNull()
62+
{
63+
var dictionary = new Dictionary<string, int> { ["one"] = 1 };
64+
IEnumerable<(string, int)> enumerable = null!;
65+
66+
var action = () => dictionary.AddMany(enumerable);
67+
68+
action.Should().Throw<NullReferenceException>();
69+
}
70+
71+
[Test]
72+
public void AddMany_ShouldAllowNullValues_WhenValueTypeIsNullable()
73+
{
74+
var dictionary = new Dictionary<string, int?> { ["one"] = 1 };
75+
var enumerable = new[] { ("two", (int?)null) };
76+
77+
dictionary.AddMany(enumerable);
78+
79+
dictionary.Should().HaveCount(2);
80+
dictionary.Should().ContainKey("two").WhoseValue.Should().Be(null);
81+
}
82+
83+
84+
[Test]
85+
public void AddMany_ShouldAllowNullValue_WhenValueIsNullable()
86+
{
87+
var dictionary = new Dictionary<int, string?>(); // Key type is int, value type is nullable string
88+
var enumerable = new[]
89+
{
90+
(1, null), // null value
91+
(2, "banana")
92+
};
93+
94+
dictionary.AddMany(enumerable);
95+
96+
dictionary.Should().ContainKey(1).WhoseValue.Should().BeNull();
97+
dictionary.Should().ContainKey(2).WhoseValue.Should().Be("banana");
98+
}
99+
100+
[Test]
101+
public void AddMany_ShouldThrowArgumentException_WhenAddingDuplicateKey()
102+
{
103+
var dictionary = new Dictionary<int, string>(); // Key type is int, value type is nullable string
104+
var enumerable = new[]
105+
{
106+
(1, "Things"), // First entry
107+
(2, "Stuff"),
108+
(1, "That Thing") // Duplicate key (should throw exception)
109+
};
110+
111+
var action = () => dictionary.AddMany(enumerable);
112+
113+
action.Should().Throw<ArgumentException>(); // Adding a duplicate key should throw ArgumentException
114+
}
115+
116+
[Test]
117+
public void AddMany_ShouldAddManyKeyValuePairs_WhenAddingLargeEnumerable()
118+
{
119+
var dictionary = new Dictionary<int, string>();
120+
var enumerable = new List<(int, string)>();
121+
122+
// Create a large enumerable
123+
for (int i = 0; i < 10000; i++)
124+
{
125+
enumerable.Add((i, "Value" + i));
126+
}
127+
128+
dictionary.AddMany(enumerable);
129+
130+
dictionary.Should().HaveCount(10000);
131+
dictionary[9999].Should().Be("Value9999");
132+
}
36133
}

0 commit comments

Comments
 (0)