Skip to content

Commit ad7324c

Browse files
Use IEnumerable Keys and fix indexer
1 parent eac254b commit ad7324c

2 files changed

Lines changed: 41 additions & 35 deletions

File tree

src/ImageSharp.Web/Commands/CommandCollection.cs

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ namespace SixLabors.ImageSharp.Web.Commands
1212
/// </summary>
1313
public sealed class CommandCollection : KeyedCollection<string, KeyValuePair<string, string>>
1414
{
15-
private readonly List<string> keys = new();
16-
1715
/// <summary>
1816
/// Initializes a new instance of the <see cref="CommandCollection"/> class.
1917
/// </summary>
@@ -28,9 +26,18 @@ private CommandCollection(IEqualityComparer<string> comparer)
2826
}
2927

3028
/// <summary>
31-
/// Gets an <see cref="ICollection{T}"/> representing the keys of the collection.
29+
/// Gets an <see cref="IEnumerable{String}"/> representing the keys of the collection.
3230
/// </summary>
33-
public ICollection<string> Keys => this.keys;
31+
public IEnumerable<string> Keys
32+
{
33+
get
34+
{
35+
foreach (KeyValuePair<string, string> item in this)
36+
{
37+
yield return this.GetKeyForItem(item);
38+
}
39+
}
40+
}
3441

3542
/// <summary>
3643
/// Gets or sets the value associated with the specified key.
@@ -60,41 +67,14 @@ private CommandCollection(IEqualityComparer<string> comparer)
6067
if (this.TryGetValue(key, out KeyValuePair<string, string> item))
6168
{
6269
this.SetItem(this.IndexOf(item), new(key, value));
63-
return;
6470
}
65-
66-
throw new KeyNotFoundException();
71+
else
72+
{
73+
this.Add(new(key, value));
74+
}
6775
}
6876
}
6977

70-
/// <inheritdoc/>
71-
protected override void InsertItem(int index, KeyValuePair<string, string> item)
72-
{
73-
base.InsertItem(index, item);
74-
this.keys.Insert(index, item.Key);
75-
}
76-
77-
/// <inheritdoc/>
78-
protected override void RemoveItem(int index)
79-
{
80-
base.RemoveItem(index);
81-
this.keys.RemoveAt(index);
82-
}
83-
84-
/// <inheritdoc/>
85-
protected override void SetItem(int index, KeyValuePair<string, string> item)
86-
{
87-
base.SetItem(index, item);
88-
this.keys[index] = item.Key;
89-
}
90-
91-
/// <inheritdoc/>
92-
protected override void ClearItems()
93-
{
94-
base.ClearItems();
95-
this.keys.Clear();
96-
}
97-
9878
/// <inheritdoc/>
9979
protected override string GetKeyForItem(KeyValuePair<string, string> item) => item.Key;
10080
}

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,5 +136,31 @@ public void CanClearCommands()
136136

137137
Assert.Empty(collection);
138138
}
139+
140+
[Fact]
141+
public void KeysAreOrdered()
142+
{
143+
string[] keys = new[] { "a", "b", "c", "d" };
144+
145+
CommandCollection collection = new();
146+
foreach (string key in keys)
147+
{
148+
collection.Insert(0, new(key, null));
149+
}
150+
151+
int counter = keys.Length - 1;
152+
foreach (string key in collection.Keys)
153+
{
154+
Assert.Equal(keys[counter--], key);
155+
}
156+
}
157+
158+
[Fact]
159+
public void GetByInvalidKeyThrowsCorrectly()
160+
=> Assert.Throws<KeyNotFoundException>(() =>
161+
{
162+
CommandCollection collection = new();
163+
return collection["a"];
164+
});
139165
}
140166
}

0 commit comments

Comments
 (0)