Skip to content

Commit

Permalink
Expand DiagnosticVerifier tests to cover additional edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
sharwell committed Jun 17, 2017
1 parent 819c488 commit a0ae67c
Showing 1 changed file with 99 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,50 @@ int PropertyName
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
}

[Fact]
public async Task TestValidBehaviorWithFullSpanAsync()
{
string testCode = @"
class ClassName
{
int property;
int PropertyName
{
get{return this.property;}
}
}
";

DiagnosticResult expected = this.CSharpDiagnostic().WithArguments(string.Empty, "followed").WithSpan(7, 33, 7, 34);

await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
}

[Fact]
public async Task TestUnexpectedLocationForProjectDiagnosticAsync()
{
string testCode = @"
class ClassName
{
int property;
int PropertyName
{
get{return this.property;}
}
}
";

// By failing to include a location, the verified thinks we're only trying to verify a project diagnostic.
DiagnosticResult expected = this.CSharpDiagnostic().WithArguments(string.Empty, "followed");

var ex = await Assert.ThrowsAnyAsync<XunitException>(
async () =>
{
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
}).ConfigureAwait(false);
Assert.StartsWith("Expected:\nA project diagnostic with No location\nActual:\n", ex.Message);
}

[Fact]
public async Task TestUnexpectedMessageAsync()
{
Expand Down Expand Up @@ -198,6 +242,31 @@ Int32 PropertyName
Assert.Contains("error CS0246", ex.Message);
}

[Fact]
public async Task TestInvalidDiagnosticIdAsync()
{
string testCode = @"
class ClassName
{
int property;
int PropertyName
{
get{return this.property;}
}
}
";

var descriptor = new DiagnosticDescriptor("SA9999", "Title", "Message", "Category", DiagnosticSeverity.Warning, isEnabledByDefault: true);
DiagnosticResult expected = this.CSharpDiagnostic(descriptor).WithLocation(7, 33);

var ex = await Assert.ThrowsAnyAsync<XunitException>(
async () =>
{
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
}).ConfigureAwait(false);
Assert.StartsWith("Expected diagnostic id to be \"SA9999\" was \"SA1002\"", ex.Message);
}

[Fact]
public async Task TestInvalidSeverityAsync()
{
Expand All @@ -223,7 +292,7 @@ int PropertyName
}

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

[Fact]
public async Task TestIncorrectLocationLine2Async()
{
string testCode = @"
class ClassName
{
int property;
int PropertyName
{
get{return this.property;}
set{this.property = value;}
}
}
";

DiagnosticResult[] expected =
{
this.CSharpDiagnostic().WithArguments(string.Empty, "followed").WithLocation(7, 33),
this.CSharpDiagnostic().WithArguments(string.Empty, "followed").WithLocation(7, 34),
};

var ex = await Assert.ThrowsAnyAsync<XunitException>(
async () =>
{
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
}).ConfigureAwait(false);
Assert.StartsWith("Expected diagnostic to start on line \"7\" was actually on line \"8\"", ex.Message);
}

[Fact]
public async Task TestIncorrectLocationColumnAsync()
{
Expand Down

0 comments on commit a0ae67c

Please sign in to comment.