1+ using FluentAssertions . Analyzers . TestUtils ;
2+ using Microsoft . CodeAnalysis ;
3+ using Microsoft . VisualStudio . TestTools . UnitTesting ;
4+
5+ namespace FluentAssertions . Analyzers . Tests . Tips ;
6+
7+ [ TestClass ]
8+ public class NunitTests
9+ {
10+
11+ [ DataTestMethod ]
12+ [ AssertionDiagnostic ( "Assert.True(actual{0});" ) ]
13+ [ AssertionDiagnostic ( "Assert.True(bool.Parse(\" true\" ){0});" ) ]
14+ [ AssertionDiagnostic ( "Assert.IsTrue(actual{0});" ) ]
15+ [ AssertionDiagnostic ( "Assert.IsTrue(bool.Parse(\" true\" ){0});" ) ]
16+ [ Implemented ]
17+ public void AssertTrue_TestAnalyzer ( string assertion ) => VerifyCSharpDiagnostic ( "bool actual" , assertion ) ;
18+
19+ [ DataTestMethod ]
20+ [ AssertionCodeFix (
21+ oldAssertion : "Assert.True(actual{0});" ,
22+ newAssertion : "actual.Should().BeTrue({0});" ) ]
23+ [ AssertionCodeFix (
24+ oldAssertion : "Assert.True(bool.Parse(\" true\" ){0});" ,
25+ newAssertion : "bool.Parse(\" true\" ).Should().BeTrue({0});" ) ]
26+ [ AssertionCodeFix (
27+ oldAssertion : "Assert.True(!actual{0});" ,
28+ newAssertion : "(!actual).Should().BeTrue({0});" ) ]
29+ [ AssertionCodeFix (
30+ oldAssertion : "Assert.True(actual == false{0});" ,
31+ newAssertion : "(actual == false).Should().BeTrue({0});" ) ]
32+ [ AssertionCodeFix (
33+ oldAssertion : "Assert.IsTrue(actual{0});" ,
34+ newAssertion : "actual.Should().BeTrue({0});" ) ]
35+ [ AssertionCodeFix (
36+ oldAssertion : "Assert.IsTrue(bool.Parse(\" true\" ){0});" ,
37+ newAssertion : "bool.Parse(\" true\" ).Should().BeTrue({0});" ) ]
38+ [ AssertionCodeFix (
39+ oldAssertion : "Assert.IsTrue(!actual{0});" ,
40+ newAssertion : "(!actual).Should().BeTrue({0});" ) ]
41+ [ AssertionCodeFix (
42+ oldAssertion : "Assert.IsTrue(actual == false{0});" ,
43+ newAssertion : "(actual == false).Should().BeTrue({0});" ) ]
44+ [ Implemented ]
45+ public void AssertTrue_TestCodeFix ( string oldAssertion , string newAssertion )
46+ => VerifyCSharpFix ( "bool actual" , oldAssertion , newAssertion ) ;
47+
48+ [ DataTestMethod ]
49+ [ AssertionDiagnostic ( "Assert.False(actual{0});" ) ]
50+ [ AssertionDiagnostic ( "Assert.False(bool.Parse(\" false\" ){0});" ) ]
51+ [ AssertionDiagnostic ( "Assert.IsFalse(actual{0});" ) ]
52+ [ AssertionDiagnostic ( "Assert.IsFalse(bool.Parse(\" false\" ){0});" ) ]
53+ [ Implemented ]
54+ public void AssertFalse_TestAnalyzer ( string assertion ) => VerifyCSharpDiagnostic ( "bool actual" , assertion ) ;
55+
56+ [ DataTestMethod ]
57+ [ AssertionCodeFix (
58+ oldAssertion : "Assert.False(actual{0});" ,
59+ newAssertion : "actual.Should().BeFalse({0});" ) ]
60+ [ AssertionCodeFix (
61+ oldAssertion : "Assert.False(bool.Parse(\" false\" ){0});" ,
62+ newAssertion : "bool.Parse(\" false\" ).Should().BeFalse({0});" ) ]
63+ [ AssertionCodeFix (
64+ oldAssertion : "Assert.IsFalse(actual{0});" ,
65+ newAssertion : "actual.Should().BeFalse({0});" ) ]
66+ [ AssertionCodeFix (
67+ oldAssertion : "Assert.IsFalse(bool.Parse(\" false\" ){0});" ,
68+ newAssertion : "bool.Parse(\" false\" ).Should().BeFalse({0});" ) ]
69+ [ Implemented ]
70+ public void AssertFalse_TestCodeFix ( string oldAssertion , string newAssertion )
71+ => VerifyCSharpFix ( "bool actual" , oldAssertion , newAssertion ) ;
72+
73+ private void VerifyCSharpDiagnostic ( string methodArguments , string assertion )
74+ {
75+ var source = GenerateCode . Nunit3Assertion ( methodArguments , assertion ) ;
76+ DiagnosticVerifier . VerifyDiagnostic ( new DiagnosticVerifierArguments ( )
77+ . WithAllAnalyzers ( )
78+ . WithSources ( source )
79+ . WithPackageReferences ( PackageReference . FluentAssertions_6_12_0 , PackageReference . Nunit_3_14_0 )
80+ . WithExpectedDiagnostics ( new DiagnosticResult
81+ {
82+ Id = AssertAnalyzer . NUnitRule . Id ,
83+ Message = AssertAnalyzer . Message ,
84+ Locations = new DiagnosticResultLocation [ ]
85+ {
86+ new ( "Test0.cs" , 15 , 13 )
87+ } ,
88+ Severity = DiagnosticSeverity . Info
89+ } )
90+ ) ;
91+ }
92+
93+ private void VerifyCSharpFix ( string methodArguments , string oldAssertion , string newAssertion )
94+ {
95+ var oldSource = GenerateCode . Nunit3Assertion ( methodArguments , oldAssertion ) ;
96+ var newSource = GenerateCode . Nunit3Assertion ( methodArguments , newAssertion ) ;
97+
98+ DiagnosticVerifier . VerifyFix ( new CodeFixVerifierArguments ( )
99+ . WithDiagnosticAnalyzer < AssertAnalyzer > ( )
100+ . WithCodeFixProvider < NunitCodeFixProvider > ( )
101+ . WithSources ( oldSource )
102+ . WithFixedSources ( newSource )
103+ . WithPackageReferences ( PackageReference . FluentAssertions_6_12_0 , PackageReference . Nunit_3_14_0 )
104+ ) ;
105+ }
106+ }
0 commit comments