diff --git a/Utilities.Tests/Extensions/DictionaryExtensionsTests.cs b/Utilities.Tests/Extensions/DictionaryExtensionsTests.cs index 9f309832..73964a29 100644 --- a/Utilities.Tests/Extensions/DictionaryExtensionsTests.cs +++ b/Utilities.Tests/Extensions/DictionaryExtensionsTests.cs @@ -33,4 +33,101 @@ public void AddMany_ShouldAddAllKeyValuePairs() dictionary.Should().ContainKey("two").WhoseValue.Should().Be(2); dictionary.Should().ContainKey("three").WhoseValue.Should().Be(3); } + + [Test] + public void AddMany_ShouldNotChangeDictionary_WhenEnumerableIsEmpty() + { + var dictionary = new Dictionary { ["one"] = 1 }; + var enumerable = Array.Empty<(string, int)>(); + + dictionary.AddMany(enumerable); + + dictionary.Should().HaveCount(1); + dictionary.Should().ContainKey("one").WhoseValue.Should().Be(1); + } + + [Test] + public void AddMany_ShouldThrowArgumentNullException_WhenDictionaryIsNull() + { + Dictionary dictionary = null!; + var enumerable = new[] { ("one", 1) }; + + var action = () => dictionary.AddMany(enumerable); + + action.Should().Throw(); + } + + [Test] + public void AddMany_ShouldThrowArgumentNullException_WhenEnumerableIsNull() + { + var dictionary = new Dictionary { ["one"] = 1 }; + IEnumerable<(string, int)> enumerable = null!; + + var action = () => dictionary.AddMany(enumerable); + + action.Should().Throw(); + } + + [Test] + public void AddMany_ShouldAllowNullValues_WhenValueTypeIsNullable() + { + var dictionary = new Dictionary { ["one"] = 1 }; + var enumerable = new[] { ("two", (int?)null) }; + + dictionary.AddMany(enumerable); + + dictionary.Should().HaveCount(2); + dictionary.Should().ContainKey("two").WhoseValue.Should().Be(null); + } + + + [Test] + public void AddMany_ShouldAllowNullValue_WhenValueIsNullable() + { + var dictionary = new Dictionary(); // Key type is int, value type is nullable string + var enumerable = new[] + { + (1, null), // null value + (2, "banana") + }; + + dictionary.AddMany(enumerable); + + dictionary.Should().ContainKey(1).WhoseValue.Should().BeNull(); + dictionary.Should().ContainKey(2).WhoseValue.Should().Be("banana"); + } + + [Test] + public void AddMany_ShouldThrowArgumentException_WhenAddingDuplicateKey() + { + var dictionary = new Dictionary(); // Key type is int, value type is nullable string + var enumerable = new[] + { + (1, "Things"), // First entry + (2, "Stuff"), + (1, "That Thing") // Duplicate key (should throw exception) + }; + + var action = () => dictionary.AddMany(enumerable); + + action.Should().Throw(); // Adding a duplicate key should throw ArgumentException + } + + [Test] + public void AddMany_ShouldAddManyKeyValuePairs_WhenAddingLargeEnumerable() + { + var dictionary = new Dictionary(); + var enumerable = new List<(int, string)>(); + + // Create a large enumerable + for (int i = 0; i < 10000; i++) + { + enumerable.Add((i, "Value" + i)); + } + + dictionary.AddMany(enumerable); + + dictionary.Should().HaveCount(10000); + dictionary[9999].Should().Be("Value9999"); + } }