Skip to content
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

Adds an analyzer for suggesting tests move to the strongly-typed TheoryData classes (xunit/xunit#1244) #171

Merged
merged 11 commits into from
Dec 4, 2023

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public async void RemovesUnusedData()
using Xunit;

public class TestClass {
public static System.Collections.Generic.IEnumerable<object[]> TestData(int n) { yield return new object[] { n }; }
public static TheoryData<int> TestData(int n) => new TheoryData<int>();

[Theory]
[MemberData(nameof(TestData), 42, {|xUnit1036:21.12|})]
Expand All @@ -22,7 +22,7 @@ public void TestMethod(int a) { }
using Xunit;

public class TestClass {
public static System.Collections.Generic.IEnumerable<object[]> TestData(int n) { yield return new object[] { n }; }
public static TheoryData<int> TestData(int n) => new TheoryData<int>();

[Theory]
[MemberData(nameof(TestData), 42)]
Expand All @@ -43,7 +43,7 @@ public async void AddsParameterWithCorrectType(
using Xunit;

public class TestClass {{
public static System.Collections.Generic.IEnumerable<object[]> TestData(int n) {{ yield return new object[] {{ n }}; }}
public static TheoryData<int> TestData(int n) => new TheoryData<int>();

[Theory]
[MemberData(nameof(TestData), 42, {{|xUnit1036:{value}|}})]
Expand All @@ -54,7 +54,7 @@ public void TestMethod(int a) {{ }}
using Xunit;

public class TestClass {{
public static System.Collections.Generic.IEnumerable<object[]> TestData(int n, {valueType} p) {{ yield return new object[] {{ n }}; }}
public static TheoryData<int> TestData(int n, {valueType} p) => new TheoryData<int>();

[Theory]
[MemberData(nameof(TestData), 42, {value})]
Expand All @@ -71,7 +71,7 @@ public async void AddsParameterWithNonConflictingName()
using Xunit;

public class TestClass {{
public static System.Collections.Generic.IEnumerable<object[]> TestData(int p) {{ yield return new object[] {{ p }}; }}
public static TheoryData<int> TestData(int p) => new TheoryData<int>();

[Theory]
[MemberData(nameof(TestData), 42, {{|xUnit1036:21.12|}})]
Expand All @@ -82,7 +82,7 @@ public void TestMethod(int n) {{ }}
using Xunit;

public class TestClass {{
public static System.Collections.Generic.IEnumerable<object[]> TestData(int p, double p_2) {{ yield return new object[] {{ p }}; }}
public static TheoryData<int> TestData(int p, double p_2) => new TheoryData<int>();

[Theory]
[MemberData(nameof(TestData), 42, 21.12)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public async void ConvertStringToNameOf()
using Xunit;

public class TestClass {
public static IEnumerable<object[]> DataSource = Array.Empty<object[]>();
public static TheoryData<int> DataSource;

[Theory]
[MemberData({|xUnit1014:""DataSource""|})]
Expand All @@ -26,7 +26,7 @@ public void TestMethod(int a) { }
using Xunit;

public class TestClass {
public static IEnumerable<object[]> DataSource = Array.Empty<object[]>();
public static TheoryData<int> DataSource;

[Theory]
[MemberData(nameof(DataSource))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public async void MakesParameterNullable()
using Xunit;

public class TestClass {
public static System.Collections.Generic.IEnumerable<object[]> TestData(int n, int k) { yield return new object[] { n }; }
public static TheoryData<int> TestData(int n, int k) => new TheoryData<int>();

[Theory]
[MemberData(nameof(TestData), 42, {|xUnit1034:null|})]
Expand All @@ -23,7 +23,7 @@ public void TestMethod(int a) { }
using Xunit;

public class TestClass {
public static System.Collections.Generic.IEnumerable<object[]> TestData(int n, int? k) { yield return new object[] { n }; }
public static TheoryData<int> TestData(int n, int? k) => new TheoryData<int>();

[Theory]
[MemberData(nameof(TestData), 42, null)]
Expand All @@ -37,29 +37,29 @@ public void TestMethod(int a) { }
public async void MakesReferenceParameterNullable()
{
var before = @"
#nullable enable

using Xunit;

#nullable enable
public class TestClass {
public static System.Collections.Generic.IEnumerable<object[]> TestData(int n, string k) { yield return new object[] { n }; }
public static TheoryData<int> TestData(int n, string k) => new TheoryData<int> { n };

[Theory]
[MemberData(nameof(TestData), 42, {|xUnit1034:null|})]
public void TestMethod(int a) { }
#nullable restore
}";

var after = @"
#nullable enable

using Xunit;

#nullable enable
public class TestClass {
public static System.Collections.Generic.IEnumerable<object[]> TestData(int n, string? k) { yield return new object[] { n }; }
public static TheoryData<int> TestData(int n, string? k) => new TheoryData<int> { n };

[Theory]
[MemberData(nameof(TestData), 42, null)]
public void TestMethod(int a) { }
#nullable restore
}";

await Verify.VerifyCodeFix(LanguageVersion.CSharp8, before, after, MemberDataShouldReferenceValidMember_NullShouldNotBeUsedForIncompatibleParameterFixer.Key_MakeParameterNullable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public async void RemovesParametersFromNonMethodMemberData()

public class TestClass
{
public static IEnumerable<object[]> DataSource = Array.Empty<object[]>();
public static TheoryData<int> DataSource = new TheoryData<int>();

[Theory]
[MemberData(nameof(DataSource), {|xUnit1021:""abc"", 123|})]
Expand All @@ -28,7 +28,7 @@ public void TestMethod(int a) { }

public class TestClass
{
public static IEnumerable<object[]> DataSource = Array.Empty<object[]>();
public static TheoryData<int> DataSource = new TheoryData<int>();

[Theory]
[MemberData(nameof(DataSource))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class TestClass {
public static IEnumerable<object[]> Data => null;

[Theory]
[MemberData(nameof(Data))]
[{|xUnit1042:MemberData(nameof(Data))|}]
public void TestMethod(int a) { }
}";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public async void MarksDataMemberAsStatic()
using Xunit;

public class TestClass {
public IEnumerable<object[]> TestData => null;
public TheoryData<int> TestData => null;

[Theory]
[{|xUnit1017:MemberData(nameof(TestData))|}]
Expand All @@ -24,7 +24,7 @@ public void TestMethod(int x) { }
using Xunit;

public class TestClass {
public static IEnumerable<object[]> TestData => null;
public static TheoryData<int> TestData => null;

[Theory]
[MemberData(nameof(TestData))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public async void SetsPublicModifier(string badModifier)
using Xunit;

public class TestClass {{
{badModifier}static IEnumerable<object[]> TestData => null;
{badModifier}static TheoryData<int> TestData => null;

[Theory]
[{{|xUnit1016:MemberData(nameof(TestData))|}}]
Expand All @@ -27,7 +27,7 @@ public void TestMethod(int x) {{ }}
using Xunit;

public class TestClass {
public static IEnumerable<object[]> TestData => null;
public static TheoryData<int> TestData => null;

[Theory]
[MemberData(nameof(TestData))]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Imported from xUnit.net v3, must be removed when this test project is upgraded

#if NETFRAMEWORK

namespace System.Runtime.CompilerServices;

[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
internal sealed class CallerArgumentExpressionAttribute : Attribute
{
public CallerArgumentExpressionAttribute(string parameterName)
{
ParameterName = parameterName;
}

public string ParameterName { get; }
}

#endif
Loading