33
44// Most of the code in this file comes from the default Roslyn Analyzer project template
55
6- using Microsoft . CodeAnalysis ;
7- using Microsoft . CodeAnalysis . Diagnostics ;
6+ using System ;
87using System . Collections . Generic ;
8+ using System . Globalization ;
99using System . Linq ;
1010using System . Text ;
11+ using Microsoft . CodeAnalysis ;
12+ using Microsoft . CodeAnalysis . Diagnostics ;
1113using Xunit ;
1214
1315namespace TestHelper
@@ -82,7 +84,7 @@ protected void VerifyBasicDiagnostic(string[] sources, params DiagnosticResult[]
8284 }
8385
8486 /// <summary>
85- /// General method that gets a collection of actual diagnostics found in the source after the analyzer is run,
87+ /// General method that gets a collection of actual diagnostics found in the source after the analyzer is run,
8688 /// then verifies each of them.
8789 /// </summary>
8890 /// <param name="sources">An array of strings to create source documents from to run the analyzers on</param>
@@ -115,7 +117,7 @@ private static void VerifyDiagnosticResults(IEnumerable<Diagnostic> actualResult
115117 string diagnosticsOutput = actualResults . Any ( ) ? FormatDiagnostics ( analyzer , actualResults . ToArray ( ) ) : " NONE." ;
116118
117119 Assert . True ( false ,
118- string . Format ( "Mismatch between number of diagnostics returned, expected \" {0}\" actual \" {1}\" \r \n \r \n Diagnostics:\r \n {2}\r \n " , expectedCount , actualCount , diagnosticsOutput ) ) ;
120+ string . Format ( CultureInfo . InvariantCulture , "Mismatch between number of diagnostics returned, expected \" {0}\" actual \" {1}\" \r \n \r \n Diagnostics:\r \n {2}\r \n " , expectedCount , actualCount , diagnosticsOutput ) ) ;
119121 }
120122
121123 for ( int i = 0 ; i < expectedResults . Length ; i ++ )
@@ -128,7 +130,7 @@ private static void VerifyDiagnosticResults(IEnumerable<Diagnostic> actualResult
128130 if ( actual . Location != Location . None )
129131 {
130132 Assert . True ( false ,
131- string . Format ( "Expected:\n A project diagnostic with No location\n Actual:\n {0}" ,
133+ string . Format ( CultureInfo . InvariantCulture , "Expected:\n A project diagnostic with No location\n Actual:\n {0}" ,
132134 FormatDiagnostics ( analyzer , actual ) ) ) ;
133135 }
134136 }
@@ -140,7 +142,9 @@ private static void VerifyDiagnosticResults(IEnumerable<Diagnostic> actualResult
140142 if ( additionalLocations . Length != expected . Locations . Length - 1 )
141143 {
142144 Assert . True ( false ,
143- string . Format ( "Expected {0} additional locations but got {1} for Diagnostic:\r \n {2}\r \n " ,
145+ string . Format (
146+ CultureInfo . InvariantCulture ,
147+ "Expected {0} additional locations but got {1} for Diagnostic:\r \n {2}\r \n " ,
144148 expected . Locations . Length - 1 , additionalLocations . Length ,
145149 FormatDiagnostics ( analyzer , actual ) ) ) ;
146150 }
@@ -154,21 +158,27 @@ private static void VerifyDiagnosticResults(IEnumerable<Diagnostic> actualResult
154158 if ( actual . Id != expected . Id )
155159 {
156160 Assert . True ( false ,
157- string . Format ( "Expected diagnostic id to be \" {0}\" was \" {1}\" \r \n \r \n Diagnostic:\r \n {2}\r \n " ,
161+ string . Format (
162+ CultureInfo . InvariantCulture ,
163+ "Expected diagnostic id to be \" {0}\" was \" {1}\" \r \n \r \n Diagnostic:\r \n {2}\r \n " ,
158164 expected . Id , actual . Id , FormatDiagnostics ( analyzer , actual ) ) ) ;
159165 }
160166
161167 if ( actual . Severity != expected . Severity )
162168 {
163169 Assert . True ( false ,
164- string . Format ( "Expected diagnostic severity to be \" {0}\" was \" {1}\" \r \n \r \n Diagnostic:\r \n {2}\r \n " ,
170+ string . Format (
171+ CultureInfo . InvariantCulture ,
172+ "Expected diagnostic severity to be \" {0}\" was \" {1}\" \r \n \r \n Diagnostic:\r \n {2}\r \n " ,
165173 expected . Severity , actual . Severity , FormatDiagnostics ( analyzer , actual ) ) ) ;
166174 }
167175
168176 if ( actual . GetMessage ( ) != expected . Message )
169177 {
170178 Assert . True ( false ,
171- string . Format ( "Expected diagnostic message to be \" {0}\" was \" {1}\" \r \n \r \n Diagnostic:\r \n {2}\r \n " ,
179+ string . Format (
180+ CultureInfo . InvariantCulture ,
181+ "Expected diagnostic message to be \" {0}\" was \" {1}\" \r \n \r \n Diagnostic:\r \n {2}\r \n " ,
172182 expected . Message , actual . GetMessage ( ) , FormatDiagnostics ( analyzer , actual ) ) ) ;
173183 }
174184 }
@@ -186,7 +196,9 @@ private static void VerifyDiagnosticLocation(DiagnosticAnalyzer analyzer, Diagno
186196 var actualSpan = actual . GetLineSpan ( ) ;
187197
188198 Assert . True ( actualSpan . Path == expected . Path || ( actualSpan . Path != null && actualSpan . Path . Contains ( "Test0." ) && expected . Path . Contains ( "Test." ) ) ,
189- string . Format ( "Expected diagnostic to be in file \" {0}\" was actually in file \" {1}\" \r \n \r \n Diagnostic:\r \n {2}\r \n " ,
199+ string . Format (
200+ CultureInfo . InvariantCulture ,
201+ "Expected diagnostic to be in file \" {0}\" was actually in file \" {1}\" \r \n \r \n Diagnostic:\r \n {2}\r \n " ,
190202 expected . Path , actualSpan . Path , FormatDiagnostics ( analyzer , diagnostic ) ) ) ;
191203
192204 var actualLinePosition = actualSpan . StartLinePosition ;
@@ -197,7 +209,9 @@ private static void VerifyDiagnosticLocation(DiagnosticAnalyzer analyzer, Diagno
197209 if ( actualLinePosition . Line + 1 != expected . Line )
198210 {
199211 Assert . True ( false ,
200- string . Format ( "Expected diagnostic to be on line \" {0}\" was actually on line \" {1}\" \r \n \r \n Diagnostic:\r \n {2}\r \n " ,
212+ string . Format (
213+ CultureInfo . InvariantCulture ,
214+ "Expected diagnostic to be on line \" {0}\" was actually on line \" {1}\" \r \n \r \n Diagnostic:\r \n {2}\r \n " ,
201215 expected . Line , actualLinePosition . Line + 1 , FormatDiagnostics ( analyzer , diagnostic ) ) ) ;
202216 }
203217 }
@@ -208,7 +222,9 @@ private static void VerifyDiagnosticLocation(DiagnosticAnalyzer analyzer, Diagno
208222 if ( actualLinePosition . Character + 1 != expected . Column )
209223 {
210224 Assert . True ( false ,
211- string . Format ( "Expected diagnostic to start at column \" {0}\" was actually at column \" {1}\" \r \n \r \n Diagnostic:\r \n {2}\r \n " ,
225+ string . Format (
226+ CultureInfo . InvariantCulture ,
227+ "Expected diagnostic to start at column \" {0}\" was actually at column \" {1}\" \r \n \r \n Diagnostic:\r \n {2}\r \n " ,
212228 expected . Column , actualLinePosition . Character + 1 , FormatDiagnostics ( analyzer , diagnostic ) ) ) ;
213229 }
214230 }
@@ -239,17 +255,19 @@ private static string FormatDiagnostics(DiagnosticAnalyzer analyzer, params Diag
239255 var location = diagnostics [ i ] . Location ;
240256 if ( location == Location . None )
241257 {
242- builder . AppendFormat ( "GetGlobalResult({0}.{1})" , analyzerType . Name , rule . Id ) ;
258+ builder . AppendFormat ( CultureInfo . InvariantCulture , "GetGlobalResult({0}.{1})" , analyzerType . Name , rule . Id ) ;
243259 }
244260 else
245261 {
246262 Assert . True ( location . IsInSource ,
247263 $ "Test base does not currently handle diagnostics in metadata locations. Diagnostic in metadata: { diagnostics [ i ] } \r \n ") ;
248264
249- string resultMethodName = diagnostics [ i ] . Location . SourceTree . FilePath . EndsWith ( ".cs" ) ? "GetCSharpResultAt" : "GetBasicResultAt" ;
265+ string resultMethodName = diagnostics [ i ] . Location . SourceTree . FilePath . EndsWith ( ".cs" , StringComparison . Ordinal ) ? "GetCSharpResultAt" : "GetBasicResultAt" ;
250266 var linePosition = diagnostics [ i ] . Location . GetLineSpan ( ) . StartLinePosition ;
251267
252- builder . AppendFormat ( "{0}({1}, {2}, {3}.{4})" ,
268+ builder . AppendFormat (
269+ CultureInfo . InvariantCulture ,
270+ "{0}({1}, {2}, {3}.{4})" ,
253271 resultMethodName ,
254272 linePosition . Line + 1 ,
255273 linePosition . Character + 1 ,
0 commit comments