11using Microsoft . VisualStudio . TestTools . UnitTesting ;
22using System . Text ;
33
4- namespace FluentAssertions . Analyzers . Tests
4+ namespace FluentAssertions . Analyzers . Tests . Tips
55{
66 [ TestClass ]
77 public class NullConditionalAssertionTests
@@ -10,13 +10,25 @@ public class NullConditionalAssertionTests
1010 [ AssertionDiagnostic ( "actual?.Should().Be(expected{0});" ) ]
1111 [ AssertionDiagnostic ( "actual?.MyProperty.Should().Be(\" test\" {0});" ) ]
1212 [ AssertionDiagnostic ( "actual.MyProperty?.Should().Be(\" test\" {0});" ) ]
13+ [ AssertionDiagnostic ( "(actual.MyProperty)?.Should().Be(\" test\" {0});" ) ]
14+ [ AssertionDiagnostic ( "(actual?.MyProperty)?.Should().Be(\" test\" {0});" ) ]
15+ [ AssertionDiagnostic ( "actual?.MyProperty.Should().Be(actual?.MyProperty{0});" ) ]
16+ [ AssertionDiagnostic ( "actual.MyList?.Where(obj => obj?.ToString() == null).Count().Should().Be(0{0});" ) ]
1317 [ Implemented ]
14- public void TestAnalyzer ( string assertion ) => VerifyCSharpDiagnostic ( assertion ) ;
18+ public void NullConditionalMayNotExecuteTest ( string assertion ) => VerifyCSharpDiagnostic ( assertion ) ;
1519
16- private void VerifyCSharpDiagnostic ( string assertion )
17- {
18- var code = new StringBuilder ( )
20+ [ AssertionDataTestMethod ]
21+ [ AssertionDiagnostic ( "(actual?.MyProperty).Should().Be(\" test\" {0});" ) ]
22+ [ AssertionDiagnostic ( "actual.MyProperty.Should().Be(actual?.MyProperty{0});" ) ]
23+ [ AssertionDiagnostic ( "actual.MyList.Where(obj => obj?.ToString() == null).Count().Should().Be(0{0});" ) ]
24+ [ Implemented ]
25+ public void NullConditionalWillStillExecuteTest ( string assertion ) => VerifyCSharpDiagnosticPass ( assertion ) ;
26+
27+ private static string Code ( string assertion ) =>
28+ new StringBuilder ( )
1929 . AppendLine ( "using System;" )
30+ . AppendLine ( "using System.Collections.Generic;" )
31+ . AppendLine ( "using System.Linq;" )
2032 . AppendLine ( "using FluentAssertions;using FluentAssertions.Extensions;" )
2133 . AppendLine ( "namespace TestNamespace" )
2234 . AppendLine ( "{" )
@@ -30,6 +42,7 @@ private void VerifyCSharpDiagnostic(string assertion)
3042 . AppendLine ( " class MyClass" )
3143 . AppendLine ( " {" )
3244 . AppendLine ( " public string MyProperty { get; set; }" )
45+ . AppendLine ( " public List<object> MyList { get; set; }" )
3346 . AppendLine ( " }" )
3447 . AppendLine ( " class Program" )
3548 . AppendLine ( " {" )
@@ -40,16 +53,19 @@ private void VerifyCSharpDiagnostic(string assertion)
4053 . AppendLine ( "}" )
4154 . ToString ( ) ;
4255
43- DiagnosticVerifier . VerifyCSharpDiagnostic < NullConditionalAssertionAnalyzer > ( code , new DiagnosticResult
56+ private static void VerifyCSharpDiagnosticPass ( string assertion )
57+ => DiagnosticVerifier . VerifyCSharpDiagnostic < NullConditionalAssertionAnalyzer > ( Code ( assertion ) ) ;
58+
59+ private static void VerifyCSharpDiagnostic ( string assertion )
60+ => DiagnosticVerifier . VerifyCSharpDiagnostic < NullConditionalAssertionAnalyzer > ( Code ( assertion ) , new DiagnosticResult
4461 {
4562 Id = NullConditionalAssertionAnalyzer . DiagnosticId ,
4663 Message = NullConditionalAssertionAnalyzer . Message ,
4764 Severity = Microsoft . CodeAnalysis . DiagnosticSeverity . Warning ,
4865 Locations = new DiagnosticResultLocation [ ]
4966 {
50- new DiagnosticResultLocation ( "Test0.cs" , 9 , 13 )
67+ new DiagnosticResultLocation ( "Test0.cs" , 11 , 13 )
5168 }
5269 } ) ;
53- }
5470 }
5571}
0 commit comments