Skip to content

Commit a0ae67c

Browse files
committed
Expand DiagnosticVerifier tests to cover additional edge cases
1 parent 819c488 commit a0ae67c

1 file changed

Lines changed: 99 additions & 1 deletion

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/Verifiers/DiagnosticVerifierTests.cs

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,50 @@ int PropertyName
101101
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
102102
}
103103

104+
[Fact]
105+
public async Task TestValidBehaviorWithFullSpanAsync()
106+
{
107+
string testCode = @"
108+
class ClassName
109+
{
110+
int property;
111+
int PropertyName
112+
{
113+
get{return this.property;}
114+
}
115+
}
116+
";
117+
118+
DiagnosticResult expected = this.CSharpDiagnostic().WithArguments(string.Empty, "followed").WithSpan(7, 33, 7, 34);
119+
120+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
121+
}
122+
123+
[Fact]
124+
public async Task TestUnexpectedLocationForProjectDiagnosticAsync()
125+
{
126+
string testCode = @"
127+
class ClassName
128+
{
129+
int property;
130+
int PropertyName
131+
{
132+
get{return this.property;}
133+
}
134+
}
135+
";
136+
137+
// By failing to include a location, the verified thinks we're only trying to verify a project diagnostic.
138+
DiagnosticResult expected = this.CSharpDiagnostic().WithArguments(string.Empty, "followed");
139+
140+
var ex = await Assert.ThrowsAnyAsync<XunitException>(
141+
async () =>
142+
{
143+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
144+
}).ConfigureAwait(false);
145+
Assert.StartsWith("Expected:\nA project diagnostic with No location\nActual:\n", ex.Message);
146+
}
147+
104148
[Fact]
105149
public async Task TestUnexpectedMessageAsync()
106150
{
@@ -198,6 +242,31 @@ Int32 PropertyName
198242
Assert.Contains("error CS0246", ex.Message);
199243
}
200244

245+
[Fact]
246+
public async Task TestInvalidDiagnosticIdAsync()
247+
{
248+
string testCode = @"
249+
class ClassName
250+
{
251+
int property;
252+
int PropertyName
253+
{
254+
get{return this.property;}
255+
}
256+
}
257+
";
258+
259+
var descriptor = new DiagnosticDescriptor("SA9999", "Title", "Message", "Category", DiagnosticSeverity.Warning, isEnabledByDefault: true);
260+
DiagnosticResult expected = this.CSharpDiagnostic(descriptor).WithLocation(7, 33);
261+
262+
var ex = await Assert.ThrowsAnyAsync<XunitException>(
263+
async () =>
264+
{
265+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
266+
}).ConfigureAwait(false);
267+
Assert.StartsWith("Expected diagnostic id to be \"SA9999\" was \"SA1002\"", ex.Message);
268+
}
269+
201270
[Fact]
202271
public async Task TestInvalidSeverityAsync()
203272
{
@@ -223,7 +292,7 @@ int PropertyName
223292
}
224293

225294
[Fact]
226-
public async Task TestIncorrectLocationLineAsync()
295+
public async Task TestIncorrectLocationLine1Async()
227296
{
228297
string testCode = @"
229298
class ClassName
@@ -246,6 +315,35 @@ int PropertyName
246315
Assert.StartsWith("Expected diagnostic to start on line \"8\" was actually on line \"7\"", ex.Message);
247316
}
248317

318+
[Fact]
319+
public async Task TestIncorrectLocationLine2Async()
320+
{
321+
string testCode = @"
322+
class ClassName
323+
{
324+
int property;
325+
int PropertyName
326+
{
327+
get{return this.property;}
328+
set{this.property = value;}
329+
}
330+
}
331+
";
332+
333+
DiagnosticResult[] expected =
334+
{
335+
this.CSharpDiagnostic().WithArguments(string.Empty, "followed").WithLocation(7, 33),
336+
this.CSharpDiagnostic().WithArguments(string.Empty, "followed").WithLocation(7, 34),
337+
};
338+
339+
var ex = await Assert.ThrowsAnyAsync<XunitException>(
340+
async () =>
341+
{
342+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
343+
}).ConfigureAwait(false);
344+
Assert.StartsWith("Expected diagnostic to start on line \"7\" was actually on line \"8\"", ex.Message);
345+
}
346+
249347
[Fact]
250348
public async Task TestIncorrectLocationColumnAsync()
251349
{

0 commit comments

Comments
 (0)