Repository navigation
Report the parameter's name for a null map key [patch] #147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
matt-edmondson
wants to merge
2
commits into
main
Choose a base branch
from
fix/null-key-param-name-146
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,103 @@ | ||
| // Copyright (c) 2023-2026 ktsu-dev contributors | ||
|
|
||
| namespace ktsu.Containers.Tests; | ||
|
|
||
| using System.Collections; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
|
||
| /// <summary> | ||
| /// A null key reports the parameter's own name, as <see cref="Dictionary{TKey, TValue}"/> does, | ||
| /// not the text of the cast that checks it (ktsu-dev/Containers#146). | ||
| /// </summary> | ||
| [TestClass] | ||
| public class NullKeyParamNameTests | ||
| { | ||
| private static void AssertParamName(string expected, Action action) | ||
| { | ||
| ArgumentNullException exception = Assert.ThrowsExactly<ArgumentNullException>(action); | ||
| Assert.AreEqual(expected, exception.ParamName); | ||
| } | ||
|
|
||
| private static void AssertParamName(string expected, Func<object?> func) => | ||
| AssertParamName(expected, () => { _ = func(); }); | ||
|
|
||
| private static bool KeysContain(ICollection<string> keys) => keys.Contains(null!); | ||
|
|
||
| [TestMethod] | ||
| public void ContiguousMap_NullKey_ReportsParameterName() | ||
| { | ||
| ContiguousMap<string, int> map = new() { ["a"] = 1 }; | ||
|
|
||
| AssertParamName("key", () => map[null!]); | ||
| AssertParamName("key", () => map[null!] = 1); | ||
| AssertParamName("key", () => map.Add(null!, 1)); | ||
| AssertParamName("key", () => map.Remove(null!)); | ||
| AssertParamName("key", () => map.ContainsKey(null!)); | ||
| AssertParamName("key", () => map.TryGetValue(null!, out _)); | ||
| AssertParamName("item", () => KeysContain(map.Keys)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void InsertionOrderMap_NullKey_ReportsParameterName() | ||
| { | ||
| InsertionOrderMap<string, int> map = new() { ["a"] = 1 }; | ||
|
|
||
| AssertParamName("key", () => map[null!]); | ||
| AssertParamName("key", () => map[null!] = 1); | ||
| AssertParamName("key", () => map.Add(null!, 1)); | ||
| AssertParamName("key", () => map.Remove(null!)); | ||
| AssertParamName("key", () => map.ContainsKey(null!)); | ||
| AssertParamName("key", () => map.TryGetValue(null!, out _)); | ||
| AssertParamName("item", () => KeysContain(map.Keys)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void OrderedMap_NullKey_ReportsParameterName() | ||
| { | ||
| OrderedMap<string, int> map = new() { ["a"] = 1 }; | ||
|
|
||
| AssertParamName("key", () => map[null!]); | ||
| AssertParamName("key", () => map[null!] = 1); | ||
| AssertParamName("key", () => map.Add(null!, 1)); | ||
| AssertParamName("key", () => map.Remove(null!)); | ||
| AssertParamName("key", () => map.ContainsKey(null!)); | ||
| AssertParamName("key", () => map.TryGetValue(null!, out _)); | ||
| AssertParamName("item", () => map.Contains(new KeyValuePair<string, int>(null!, 1))); | ||
| AssertParamName("item", () => map.Remove(new KeyValuePair<string, int>(null!, 1))); | ||
| AssertParamName("item", () => KeysContain(map.Keys)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void OrderedMap_DictionaryConstructorWithNullKey_ReportsDictionary() | ||
| { | ||
| NullKeyDictionary source = new(); | ||
|
|
||
| AssertParamName("dictionary", () => new OrderedMap<string, int>(source)); | ||
| AssertParamName("dictionary", () => new OrderedMap<string, int>(source, StringComparer.Ordinal)); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// A dictionary that enumerates one pair with a null key, which <see cref="Dictionary{TKey, TValue}"/> can't hold. | ||
| /// </summary> | ||
| private sealed class NullKeyDictionary : IDictionary<string, int> | ||
| { | ||
| private readonly List<KeyValuePair<string, int>> pairs = [new(null!, 1)]; | ||
|
|
||
| public int this[string key] { get => throw new NotSupportedException(); set => throw new NotSupportedException(); } | ||
| public ICollection<string> Keys => throw new NotSupportedException(); | ||
| public ICollection<int> Values => throw new NotSupportedException(); | ||
| public int Count => pairs.Count; | ||
| public bool IsReadOnly => true; | ||
| public void Add(string key, int value) => throw new NotSupportedException(); | ||
| public void Add(KeyValuePair<string, int> item) => throw new NotSupportedException(); | ||
| public void Clear() => throw new NotSupportedException(); | ||
| public bool Contains(KeyValuePair<string, int> item) => throw new NotSupportedException(); | ||
| public bool ContainsKey(string key) => throw new NotSupportedException(); | ||
| public void CopyTo(KeyValuePair<string, int>[] array, int arrayIndex) => pairs.CopyTo(array, arrayIndex); | ||
| public IEnumerator<KeyValuePair<string, int>> GetEnumerator() => pairs.GetEnumerator(); | ||
| public bool Remove(string key) => throw new NotSupportedException(); | ||
| public bool Remove(KeyValuePair<string, int> item) => throw new NotSupportedException(); | ||
| public bool TryGetValue(string key, out int value) => throw new NotSupportedException(); | ||
| IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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,36 @@ | ||
| // Copyright (c) 2023-2026 ktsu-dev contributors | ||
|
|
||
| namespace ktsu.Containers; | ||
|
|
||
| /// <summary> | ||
| /// Rejects a null key for a map whose key type is unconstrained, naming the parameter the caller passed it as. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <see cref="Ensure.NotNull{T}(T, string)"/> takes its name from <c>[CallerArgumentExpression]</c>, which records | ||
| /// the whole argument text. At a call site, the <c>(object?)</c> cast an unconstrained key needs would end up in | ||
| /// <see cref="ArgumentException.ParamName"/>. Each check here takes the key through a parameter named for what | ||
| /// <see cref="Dictionary{TKey, TValue}"/> reports, so the recorded expression is exactly that name. | ||
| /// </remarks> | ||
| internal static class NullKey | ||
| { | ||
| /// <summary> | ||
| /// Throws when a key passed as <c>key</c> is null. | ||
| /// </summary> | ||
| /// <param name="key">The key to check.</param> | ||
| /// <exception cref="ArgumentNullException">Thrown, with a ParamName of <c>key</c>, when <paramref name="key"/> is null.</exception> | ||
| internal static void ThrowIfNull(object? key) => Ensure.NotNull(key); | ||
|
|
||
| /// <summary> | ||
| /// Throws when a key passed as <c>item</c>, as to a key collection's <c>Contains</c>, is null. | ||
| /// </summary> | ||
| /// <param name="item">The key to check.</param> | ||
| /// <exception cref="ArgumentNullException">Thrown, with a ParamName of <c>item</c>, when <paramref name="item"/> is null.</exception> | ||
| internal static void ThrowIfNullItem(object? item) => Ensure.NotNull(item); | ||
|
|
||
| /// <summary> | ||
| /// Throws when a key read from a source dictionary passed as <c>dictionary</c> is null. | ||
| /// </summary> | ||
| /// <param name="dictionary">The key to check.</param> | ||
| /// <exception cref="ArgumentNullException">Thrown, with a ParamName of <c>dictionary</c>, when <paramref name="dictionary"/> is null.</exception> | ||
| internal static void ThrowIfNullIn(object? dictionary) => Ensure.NotNull(dictionary); | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is intentional.
NullKeyDictionaryis a test-only stub with one job: to enumerate a pair with a null key into theOrderedMapconstructors, because a realDictionarycan't hold one. The constructors only read it throughCountand enumeration, so every other member, the indexer setter included, throwsNotSupportedExceptioninstead of pretending to work. Leaving it as is.Generated by Claude Code