8000 #3031: Changes to TheoryData break tests with arrays of objects · xunit/xunit@0f8f156 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0f8f156

Browse files
committed
#3031: Changes to TheoryData break tests with arrays of objects
1 parent 502f6d1 commit 0f8f156

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

src/xunit.core/MemberDataAttributeBase.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ public override IEnumerable<object[]> GetData(MethodInfo testMethod)
7474
var obj = accessor();
7575
if (obj == null)
7676
return null;
77+
if (obj is ITheoryData theoryData)
78+
return theoryData.GetData();
7779
if (obj is IEnumerable<object[]> arrayEnumerable)
7880
return arrayEnumerable;
7981
if (obj is not IEnumerable enumerable)

src/xunit.core/Sdk/ITheoryData.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.Collections.Generic;
2+
3+
namespace Xunit.Sdk
4+
{
5+
// https://github.com/xunit/xunit/issues/3031
6+
// When the user provides TheoryData<SomeType[]>, the covariance of IEnumerable<T> will silently
7+
// convert SomeType[] into object[], and thus try to present the data as multiple parameter values
8+
// of SomeType instead of a single parameter value of SomeType[]. Bypassing tests for IEnumerable<object[]>
9+
// here allows us to sidestep the covariance problem.
10+
internal interface ITheoryData
11+
{
12+
IReadOnlyCollection<object[]> GetData();
13+
}
14+
}

src/xunit.core/TheoryData.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
using System.Collections;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using Xunit.Sdk;
45

56
namespace Xunit
67
{
78
/// <summary>
89
/// Provides data for theories based on collection initialization syntax.
910
/// </summary>
10-
public abstract class TheoryData : IReadOnlyCollection<object[]>
11+
public abstract class TheoryData : IReadOnlyCollection<object[]>, ITheoryData
1112
{
1213
readonly List<object[]> data = [];
1314

@@ -42,6 +43,8 @@ protected void AddRows(IEnumerable<object[]> rows)
4243
AddRow(row);
4344
}
4445

46+
IReadOnlyCollection<object[]> ITheoryData.GetData() => data;
47+
4548
/// <inheritdoc/>
4649
public IEnumerator<object[]> GetEnumerator() => data.GetEnumerator();
4750

test/test.xunit.execution/Acceptance/Xunit2TheoryAcceptanceTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,32 @@ public static IEnumerable<object[]> GenericData
511511
[Theory, MemberData("GenericData")]
512512
public void GenericTest<T>(T value) { }
513513
}
514+
515+
// https://github.com/xunit/xunit/issues/3031
516+
[Fact]
517+
public void TheoryDataOfArray()
518+
{
519+
var results = Run<ITestResultMessage>(typeof(ClassWithTheoryDataOfArray));
520+
521+
Assert.Collection(
522+
results.Cast<ITestPassed>().OrderBy(r => r.Test.DisplayName),
523+
result => Assert.Equal("Xunit2TheoryAcceptanceTests+TheoryTests+ClassWithTheoryDataOfArray.TestMethod(_: [\"0\", \"2\", \"4\"])", result.Test.DisplayName),
524+
result => Assert.Equal("Xunit2TheoryAcceptanceTests+TheoryTests+ClassWithTheoryDataOfArray.TestMethod(_: [\"0\", \"8\", \"6\"])", result.Test.DisplayName)
525+
);
526+
}
527+
528+
class ClassWithTheoryDataOfArray
529+
{
530+
public static TheoryData<string[]> DataSource =>
531+
[
532+
["0", "2", "4"],
533+
["0", "8", "6"],
534+
];
535+
536+
[Theory]
537+
[MemberData(nameof(DataSource))]
538+
public void TestMethod(string[] _) { }
539+
}
514540
}
515541

516542
public class InlineDataTests : AcceptanceTestV2

0 commit comments

Comments
 (0)
0