-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use Microsoft.CodeAnalysis.Testing for analyzer testing #122
Changes from all commits
1e24153
7e6c633
1d97cbd
d0da7b1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,9 @@ | ||
using System.Collections.Generic; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Verify = Xunit.Analyzers.CSharpVerifier<Xunit.Analyzers.AssertCollectionContainsShouldNotUseBoolCheck>; | ||
|
||
namespace Xunit.Analyzers | ||
{ | ||
public class AssertCollectionContainsShouldNotUseBoolCheckTests | ||
{ | ||
private readonly DiagnosticAnalyzer analyzer = new AssertCollectionContainsShouldNotUseBoolCheck(); | ||
|
||
public static TheoryData<string> Collections { get; } = new TheoryData<string> | ||
{ | ||
"new System.Collections.Generic.List<int>()", | ||
|
@@ -21,165 +17,155 @@ public class AssertCollectionContainsShouldNotUseBoolCheckTests | |
"System.Linq.Enumerable.Empty<int>()" | ||
}; | ||
|
||
private static void CheckDiagnostics(IEnumerable<Diagnostic> diagnostics) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We appear to be losing a significant part of the testing here; namely, that we are validating the code, message, and severity of the check. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are unlikely to change in a way that leads to a regression. With that said, there are a few things to keep in mind:
I'd recommend keeping most tests in this simplified form, and adding a minimal set of new tests to cover cases that might be prone to regression. |
||
{ | ||
Assert.Collection(diagnostics, d => | ||
{ | ||
Assert.Equal("Do not use Contains() to check if a value exists in a collection.", d.GetMessage()); | ||
Assert.Equal("xUnit2017", d.Id); | ||
Assert.Equal(DiagnosticSeverity.Warning, d.Severity); | ||
}); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(Collections))] | ||
public async void FindsWarningForTrueCollectionContainsCheck(string collection) | ||
{ | ||
var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer, | ||
var source = | ||
@"class TestClass { void TestMethod() { | ||
Xunit.Assert.True(" + collection + @".Contains(1)); | ||
} }"); | ||
[|Xunit.Assert.True(" + collection + @".Contains(1))|]; | ||
} }"; | ||
|
||
CheckDiagnostics(diagnostics); | ||
await Verify.VerifyAnalyzerAsync(source); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(Collections))] | ||
public async void FindsWarningForFalseCollectionContainsCheck(string collection) | ||
{ | ||
var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer, | ||
var source = | ||
@"class TestClass { void TestMethod() { | ||
Xunit.Assert.False(" + collection + @".Contains(1)); | ||
} }"); | ||
[|Xunit.Assert.False(" + collection + @".Contains(1))|]; | ||
} }"; | ||
|
||
CheckDiagnostics(diagnostics); | ||
await Verify.VerifyAnalyzerAsync(source); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(Enumerables))] | ||
public async void FindsWarningForTrueLinqContainsCheck(string enumerable) | ||
{ | ||
var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer, | ||
var source = | ||
@"using System.Linq; | ||
class TestClass { void TestMethod() { | ||
Xunit.Assert.True(" + enumerable + @".Contains(1)); | ||
} }"); | ||
[|Xunit.Assert.True(" + enumerable + @".Contains(1))|]; | ||
} }"; | ||
|
||
CheckDiagnostics(diagnostics); | ||
await Verify.VerifyAnalyzerAsync(source); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(Enumerables))] | ||
public async void FindsWarningForTrueLinqContainsCheckWithEqualityComparer(string enumerable) | ||
{ | ||
var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer, | ||
var source = | ||
@"using System.Linq; | ||
class TestClass { void TestMethod() { | ||
Xunit.Assert.True(" + enumerable + @".Contains(1, System.Collections.Generic.EqualityComparer<int>.Default)); | ||
} }"); | ||
[|Xunit.Assert.True(" + enumerable + @".Contains(1, System.Collections.Generic.EqualityComparer<int>.Default))|]; | ||
} }"; | ||
|
||
CheckDiagnostics(diagnostics); | ||
await Verify.VerifyAnalyzerAsync(source); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(Enumerables))] | ||
public async void FindsWarningForFalseLinqContainsCheck(string enumerable) | ||
{ | ||
var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer, | ||
var source = | ||
@"using System.Linq; | ||
class TestClass { void TestMethod() { | ||
Xunit.Assert.False(" + enumerable + @".Contains(1)); | ||
} }"); | ||
[|Xunit.Assert.False(" + enumerable + @".Contains(1))|]; | ||
} }"; | ||
|
||
CheckDiagnostics(diagnostics); | ||
await Verify.VerifyAnalyzerAsync(source); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(Enumerables))] | ||
public async void FindsWarningForFalseLinqContainsCheckWithEqualityComparer(string enumerable) | ||
{ | ||
var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer, | ||
var source = | ||
@"using System.Linq; | ||
class TestClass { void TestMethod() { | ||
Xunit.Assert.False(" + enumerable + @".Contains(1, System.Collections.Generic.EqualityComparer<int>.Default)); | ||
} }"); | ||
[|Xunit.Assert.False(" + enumerable + @".Contains(1, System.Collections.Generic.EqualityComparer<int>.Default))|]; | ||
} }"; | ||
|
||
CheckDiagnostics(diagnostics); | ||
await Verify.VerifyAnalyzerAsync(source); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(Collections))] | ||
public async void DoesNotFindWarningForTrueCollectionContainsCheckWithAssertionMessage(string collection) | ||
{ | ||
var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer, | ||
var source = | ||
@"class TestClass { void TestMethod() { | ||
Xunit.Assert.True(" + collection + @".Contains(1), ""Custom message""); | ||
} }"); | ||
} }"; | ||
|
||
Assert.Empty(diagnostics); | ||
await Verify.VerifyAnalyzerAsync(source); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(Collections))] | ||
public async void DoesNotFindWarningForFalseCollectionContainsCheckWithAssertionMessage(string collection) | ||
{ | ||
var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer, | ||
var source = | ||
@"class TestClass { void TestMethod() { | ||
Xunit.Assert.False(" + collection + @".Contains(1), ""Custom message""); | ||
} }"); | ||
} }"; | ||
|
||
Assert.Empty(diagnostics); | ||
await Verify.VerifyAnalyzerAsync(source); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(Enumerables))] | ||
public async void DoesNotFindWarningForTrueLinqContainsCheckWithAssertionMessage(string enumerable) | ||
{ | ||
var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer, | ||
var source = | ||
@"using System.Linq; | ||
class TestClass { void TestMethod() { | ||
Xunit.Assert.True(" + enumerable + @".Contains(1), ""Custom message""); | ||
} }"); | ||
} }"; | ||
|
||
Assert.Empty(diagnostics); | ||
await Verify.VerifyAnalyzerAsync(source); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(Enumerables))] | ||
public async void DoesNotFindWarningForFalseLinqContainsCheckWithAssertionMessage(string enumerable) | ||
{ | ||
var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer, | ||
var source = | ||
@"using System.Linq; | ||
class TestClass { void TestMethod() { | ||
Xunit.Assert.False(" + enumerable + @".Contains(1), ""Custom message""); | ||
} }"); | ||
} }"; | ||
|
||
Assert.Empty(diagnostics); | ||
await Verify.VerifyAnalyzerAsync(source); | ||
} | ||
|
||
[Fact] | ||
public async void DoesNotCrashForCollectionWithDifferentTypeParametersThanICollectionImplementation_ZeroParameters() | ||
{ | ||
var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer, | ||
var source = | ||
@"using System.Collections.Generic; | ||
class IntList : List<int> { } | ||
class TestClass { void TestMethod() { | ||
Xunit.Assert.False(new IntList().Contains(1)); | ||
} }"); | ||
[|Xunit.Assert.False(new IntList().Contains(1))|]; | ||
} }"; | ||
|
||
CheckDiagnostics(diagnostics); | ||
await Verify.VerifyAnalyzerAsync(source); | ||
} | ||
|
||
[Fact] | ||
public async void DoesNotCrashForCollectionWithDifferentTypeParametersThanICollectionImplementation_TwoParameters() | ||
{ | ||
var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer, | ||
var source = | ||
@"using System.Collections.Generic; | ||
class TestClass { void TestMethod() { | ||
Xunit.Assert.False(new Dictionary<int, int>().ContainsKey(1)); | ||
} }"); | ||
} }"; | ||
|
||
Assert.Empty(diagnostics); | ||
await Verify.VerifyAnalyzerAsync(source); | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 This version has a direct impact on downstream consumers. It should only be updated when necessary to use a new API, and even then alternatives should be used when possible.