Skip to content

Commit c9a41b7

Browse files
Allow set via key
1 parent 9cb3584 commit c9a41b7

2 files changed

Lines changed: 52 additions & 4 deletions

File tree

src/ImageSharp.Web/Commands/CommandCollection.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,13 @@ private CommandCollection(IEqualityComparer<string> comparer)
3333
public ICollection<string> Keys => this.keys;
3434

3535
/// <summary>
36-
/// Gets the command value with the specified key.
36+
/// Gets or sets the value associated with the specified key.
3737
/// </summary>
38-
/// <param name="key">The key of the element to get.</param>
38+
/// <param name="key">The key of the value to get or set.</param>
3939
/// <returns>
40-
/// The command value with the specified key. If a value with the specified key is not
41-
/// found, an exception is thrown.
40+
/// The value associated with the specified key. If the specified key is not found,
41+
/// a get operation throws a <see cref="KeyNotFoundException"/>, and
42+
/// a set operation creates a new element with the specified key.
4243
/// </returns>
4344
/// <exception cref="ArgumentNullException"><paramref name="key"/> is null.</exception>
4445
/// <exception cref="KeyNotFoundException">An element with the specified key does not exist in the collection.</exception>
@@ -53,6 +54,17 @@ private CommandCollection(IEqualityComparer<string> comparer)
5354

5455
throw new KeyNotFoundException();
5556
}
57+
58+
set
59+
{
60+
if (this.TryGetValue(key, out KeyValuePair<string, string> item))
61+
{
62+
this.SetItem(this.IndexOf(item), new(key, value));
63+
return;
64+
}
65+
66+
throw new KeyNotFoundException();
67+
}
5668
}
5769

5870
/// <inheritdoc/>

tests/ImageSharp.Web.Tests/Commands/CommandCollectionTests.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,42 @@ public void CanInsertCommands()
4848
Assert.Equal(0, collection.IndexOf(kv2));
4949
}
5050

51+
[Fact]
52+
public void CanSetCommandsViaIndex()
53+
{
54+
KeyValuePair<string, string> kv1 = new("a", "b");
55+
KeyValuePair<string, string> kv2 = new("c", "d");
56+
CommandCollection collection = new();
57+
58+
collection.Add(kv1);
59+
Assert.Single(collection);
60+
Assert.Equal(0, collection.IndexOf(kv1));
61+
Assert.Equal(kv1.Value, collection[kv1.Key]);
62+
63+
collection[0] = kv2;
64+
Assert.Single(collection);
65+
Assert.Equal(0, collection.IndexOf(kv2));
66+
Assert.Equal(kv2.Value, collection[kv2.Key]);
67+
}
68+
69+
[Fact]
70+
public void CanSetCommandsViaKey()
71+
{
72+
KeyValuePair<string, string> kv1 = new("a", "b");
73+
KeyValuePair<string, string> kv2 = new("a", "d");
74+
CommandCollection collection = new();
75+
76+
collection.Add(kv1);
77+
Assert.Single(collection);
78+
Assert.Equal(0, collection.IndexOf(kv1));
79+
Assert.Equal(kv1.Value, collection[kv1.Key]);
80+
81+
collection[kv1.Key] = kv2.Value;
82+
Assert.Single(collection);
83+
Assert.Equal(0, collection.IndexOf(kv2));
84+
Assert.Equal(kv2.Value, collection[kv2.Key]);
85+
}
86+
5187
[Fact]
5288
public void CanRemoveCommands()
5389
{

0 commit comments

Comments
 (0)