Added tests for null key handling in DictionaryExtensions #498
+97
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request introduces the following changes to the DictionaryExtensions tests:
Test for Null Key Handling: Although an attempt was made to test how the AddMany method handles null keys when the key type is nullable (string?), it was not possible to implement this test. C# dictionaries do not allow null as a key, even for nullable key types like string?, because of the notnull constraint on the TKey parameter in the Dictionary class. The notnull constraint prevents nullable types like string? from being used as dictionary keys, and as a result, this test could not be completed due to the C# language limitation.
Test for Nullable Value Types: Included tests to verify that nullable value types (such as int?) are correctly handled by the AddMany method, ensuring that null can be assigned as a value.
Test for Duplicate Keys: Added a test case to check that an ArgumentException is thrown when attempting to add duplicate keys to the dictionary using AddMany.
Edge Case Coverage: Ensured edge cases, such as adding large collections of key-value pairs, are properly handled.
Motivation and Context:
The existing tests did not adequately cover scenarios involving null keys or nullable value types. This change is necessary to ensure that the AddMany method works as expected in edge cases, improving the overall test coverage.
The additional checks for duplicate keys ensure that the method behaves correctly when invalid inputs (such as duplicate keys) are provided, preventing potential runtime errors.