22using Microsoft . VisualStudio . TestTools . UnitTesting ;
33using System ;
44using System . Collections ;
5+ using System . Text ;
56using System . Text . RegularExpressions ;
67using System . Threading . Tasks ;
78
@@ -16,6 +17,174 @@ public class MsTestTests
1617 [ Implemented ]
1718 public void AssertIsTrue_TestAnalyzer ( string assertion ) => VerifyCSharpDiagnostic < AssertIsTrueAnalyzer > ( "bool actual" , assertion ) ;
1819
20+ [ AssertionDataTestMethod ]
21+ [ AssertionDiagnostic ( "Assert.IsTrue(actual{0});" ) ]
22+ [ AssertionDiagnostic ( "Assert.IsTrue(bool.Parse(\" true\" ){0});" ) ]
23+ [ Implemented ]
24+ public void AssertIsTrue_NestedUsingInNamespace1_TestAnalyzer ( string assertion )
25+ => VerifyCSharpDiagnostic < AssertIsTrueAnalyzer > ( "bool actual" , assertion , new StringBuilder ( )
26+ . AppendLine ( "using System;" )
27+ . AppendLine ( "using FluentAssertions;" )
28+ . AppendLine ( "using FluentAssertions.Extensions;" )
29+ . AppendLine ( "using System.Threading.Tasks;" )
30+ . AppendLine ( "namespace Microsoft.VisualStudio.TestTools" )
31+ . AppendLine ( "{" )
32+ . AppendLine ( " using UnitTesting;" )
33+ . AppendLine ( " class TestClass" )
34+ . AppendLine ( " {" )
35+ . AppendLine ( $ " void TestMethod(bool actual)")
36+ . AppendLine ( " {" )
37+ . AppendLine ( $ " { assertion } ")
38+ . AppendLine ( " }" )
39+ . AppendLine ( " }" )
40+ . AppendMainMethod ( )
41+ . AppendLine ( "}" )
42+ . ToString ( ) ) ;
43+
44+ [ AssertionDataTestMethod ]
45+ [ AssertionDiagnostic ( "Assert.IsTrue(actual{0});" ) ]
46+ [ AssertionDiagnostic ( "Assert.IsTrue(bool.Parse(\" true\" ){0});" ) ]
47+ [ Implemented ]
48+ public void AssertIsTrue_NestedUsingInNamespace2_TestAnalyzer ( string assertion )
49+ => VerifyCSharpDiagnostic < AssertIsTrueAnalyzer > ( "bool actual" , assertion , new StringBuilder ( )
50+ . AppendLine ( "using System;" )
51+ . AppendLine ( "using FluentAssertions;" )
52+ . AppendLine ( "using FluentAssertions.Extensions;" )
53+ . AppendLine ( "using System.Threading.Tasks;" )
54+ . AppendLine ( "namespace Microsoft.VisualStudio" )
55+ . AppendLine ( "{" )
56+ . AppendLine ( " using TestTools.UnitTesting;" )
57+ . AppendLine ( " class TestClass" )
58+ . AppendLine ( " {" )
59+ . AppendLine ( $ " void TestMethod(bool actual)")
60+ . AppendLine ( " {" )
61+ . AppendLine ( $ " { assertion } ")
62+ . AppendLine ( " }" )
63+ . AppendLine ( " }" )
64+ . AppendMainMethod ( )
65+ . AppendLine ( "}" )
66+ . ToString ( ) ) ;
67+
68+ [ AssertionDataTestMethod ]
69+ [ AssertionDiagnostic ( "Assert.IsTrue(actual{0});" ) ]
70+ [ AssertionDiagnostic ( "Assert.IsTrue(bool.Parse(\" true\" ){0});" ) ]
71+ [ Implemented ]
72+ public void AssertIsTrue_NestedUsingInNamespace3_TestAnalyzer ( string assertion )
73+ => VerifyCSharpDiagnostic < AssertIsTrueAnalyzer > ( "bool actual" , assertion , new StringBuilder ( )
74+ . AppendLine ( "using System;" )
75+ . AppendLine ( "using FluentAssertions;" )
76+ . AppendLine ( "using FluentAssertions.Extensions;" )
77+ . AppendLine ( "using System.Threading.Tasks;" )
78+ . AppendLine ( "namespace Microsoft" )
79+ . AppendLine ( "{ namespace VisualStudio {" )
80+ . AppendLine ( " using TestTools.UnitTesting;" )
81+ . AppendLine ( " class TestClass" )
82+ . AppendLine ( " {" )
83+ . AppendLine ( $ " void TestMethod(bool actual)")
84+ . AppendLine ( " {" )
85+ . AppendLine ( $ " { assertion } ")
86+ . AppendLine ( " }" )
87+ . AppendLine ( " }}" )
88+ . AppendMainMethod ( )
89+ . AppendLine ( "}" )
90+ . ToString ( ) ) ;
91+
92+ [ AssertionDataTestMethod ]
93+ [ AssertionDiagnostic ( "Assert.IsTrue(actual{0});" ) ]
94+ [ AssertionDiagnostic ( "Assert.IsTrue(bool.Parse(\" true\" ){0});" ) ]
95+ [ Implemented ]
96+ public void AssertIsTrue_NestedUsingInNamespace4_TestAnalyzer ( string assertion )
97+ => VerifyCSharpDiagnostic < AssertIsTrueAnalyzer > ( "bool actual" , assertion , new StringBuilder ( )
98+ . AppendLine ( "using System;" )
99+ . AppendLine ( "using FluentAssertions;" )
100+ . AppendLine ( "using FluentAssertions.Extensions;" )
101+ . AppendLine ( "using System.Threading.Tasks;" )
102+ . AppendLine ( "namespace Microsoft" )
103+ . AppendLine ( "{ namespace VisualStudio {" )
104+ . AppendLine ( " using TestTools . UnitTesting;" )
105+ . AppendLine ( " class TestClass" )
106+ . AppendLine ( " {" )
107+ . AppendLine ( $ " void TestMethod(bool actual)")
108+ . AppendLine ( " {" )
109+ . AppendLine ( $ " { assertion } ")
110+ . AppendLine ( " }" )
111+ . AppendLine ( " }}" )
112+ . AppendMainMethod ( )
113+ . AppendLine ( "}" )
114+ . ToString ( ) ) ;
115+
116+ [ AssertionDataTestMethod ]
117+ [ AssertionDiagnostic ( "Assert.IsTrue(actual{0});" ) ]
118+ [ AssertionDiagnostic ( "Assert.IsTrue(bool.Parse(\" true\" ){0});" ) ]
119+ [ Implemented ]
120+ public void AssertIsTrue_NestedUsingInNamespace5_TestAnalyzer ( string assertion )
121+ => VerifyCSharpDiagnostic < AssertIsTrueAnalyzer > ( "bool actual" , assertion , new StringBuilder ( )
122+ . AppendLine ( "using System;" )
123+ . AppendLine ( "using FluentAssertions;" )
124+ . AppendLine ( "using FluentAssertions.Extensions;" )
125+ . AppendLine ( "using System.Threading.Tasks;" )
126+ . AppendLine ( "using Microsoft . VisualStudio . TestTools . UnitTesting;" )
127+ . AppendLine ( "namespace Testing" )
128+ . AppendLine ( "{" )
129+ . AppendLine ( " class TestClass" )
130+ . AppendLine ( " {" )
131+ . AppendLine ( $ " void TestMethod(bool actual)")
132+ . AppendLine ( " {" )
133+ . AppendLine ( $ " { assertion } ")
134+ . AppendLine ( " }" )
135+ . AppendLine ( " }" )
136+ . AppendMainMethod ( )
137+ . AppendLine ( "}" )
138+ . ToString ( ) ) ;
139+
140+ [ AssertionDataTestMethod ]
141+ [ AssertionDiagnostic ( "Assert.IsTrue(actual{0});" ) ]
142+ [ AssertionDiagnostic ( "Assert.IsTrue(bool.Parse(\" true\" ){0});" ) ]
143+ [ Implemented ]
144+ public void AssertIsTrue_NestedUsingInNamespace6_TestAnalyzer ( string assertion )
145+ => VerifyCSharpDiagnostic < AssertIsTrueAnalyzer > ( "bool actual" , assertion , new StringBuilder ( )
146+ . AppendLine ( "using System;" )
147+ . AppendLine ( "using FluentAssertions;" )
148+ . AppendLine ( "using FluentAssertions.Extensions;" )
149+ . AppendLine ( "using System.Threading.Tasks; using static Microsoft.VisualStudio.TestTools.UnitTesting.Assert;" )
150+ . AppendLine ( "using Microsoft . VisualStudio . TestTools . UnitTesting;" )
151+ . AppendLine ( "namespace Testing" )
152+ . AppendLine ( "{" )
153+ . AppendLine ( " class TestClass" )
154+ . AppendLine ( " {" )
155+ . AppendLine ( $ " void TestMethod(bool actual)")
156+ . AppendLine ( " {" )
157+ . AppendLine ( $ " { assertion } ")
158+ . AppendLine ( " }" )
159+ . AppendLine ( " }" )
160+ . AppendMainMethod ( )
161+ . AppendLine ( "}" )
162+ . ToString ( ) ) ;
163+
164+ [ AssertionDataTestMethod ]
165+ [ AssertionDiagnostic ( "Assert.IsTrue(actual{0});" ) ]
166+ [ AssertionDiagnostic ( "Assert.IsTrue(bool.Parse(\" true\" ){0});" ) ]
167+ [ Implemented ]
168+ public void AssertIsTrue_NestedUsingInNamespace7_TestAnalyzer ( string assertion )
169+ => VerifyCSharpDiagnostic < AssertIsTrueAnalyzer > ( "bool actual" , assertion , new StringBuilder ( )
170+ . AppendLine ( "using System;" )
171+ . AppendLine ( "using FluentAssertions;" )
172+ . AppendLine ( "using FluentAssertions.Extensions;" )
173+ . AppendLine ( "using System.Threading.Tasks; using MsAssert = Microsoft.VisualStudio.TestTools.UnitTesting.Assert;" )
174+ . AppendLine ( "using Microsoft . VisualStudio . TestTools . UnitTesting;" )
175+ . AppendLine ( "namespace Testing" )
176+ . AppendLine ( "{" )
177+ . AppendLine ( " class TestClass" )
178+ . AppendLine ( " {" )
179+ . AppendLine ( $ " void TestMethod(bool actual)")
180+ . AppendLine ( " {" )
181+ . AppendLine ( $ " { assertion } ")
182+ . AppendLine ( " }" )
183+ . AppendLine ( " }" )
184+ . AppendMainMethod ( )
185+ . AppendLine ( "}" )
186+ . ToString ( ) ) ;
187+
19188 [ AssertionDataTestMethod ]
20189 [ AssertionCodeFix (
21190 oldAssertion : "Assert.IsTrue(actual{0});" ,
@@ -512,10 +681,8 @@ public void AssertThrowsExceptionAsync_TestCodeFix(string oldAssertion, string n
512681 [ Implemented ]
513682 public void StringAssertDoesNotMatch_TestCodeFix ( string oldAssertion , string newAssertion ) => VerifyCSharpFix < StringAssertDoesNotMatchCodeFix , StringAssertDoesNotMatchAnalyzer > ( "string actual, System.Text.RegularExpressions.Regex pattern" , oldAssertion , newAssertion ) ;
514683
515- private void VerifyCSharpDiagnostic < TDiagnosticAnalyzer > ( string methodArguments , string assertion ) where TDiagnosticAnalyzer : Microsoft . CodeAnalysis . Diagnostics . DiagnosticAnalyzer , new ( )
684+ private void VerifyCSharpDiagnostic < TDiagnosticAnalyzer > ( string methodArguments , string assertion , string source ) where TDiagnosticAnalyzer : Microsoft . CodeAnalysis . Diagnostics . DiagnosticAnalyzer , new ( )
516685 {
517- var source = GenerateCode . MsTestAssertion ( methodArguments , assertion ) ;
518-
519686 var type = typeof ( TDiagnosticAnalyzer ) ;
520687 var diagnosticId = ( string ) type . GetField ( "DiagnosticId" ) . GetValue ( null ) ;
521688 var message = ( string ) type . GetField ( "Message" ) . GetValue ( null ) ;
@@ -531,6 +698,8 @@ public void AssertThrowsExceptionAsync_TestCodeFix(string oldAssertion, string n
531698 Severity = DiagnosticSeverity . Info
532699 } ) ;
533700 }
701+ private void VerifyCSharpDiagnostic < TDiagnosticAnalyzer > ( string methodArguments , string assertion ) where TDiagnosticAnalyzer : Microsoft . CodeAnalysis . Diagnostics . DiagnosticAnalyzer , new ( )
702+ => VerifyCSharpDiagnostic < TDiagnosticAnalyzer > ( methodArguments , assertion , GenerateCode . MsTestAssertion ( methodArguments , assertion ) ) ;
534703
535704 private void VerifyCSharpFix < TCodeFixProvider , TDiagnosticAnalyzer > ( string methodArguments , string oldAssertion , string newAssertion )
536705 where TCodeFixProvider : Microsoft . CodeAnalysis . CodeFixes . CodeFixProvider , new ( )
0 commit comments