From 502f6d10bf015ac570f8f1ca378882a9526fe0bc Mon Sep 17 00:00:00 2001 From: Brad Wilson Date: Sun, 22 Sep 2024 13:59:50 -0700 Subject: [PATCH 1/3] Bump up to v2.9.2-pre --- version.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version.json b/version.json index 392367442..c4bc1a514 100644 --- a/version.json +++ b/version.json @@ -1,6 +1,6 @@ { "$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json", - "version": "2.9.1", + "version": "2.9.2-pre.{height}", "nuGetPackageVersion": { "semVer": 2 }, From 0f8f1563876f2cbbb9af27e0bb14b824372fd4a8 Mon Sep 17 00:00:00 2001 From: Brad Wilson Date: Mon, 23 Sep 2024 20:09:47 -0700 Subject: [PATCH 2/3] #3031: Changes to TheoryData break tests with arrays of objects --- src/xunit.core/MemberDataAttributeBase.cs | 2 ++ src/xunit.core/Sdk/ITheoryData.cs | 14 ++++++++++ src/xunit.core/TheoryData.cs | 5 +++- .../Acceptance/Xunit2TheoryAcceptanceTests.cs | 26 +++++++++++++++++++ 4 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 src/xunit.core/Sdk/ITheoryData.cs diff --git a/src/xunit.core/MemberDataAttributeBase.cs b/src/xunit.core/MemberDataAttributeBase.cs index 621b065df..9e12fd34c 100644 --- a/src/xunit.core/MemberDataAttributeBase.cs +++ b/src/xunit.core/MemberDataAttributeBase.cs @@ -74,6 +74,8 @@ public override IEnumerable GetData(MethodInfo testMethod) var obj = accessor(); if (obj == null) return null; + if (obj is ITheoryData theoryData) + return theoryData.GetData(); if (obj is IEnumerable arrayEnumerable) return arrayEnumerable; if (obj is not IEnumerable enumerable) diff --git a/src/xunit.core/Sdk/ITheoryData.cs b/src/xunit.core/Sdk/ITheoryData.cs new file mode 100644 index 000000000..80d9d5651 --- /dev/null +++ b/src/xunit.core/Sdk/ITheoryData.cs @@ -0,0 +1,14 @@ +using System.Collections.Generic; + +namespace Xunit.Sdk +{ + // https://github.com/xunit/xunit/issues/3031 + // When the user provides TheoryData, the covariance of IEnumerable will silently + // convert SomeType[] into object[], and thus try to present the data as multiple parameter values + // of SomeType instead of a single parameter value of SomeType[]. Bypassing tests for IEnumerable + // here allows us to sidestep the covariance problem. + internal interface ITheoryData + { + IReadOnlyCollection GetData(); + } +} diff --git a/src/xunit.core/TheoryData.cs b/src/xunit.core/TheoryData.cs index b5fca749e..e789889f8 100644 --- a/src/xunit.core/TheoryData.cs +++ b/src/xunit.core/TheoryData.cs @@ -1,13 +1,14 @@ using System.Collections; using System.Collections.Generic; using System.Linq; +using Xunit.Sdk; namespace Xunit { /// /// Provides data for theories based on collection initialization syntax. /// - public abstract class TheoryData : IReadOnlyCollection + public abstract class TheoryData : IReadOnlyCollection, ITheoryData { readonly List data = []; @@ -42,6 +43,8 @@ protected void AddRows(IEnumerable rows) AddRow(row); } + IReadOnlyCollection ITheoryData.GetData() => data; + /// public IEnumerator GetEnumerator() => data.GetEnumerator(); diff --git a/test/test.xunit.execution/Acceptance/Xunit2TheoryAcceptanceTests.cs b/test/test.xunit.execution/Acceptance/Xunit2TheoryAcceptanceTests.cs index c39930cdc..be267c684 100644 --- a/test/test.xunit.execution/Acceptance/Xunit2TheoryAcceptanceTests.cs +++ b/test/test.xunit.execution/Acceptance/Xunit2TheoryAcceptanceTests.cs @@ -511,6 +511,32 @@ public static IEnumerable GenericData [Theory, MemberData("GenericData")] public void GenericTest(T value) { } } + + // https://github.com/xunit/xunit/issues/3031 + [Fact] + public void TheoryDataOfArray() + { + var results = Run(typeof(ClassWithTheoryDataOfArray)); + + Assert.Collection( + results.Cast().OrderBy(r => r.Test.DisplayName), + result => Assert.Equal("Xunit2TheoryAcceptanceTests+TheoryTests+ClassWithTheoryDataOfArray.TestMethod(_: [\"0\", \"2\", \"4\"])", result.Test.DisplayName), + result => Assert.Equal("Xunit2TheoryAcceptanceTests+TheoryTests+ClassWithTheoryDataOfArray.TestMethod(_: [\"0\", \"8\", \"6\"])", result.Test.DisplayName) + ); + } + + class ClassWithTheoryDataOfArray + { + public static TheoryData DataSource => + [ + ["0", "2", "4"], + ["0", "8", "6"], + ]; + + [Theory] + [MemberData(nameof(DataSource))] + public void TestMethod(string[] _) { } + } } public class InlineDataTests : AcceptanceTestV2 From 82543a6df6f5f13b5b70f8a9f9ccb41cd676084f Mon Sep 17 00:00:00 2001 From: Brad Wilson Date: Thu, 26 Sep 2024 20:04:33 -0700 Subject: [PATCH 3/3] v2.9.2 --- version.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version.json b/version.json index c4bc1a514..4f1d650c5 100644 --- a/version.json +++ b/version.json @@ -1,6 +1,6 @@ { "$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json", - "version": "2.9.2-pre.{height}", + "version": "2.9.2", "nuGetPackageVersion": { "semVer": 2 },