Update xUnit2007 for the new IsType/IsNotType overloads with exact match flag

This commit is contained in:
Brad Wilson 2024-11-02 20:49:11 -07:00
Родитель 17d74da8a4
Коммит 99947a946b
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 0B7BD15AD1EC5FDE
4 изменённых файлов: 103 добавлений и 52 удалений

Просмотреть файл

@ -1,44 +0,0 @@
using System.Threading.Tasks;
using Xunit;
using Verify = CSharpVerifier<Xunit.Analyzers.AssertIsTypeShouldUseGenericOverloadType>;
public class AssertIsTypeShouldUseGenericOverloadTests
{
public static TheoryData<string> Methods =
[
"IsType",
"IsNotType",
"IsAssignableFrom",
];
[Theory]
[MemberData(nameof(Methods))]
public async Task ForNonGenericCall_Triggers(string method)
{
var source = string.Format(/* lang=c#-test */ """
class TestClass {{
void TestMethod() {{
{{|#0:Xunit.Assert.{0}(typeof(int), 1)|}};
}}
}}
""", method);
var expected = Verify.Diagnostic().WithLocation(0).WithArguments("int");
await Verify.VerifyAnalyzer(source, expected);
}
[Theory]
[MemberData(nameof(Methods))]
public async Task ForGenericCall_DoesNotTrigger(string method)
{
var source = string.Format(/* lang=c#-test */ """
class TestClass {{
void TestMethod() {{
Xunit.Assert.{0}<int>(1);
}}
}}
""", method);
await Verify.VerifyAnalyzer(source);
}
}

Просмотреть файл

@ -1,12 +1,70 @@
#if NETCOREAPP
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CSharp;
using Xunit;
using Verify = CSharpVerifier<Xunit.Analyzers.AssertIsTypeShouldUseGenericOverloadType>;
#if NETCOREAPP
using Microsoft.CodeAnalysis.CSharp;
#endif
public class AssertIsTypeShouldUseGenericOverloadTypeTests
{
public static TheoryData<string> Methods =
[
"IsType",
"IsNotType",
"IsAssignableFrom",
];
[Theory]
[MemberData(nameof(Methods))]
public async Task ForNonGenericCall_Triggers(string method)
{
var source = string.Format(/* lang=c#-test */ """
class TestClass {{
void TestMethod() {{
{{|#0:Xunit.Assert.{0}(typeof(int), 1)|}};
}}
}}
""", method);
var expected = Verify.Diagnostic().WithLocation(0).WithArguments("int");
await Verify.VerifyAnalyzer(source, expected);
}
[Theory]
[MemberData(nameof(Methods))]
public async Task ForGenericCall_DoesNotTrigger(string method)
{
var source = string.Format(/* lang=c#-test */ """
class TestClass {{
void TestMethod() {{
Xunit.Assert.{0}<int>(1);
}}
}}
""", method);
await Verify.VerifyAnalyzer(source);
}
[Theory]
[InlineData("IsType")]
[InlineData("IsNotType")]
public async Task ForGenericCall_WithExactMatchFlag_DoesNotTrigger(string method)
{
var source = string.Format(/* lang=c#-test */ """
class TestClass {{
void TestMethod() {{
Xunit.Assert.{0}<int>(1, false);
Xunit.Assert.{0}<System.Type>(typeof(int), true);
}}
}}
""", method);
await Verify.VerifyAnalyzer(source);
}
#if NETCOREAPP
public class StaticAbstractInterfaceMethods
{
#if ROSLYN_LATEST // C# 11 is required for static abstract methods
@ -88,6 +146,6 @@ public class AssertIsTypeShouldUseGenericOverloadTypeTests
await Verify.VerifyAnalyzer(LanguageVersion.CSharp8, source, expected);
}
}
}
#endif
}

Просмотреть файл

@ -7,7 +7,7 @@ using Verify_X2015 = CSharpVerifier<Xunit.Analyzers.AssertThrowsShouldUseGeneric
public class UseGenericOverloadFixTests
{
[Fact]
public async Task X2007_SwitchesToGenericIsType()
public async Task X2007_SwitchesToGenericIsAssignableFrom()
{
var before = /* lang=c#-test */ """
using Xunit;
@ -17,7 +17,7 @@ public class UseGenericOverloadFixTests
public void TestMethod() {
var result = 123;
[|Assert.IsType(typeof(int), result)|];
[|Assert.IsAssignableFrom(typeof(int), result)|];
}
}
""";
@ -29,7 +29,7 @@ public class UseGenericOverloadFixTests
public void TestMethod() {
var result = 123;
Assert.IsType<int>(result);
Assert.IsAssignableFrom<int>(result);
}
}
""";
@ -37,6 +37,40 @@ public class UseGenericOverloadFixTests
await Verify_X2007.VerifyCodeFix(before, after, UseGenericOverloadFix.Key_UseAlternateAssert);
}
[Theory]
[InlineData("result")]
[InlineData("result, true")]
[InlineData("result, false")]
public async Task X2007_SwitchesToGenericIsType(string arguments)
{
var before = string.Format(/* lang=c#-test */ """
using Xunit;
public class TestClass {{
[Fact]
public void TestMethod() {{
var result = 123;
[|Assert.IsType(typeof(int), {0})|];
}}
}}
""", arguments);
var after = string.Format(/* lang=c#-test */ """
using Xunit;
public class TestClass {{
[Fact]
public void TestMethod() {{
var result = 123;
Assert.IsType<int>({0});
}}
}}
""", arguments);
await Verify_X2007.VerifyCodeFix(before, after, UseGenericOverloadFix.Key_UseAlternateAssert);
}
[Fact]
public async Task X2015_SwitchesToGenericThrows()
{

Просмотреть файл

@ -31,8 +31,11 @@ public class AssertIsTypeShouldUseGenericOverloadType : AssertUsageAnalyzerBase
Guard.ArgumentNotNull(invocationOperation);
Guard.ArgumentNotNull(method);
if (method.IsGenericMethod)
return;
var parameters = invocationOperation.TargetMethod.Parameters;
if (parameters.Length != 2)
if (parameters.Length < 2)
return;
var typeArgument = invocationOperation.Arguments.FirstOrDefault(arg => SymbolEqualityComparer.Default.Equals(arg.Parameter, parameters[0]));