|
| 1 | +namespace TUnit.Assertions.Tests; |
| 2 | + |
| 3 | +/// <summary> |
| 4 | +/// Tests to ensure that Assert.That() accepts nullable collection types without generating |
| 5 | +/// nullability warnings (CS8604, CS8625, etc.). This validates the fix for GitHub issue reporting |
| 6 | +/// false positive nullability warnings for List, IList, Dictionary, IDictionary types. |
| 7 | +/// </summary> |
| 8 | +public class CollectionNullabilityWarningTests |
| 9 | +{ |
| 10 | + // =================================== |
| 11 | + // List<T>? Tests |
| 12 | + // =================================== |
| 13 | + |
| 14 | + [Test] |
| 15 | + public async Task NullableList_AcceptsNullableValue_NoWarning() |
| 16 | + { |
| 17 | + List<string>? list = ["a", "b", "c"]; |
| 18 | + await Assert.That(list).IsNotNull(); |
| 19 | + } |
| 20 | + |
| 21 | + [Test] |
| 22 | + public async Task NullableList_WithNullValue_IsNotNull_Fails() |
| 23 | + { |
| 24 | + List<string>? list = null; |
| 25 | + var action = async () => await Assert.That(list).IsNotNull(); |
| 26 | + await Assert.That(action).Throws<AssertionException>(); |
| 27 | + } |
| 28 | + |
| 29 | + [Test] |
| 30 | + public async Task NullableList_WithNullValue_IsNull_Passes() |
| 31 | + { |
| 32 | + List<string>? list = null; |
| 33 | + await Assert.That(list).IsNull(); |
| 34 | + } |
| 35 | + |
| 36 | + // =================================== |
| 37 | + // IList<T>? Tests |
| 38 | + // =================================== |
| 39 | + |
| 40 | + [Test] |
| 41 | + public async Task NullableIList_AcceptsNullableValue_NoWarning() |
| 42 | + { |
| 43 | + IList<string>? list = new List<string> { "a", "b", "c" }; |
| 44 | + await Assert.That(list).IsNotNull(); |
| 45 | + } |
| 46 | + |
| 47 | + [Test] |
| 48 | + public async Task NullableIList_WithNullValue_IsNotNull_Fails() |
| 49 | + { |
| 50 | + IList<string>? list = null; |
| 51 | + var action = async () => await Assert.That(list).IsNotNull(); |
| 52 | + await Assert.That(action).Throws<AssertionException>(); |
| 53 | + } |
| 54 | + |
| 55 | + [Test] |
| 56 | + public async Task NullableIList_WithNullValue_IsNull_Passes() |
| 57 | + { |
| 58 | + IList<string>? list = null; |
| 59 | + await Assert.That(list).IsNull(); |
| 60 | + } |
| 61 | + |
| 62 | + // =================================== |
| 63 | + // Dictionary<TKey, TValue>? Tests |
| 64 | + // =================================== |
| 65 | + |
| 66 | + [Test] |
| 67 | + public async Task NullableDictionary_AcceptsNullableValue_NoWarning() |
| 68 | + { |
| 69 | + Dictionary<string, int>? dict = new() { ["key"] = 1 }; |
| 70 | + await Assert.That(dict).IsNotNull(); |
| 71 | + } |
| 72 | + |
| 73 | + [Test] |
| 74 | + public async Task NullableDictionary_WithNullValue_IsNotNull_Fails() |
| 75 | + { |
| 76 | + Dictionary<string, int>? dict = null; |
| 77 | + var action = async () => await Assert.That(dict).IsNotNull(); |
| 78 | + await Assert.That(action).Throws<AssertionException>(); |
| 79 | + } |
| 80 | + |
| 81 | + [Test] |
| 82 | + public async Task NullableDictionary_WithNullValue_IsNull_Passes() |
| 83 | + { |
| 84 | + Dictionary<string, int>? dict = null; |
| 85 | + await Assert.That(dict).IsNull(); |
| 86 | + } |
| 87 | + |
| 88 | + // =================================== |
| 89 | + // IDictionary<TKey, TValue>? Tests |
| 90 | + // =================================== |
| 91 | + |
| 92 | + [Test] |
| 93 | + public async Task NullableIDictionary_AcceptsNullableValue_NoWarning() |
| 94 | + { |
| 95 | + IDictionary<string, int>? dict = new Dictionary<string, int> { ["key"] = 1 }; |
| 96 | + await Assert.That(dict).IsNotNull(); |
| 97 | + } |
| 98 | + |
| 99 | + [Test] |
| 100 | + public async Task NullableIDictionary_WithNullValue_IsNotNull_Fails() |
| 101 | + { |
| 102 | + IDictionary<string, int>? dict = null; |
| 103 | + var action = async () => await Assert.That(dict).IsNotNull(); |
| 104 | + await Assert.That(action).Throws<AssertionException>(); |
| 105 | + } |
| 106 | + |
| 107 | + [Test] |
| 108 | + public async Task NullableIDictionary_WithNullValue_IsNull_Passes() |
| 109 | + { |
| 110 | + IDictionary<string, int>? dict = null; |
| 111 | + await Assert.That(dict).IsNull(); |
| 112 | + } |
| 113 | + |
| 114 | + // =================================== |
| 115 | + // IReadOnlyDictionary<TKey, TValue>? Tests |
| 116 | + // =================================== |
| 117 | + |
| 118 | + [Test] |
| 119 | + public async Task NullableIReadOnlyDictionary_AcceptsNullableValue_NoWarning() |
| 120 | + { |
| 121 | + IReadOnlyDictionary<string, int>? dict = new Dictionary<string, int> { ["key"] = 1 }; |
| 122 | + await Assert.That(dict).IsNotNull(); |
| 123 | + } |
| 124 | + |
| 125 | + [Test] |
| 126 | + public async Task NullableIReadOnlyDictionary_WithNullValue_IsNotNull_Fails() |
| 127 | + { |
| 128 | + IReadOnlyDictionary<string, int>? dict = null; |
| 129 | + var action = async () => await Assert.That(dict).IsNotNull(); |
| 130 | + await Assert.That(action).Throws<AssertionException>(); |
| 131 | + } |
| 132 | + |
| 133 | + [Test] |
| 134 | + public async Task NullableIReadOnlyDictionary_WithNullValue_IsNull_Passes() |
| 135 | + { |
| 136 | + IReadOnlyDictionary<string, int>? dict = null; |
| 137 | + await Assert.That(dict).IsNull(); |
| 138 | + } |
| 139 | + |
| 140 | + // =================================== |
| 141 | + // Array? Tests |
| 142 | + // =================================== |
| 143 | + |
| 144 | + [Test] |
| 145 | + public async Task NullableArray_AcceptsNullableValue_NoWarning() |
| 146 | + { |
| 147 | + string[]? arr = ["a", "b", "c"]; |
| 148 | + await Assert.That(arr).IsNotNull(); |
| 149 | + } |
| 150 | + |
| 151 | + [Test] |
| 152 | + public async Task NullableArray_WithNullValue_IsNotNull_Fails() |
| 153 | + { |
| 154 | + string[]? arr = null; |
| 155 | + var action = async () => await Assert.That(arr).IsNotNull(); |
| 156 | + await Assert.That(action).Throws<AssertionException>(); |
| 157 | + } |
| 158 | + |
| 159 | + [Test] |
| 160 | + public async Task NullableArray_WithNullValue_IsNull_Passes() |
| 161 | + { |
| 162 | + string[]? arr = null; |
| 163 | + await Assert.That(arr).IsNull(); |
| 164 | + } |
| 165 | + |
| 166 | + // =================================== |
| 167 | + // ISet<T>? Tests |
| 168 | + // =================================== |
| 169 | + |
| 170 | + [Test] |
| 171 | + public async Task NullableISet_AcceptsNullableValue_NoWarning() |
| 172 | + { |
| 173 | + ISet<string>? set = new HashSet<string> { "a", "b", "c" }; |
| 174 | + await Assert.That(set).IsNotNull(); |
| 175 | + } |
| 176 | + |
| 177 | + [Test] |
| 178 | + public async Task NullableISet_WithNullValue_IsNotNull_Fails() |
| 179 | + { |
| 180 | + ISet<string>? set = null; |
| 181 | + var action = async () => await Assert.That(set).IsNotNull(); |
| 182 | + await Assert.That(action).Throws<AssertionException>(); |
| 183 | + } |
| 184 | + |
| 185 | + [Test] |
| 186 | + public async Task NullableISet_WithNullValue_IsNull_Passes() |
| 187 | + { |
| 188 | + ISet<string>? set = null; |
| 189 | + await Assert.That(set).IsNull(); |
| 190 | + } |
| 191 | + |
| 192 | + // =================================== |
| 193 | + // HashSet<T>? Tests |
| 194 | + // =================================== |
| 195 | + |
| 196 | + [Test] |
| 197 | + public async Task NullableHashSet_AcceptsNullableValue_NoWarning() |
| 198 | + { |
| 199 | + HashSet<string>? set = ["a", "b", "c"]; |
| 200 | + await Assert.That(set).IsNotNull(); |
| 201 | + } |
| 202 | + |
| 203 | + [Test] |
| 204 | + public async Task NullableHashSet_WithNullValue_IsNotNull_Fails() |
| 205 | + { |
| 206 | + HashSet<string>? set = null; |
| 207 | + var action = async () => await Assert.That(set).IsNotNull(); |
| 208 | + await Assert.That(action).Throws<AssertionException>(); |
| 209 | + } |
| 210 | + |
| 211 | + [Test] |
| 212 | + public async Task NullableHashSet_WithNullValue_IsNull_Passes() |
| 213 | + { |
| 214 | + HashSet<string>? set = null; |
| 215 | + await Assert.That(set).IsNull(); |
| 216 | + } |
| 217 | + |
| 218 | +#if NET5_0_OR_GREATER |
| 219 | + // =================================== |
| 220 | + // IReadOnlySet<T>? Tests |
| 221 | + // =================================== |
| 222 | + |
| 223 | + [Test] |
| 224 | + public async Task NullableIReadOnlySet_AcceptsNullableValue_NoWarning() |
| 225 | + { |
| 226 | + IReadOnlySet<string>? set = new HashSet<string> { "a", "b", "c" }; |
| 227 | + await Assert.That(set).IsNotNull(); |
| 228 | + } |
| 229 | + |
| 230 | + [Test] |
| 231 | + public async Task NullableIReadOnlySet_WithNullValue_IsNotNull_Fails() |
| 232 | + { |
| 233 | + IReadOnlySet<string>? set = null; |
| 234 | + var action = async () => await Assert.That(set).IsNotNull(); |
| 235 | + await Assert.That(action).Throws<AssertionException>(); |
| 236 | + } |
| 237 | + |
| 238 | + [Test] |
| 239 | + public async Task NullableIReadOnlySet_WithNullValue_IsNull_Passes() |
| 240 | + { |
| 241 | + IReadOnlySet<string>? set = null; |
| 242 | + await Assert.That(set).IsNull(); |
| 243 | + } |
| 244 | +#endif |
| 245 | + |
| 246 | + // =================================== |
| 247 | + // IReadOnlyList<T>? Tests |
| 248 | + // =================================== |
| 249 | + |
| 250 | + [Test] |
| 251 | + public async Task NullableIReadOnlyList_AcceptsNullableValue_NoWarning() |
| 252 | + { |
| 253 | + IReadOnlyList<string>? list = new List<string> { "a", "b", "c" }; |
| 254 | + await Assert.That(list).IsNotNull(); |
| 255 | + } |
| 256 | + |
| 257 | + [Test] |
| 258 | + public async Task NullableIReadOnlyList_WithNullValue_IsNotNull_Fails() |
| 259 | + { |
| 260 | + IReadOnlyList<string>? list = null; |
| 261 | + var action = async () => await Assert.That(list).IsNotNull(); |
| 262 | + await Assert.That(action).Throws<AssertionException>(); |
| 263 | + } |
| 264 | + |
| 265 | + [Test] |
| 266 | + public async Task NullableIReadOnlyList_WithNullValue_IsNull_Passes() |
| 267 | + { |
| 268 | + IReadOnlyList<string>? list = null; |
| 269 | + await Assert.That(list).IsNull(); |
| 270 | + } |
| 271 | + |
| 272 | + // =================================== |
| 273 | + // Method parameter tests (original issue scenario) |
| 274 | + // =================================== |
| 275 | + |
| 276 | + [Test] |
| 277 | + public async Task MethodWithNullableListParameter_NoWarning() |
| 278 | + { |
| 279 | + List<string>? list = ["test"]; |
| 280 | + await VerifyNotNull(list); |
| 281 | + } |
| 282 | + |
| 283 | + [Test] |
| 284 | + public async Task MethodWithNullableIDictionaryParameter_NoWarning() |
| 285 | + { |
| 286 | + IDictionary<string, int>? dict = new Dictionary<string, int> { ["key"] = 1 }; |
| 287 | + await VerifyDictionaryNotNull(dict); |
| 288 | + } |
| 289 | + |
| 290 | + private static async Task VerifyNotNull(List<string>? actual) |
| 291 | + { |
| 292 | + await Assert.That(actual).IsNotNull(); |
| 293 | + } |
| 294 | + |
| 295 | + private static async Task VerifyDictionaryNotNull(IDictionary<string, int>? actual) |
| 296 | + { |
| 297 | + await Assert.That(actual).IsNotNull(); |
| 298 | + } |
| 299 | +} |
0 commit comments