|
1 | 1 | using System.Threading.Tasks; |
| 2 | +using Microsoft.CodeAnalysis; |
2 | 3 | using Microsoft.CodeAnalysis.CSharp; |
3 | 4 | using Xunit; |
4 | 5 | using Xunit.Analyzers; |
5 | 6 | using Verify = CSharpVerifier<Xunit.Analyzers.BooleanAssertsShouldNotBeUsedForSimpleEqualityCheck>; |
| 7 | +using Verify_v3_Pre_301 = CSharpVerifier<BooleanAssertsShouldNotBeUsedForSimpleEqualityCheckTests.Analyzer_v3_Pre301>; |
6 | 8 |
|
7 | 9 | public class BooleanAssertsShouldNotBeUsedForSimpleEqualityCheckTests |
8 | 10 | { |
@@ -173,6 +175,73 @@ public void TestMethod() {{ |
173 | 175 |
|
174 | 176 | await Verify.VerifyAnalyzer(LanguageVersion.CSharp8, source, expected); |
175 | 177 | } |
| 178 | + |
| 179 | + [Fact] |
| 180 | + public async Task ComparingAgainstNullPointer_DoesNotTrigger() |
| 181 | + { |
| 182 | + var source = /* lang=c#-test */ """ |
| 183 | + using Xunit; |
| 184 | +
|
| 185 | + public class TestClass { |
| 186 | + public unsafe void TestMethod() { |
| 187 | + var value = 42; |
| 188 | + var ptr = &value; |
| 189 | +
|
| 190 | + Assert.True(ptr == null); |
| 191 | + Assert.True(null == ptr); |
| 192 | + Assert.True(ptr != null); |
| 193 | + Assert.True(null != ptr); |
| 194 | +
|
| 195 | + Assert.False(ptr == null); |
| 196 | + Assert.False(null == ptr); |
| 197 | + Assert.False(ptr != null); |
| 198 | + Assert.False(null != ptr); |
| 199 | + } |
| 200 | + } |
| 201 | + """; |
| 202 | + |
| 203 | + await Verify.VerifyAnalyzerV2(source); |
| 204 | + await Verify_v3_Pre_301.VerifyAnalyzerV3(source); |
| 205 | + } |
| 206 | + |
| 207 | + [Fact] |
| 208 | + public async Task ComparingAgainstNullPointer_v3_301_Triggers() |
| 209 | + { |
| 210 | + var source = /* lang=c#-test */ """ |
| 211 | + using Xunit; |
| 212 | +
|
| 213 | + public class TestClass { |
| 214 | + public unsafe void TestMethod() { |
| 215 | + var value = 42; |
| 216 | + var ptr = &value; |
| 217 | +
|
| 218 | + {|#0:Assert.True(ptr == null)|}; |
| 219 | + {|#1:Assert.True(null == ptr)|}; |
| 220 | + {|#2:Assert.True(ptr != null)|}; |
| 221 | + {|#3:Assert.True(null != ptr)|}; |
| 222 | +
|
| 223 | + {|#10:Assert.False(ptr == null)|}; |
| 224 | + {|#11:Assert.False(null == ptr)|}; |
| 225 | + {|#12:Assert.False(ptr != null)|}; |
| 226 | + {|#13:Assert.False(null != ptr)|}; |
| 227 | + } |
| 228 | + } |
| 229 | + """; |
| 230 | + var expected = new[] |
| 231 | + { |
| 232 | + Verify.Diagnostic("xUnit2024").WithLocation(0).WithArguments("True", "Null"), |
| 233 | + Verify.Diagnostic("xUnit2024").WithLocation(1).WithArguments("True", "Null"), |
| 234 | + Verify.Diagnostic("xUnit2024").WithLocation(2).WithArguments("True", "NotNull"), |
| 235 | + Verify.Diagnostic("xUnit2024").WithLocation(3).WithArguments("True", "NotNull"), |
| 236 | + |
| 237 | + Verify.Diagnostic("xUnit2024").WithLocation(10).WithArguments("False", "NotNull"), |
| 238 | + Verify.Diagnostic("xUnit2024").WithLocation(11).WithArguments("False", "NotNull"), |
| 239 | + Verify.Diagnostic("xUnit2024").WithLocation(12).WithArguments("False", "Null"), |
| 240 | + Verify.Diagnostic("xUnit2024").WithLocation(13).WithArguments("False", "Null"), |
| 241 | + }; |
| 242 | + |
| 243 | + await Verify.VerifyAnalyzerV3(source, expected); |
| 244 | + } |
176 | 245 | } |
177 | 246 |
|
178 | 247 | public class X2025_BooleanAssertionCanBeSimplified |
@@ -216,4 +285,10 @@ void TestMethod() {{ |
216 | 285 | await Verify.VerifyAnalyzer(source, expected); |
217 | 286 | } |
218 | 287 | } |
| 288 | + |
| 289 | + public class Analyzer_v3_Pre301 : BooleanAssertsShouldNotBeUsedForSimpleEqualityCheck |
| 290 | + { |
| 291 | + protected override XunitContext CreateXunitContext(Compilation compilation) => |
| 292 | + XunitContext.ForV3(compilation, new(3, 0, 0)); |
| 293 | + } |
219 | 294 | } |
0 commit comments