Skip to content

Commit 31ead26

Browse files
committed
Fix + unit tests
1 parent 1d2fc6d commit 31ead26

File tree

2 files changed

+216
-8
lines changed

2 files changed

+216
-8
lines changed

src/Components/Components/src/TempData.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,13 @@ public void Clear()
108108

109109
void IDictionary<string, object?>.Add(string key, object? value)
110110
{
111-
_data.Add(key, value);
112-
_retainedKeys.Add(key);
111+
this[key] = value;
113112
}
114113

115114
bool IDictionary<string, object?>.TryGetValue(string key, out object? value)
116115
{
117-
_retainedKeys.Remove(key);
118-
return _data.TryGetValue(key, out value);
116+
value = Get(key);
117+
return ContainsKey(key);
119118
}
120119

121120
void ICollection<KeyValuePair<string, object?>>.Add(KeyValuePair<string, object?> item)
@@ -125,7 +124,7 @@ public void Clear()
125124

126125
bool ICollection<KeyValuePair<string, object?>>.Contains(KeyValuePair<string, object?> item)
127126
{
128-
return ContainsKey(item.Key) && _data[item.Key] == item.Value;
127+
return ContainsKey(item.Key) && Equals(Peek(item.Key), item.Value);
129128
}
130129

131130
void ICollection<KeyValuePair<string, object?>>.CopyTo(KeyValuePair<string, object?>[] array, int arrayIndex)
@@ -135,10 +134,9 @@ public void Clear()
135134

136135
bool ICollection<KeyValuePair<string, object?>>.Remove(KeyValuePair<string, object?> item)
137136
{
138-
if (((ICollection<KeyValuePair<string, object?>>)_data).Remove(item))
137+
if (ContainsKey(item.Key) && Equals(Peek(item.Key), item.Value))
139138
{
140-
_retainedKeys.Remove(item.Key);
141-
return true;
139+
return Remove(item.Key);
142140
}
143141
return false;
144142
}
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Microsoft.AspNetCore.Components;
5+
6+
public class TempDataTest
7+
{
8+
[Fact]
9+
public void Indexer_CanSetAndGetValues()
10+
{
11+
var tempData = new TempData();
12+
tempData["Key1"] = "Value1";
13+
var value = tempData["Key1"];
14+
Assert.Equal("Value1", value);
15+
}
16+
17+
[Fact]
18+
public void Get_ReturnsValueAndRemovesFromRetainedKeys()
19+
{
20+
var tempData = new TempData();
21+
tempData["Key1"] = "Value1";
22+
23+
var value = tempData.Get("Key1");
24+
25+
Assert.Equal("Value1", value);
26+
var saved = tempData.Save();
27+
Assert.Empty(saved);
28+
}
29+
30+
[Fact]
31+
public void Get_ReturnsNullForNonExistentKey()
32+
{
33+
var tempData = new TempData();
34+
var value = tempData.Get("NonExistent");
35+
Assert.Null(value);
36+
}
37+
38+
[Fact]
39+
public void Peek_ReturnsValueWithoutRemovingFromRetainedKeys()
40+
{
41+
var tempData = new TempData();
42+
tempData["Key1"] = "Value1";
43+
var value = tempData.Peek("Key1");
44+
Assert.Equal("Value1", value);
45+
value = tempData.Get("Key1");
46+
Assert.Equal("Value1", value);
47+
}
48+
49+
[Fact]
50+
public void Peek_ReturnsNullForNonExistentKey()
51+
{
52+
var tempData = new TempData();
53+
var value = tempData.Peek("NonExistent");
54+
Assert.Null(value);
55+
}
56+
57+
[Fact]
58+
public void Keep_RetainsAllKeys()
59+
{
60+
var tempData = new TempData();
61+
tempData["Key1"] = "Value1";
62+
tempData["Key2"] = "Value2";
63+
_ = tempData.Get("Key1");
64+
_ = tempData.Get("Key2");
65+
66+
tempData.Keep();
67+
68+
var value1 = tempData.Get("Key1");
69+
var value2 = tempData.Get("Key2");
70+
Assert.Equal("Value1", value1);
71+
Assert.Equal("Value2", value2);
72+
}
73+
74+
[Fact]
75+
public void KeepWithKey_RetainsSpecificKey()
76+
{
77+
var tempData = new TempData();
78+
tempData["Key1"] = "Value1";
79+
tempData["Key2"] = "Value2";
80+
_ = tempData.Get("Key1");
81+
_ = tempData.Get("Key2");
82+
83+
tempData.Keep("Key1");
84+
85+
var saved = tempData.Save();
86+
Assert.Single(saved);
87+
Assert.Equal("Value1", saved["Key1"]);
88+
}
89+
90+
[Fact]
91+
public void KeepWithKey_DoesNothingForNonExistentKey()
92+
{
93+
var tempData = new TempData();
94+
tempData["Key1"] = "Value1";
95+
_ = tempData.Get("Key1");
96+
97+
tempData.Keep("NonExistent");
98+
99+
var value = tempData.Get("NonExistent");
100+
Assert.Null(value);
101+
}
102+
103+
[Fact]
104+
public void ContainsKey_ReturnsTrueForExistingKey()
105+
{
106+
var tempData = new TempData();
107+
tempData["Key1"] = "Value1";
108+
var result = tempData.ContainsKey("Key1");
109+
Assert.True(result);
110+
}
111+
112+
[Fact]
113+
public void ContainsKey_ReturnsFalseForNonExistentKey()
114+
{
115+
var tempData = new TempData();
116+
var result = tempData.ContainsKey("NonExistent");
117+
Assert.False(result);
118+
}
119+
120+
[Fact]
121+
public void Remove_RemovesKeyAndReturnsTrue()
122+
{
123+
var tempData = new TempData();
124+
tempData["Key1"] = "Value1";
125+
126+
var result = tempData.Remove("Key1");
127+
128+
Assert.True(result);
129+
var value = tempData.Get("Key1");
130+
Assert.Null(value);
131+
}
132+
133+
[Fact]
134+
public void Remove_ReturnsFalseForNonExistentKey()
135+
{
136+
var tempData = new TempData();
137+
var result = tempData.Remove("NonExistent");
138+
Assert.False(result);
139+
}
140+
141+
[Fact]
142+
public void Save_ReturnsOnlyRetainedKeys()
143+
{
144+
var tempData = new TempData();
145+
tempData["Key1"] = "Value1";
146+
tempData["Key2"] = "Value2";
147+
tempData["Key3"] = "Value3";
148+
_ = tempData.Get("Key1");
149+
_ = tempData.Get("Key2");
150+
151+
var saved = tempData.Save();
152+
153+
Assert.Single(saved);
154+
Assert.Equal("Value3", saved["Key3"]);
155+
}
156+
157+
[Fact]
158+
public void Load_PopulatesDataFromDictionary()
159+
{
160+
var tempData = new TempData();
161+
var dataToLoad = new Dictionary<string, object?>
162+
{
163+
["Key1"] = "Value1",
164+
["Key2"] = "Value2"
165+
};
166+
167+
tempData.Load(dataToLoad);
168+
169+
Assert.Equal("Value1", tempData.Get("Key1"));
170+
Assert.Equal("Value2", tempData.Get("Key2"));
171+
}
172+
173+
[Fact]
174+
public void Load_ClearsExistingDataBeforeLoading()
175+
{
176+
var tempData = new TempData();
177+
tempData["ExistingKey"] = "ExistingValue";
178+
var dataToLoad = new Dictionary<string, object?>
179+
{
180+
["NewKey"] = "NewValue"
181+
};
182+
183+
tempData.Load(dataToLoad);
184+
185+
Assert.False(tempData.ContainsKey("ExistingKey"));
186+
Assert.True(tempData.ContainsKey("NewKey"));
187+
}
188+
189+
[Fact]
190+
public void Clear_RemovesAllData()
191+
{
192+
var tempData = new TempData();
193+
tempData["Key1"] = "Value1";
194+
tempData["Key2"] = "Value2";
195+
196+
tempData.Clear();
197+
198+
Assert.Null(tempData.Get("Key1"));
199+
Assert.Null(tempData.Get("Key2"));
200+
}
201+
202+
[Fact]
203+
public void Indexer_IsCaseInsensitive()
204+
{
205+
var tempData = new TempData();
206+
tempData["Key1"] = "Value1";
207+
var value = tempData["KEY1"];
208+
Assert.Equal("Value1", value);
209+
}
210+
}

0 commit comments

Comments
 (0)