diff --git a/.editorconfig b/.editorconfig
index 0194b90c..40b29b9d 100644
--- a/.editorconfig
+++ b/.editorconfig
@@ -10,6 +10,7 @@ insert_final_newline = true
# Sort using and Import directives with System.* appearing first
dotnet_sort_system_directives_first = true
+csharp_using_directive_placement = outside_namespace:warning
# Avoid "this." and "Me." if not necessary
dotnet_style_qualification_for_field = false:suggestion
diff --git a/NuGet.Config b/NuGet.Config
index fe6bfad5..07534d7a 100644
--- a/NuGet.Config
+++ b/NuGet.Config
@@ -4,5 +4,6 @@
+
diff --git a/src/xunit.analyzers.fixes/xunit.analyzers.fixes.csproj b/src/xunit.analyzers.fixes/xunit.analyzers.fixes.csproj
index df0e71ae..39b5306a 100644
--- a/src/xunit.analyzers.fixes/xunit.analyzers.fixes.csproj
+++ b/src/xunit.analyzers.fixes/xunit.analyzers.fixes.csproj
@@ -9,14 +9,15 @@
false
false
false
+ portable-net45+win8
Xunit.Analyzers
- netstandard1.3
+ netstandard1.1
full
-
-
+
+
diff --git a/src/xunit.analyzers.nuspec b/src/xunit.analyzers.nuspec
index f494b24e..80d715d2 100644
--- a/src/xunit.analyzers.nuspec
+++ b/src/xunit.analyzers.nuspec
@@ -15,8 +15,8 @@
xunit.analyzers, analyzers, roslyn, xunit, xunit.net
-
-
+
+
\ No newline at end of file
diff --git a/src/xunit.analyzers/InlineDataMustMatchTheoryParameters.cs b/src/xunit.analyzers/InlineDataMustMatchTheoryParameters.cs
index e4615638..68c803b3 100644
--- a/src/xunit.analyzers/InlineDataMustMatchTheoryParameters.cs
+++ b/src/xunit.analyzers/InlineDataMustMatchTheoryParameters.cs
@@ -23,7 +23,7 @@ public InlineDataMustMatchTheoryParameters()
}
/// For testing purposes only.
- public InlineDataMustMatchTheoryParameters(string assemblyVersion) : base(new Version(assemblyVersion))
+ protected InlineDataMustMatchTheoryParameters(string assemblyVersion) : base(new Version(assemblyVersion))
{
}
diff --git a/src/xunit.analyzers/TheoryMethodCannotHaveDefaultParameter.cs b/src/xunit.analyzers/TheoryMethodCannotHaveDefaultParameter.cs
index ab12d8ef..b28dc989 100644
--- a/src/xunit.analyzers/TheoryMethodCannotHaveDefaultParameter.cs
+++ b/src/xunit.analyzers/TheoryMethodCannotHaveDefaultParameter.cs
@@ -13,7 +13,7 @@ public class TheoryMethodCannotHaveDefaultParameter : XunitDiagnosticAnalyzer
public TheoryMethodCannotHaveDefaultParameter() { }
/// For testing purposes only.
- public TheoryMethodCannotHaveDefaultParameter(string assemblyVersion) : base(new Version(assemblyVersion)) { }
+ protected TheoryMethodCannotHaveDefaultParameter(string assemblyVersion) : base(new Version(assemblyVersion)) { }
public override ImmutableArray SupportedDiagnostics =>
ImmutableArray.Create(Descriptors.X1023_TheoryMethodCannotHaveDefaultParameter);
diff --git a/src/xunit.analyzers/TheoryMethodCannotHaveParamsArray.cs b/src/xunit.analyzers/TheoryMethodCannotHaveParamsArray.cs
index 0b39f3bc..91caae54 100644
--- a/src/xunit.analyzers/TheoryMethodCannotHaveParamsArray.cs
+++ b/src/xunit.analyzers/TheoryMethodCannotHaveParamsArray.cs
@@ -12,7 +12,7 @@ public class TheoryMethodCannotHaveParamsArray : XunitDiagnosticAnalyzer
public TheoryMethodCannotHaveParamsArray() { }
/// For testing purposes only.
- public TheoryMethodCannotHaveParamsArray(string assemblyVersion) : base(new Version(assemblyVersion)) { }
+ protected TheoryMethodCannotHaveParamsArray(string assemblyVersion) : base(new Version(assemblyVersion)) { }
public override ImmutableArray SupportedDiagnostics =>
ImmutableArray.Create(Descriptors.X1022_TheoryMethodCannotHaveParameterArray);
diff --git a/src/xunit.analyzers/xunit.analyzers.csproj b/src/xunit.analyzers/xunit.analyzers.csproj
index 9d23e53e..e0b32a1f 100644
--- a/src/xunit.analyzers/xunit.analyzers.csproj
+++ b/src/xunit.analyzers/xunit.analyzers.csproj
@@ -9,14 +9,15 @@
false
false
false
+ portable-net45+win8
Xunit.Analyzers
- netstandard1.3
+ netstandard1.1
full
-
-
+
+
\ No newline at end of file
diff --git a/test/xunit.analyzers.tests/AssertCollectionContainsShouldNotUseBoolCheckTests.cs b/test/xunit.analyzers.tests/AssertCollectionContainsShouldNotUseBoolCheckTests.cs
index c979949e..db281bba 100644
--- a/test/xunit.analyzers.tests/AssertCollectionContainsShouldNotUseBoolCheckTests.cs
+++ b/test/xunit.analyzers.tests/AssertCollectionContainsShouldNotUseBoolCheckTests.cs
@@ -1,13 +1,9 @@
-using System.Collections.Generic;
-using Microsoft.CodeAnalysis;
-using Microsoft.CodeAnalysis.Diagnostics;
+using Verify = Xunit.Analyzers.CSharpVerifier;
namespace Xunit.Analyzers
{
public class AssertCollectionContainsShouldNotUseBoolCheckTests
{
- private readonly DiagnosticAnalyzer analyzer = new AssertCollectionContainsShouldNotUseBoolCheck();
-
public static TheoryData Collections { get; } = new TheoryData
{
"new System.Collections.Generic.List()",
@@ -21,165 +17,155 @@ public class AssertCollectionContainsShouldNotUseBoolCheckTests
"System.Linq.Enumerable.Empty()"
};
- private static void CheckDiagnostics(IEnumerable diagnostics)
- {
- 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.Default));
-} }");
+ [|Xunit.Assert.True(" + enumerable + @".Contains(1, System.Collections.Generic.EqualityComparer.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.Default));
-} }");
+ [|Xunit.Assert.False(" + enumerable + @".Contains(1, System.Collections.Generic.EqualityComparer.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 { }
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().ContainsKey(1));
-} }");
+} }";
- Assert.Empty(diagnostics);
+ await Verify.VerifyAnalyzerAsync(source);
}
}
-}
\ No newline at end of file
+}
diff --git a/test/xunit.analyzers.tests/AssertEmptyCollectionCheckShouldNotBeUsedTests.cs b/test/xunit.analyzers.tests/AssertEmptyCollectionCheckShouldNotBeUsedTests.cs
index ecd3e227..f9df19e8 100644
--- a/test/xunit.analyzers.tests/AssertEmptyCollectionCheckShouldNotBeUsedTests.cs
+++ b/test/xunit.analyzers.tests/AssertEmptyCollectionCheckShouldNotBeUsedTests.cs
@@ -1,12 +1,9 @@
-using Microsoft.CodeAnalysis;
-using Microsoft.CodeAnalysis.Diagnostics;
+using Verify = Xunit.Analyzers.CSharpVerifier;
namespace Xunit.Analyzers
{
public class AssertEmptyCollectionCheckShouldNotBeUsedTests
{
- private readonly DiagnosticAnalyzer analyzer = new AssertEmptyCollectionCheckShouldNotBeUsed();
-
public static TheoryData Collections { get; } = new TheoryData
{
"new int[0]",
@@ -20,28 +17,23 @@ public class AssertEmptyCollectionCheckShouldNotBeUsedTests
[MemberData(nameof(Collections))]
public async void FindsWarningForCollectionCheckWithoutAction(string collection)
{
- var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer,
+ var source =
@"class TestClass { void TestMethod() {
- Xunit.Assert.Collection(" + collection + @");
-} }");
+ [|Xunit.Assert.Collection(" + collection + @")|];
+} }";
- Assert.Collection(diagnostics, d =>
- {
- Assert.Equal("Do not use Assert.Collection() to check for empty collections.", d.GetMessage());
- Assert.Equal("xUnit2011", d.Id);
- Assert.Equal(DiagnosticSeverity.Warning, d.Severity);
- });
+ await Verify.VerifyAnalyzerAsync(source);
}
[Theory]
[MemberData(nameof(Collections))]
public async void DoesNotFindWarningForCollectionCheckWithAction(string collection)
{
- var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer,
+ var source =
@"class TestClass { void TestMethod() {
Xunit.Assert.Collection(" + collection + @", i => Xunit.Assert.True(true));
-} }");
- Assert.Empty(diagnostics);
+} }";
+ await Verify.VerifyAnalyzerAsync(source);
}
}
}
diff --git a/test/xunit.analyzers.tests/AssertEnumerableAnyCheckShouldNotBeUsedForCollectionContainsCheckTests.cs b/test/xunit.analyzers.tests/AssertEnumerableAnyCheckShouldNotBeUsedForCollectionContainsCheckTests.cs
index e8ac7cc0..e9d8df02 100644
--- a/test/xunit.analyzers.tests/AssertEnumerableAnyCheckShouldNotBeUsedForCollectionContainsCheckTests.cs
+++ b/test/xunit.analyzers.tests/AssertEnumerableAnyCheckShouldNotBeUsedForCollectionContainsCheckTests.cs
@@ -1,30 +1,22 @@
-using Microsoft.CodeAnalysis;
-using Microsoft.CodeAnalysis.Diagnostics;
+using Verify = Xunit.Analyzers.CSharpVerifier;
namespace Xunit.Analyzers
{
public class AssertEnumerableAnyCheckShouldNotBeUsedForCollectionContainsCheckTests
{
- private readonly DiagnosticAnalyzer analyzer = new AssertEnumerableAnyCheckShouldNotBeUsedForCollectionContainsCheck();
-
public static TheoryData BooleanMethods = new TheoryData { "True", "False" };
[Theory]
[MemberData(nameof(BooleanMethods))]
public async void FindsWarning_ForLinqAnyCheck(string method)
{
- var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer,
+ var source =
@"using System.Linq;
class TestClass { void TestMethod() {
- Xunit.Assert." + method + @"(new [] { 1 }.Any(i => true));
-} }");
+ [|Xunit.Assert." + method + @"(new [] { 1 }.Any(i => true))|];
+} }";
- Assert.Collection(diagnostics, d =>
- {
- Assert.Equal("Do not use Enumerable.Any() to check if a value exists in a collection.", d.GetMessage());
- Assert.Equal("xUnit2012", d.Id);
- Assert.Equal(DiagnosticSeverity.Warning, d.Severity);
- });
+ await Verify.VerifyAnalyzerAsync(source);
}
}
-}
\ No newline at end of file
+}
diff --git a/test/xunit.analyzers.tests/AssertEqualGenericShouldNotBeUsedForStringValueTests.cs b/test/xunit.analyzers.tests/AssertEqualGenericShouldNotBeUsedForStringValueTests.cs
index 41a36e1d..a1753d16 100644
--- a/test/xunit.analyzers.tests/AssertEqualGenericShouldNotBeUsedForStringValueTests.cs
+++ b/test/xunit.analyzers.tests/AssertEqualGenericShouldNotBeUsedForStringValueTests.cs
@@ -1,12 +1,9 @@
-using Microsoft.CodeAnalysis;
-using Microsoft.CodeAnalysis.Diagnostics;
+using Verify = Xunit.Analyzers.CSharpVerifier;
namespace Xunit.Analyzers
{
public class AssertEqualGenericShouldNotBeUsedForStringValueTests
{
- readonly DiagnosticAnalyzer analyzer = new AssertEqualGenericShouldNotBeUsedForStringValue();
-
public static TheoryData Data { get; } = new TheoryData
{
{"true.ToString()", "\"True\""},
@@ -22,62 +19,47 @@ public class AssertEqualGenericShouldNotBeUsedForStringValueTests
[MemberData(nameof(Data))]
public async void DoesNotFindWarningForStringEqualityCheckWithoutGenericType(string expected, string value)
{
- var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer,
+ var source =
@"class TestClass { void TestMethod() {
Xunit.Assert.Equal(" + expected + @", " + value + @");
-} }");
- Assert.Empty(diagnostics);
+} }";
+ await Verify.VerifyAnalyzerAsync(source);
}
[Theory]
[MemberData(nameof(Data))]
public async void FindsWarningForStringEqualityCheckWithGenericType(string expected, string value)
{
- var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer,
+ var source =
@"class TestClass { void TestMethod() {
- Xunit.Assert.Equal(" + expected + @", " + value + @");
-} }");
+ [|Xunit.Assert.Equal(" + expected + @", " + value + @")|];
+} }";
- Assert.Collection(diagnostics, d =>
- {
- Assert.Equal("Do not use generic Assert.Equal overload to test for string equality.", d.GetMessage());
- Assert.Equal("xUnit2006", d.Id);
- Assert.Equal(DiagnosticSeverity.Warning, d.Severity);
- });
+ await Verify.VerifyAnalyzerAsync(source);
}
[Theory]
[MemberData(nameof(Data))]
public async void FindsWarningForStrictStringEqualityCheck(string expected, string value)
{
- var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer,
+ var source =
@"class TestClass { void TestMethod() {
- Xunit.Assert.StrictEqual(" + expected + @", " + value + @");
-} }");
+ [|Xunit.Assert.StrictEqual(" + expected + @", " + value + @")|];
+} }";
- Assert.Collection(diagnostics, d =>
- {
- Assert.Equal("Do not use Assert.StrictEqual to test for string equality.", d.GetMessage());
- Assert.Equal("xUnit2006", d.Id);
- Assert.Equal(DiagnosticSeverity.Warning, d.Severity);
- });
+ await Verify.VerifyAnalyzerAsync(source);
}
[Theory]
[MemberData(nameof(Data))]
public async void FindsWarningForStrictStringEqualityCheckWithGenericType(string expected, string value)
{
- var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer,
+ var source =
@"class TestClass { void TestMethod() {
- Xunit.Assert.StrictEqual(" + expected + @", " + value + @");
-} }");
+ [|Xunit.Assert.StrictEqual(" + expected + @", " + value + @")|];
+} }";
- Assert.Collection(diagnostics, d =>
- {
- Assert.Equal("Do not use Assert.StrictEqual to test for string equality.", d.GetMessage());
- Assert.Equal("xUnit2006", d.Id);
- Assert.Equal(DiagnosticSeverity.Warning, d.Severity);
- });
+ await Verify.VerifyAnalyzerAsync(source);
}
}
}
diff --git a/test/xunit.analyzers.tests/AssertEqualLiteralValueShouldBeFirstFixerTests.cs b/test/xunit.analyzers.tests/AssertEqualLiteralValueShouldBeFirstFixerTests.cs
index 46b6fd9d..0dc03670 100644
--- a/test/xunit.analyzers.tests/AssertEqualLiteralValueShouldBeFirstFixerTests.cs
+++ b/test/xunit.analyzers.tests/AssertEqualLiteralValueShouldBeFirstFixerTests.cs
@@ -1,13 +1,10 @@
-using Microsoft.CodeAnalysis.CodeFixes;
-using Microsoft.CodeAnalysis.Diagnostics;
+using Microsoft.CodeAnalysis.Testing;
+using Verify = Xunit.Analyzers.CSharpVerifier;
namespace Xunit.Analyzers
{
public class AssertEqualLiteralValueShouldBeFirstFixerTests
{
- readonly DiagnosticAnalyzer analyzer = new AssertEqualLiteralValueShouldBeFirst();
- readonly CodeFixProvider fixer = new AssertEqualLiteralValueShouldBeFirstFixer();
-
static readonly string Template = @"
public class TestClass
{{
@@ -23,44 +20,40 @@ public void TestMethod()
public async void SwapArguments()
{
var source = string.Format(Template, "Assert.Equal(i, 0)");
- var expected = string.Format(Template, "Assert.Equal(0, i)");
-
- var actual = await CodeAnalyzerHelper.GetFixedCodeAsync(analyzer, fixer, source);
+ var fixedSource = string.Format(Template, "Assert.Equal(0, i)");
- Assert.Equal(expected, actual);
+ var expected = Verify.Diagnostic().WithLocation(8, 9).WithArguments("0", "Assert.Equal(expected, actual)", "TestMethod", "TestClass");
+ await Verify.VerifyCodeFixAsync(source, expected, fixedSource);
}
[Fact]
public async void NamedArgumentsOnlySwapsArgumentValues()
{
var source = string.Format(Template, "Assert.Equal(actual: 0, expected: i)");
- var expected = string.Format(Template, "Assert.Equal(actual: i, expected: 0)");
+ var fixedSource = string.Format(Template, "Assert.Equal(actual: i, expected: 0)");
- var actual = await CodeAnalyzerHelper.GetFixedCodeAsync(analyzer, fixer, source);
-
- Assert.Equal(expected, actual);
+ var expected = Verify.Diagnostic().WithLocation(8, 9).WithArguments("0", "Assert.Equal(expected, actual)", "TestMethod", "TestClass");
+ await Verify.VerifyCodeFixAsync(source, expected, fixedSource);
}
[Fact]
public async void NamedArgumentsInCorrectPositionOnlySwapsArgumentValues()
{
var source = string.Format(Template, "Assert.Equal(expected: i, actual: 0)");
- var expected = string.Format(Template, "Assert.Equal(expected: 0, actual: i)");
-
- var actual = await CodeAnalyzerHelper.GetFixedCodeAsync(analyzer, fixer, source);
+ var fixedSource = string.Format(Template, "Assert.Equal(expected: 0, actual: i)");
- Assert.Equal(expected, actual);
+ var expected = Verify.Diagnostic().WithLocation(8, 9).WithArguments("0", "Assert.Equal(expected, actual)", "TestMethod", "TestClass");
+ await Verify.VerifyCodeFixAsync(source, expected, fixedSource);
}
[Fact]
public async void NamedArgumentsTakePossibleThirdParameterIntoAccount()
{
var source = string.Format(Template, "Assert.Equal(comparer: null, actual: 0, expected: i)");
- var expected = string.Format(Template, "Assert.Equal(comparer: null, actual: i, expected: 0)");
+ var fixedSource = string.Format(Template, "Assert.Equal(comparer: null, actual: i, expected: 0)");
- var actual = await CodeAnalyzerHelper.GetFixedCodeAsync(analyzer, fixer, source);
-
- Assert.Equal(expected, actual);
+ var expected = Verify.Diagnostic().WithLocation(8, 9).WithArguments("0", "Assert.Equal(expected, actual, comparer)", "TestMethod", "TestClass");
+ await Verify.VerifyCodeFixAsync(source, expected, fixedSource);
}
[Fact]
@@ -69,11 +62,14 @@ public async void PartiallyNamedArgumentsInCorrectPositionOnlySwapsArgumentValue
// C# 7.2 supports this new supported "non-trailing named arguments"
var source = string.Format(Template, "Assert.Equal(expected: i, 0)");
- var expected = string.Format(Template, "Assert.Equal(expected: 0, i)");
-
- var actual = await CodeAnalyzerHelper.GetFixedCodeAsync(analyzer, fixer, source, CompilationReporting.IgnoreErrors);
-
- Assert.Equal(expected, actual);
+ var fixedSource = string.Format(Template, "Assert.Equal(expected: 0, i)");
+
+ DiagnosticResult[] expected =
+ {
+ Verify.Diagnostic().WithLocation(8, 9).WithArguments("0", "Assert.Equal(expected, actual)", "TestMethod", "TestClass"),
+ Verify.CompilerError("CS1738").WithLocation(8, 41).WithMessage("Named argument specifications must appear after all fixed arguments have been specified"),
+ };
+ await Verify.VerifyCodeFixAsync(source, expected, fixedSource);
}
}
}
diff --git a/test/xunit.analyzers.tests/AssertEqualLiteralValueShouldBeFirstTests.cs b/test/xunit.analyzers.tests/AssertEqualLiteralValueShouldBeFirstTests.cs
index e73b227f..0da2a139 100644
--- a/test/xunit.analyzers.tests/AssertEqualLiteralValueShouldBeFirstTests.cs
+++ b/test/xunit.analyzers.tests/AssertEqualLiteralValueShouldBeFirstTests.cs
@@ -1,21 +1,18 @@
-using Microsoft.CodeAnalysis;
-using Microsoft.CodeAnalysis.Diagnostics;
+using Verify = Xunit.Analyzers.CSharpVerifier;
namespace Xunit.Analyzers
{
public class AssertEqualLiteralValueShouldBeFirstTests
{
- readonly DiagnosticAnalyzer analyzer = new AssertEqualLiteralValueShouldBeFirst();
-
[Fact]
public async void DoesNotFindWarningWhenConstantOrLiteralUsedForBothArguments()
{
- var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer,
+ var source =
@"class TestClass { void TestMethod() {
Xunit.Assert.Equal(""TestMethod"", nameof(TestMethod));
-} }");
+} }";
- Assert.Empty(diagnostics);
+ await Verify.VerifyAnalyzerAsync(source);
}
public static TheoryData TypesAndValues { get; } = new TheoryData
@@ -35,30 +32,26 @@ public async void DoesNotFindWarningWhenConstantOrLiteralUsedForBothArguments()
[MemberData(nameof(TypesAndValues))]
public async void DoesNotFindWarningForExpectedConstantOrLiteralValueAsFirstArgument(string type, string value)
{
- var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer,
+ var source =
@"class TestClass { void TestMethod() {
var v = default(" + type + @");
Xunit.Assert.Equal(" + value + @", v);
-} }");
- Assert.Empty(diagnostics);
+} }";
+ await Verify.VerifyAnalyzerAsync(source);
}
[Theory]
[MemberData(nameof(TypesAndValues))]
public async void FindsWarningForExpectedConstantOrLiteralValueAsSecondArgument(string type, string value)
{
- var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer,
+ var source =
@"class TestClass { void TestMethod() {
var v = default(" + type + @");
Xunit.Assert.Equal(v, " + value + @");
-} }");
+} }";
- Assert.Collection(diagnostics, d =>
- {
- Assert.Equal($"The literal or constant value {value} should be passed as the 'expected' argument in the call to 'Assert.Equal(expected, actual)' in method 'TestMethod' on type 'TestClass'.", d.GetMessage());
- Assert.Equal("xUnit2000", d.Id);
- Assert.Equal(DiagnosticSeverity.Warning, d.Severity);
- });
+ var expected = Verify.Diagnostic().WithLocation(3, 5).WithArguments(value, "Assert.Equal(expected, actual)", "TestMethod", "TestClass");
+ await Verify.VerifyAnalyzerAsync(source, expected);
}
[Theory]
@@ -67,47 +60,43 @@ public async void FindsWarningForExpectedConstantOrLiteralValueAsSecondArgument(
public async void DoesNotFindWarningForExpectedConstantOrLiteralValueAsNamedExpectedArgument(bool useAlternateForm)
{
var s = useAlternateForm ? "@" : "";
- var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer,
+ var source =
@"class TestClass { void TestMethod() {
var v = default(int);
Xunit.Assert.Equal(" + s + "actual: v, " + s + @"expected: 0);
-} }");
+} }";
- Assert.Empty(diagnostics);
+ await Verify.VerifyAnalyzerAsync(source);
}
[Theory]
[MemberData(nameof(TypesAndValues))]
public async void FindsWarningForExpectedConstantOrLiteralValueAsNamedExpectedArgument(string type, string value)
{
- var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer,
+ var source =
@"class TestClass { void TestMethod() {
var v = default(" + type + @");
Xunit.Assert.Equal(actual: " + value + @",expected: v);
-} }");
+} }";
- Assert.Collection(diagnostics, d =>
- {
- Assert.Equal($"The literal or constant value {value} should be passed as the 'expected' argument in the call to 'Assert.Equal(expected, actual)' in method 'TestMethod' on type 'TestClass'.", d.GetMessage());
- Assert.Equal("xUnit2000", d.Id);
- Assert.Equal(DiagnosticSeverity.Warning, d.Severity);
- });
+ var expected = Verify.Diagnostic().WithLocation(3, 5).WithArguments(value, "Assert.Equal(expected, actual)", "TestMethod", "TestClass");
+ await Verify.VerifyAnalyzerAsync(source, expected);
}
[Theory]
- [InlineData("act", "exp")]
- [InlineData("expected", "expected")]
- [InlineData("actual", "actual")]
- [InlineData("foo", "bar")]
+ [InlineData("{|CS1739:act|}", "exp")]
+ [InlineData("expected", "{|CS1740:expected|}")]
+ [InlineData("actual", "{|CS1740:actual|}")]
+ [InlineData("{|CS1739:foo|}", "bar")]
public async void DoesNotFindWarningWhenArgumentsAreNotNamedCorrectly(string firstArgumentName, string secondArgumentName)
{
- var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer, CompilationReporting.IgnoreErrors,
+ var source =
@"class TestClass { void TestMethod() {
var v = default(int);
Xunit.Assert.Equal(" + firstArgumentName + @": 1, " + secondArgumentName + @": v);
-} }");
+} }";
- Assert.Empty(diagnostics);
+ await Verify.VerifyAnalyzerAsync(source);
}
}
}
diff --git a/test/xunit.analyzers.tests/AssertEqualPrecisionShouldBeInRangeTest.cs b/test/xunit.analyzers.tests/AssertEqualPrecisionShouldBeInRangeTest.cs
index b91f405f..5371a2cf 100644
--- a/test/xunit.analyzers.tests/AssertEqualPrecisionShouldBeInRangeTest.cs
+++ b/test/xunit.analyzers.tests/AssertEqualPrecisionShouldBeInRangeTest.cs
@@ -1,93 +1,76 @@
-using System.Collections.Immutable;
-using System.Threading.Tasks;
-using Microsoft.CodeAnalysis;
-using Microsoft.CodeAnalysis.Diagnostics;
+using Microsoft.CodeAnalysis;
+using Verify = Xunit.Analyzers.CSharpVerifier;
namespace Xunit.Analyzers
{
public class AssertEqualPrecisionShouldBeInRangeTest
{
- public class Analyzer
- {
- readonly DiagnosticAnalyzer analyzer = new AssertEqualPrecisionShouldBeInRange();
-
- [Theory]
- [InlineData(0)]
- [InlineData(1)]
- [InlineData(8)]
- [InlineData(14)]
- [InlineData(15)]
- public async void DoesNotFindError_ForDoubleArgumentWithPrecisionProvidedInRange(int precision)
- {
- var diagnostics = await AnalyzeTestMethod(
- "double num = 0.133d;" +
- $"Xunit.Assert.Equal(0.13d, num, {precision});");
+ static readonly string Template = "class TestClass {{ void TestMethod() {{{0}}}}}";
- Assert.Empty(diagnostics);
- }
+ [Theory]
+ [InlineData(0)]
+ [InlineData(1)]
+ [InlineData(8)]
+ [InlineData(14)]
+ [InlineData(15)]
+ public async void DoesNotFindError_ForDoubleArgumentWithPrecisionProvidedInRange(int precision)
+ {
+ var source = string.Format(Template,
+ "double num = 0.133d;" +
+ $"Xunit.Assert.Equal(0.13d, num, {precision});");
- [Theory]
- [InlineData(int.MinValue)]
- [InlineData(-2000)]
- [InlineData(-1)]
- [InlineData(16)]
- [InlineData(17000)]
- [InlineData(int.MaxValue)]
- public async void FindsError_ForDoubleArgumentWithPrecisionProvidedOutOfRange(int precision)
- {
- var diagnostics = await AnalyzeTestMethod(
- "double num = 0.133d;" +
- $"Xunit.Assert.Equal(0.13d, num, {precision});");
+ await Verify.VerifyAnalyzerAsync(source);
+ }
- Assert.Collection(diagnostics, d =>
- {
- Assert.Equal("Keep precision in range [0..15] when asserting equality of double typed actual value.", d.GetMessage());
- Assert.Equal("xUnit2016", d.Id);
- Assert.Equal(DiagnosticSeverity.Error, d.Severity);
- });
- }
+ [Theory]
+ [InlineData(int.MinValue)]
+ [InlineData(-2000)]
+ [InlineData(-1)]
+ [InlineData(16)]
+ [InlineData(17000)]
+ [InlineData(int.MaxValue)]
+ public async void FindsError_ForDoubleArgumentWithPrecisionProvidedOutOfRange(int precision)
+ {
+ var source = string.Format(Template,
+ "double num = 0.133d;" +
+ $"Xunit.Assert.Equal(0.13d, num, {precision});");
- [Theory]
- [InlineData(0)]
- [InlineData(1)]
- [InlineData(14)]
- [InlineData(27)]
- [InlineData(28)]
- public async void DoesNotFindError_ForDecimalArgumentWithPrecisionProvidedInRange(int precision)
- {
- var diagnostics = await AnalyzeTestMethod(
- "decimal num = 0.133m;" +
- $"Xunit.Assert.Equal(0.13m, num, {precision});");
+ var expected = Verify.Diagnostic().WithLocation(1, 89).WithSeverity(DiagnosticSeverity.Error)
+ .WithArguments("[0..15]", "double");
+ await Verify.VerifyAnalyzerAsync(source, expected);
+ }
- Assert.Empty(diagnostics);
- }
+ [Theory]
+ [InlineData(0)]
+ [InlineData(1)]
+ [InlineData(14)]
+ [InlineData(27)]
+ [InlineData(28)]
+ public async void DoesNotFindError_ForDecimalArgumentWithPrecisionProvidedInRange(int precision)
+ {
+ var source = string.Format(Template,
+ "decimal num = 0.133m;" +
+ $"Xunit.Assert.Equal(0.13m, num, {precision});");
- [Theory]
- [InlineData(int.MinValue)]
- [InlineData(-2000)]
- [InlineData(-1)]
- [InlineData(29)]
- [InlineData(30000)]
- [InlineData(int.MaxValue)]
- public async void FindsError_ForDecimalArgumentWithPrecisionProvidedOutOfRange(int precision)
- {
- var diagnostics = await AnalyzeTestMethod(
- "decimal num = 0.133m;" +
- $"Xunit.Assert.Equal(0.13m, num, {precision});");
+ await Verify.VerifyAnalyzerAsync(source);
+ }
- Assert.Collection(diagnostics, d =>
- {
- Assert.Equal("Keep precision in range [0..28] when asserting equality of decimal typed actual value.", d.GetMessage());
- Assert.Equal("xUnit2016", d.Id);
- Assert.Equal(DiagnosticSeverity.Error, d.Severity);
- });
- }
+ [Theory]
+ [InlineData(int.MinValue)]
+ [InlineData(-2000)]
+ [InlineData(-1)]
+ [InlineData(29)]
+ [InlineData(30000)]
+ [InlineData(int.MaxValue)]
+ public async void FindsError_ForDecimalArgumentWithPrecisionProvidedOutOfRange(int precision)
+ {
+ var source = string.Format(Template,
+ "decimal num = 0.133m;" +
+ $"Xunit.Assert.Equal(0.13m, num, {precision});");
- private async Task> AnalyzeTestMethod(string methodBody)
- {
- return await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer,
- "class TestClass { void TestMethod() {" + methodBody + "}}");
- }
+ var expected = Verify.Diagnostic().WithLocation(1, 90).WithSeverity(DiagnosticSeverity.Error)
+ .WithArguments("[0..28]", "decimal");
+ await Verify.VerifyAnalyzerAsync(source, expected);
}
}
-}
\ No newline at end of file
+}
diff --git a/test/xunit.analyzers.tests/AssertEqualShouldNotBeUsedForBoolLiteralCheckTests.cs b/test/xunit.analyzers.tests/AssertEqualShouldNotBeUsedForBoolLiteralCheckTests.cs
index 44f5c81f..e19b5182 100644
--- a/test/xunit.analyzers.tests/AssertEqualShouldNotBeUsedForBoolLiteralCheckTests.cs
+++ b/test/xunit.analyzers.tests/AssertEqualShouldNotBeUsedForBoolLiteralCheckTests.cs
@@ -1,36 +1,24 @@
-using System.Collections.Generic;
-using Microsoft.CodeAnalysis;
-using Microsoft.CodeAnalysis.Diagnostics;
+using Microsoft.CodeAnalysis;
+using Verify = Xunit.Analyzers.CSharpVerifier;
namespace Xunit.Analyzers
{
public class AssertEqualShouldNotBeUsedForBoolLiteralCheckTests
{
- readonly DiagnosticAnalyzer analyzer = new AssertEqualShouldNotBeUsedForBoolLiteralCheck();
-
public static TheoryData Methods = new TheoryData { "Equal", "NotEqual", "StrictEqual", "NotStrictEqual", };
- private static void AssertHasDiagnostic(IEnumerable diagnostics, string method)
- {
- Assert.Collection(diagnostics, d =>
- {
- Assert.Equal($"Do not use Assert.{method}() to check for boolean conditions.", d.GetMessage());
- Assert.Equal("xUnit2004", d.Id);
- Assert.Equal(DiagnosticSeverity.Warning, d.Severity);
- });
- }
-
[Theory]
[MemberData(nameof(Methods))]
public async void FindsWarning_ForFirstBoolLiteral(string method)
{
- var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer,
+ var source =
@"class TestClass { void TestMethod() {
bool val = true;
Xunit.Assert." + method + @"(true, val);
-} }");
+} }";
- AssertHasDiagnostic(diagnostics, method);
+ var expected = Verify.Diagnostic().WithSpan(3, 5, 3, 29 + method.Length).WithSeverity(DiagnosticSeverity.Warning).WithArguments($"Assert.{method}()");
+ await Verify.VerifyAnalyzerAsync(source, expected);
}
[Theory]
@@ -38,52 +26,53 @@ public async void FindsWarning_ForFirstBoolLiteral(string method)
[InlineData("NotEqual")]
public async void FindsWarning_ForFirstBoolLiteral_WithCustomComparer(string method)
{
- var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer,
+ var source =
@"class TestClass { void TestMethod() {
bool val = false;
Xunit.Assert." + method + @"(false, val, System.Collections.Generic.EqualityComparer.Default);
-} }");
+} }";
- AssertHasDiagnostic(diagnostics, method);
+ var expected = Verify.Diagnostic().WithSpan(3, 5, 3, 89 + method.Length).WithSeverity(DiagnosticSeverity.Warning).WithArguments($"Assert.{method}()");
+ await Verify.VerifyAnalyzerAsync(source, expected);
}
[Theory]
[MemberData(nameof(Methods))]
public async void DoesNotFindWarning_ForFirstBoolLiteral_ObjectOverload(string method)
{
- var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer,
+ var source =
@"class TestClass { void TestMethod() {
object val = false;
Xunit.Assert." + method + @"(true, val);
-} }");
+} }";
- Assert.Empty(diagnostics);
+ await Verify.VerifyAnalyzerAsync(source);
}
[Theory]
[MemberData(nameof(Methods))]
public async void DoesNotFindWarning_ForOtherLiteral(string method)
{
- var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer,
+ var source =
@"class TestClass { void TestMethod() {
int val = 1;
Xunit.Assert." + method + @"(1, val);
-} }");
+} }";
- Assert.Empty(diagnostics);
+ await Verify.VerifyAnalyzerAsync(source);
}
[Theory]
[MemberData(nameof(Methods))]
public async void DoesNotFindWarning_ForSecondBoolLiteral(string method)
{
- var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer,
+ var source =
@"class TestClass { void TestMethod() {
bool val = false;
Xunit.Assert." + method + @"(val, true);
-} }");
+} }";
- Assert.Empty(diagnostics);
+ await Verify.VerifyAnalyzerAsync(source);
}
}
}
diff --git a/test/xunit.analyzers.tests/AssertEqualShouldNotBeUsedForCollectionSizeCheckTests.cs b/test/xunit.analyzers.tests/AssertEqualShouldNotBeUsedForCollectionSizeCheckTests.cs
index b3e5e30d..685c82fc 100644
--- a/test/xunit.analyzers.tests/AssertEqualShouldNotBeUsedForCollectionSizeCheckTests.cs
+++ b/test/xunit.analyzers.tests/AssertEqualShouldNotBeUsedForCollectionSizeCheckTests.cs
@@ -1,13 +1,10 @@
-using System.Collections.Generic;
using Microsoft.CodeAnalysis;
-using Microsoft.CodeAnalysis.Diagnostics;
+using Verify = Xunit.Analyzers.CSharpVerifier;
namespace Xunit.Analyzers
{
public class AssertEqualShouldNotBeUsedForCollectionSizeCheckTests
{
- private readonly DiagnosticAnalyzer analyzer = new AssertEqualShouldNotBeUsedForCollectionSizeCheck();
-
public static TheoryData Collections { get; } = new TheoryData
{
"new int[0].Length",
@@ -32,59 +29,52 @@ public class AssertEqualShouldNotBeUsedForCollectionSizeCheckTests
{ "System.Linq.Enumerable.Empty().Count()", 354 },
};
- private static void CheckDiagnostics(IEnumerable diagnostics, string method)
- {
- Assert.Collection(diagnostics, d =>
- {
- Assert.Equal($"Do not use Assert.{method}() to check for collection size.", d.GetMessage());
- Assert.Equal("xUnit2013", d.Id);
- Assert.Equal(DiagnosticSeverity.Warning, d.Severity);
- });
- }
-
[Theory]
[MemberData(nameof(Collections))]
public async void FindsWarningForEmptyCollectionSizeCheck(string collection)
{
- var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer,
+ var source =
@"using System.Linq;
class TestClass { void TestMethod() {
Xunit.Assert.Equal(0, " + collection + @");
-} }");
+} }";
- CheckDiagnostics(diagnostics, "Equal");
+ var expected = Verify.Diagnostic().WithSpan(3, 5, 3, 28 + collection.Length).WithSeverity(DiagnosticSeverity.Warning).WithArguments("Assert.Equal()");
+ await Verify.VerifyAnalyzerAsync(source, expected);
}
[Theory]
[MemberData(nameof(Collections))]
public async void FindsWarningForNonEmptyCollectionSizeCheck(string collection)
{
- var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer,
+ var source =
@"using System.Linq;
class TestClass { void TestMethod() {
Xunit.Assert.NotEqual(0, " + collection + @");
- } }");
+ } }";
- CheckDiagnostics(diagnostics, "NotEqual");
+ var expected = Verify.Diagnostic().WithSpan(3, 13, 3, 39 + collection.Length).WithSeverity(DiagnosticSeverity.Warning).WithArguments("Assert.NotEqual()");
+ await Verify.VerifyAnalyzerAsync(source, expected);
}
[Theory]
[MemberData(nameof(Collections))]
public async void FindsWarningForSingleItemCollectionSizeCheck(string collection)
{
- var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer,
+ var source =
@"using System.Linq;
class TestClass { void TestMethod() {
Xunit.Assert.Equal(1, " + collection + @");
- } }");
+ } }";
- CheckDiagnostics(diagnostics, "Equal");
+ var expected = Verify.Diagnostic().WithSpan(3, 13, 3, 36 + collection.Length).WithSeverity(DiagnosticSeverity.Warning).WithArguments("Assert.Equal()");
+ await Verify.VerifyAnalyzerAsync(source, expected);
}
[Fact]
public async void FindsWarningForSymbolDeclaringTypeHasZeroArity_ImplementsICollectionOfT()
{
- var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer, @"
+ var source = @"
using System.Collections;
using System.Collections.Generic;
using Xunit;
@@ -108,89 +98,90 @@ void TestMethod()
{
Assert.Equal(1, new IntCollection().Count);
}
-}");
+}";
- CheckDiagnostics(diagnostics, "Equal");
+ var expected = Verify.Diagnostic().WithSpan(23, 9, 23, 51).WithSeverity(DiagnosticSeverity.Warning).WithArguments("Assert.Equal()");
+ await Verify.VerifyAnalyzerAsync(source, expected);
}
[Theory]
[MemberData(nameof(Collections))]
public async void DoesNotFindWarningForNonSingleItemCollectionSizeCheck(string collection)
{
- var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer,
+ var source =
@"using System.Linq;
class TestClass { void TestMethod() {
Xunit.Assert.NotEqual(1, " + collection + @");
- } }");
+ } }";
- Assert.Empty(diagnostics);
+ await Verify.VerifyAnalyzerAsync(source);
}
[Theory]
[MemberData(nameof(CollectionsWithUnsupportedSize))]
public async void DoesNotFindWarningForUnsupportedCollectionSizeCheck(string collection, int size)
{
- var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer,
+ var source =
@"using System.Linq;
class TestClass { void TestMethod() {
Xunit.Assert.Equal(" + size + ", " + collection + @");
- } }");
+ } }";
- Assert.Empty(diagnostics);
+ await Verify.VerifyAnalyzerAsync(source);
}
[Theory]
[MemberData(nameof(CollectionsWithUnsupportedSize))]
public async void DoesNotFindWarningForUnsupportedNonEqualCollectionSizeCheck(string collection, int size)
{
- var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer,
+ var source =
@"using System.Linq;
class TestClass { void TestMethod() {
Xunit.Assert.NotEqual(" + size + ", " + collection + @");
- } }");
+ } }";
- Assert.Empty(diagnostics);
+ await Verify.VerifyAnalyzerAsync(source);
}
[Fact]
public async void DoesNotCrashForSymbolDeclaringTypeHasDifferentArityThanICollection_Zero()
{
- var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer,
+ var source =
@"using System.Collections.Generic;
interface IIntCollection : ICollection {
new int Count { get; }
}
class TestClass { void TestMethod() {
Xunit.Assert.Equal(1, ((IIntCollection)null).Count);
- } }");
+ } }";
- Assert.Empty(diagnostics);
+ await Verify.VerifyAnalyzerAsync(source);
}
[Fact]
public async void DoesNotCrashForSymbolDeclaringTypeHasDifferentArityThanICollection_Two()
{
- var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer,
+ var source =
@"using System.Collections.Generic;
interface IDictionary2 : ICollection> {
new int Count { get; }
}
class TestClass { void TestMethod() {
Xunit.Assert.Equal(1, ((IDictionary2)null).Count);
- } }");
+ } }";
- Assert.Empty(diagnostics);
+ await Verify.VerifyAnalyzerAsync(source);
}
[Fact]
public async void DoesNotCrash_ForNonIntArguments()
{
- var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer,
+ var source =
@"class TestClass { void TestMethod() {
Xunit.Assert.Equal('b', new int[0].Length);
- } }");
+ } }";
- Assert.Empty(diagnostics);
+ await Verify.VerifyAnalyzerAsync(source);
}
}
}
diff --git a/test/xunit.analyzers.tests/AssertEqualShouldNotBeUsedForNullCheckTests.cs b/test/xunit.analyzers.tests/AssertEqualShouldNotBeUsedForNullCheckTests.cs
index 256dbdf1..e905d471 100644
--- a/test/xunit.analyzers.tests/AssertEqualShouldNotBeUsedForNullCheckTests.cs
+++ b/test/xunit.analyzers.tests/AssertEqualShouldNotBeUsedForNullCheckTests.cs
@@ -1,12 +1,10 @@
using Microsoft.CodeAnalysis;
-using Microsoft.CodeAnalysis.Diagnostics;
+using Verify = Xunit.Analyzers.CSharpVerifier;
namespace Xunit.Analyzers
{
public class AssertEqualShouldNotBeUsedForNullCheckTests
{
- readonly DiagnosticAnalyzer analyzer = new AssertEqualShouldNotBeUsedForNullCheck();
-
public static TheoryData Methods = new TheoryData { "Equal", "NotEqual", "StrictEqual", "NotStrictEqual", "Same", "NotSame" };
[Theory]
@@ -14,18 +12,14 @@ public class AssertEqualShouldNotBeUsedForNullCheckTests
[InlineData("NotEqual")]
public async void FindsWarning_ForFirstNullLiteral_StringOverload(string method)
{
- var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer,
+ var source =
@"class TestClass { void TestMethod() {
string val = null;
Xunit.Assert." + method + @"(null, val);
-} }");
-
- Assert.Collection(diagnostics, d =>
- {
- Assert.Equal($"Do not use Assert.{method}() to check for null value.", d.GetMessage());
- Assert.Equal("xUnit2003", d.Id);
- Assert.Equal(DiagnosticSeverity.Warning, d.Severity);
- });
+} }";
+
+ var expected = Verify.Diagnostic().WithSpan(3, 5, 3, 29 + method.Length).WithSeverity(DiagnosticSeverity.Warning).WithArguments($"Assert.{method}()");
+ await Verify.VerifyAnalyzerAsync(source, expected);
}
[Theory]
@@ -33,36 +27,28 @@ public async void FindsWarning_ForFirstNullLiteral_StringOverload(string method)
[InlineData("NotEqual")]
public async void FindsWarning_ForFirstNullLiteral_StringOverload_WithCustomComparer(string method)
{
- var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer,
+ var source =
@"class TestClass { void TestMethod() {
string val = null;
Xunit.Assert." + method + @"(null, val, System.StringComparer.Ordinal);
-} }");
-
- Assert.Collection(diagnostics, d =>
- {
- Assert.Equal($"Do not use Assert.{method}() to check for null value.", d.GetMessage());
- Assert.Equal("xUnit2003", d.Id);
- Assert.Equal(DiagnosticSeverity.Warning, d.Severity);
- });
+} }";
+
+ var expected = Verify.Diagnostic().WithSpan(3, 5, 3, 60 + method.Length).WithSeverity(DiagnosticSeverity.Warning).WithArguments($"Assert.{method}()");
+ await Verify.VerifyAnalyzerAsync(source, expected);
}
[Theory]
[MemberData(nameof(Methods))]
public async void FindsWarning_ForFirstNullLiteral_ObjectOverload(string method)
{
- var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer,
+ var source =
@"class TestClass { void TestMethod() {
object val = null;
Xunit.Assert." + method + @"(null, val);
-} }");
-
- Assert.Collection(diagnostics, d =>
- {
- Assert.Equal($"Do not use Assert.{method}() to check for null value.", d.GetMessage());
- Assert.Equal("xUnit2003", d.Id);
- Assert.Equal(DiagnosticSeverity.Warning, d.Severity);
- });
+} }";
+
+ var expected = Verify.Diagnostic().WithSpan(3, 5, 3, 29 + method.Length).WithSeverity(DiagnosticSeverity.Warning).WithArguments($"Assert.{method}()");
+ await Verify.VerifyAnalyzerAsync(source, expected);
}
[Theory]
@@ -70,18 +56,14 @@ public async void FindsWarning_ForFirstNullLiteral_ObjectOverload(string method)
[InlineData("NotEqual")]
public async void FindsWarning_ForFirstNullLiteral_ObjectOverload_WithCustomComparer(string method)
{
- var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer,
+ var source =
@"class TestClass { void TestMethod() {
object val = null;
Xunit.Assert." + method + @"(null, val, System.Collections.Generic.EqualityComparer
diff --git a/tools/DocBuilder/DocBuilder.csproj b/tools/DocBuilder/DocBuilder.csproj
index 34490c95..762fe7e6 100644
--- a/tools/DocBuilder/DocBuilder.csproj
+++ b/tools/DocBuilder/DocBuilder.csproj
@@ -10,6 +10,10 @@
true
+
+
+
+