|
| 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