Skip to content

Commit

Permalink
xunit/xunit#2852: Array initializer in MemberData is not type checked…
Browse files Browse the repository at this point in the history
… correctly
  • Loading branch information
bradwilson committed Dec 23, 2023
1 parent 3caefc8 commit cf1f089
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,56 @@ public enum Foo {{ Bar }}
await Verify.VerifyAnalyzer(source);
}

// https://github.com/xunit/xunit/issues/2852
[Theory]
[InlineData("")]
[InlineData("#nullable enable")]
public async void ArrayInitializerWithCorrectType_DoesNotTrigger(string header)
{
var source = $@"
{header}
using System.Collections.Generic;
using Xunit;
public class TestClass {{
public static TheoryData<int> GetSequences(IEnumerable<int> seq) => new TheoryData<int> {{ 42, 2112 }};
[Theory]
[MemberData(nameof(GetSequences), new int[] {{ 1, 2 }})]
public void Test(int value) {{ }}
}}";

await Verify.VerifyAnalyzer(LanguageVersion.CSharp8, source);
}

[Theory]
[InlineData("")]
[InlineData("#nullable enable")]
public async void ArrayInitializerWithIncorrectType_Triggers(string header)
{
var source = $@"
{header}
using System.Collections.Generic;
using Xunit;
public class TestClass {{
public static TheoryData<int> GetSequences(IEnumerable<int> seq) => new TheoryData<int> {{ 42, 2112 }};
[Theory]
[MemberData(nameof(GetSequences), new char[] {{ 'a', 'b' }})]
public void Test(int value) {{ }}
}}";
var expected =
Verify
.Diagnostic("xUnit1035")
.WithSpan(11, 39, 11, 62)
.WithArguments("seq", "System.Collections.Generic.IEnumerable<int>");

await Verify.VerifyAnalyzer(LanguageVersion.CSharp8, source, expected);
}

[Fact]
public async void ValidMemberWithIncorrectArgumentTypes_Triggers()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ static void VerifyDataMethodParameterUsage(
break;
}

if (!value.HasValue || value.Value is null)
if (value.HasValue && value.Value is null)
{
var isValueTypeParam =
paramsElementType is not null
Expand Down

0 comments on commit cf1f089

Please sign in to comment.