Skip to content

Commit

Permalink
Revert "Use Microsoft.CodeAnalysis.Testing for analyzer testing (#122)…
Browse files Browse the repository at this point in the history
…" (#127)

This reverts commit 2c05a98.
  • Loading branch information
bradwilson authored Sep 3, 2019
1 parent 2c05a98 commit af7a545
Show file tree
Hide file tree
Showing 56 changed files with 2,197 additions and 1,816 deletions.
1 change: 0 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ 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
Expand Down
1 change: 0 additions & 1 deletion NuGet.Config
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@
<clear/>
<add key="myget.org/F/xunit" value="https://www.myget.org/F/xunit/api/v3/index.json" protocolVersion="3" />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
<add key="roslyn-analyzers" value="https://dotnet.myget.org/F/roslyn-analyzers/api/v3/index.json" protocolVersion="3" />
</packageSources>
</configuration>
7 changes: 3 additions & 4 deletions src/xunit.analyzers.fixes/xunit.analyzers.fixes.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<GenerateAssemblyTitleAttribute>false</GenerateAssemblyTitleAttribute>
<GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute>
<PackageTargetFallback>portable-net45+win8</PackageTargetFallback>
<RootNamespace>Xunit.Analyzers</RootNamespace>
<TargetFramework>netstandard1.1</TargetFramework>
<TargetFramework>netstandard1.3</TargetFramework>
<DebugType>full</DebugType>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="2.9.4" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="1.2.2" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="2.9.4" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="2.10.0" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions src/xunit.analyzers.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
<tags>xunit.analyzers, analyzers, roslyn, xunit, xunit.net</tags>
</metadata>
<files>
<file src="xunit.analyzers.fixes\bin\$Configuration$\netstandard1.1\xunit.analyzers.dll" target="analyzers\dotnet\cs\" />
<file src="xunit.analyzers.fixes\bin\$Configuration$\netstandard1.1\xunit.analyzers.fixes.dll" target="analyzers\dotnet\cs\" />
<file src="xunit.analyzers.fixes\bin\$Configuration$\netstandard1.3\xunit.analyzers.dll" target="analyzers\dotnet\cs\" />
<file src="xunit.analyzers.fixes\bin\$Configuration$\netstandard1.3\xunit.analyzers.fixes.dll" target="analyzers\dotnet\cs\" />
<file src="xunit.analyzers.fixes\tools\*.ps1" target="tools\" />
</files>
</package>
2 changes: 1 addition & 1 deletion src/xunit.analyzers/InlineDataMustMatchTheoryParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public InlineDataMustMatchTheoryParameters()
}

/// <summary>For testing purposes only.</summary>
protected InlineDataMustMatchTheoryParameters(string assemblyVersion) : base(new Version(assemblyVersion))
public InlineDataMustMatchTheoryParameters(string assemblyVersion) : base(new Version(assemblyVersion))
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class TheoryMethodCannotHaveDefaultParameter : XunitDiagnosticAnalyzer
public TheoryMethodCannotHaveDefaultParameter() { }

/// <summary>For testing purposes only.</summary>
protected TheoryMethodCannotHaveDefaultParameter(string assemblyVersion) : base(new Version(assemblyVersion)) { }
public TheoryMethodCannotHaveDefaultParameter(string assemblyVersion) : base(new Version(assemblyVersion)) { }

public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics =>
ImmutableArray.Create(Descriptors.X1023_TheoryMethodCannotHaveDefaultParameter);
Expand Down
2 changes: 1 addition & 1 deletion src/xunit.analyzers/TheoryMethodCannotHaveParamsArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class TheoryMethodCannotHaveParamsArray : XunitDiagnosticAnalyzer
public TheoryMethodCannotHaveParamsArray() { }

/// <summary>For testing purposes only.</summary>
protected TheoryMethodCannotHaveParamsArray(string assemblyVersion) : base(new Version(assemblyVersion)) { }
public TheoryMethodCannotHaveParamsArray(string assemblyVersion) : base(new Version(assemblyVersion)) { }

public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics =>
ImmutableArray.Create(Descriptors.X1022_TheoryMethodCannotHaveParameterArray);
Expand Down
7 changes: 3 additions & 4 deletions src/xunit.analyzers/xunit.analyzers.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<GenerateAssemblyTitleAttribute>false</GenerateAssemblyTitleAttribute>
<GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute>
<PackageTargetFallback>portable-net45+win8</PackageTargetFallback>
<RootNamespace>Xunit.Analyzers</RootNamespace>
<TargetFramework>netstandard1.1</TargetFramework>
<TargetFramework>netstandard1.3</TargetFramework>
<DebugType>full</DebugType>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="2.9.4" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="1.2.2" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="2.9.4" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="2.10.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
using Verify = Xunit.Analyzers.CSharpVerifier<Xunit.Analyzers.AssertCollectionContainsShouldNotUseBoolCheck>;
using System.Collections.Generic;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;

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>()",
Expand All @@ -17,155 +21,165 @@ public class AssertCollectionContainsShouldNotUseBoolCheckTests
"System.Linq.Enumerable.Empty<int>()"
};

private static void CheckDiagnostics(IEnumerable<Diagnostic> 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 source =
var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer,
@"class TestClass { void TestMethod() {
[|Xunit.Assert.True(" + collection + @".Contains(1))|];
} }";
Xunit.Assert.True(" + collection + @".Contains(1));
} }");

await Verify.VerifyAnalyzerAsync(source);
CheckDiagnostics(diagnostics);
}

[Theory]
[MemberData(nameof(Collections))]
public async void FindsWarningForFalseCollectionContainsCheck(string collection)
{
var source =
var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer,
@"class TestClass { void TestMethod() {
[|Xunit.Assert.False(" + collection + @".Contains(1))|];
} }";
Xunit.Assert.False(" + collection + @".Contains(1));
} }");

await Verify.VerifyAnalyzerAsync(source);
CheckDiagnostics(diagnostics);
}

[Theory]
[MemberData(nameof(Enumerables))]
public async void FindsWarningForTrueLinqContainsCheck(string enumerable)
{
var source =
var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer,
@"using System.Linq;
class TestClass { void TestMethod() {
[|Xunit.Assert.True(" + enumerable + @".Contains(1))|];
} }";
Xunit.Assert.True(" + enumerable + @".Contains(1));
} }");

await Verify.VerifyAnalyzerAsync(source);
CheckDiagnostics(diagnostics);
}

[Theory]
[MemberData(nameof(Enumerables))]
public async void FindsWarningForTrueLinqContainsCheckWithEqualityComparer(string enumerable)
{
var source =
var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer,
@"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));
} }");

await Verify.VerifyAnalyzerAsync(source);
CheckDiagnostics(diagnostics);
}

[Theory]
[MemberData(nameof(Enumerables))]
public async void FindsWarningForFalseLinqContainsCheck(string enumerable)
{
var source =
var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer,
@"using System.Linq;
class TestClass { void TestMethod() {
[|Xunit.Assert.False(" + enumerable + @".Contains(1))|];
} }";
Xunit.Assert.False(" + enumerable + @".Contains(1));
} }");

await Verify.VerifyAnalyzerAsync(source);
CheckDiagnostics(diagnostics);
}

[Theory]
[MemberData(nameof(Enumerables))]
public async void FindsWarningForFalseLinqContainsCheckWithEqualityComparer(string enumerable)
{
var source =
var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer,
@"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));
} }");

await Verify.VerifyAnalyzerAsync(source);
CheckDiagnostics(diagnostics);
}

[Theory]
[MemberData(nameof(Collections))]
public async void DoesNotFindWarningForTrueCollectionContainsCheckWithAssertionMessage(string collection)
{
var source =
var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer,
@"class TestClass { void TestMethod() {
Xunit.Assert.True(" + collection + @".Contains(1), ""Custom message"");
} }";
} }");

await Verify.VerifyAnalyzerAsync(source);
Assert.Empty(diagnostics);
}

[Theory]
[MemberData(nameof(Collections))]
public async void DoesNotFindWarningForFalseCollectionContainsCheckWithAssertionMessage(string collection)
{
var source =
var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer,
@"class TestClass { void TestMethod() {
Xunit.Assert.False(" + collection + @".Contains(1), ""Custom message"");
} }";
} }");

await Verify.VerifyAnalyzerAsync(source);
Assert.Empty(diagnostics);
}

[Theory]
[MemberData(nameof(Enumerables))]
public async void DoesNotFindWarningForTrueLinqContainsCheckWithAssertionMessage(string enumerable)
{
var source =
var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer,
@"using System.Linq;
class TestClass { void TestMethod() {
Xunit.Assert.True(" + enumerable + @".Contains(1), ""Custom message"");
} }";
} }");

await Verify.VerifyAnalyzerAsync(source);
Assert.Empty(diagnostics);
}

[Theory]
[MemberData(nameof(Enumerables))]
public async void DoesNotFindWarningForFalseLinqContainsCheckWithAssertionMessage(string enumerable)
{
var source =
var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer,
@"using System.Linq;
class TestClass { void TestMethod() {
Xunit.Assert.False(" + enumerable + @".Contains(1), ""Custom message"");
} }";
} }");

await Verify.VerifyAnalyzerAsync(source);
Assert.Empty(diagnostics);
}

[Fact]
public async void DoesNotCrashForCollectionWithDifferentTypeParametersThanICollectionImplementation_ZeroParameters()
{
var source =
var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer,
@"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));
} }");

await Verify.VerifyAnalyzerAsync(source);
CheckDiagnostics(diagnostics);
}

[Fact]
public async void DoesNotCrashForCollectionWithDifferentTypeParametersThanICollectionImplementation_TwoParameters()
{
var source =
var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer,
@"using System.Collections.Generic;
class TestClass { void TestMethod() {
Xunit.Assert.False(new Dictionary<int, int>().ContainsKey(1));
} }";
} }");

await Verify.VerifyAnalyzerAsync(source);
Assert.Empty(diagnostics);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using Verify = Xunit.Analyzers.CSharpVerifier<Xunit.Analyzers.AssertEmptyCollectionCheckShouldNotBeUsed>;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;

namespace Xunit.Analyzers
{
public class AssertEmptyCollectionCheckShouldNotBeUsedTests
{
private readonly DiagnosticAnalyzer analyzer = new AssertEmptyCollectionCheckShouldNotBeUsed();

public static TheoryData<string> Collections { get; } = new TheoryData<string>
{
"new int[0]",
Expand All @@ -17,23 +20,28 @@ public class AssertEmptyCollectionCheckShouldNotBeUsedTests
[MemberData(nameof(Collections))]
public async void FindsWarningForCollectionCheckWithoutAction(string collection)
{
var source =
var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer,
@"class TestClass { void TestMethod() {
[|Xunit.Assert.Collection(" + collection + @")|];
} }";
Xunit.Assert.Collection(" + collection + @");
} }");

await Verify.VerifyAnalyzerAsync(source);
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);
});
}

[Theory]
[MemberData(nameof(Collections))]
public async void DoesNotFindWarningForCollectionCheckWithAction(string collection)
{
var source =
var diagnostics = await CodeAnalyzerHelper.GetDiagnosticsAsync(analyzer,
@"class TestClass { void TestMethod() {
Xunit.Assert.Collection(" + collection + @", i => Xunit.Assert.True(true));
} }";
await Verify.VerifyAnalyzerAsync(source);
} }");
Assert.Empty(diagnostics);
}
}
}
Loading

0 comments on commit af7a545

Please sign in to comment.