From 1751f1278309e0f78afb8d808090a549a2b838aa Mon Sep 17 00:00:00 2001 From: GersonJr Date: Mon, 25 Nov 2024 22:57:53 -0300 Subject: [PATCH 1/2] Added tests for null key handling in DictionaryExtensions --- .../Extensions/DictionaryExtensionsTests.cs | 99 +++++++++++++++++++ 1 file changed, 99 insertions(+) diff --git a/Utilities.Tests/Extensions/DictionaryExtensionsTests.cs b/Utilities.Tests/Extensions/DictionaryExtensionsTests.cs index 9f309832..568ff780 100644 --- a/Utilities.Tests/Extensions/DictionaryExtensionsTests.cs +++ b/Utilities.Tests/Extensions/DictionaryExtensionsTests.cs @@ -33,4 +33,103 @@ 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"); + } + + } From 21c543b0b8185903a988dca947fc62090ccef4fb Mon Sep 17 00:00:00 2001 From: Andrii Siriak Date: Tue, 26 Nov 2024 10:13:08 +0200 Subject: [PATCH 2/2] Update Utilities.Tests/Extensions/DictionaryExtensionsTests.cs --- Utilities.Tests/Extensions/DictionaryExtensionsTests.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/Utilities.Tests/Extensions/DictionaryExtensionsTests.cs b/Utilities.Tests/Extensions/DictionaryExtensionsTests.cs index 568ff780..73964a29 100644 --- a/Utilities.Tests/Extensions/DictionaryExtensionsTests.cs +++ b/Utilities.Tests/Extensions/DictionaryExtensionsTests.cs @@ -130,6 +130,4 @@ public void AddMany_ShouldAddManyKeyValuePairs_WhenAddingLargeEnumerable() dictionary.Should().HaveCount(10000); dictionary[9999].Should().Be("Value9999"); } - - }