-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update xUnit2018 fixer to use exactMatch (when supported) instead of …
…swapping asserts
- Loading branch information
1 parent
6b8347c
commit cf7f210
Showing
4 changed files
with
174 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 107 additions & 32 deletions
139
...xunit.analyzers.tests/Fixes/X2000/AssertIsTypeShouldNotBeUsedForAbstractTypeFixerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,119 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis; | ||
using Xunit; | ||
using Xunit.Analyzers; | ||
using Xunit.Analyzers.Fixes; | ||
using Verify = CSharpVerifier<Xunit.Analyzers.AssertIsTypeShouldNotBeUsedForAbstractType>; | ||
using Verify_v2_Pre2_9_3 = CSharpVerifier<AssertIsTypeShouldNotBeUsedForAbstractTypeFixerTests.Analyzer_v2_Pre2_9_3>; | ||
using Verify_v3_Pre0_6_0 = CSharpVerifier<AssertIsTypeShouldNotBeUsedForAbstractTypeFixerTests.Analyzer_v3_Pre0_6_0>; | ||
|
||
public class AssertIsTypeShouldNotBeUsedForAbstractTypeFixerTests | ||
{ | ||
const string template = /* lang=c#-test */ """ | ||
using System; | ||
using Xunit; | ||
public abstract class TestClass {{ | ||
[Fact] | ||
public void TestMethod() {{ | ||
var data = new object(); | ||
{0}; | ||
}} | ||
}} | ||
"""; | ||
|
||
[Theory] | ||
[InlineData( | ||
/* lang=c#-test */ "[|Assert.IsType<IDisposable>(data)|]", | ||
/* lang=c#-test */ "Assert.IsAssignableFrom<IDisposable>(data)")] | ||
[InlineData( | ||
/* lang=c#-test */ "[|Assert.IsType<TestClass>(data)|]", | ||
/* lang=c#-test */ "Assert.IsAssignableFrom<TestClass>(data)")] | ||
[InlineData( | ||
/* lang=c#-test */ "[|Assert.IsNotType<IDisposable>(data)|]", | ||
/* lang=c#-test */ "Assert.IsNotAssignableFrom<IDisposable>(data)")] | ||
[InlineData( | ||
/* lang=c#-test */ "[|Assert.IsNotType<TestClass>(data)|]", | ||
/* lang=c#-test */ "Assert.IsNotAssignableFrom<TestClass>(data)")] | ||
public async Task Conversions( | ||
string beforeAssert, | ||
string afterAssert) | ||
[Fact] | ||
public async Task Conversions_WithoutExactMatch() | ||
{ | ||
var before = string.Format(template, beforeAssert); | ||
var after = string.Format(template, afterAssert); | ||
var before = /* lang=c#-test */ """ | ||
using System; | ||
using Xunit; | ||
public abstract class TestClass { | ||
[Fact] | ||
public void TestMethod() { | ||
var data = new object(); | ||
[|Assert.IsType<IDisposable>(data)|]; | ||
[|Assert.IsType<TestClass>(data)|]; | ||
[|Assert.IsNotType<IDisposable>(data)|]; | ||
[|Assert.IsNotType<TestClass>(data)|]; | ||
} | ||
} | ||
"""; | ||
var after = /* lang=c#-test */ """ | ||
using System; | ||
using Xunit; | ||
public abstract class TestClass { | ||
[Fact] | ||
public void TestMethod() { | ||
var data = new object(); | ||
Assert.IsAssignableFrom<IDisposable>(data); | ||
Assert.IsAssignableFrom<TestClass>(data); | ||
Assert.IsNotAssignableFrom<IDisposable>(data); | ||
Assert.IsNotAssignableFrom<TestClass>(data); | ||
} | ||
} | ||
"""; | ||
|
||
await Verify_v2_Pre2_9_3.VerifyCodeFix(before, after, AssertIsTypeShouldNotBeUsedForAbstractTypeFixer.Key_UseAlternateAssert); | ||
await Verify_v3_Pre0_6_0.VerifyCodeFix(before, after, AssertIsTypeShouldNotBeUsedForAbstractTypeFixer.Key_UseAlternateAssert); | ||
} | ||
|
||
[Fact] | ||
public async Task Conversions_WithExactMatch() | ||
{ | ||
var before = /* lang=c#-test */ """ | ||
using System; | ||
using Xunit; | ||
public abstract class TestClass { | ||
[Fact] | ||
public void TestMethod() { | ||
var data = new object(); | ||
[|Assert.IsType<IDisposable>(data)|]; | ||
[|Assert.IsType<IDisposable>(data, true)|]; | ||
[|Assert.IsType<IDisposable>(data, exactMatch: true)|]; | ||
[|Assert.IsType<TestClass>(data)|]; | ||
[|Assert.IsType<TestClass>(data, true)|]; | ||
[|Assert.IsType<TestClass>(data, exactMatch: true)|]; | ||
[|Assert.IsNotType<IDisposable>(data)|]; | ||
[|Assert.IsNotType<IDisposable>(data, true)|]; | ||
[|Assert.IsNotType<IDisposable>(data, exactMatch: true)|]; | ||
[|Assert.IsNotType<TestClass>(data)|]; | ||
[|Assert.IsNotType<TestClass>(data, true)|]; | ||
[|Assert.IsNotType<TestClass>(data, exactMatch: true)|]; | ||
} | ||
} | ||
"""; | ||
var after = /* lang=c#-test */ """ | ||
using System; | ||
using Xunit; | ||
public abstract class TestClass { | ||
[Fact] | ||
public void TestMethod() { | ||
var data = new object(); | ||
Assert.IsType<IDisposable>(data, exactMatch: false); | ||
Assert.IsType<IDisposable>(data, exactMatch: false); | ||
Assert.IsType<IDisposable>(data, exactMatch: false); | ||
Assert.IsType<TestClass>(data, exactMatch: false); | ||
Assert.IsType<TestClass>(data, exactMatch: false); | ||
Assert.IsType<TestClass>(data, exactMatch: false); | ||
Assert.IsNotType<IDisposable>(data, exactMatch: false); | ||
Assert.IsNotType<IDisposable>(data, exactMatch: false); | ||
Assert.IsNotType<IDisposable>(data, exactMatch: false); | ||
Assert.IsNotType<TestClass>(data, exactMatch: false); | ||
Assert.IsNotType<TestClass>(data, exactMatch: false); | ||
Assert.IsNotType<TestClass>(data, exactMatch: false); | ||
} | ||
} | ||
"""; | ||
|
||
await Verify.VerifyCodeFix(before, after, AssertIsTypeShouldNotBeUsedForAbstractTypeFixer.Key_UseAlternateAssert); | ||
} | ||
|
||
internal class Analyzer_v2_Pre2_9_3 : AssertIsTypeShouldNotBeUsedForAbstractType | ||
{ | ||
protected override XunitContext CreateXunitContext(Compilation compilation) => | ||
XunitContext.ForV2(compilation, new Version(2, 9, 2)); | ||
} | ||
|
||
internal class Analyzer_v3_Pre0_6_0 : AssertIsTypeShouldNotBeUsedForAbstractType | ||
{ | ||
protected override XunitContext CreateXunitContext(Compilation compilation) => | ||
XunitContext.ForV3(compilation, new Version(0, 5, 999)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters