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

Fix Tuple/ValueTuple handling of TRest #4639

Merged
merged 2 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -192,28 +192,60 @@ private static bool TryHandleTupleDataSource(object data, List<object[]> objects
return true;
}
#else
Type type = data.GetType();
if (IsTupleOrValueTuple(data.GetType(), out int tupleSize)
if (IsTupleOrValueTuple(data, out int tupleSize)
&& (objects.Count == 0 || objects[objects.Count - 1].Length == tupleSize))
{
object[] array = new object[tupleSize];
for (int i = 0; i < tupleSize; i++)
{
array[i] = type.GetField($"Item{i + 1}")?.GetValue(data)!;
}
ProcessTuple(data, array, 0);

objects.Add(array);
return true;
}

static void ProcessTuple(object data, object[] array, int startingIndex)
{
Type type = data.GetType();
int tupleSize = type.GenericTypeArguments.Length;
for (int i = 0; i < tupleSize; i++)
{
if (i != 7)
{
// Note: ItemN are properties on Tuple, but are fields on ValueTuple
array[startingIndex + i] = type.GetField($"Item{i + 1}")?.GetValue(data)
?? type.GetProperty($"Item{i + 1}").GetValue(data);
continue;
}

object rest = type.GetProperty("Rest")?.GetValue(data) ??
type.GetField("Rest").GetValue(data)!;
if (IsTupleOrValueTuple(rest, out _))
{
ProcessTuple(rest, array, startingIndex + 7);
}
else
{
array[startingIndex + i] = rest;
}

return;
}
}
#endif

return false;
}

#if !NET471_OR_GREATER && !NETCOREAPP
private static bool IsTupleOrValueTuple(Type type, out int tupleSize)
private static bool IsTupleOrValueTuple(object? data, out int tupleSize)
{
tupleSize = 0;

if (data is null)
{
return false;
}

Type type = data.GetType();
if (!type.IsGenericType)
{
return false;
Expand All @@ -227,34 +259,69 @@ private static bool IsTupleOrValueTuple(Type type, out int tupleSize)
genericTypeDefinition == typeof(Tuple<,,,>) ||
genericTypeDefinition == typeof(Tuple<,,,,>) ||
genericTypeDefinition == typeof(Tuple<,,,,,>) ||
genericTypeDefinition == typeof(Tuple<,,,,,,>) ||
genericTypeDefinition == typeof(Tuple<,,,,,,,>))
genericTypeDefinition == typeof(Tuple<,,,,,,>))
{
tupleSize = type.GetGenericArguments().Length;
return true;
}

if (genericTypeDefinition == typeof(Tuple<,,,,,,,>))
{
object? last = type.GetProperty("Rest").GetValue(data);
if (IsTupleOrValueTuple(last, out int restSize))
{
tupleSize = 7 + restSize;
return true;
}
else
{
tupleSize = 8;
return true;
}
}

#if NET462
// TODO: https://github.com/microsoft/testfx/issues/4624
if (genericTypeDefinition.FullName.StartsWith("System.ValueTuple`", StringComparison.Ordinal))
{
tupleSize = type.GetGenericArguments().Length;
if (tupleSize == 8)
{
object? last = type.GetField("Rest").GetValue(data);
if (IsTupleOrValueTuple(last, out int restSize))
{
tupleSize = 7 + restSize;
}
}

return true;
}
#else
// TODO: https://github.com/microsoft/testfx/issues/4624
if (genericTypeDefinition == typeof(ValueTuple<>) ||
genericTypeDefinition == typeof(ValueTuple<,>) ||
genericTypeDefinition == typeof(ValueTuple<,,>) ||
genericTypeDefinition == typeof(ValueTuple<,,,>) ||
genericTypeDefinition == typeof(ValueTuple<,,,,>) ||
genericTypeDefinition == typeof(ValueTuple<,,,,,>) ||
genericTypeDefinition == typeof(ValueTuple<,,,,,,>) ||
genericTypeDefinition == typeof(ValueTuple<,,,,,,,>))
genericTypeDefinition == typeof(ValueTuple<,,,,,,>))
{
tupleSize = type.GetGenericArguments().Length;
return true;
}

if (genericTypeDefinition == typeof(ValueTuple<,,,,,,,>))
{
object? last = type.GetField("Rest").GetValue(data);
if (IsTupleOrValueTuple(last, out int restSize))
{
tupleSize = 7 + restSize;
return true;
}
else
{
tupleSize = 8;
return true;
}
}
#endif

return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Microsoft.Testing.Platform.Acceptance.IntegrationTests;
using Microsoft.Testing.Platform.Acceptance.IntegrationTests.Helpers;
using Microsoft.Testing.Platform.Helpers;

namespace MSTest.Acceptance.IntegrationTests;

[TestClass]
public sealed class TupleDynamicDataTests : AcceptanceTestBase<TupleDynamicDataTests.TestAssetFixture>
{
[TestMethod]
[DynamicData(nameof(TargetFrameworks.AllForDynamicData), typeof(TargetFrameworks))]
public async Task CanUseLongTuplesAndValueTuplesForAllFrameworks(string tfm)
{
var testHost = TestHost.LocateFrom(AssetFixture.ProjectPath, TestAssetFixture.ProjectName, tfm);
TestHostResult testHostResult = await testHost.ExecuteAsync("--settings my.runsettings");

// Assert
testHostResult.AssertExitCodeIs(ExitCodes.Success);
testHostResult.AssertOutputContains("""
1, 2, 3, 4, 5, 6, 7, 8
9, 10, 11, 12, 13, 14, 15, 16
1, 2, 3, 4, 5, 6, 7, 8
9, 10, 11, 12, 13, 14, 15, 16
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
11, 12, 13, 14, 15, 16, 17, 18, 19, 20
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
11, 12, 13, 14, 15, 16, 17, 18, 19, 20
""");
testHostResult.AssertOutputContainsSummary(failed: 0, passed: 8, skipped: 0);
}

public sealed class TestAssetFixture() : TestAssetFixtureBase(AcceptanceFixture.NuGetGlobalPackagesFolder)
{
public const string ProjectName = "TupleDynamicDataTests";

public string ProjectPath => GetAssetPath(ProjectName);

public override IEnumerable<(string ID, string Name, string Code)> GetAssetsToGenerate()
{
yield return (ProjectName, ProjectName,
SourceCode
.PatchTargetFrameworks(TargetFrameworks.All)
.PatchCodeWithReplace("$MSTestVersion$", MSTestVersion));
}

private const string SourceCode = """
#file TupleDynamicDataTests.csproj
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<EnableMSTestRunner>true</EnableMSTestRunner>
<TargetFrameworks>$TargetFrameworks$</TargetFrameworks>
<LangVersion>preview</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MSTest.TestAdapter" Version="$MSTestVersion$" />
<PackageReference Include="MSTest.TestFramework" Version="$MSTestVersion$" />
</ItemGroup>

<ItemGroup>
<None Update="*.runsettings">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

#file UnitTest1.cs
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public class UnitTest1
{
private static readonly StringBuilder s_builder = new();

[ClassCleanup]
public static void ClassCleanup()
{
Console.WriteLine(s_builder.ToString());
}

[DynamicData(nameof(DataTuple8))]
[DynamicData(nameof(DataValueTuple8))]
[TestMethod]
public void TestMethod1(int p1, int p2, int p3, int p4, int p5, int p6, int p7, int p8)
{
s_builder.AppendLine($"{p1}, {p2}, {p3}, {p4}, {p5}, {p6}, {p7}, {p8}");
}

[DynamicData(nameof(DataTuple10))]
[DynamicData(nameof(DataValueTuple10))]
[TestMethod]
public void TestMethod1(int p1, int p2, int p3, int p4, int p5, int p6, int p7, int p8, int p9, int p10)
{
s_builder.AppendLine($"{p1}, {p2}, {p3}, {p4}, {p5}, {p6}, {p7}, {p8}, {p9}, {p10}");
}

public static IEnumerable<Tuple<int, int, int, int, int, int, int, Tuple<int>>> DataTuple8 =>
[
(1, 2, 3, 4, 5, 6, 7, 8).ToTuple(),
(9, 10, 11, 12, 13, 14, 15, 16).ToTuple(),
];

public static IEnumerable<ValueTuple<int, int, int, int, int, int, int, ValueTuple<int>>> DataValueTuple8 =>
[
(1, 2, 3, 4, 5, 6, 7, 8),
(9, 10, 11, 12, 13, 14, 15, 16),
];

public static IEnumerable<Tuple<int, int, int, int, int, int, int, Tuple<int, int, int>>> DataTuple10 =>
[
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).ToTuple(),
(11, 12, 13, 14, 15, 16, 17, 18, 19, 20).ToTuple(),
];

public static IEnumerable<ValueTuple<int, int, int, int, int, int, int, ValueTuple<int, int, int>>> DataValueTuple10 =>
[
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
(11, 12, 13, 14, 15, 16, 17, 18, 19, 20),
];
}


#file my.runsettings
<RunSettings>
<MSTest>
<CaptureTraceOutput>false</CaptureTraceOutput>
</MSTest>
</RunSettings>
""";
}
}
Loading