Skip to content

Commit

Permalink
Fix tuple regression before it's shipped :) (#4678)
Browse files Browse the repository at this point in the history
Co-authored-by: Amaury Levé <[email protected]>
  • Loading branch information
Youssef1313 and Evangelink authored Jan 16, 2025
1 parent b710679 commit 4a47388
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,13 @@ private static bool TryHandleTupleDataSource(object data, List<object[]> objects
return true;
}

static object GetFieldOrProperty(Type type, object data, string fieldOrPropertyName)
// ValueTuple is a value type, and uses fields for Items.
// Tuple is a reference type, and uses properties for Items.
=> type.IsValueType
? type.GetField(fieldOrPropertyName).GetValue(data)
: type.GetProperty(fieldOrPropertyName).GetValue(data);

static void ProcessTuple(object data, object[] array, int startingIndex)
{
Type type = data.GetType();
Expand All @@ -211,13 +218,11 @@ static void ProcessTuple(object data, object[] array, int startingIndex)
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);
array[startingIndex + i] = GetFieldOrProperty(type, data, $"Item{i + 1}");
continue;
}

object rest = type.GetProperty("Rest")?.GetValue(data) ??
type.GetField("Rest").GetValue(data)!;
object rest = GetFieldOrProperty(type, data, "Rest");
if (IsTupleOrValueTuple(rest, out _))
{
ProcessTuple(rest, array, startingIndex + 7);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// 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;
Expand Down Expand Up @@ -28,8 +28,12 @@ public async Task CanUseLongTuplesAndValueTuplesForAllFrameworks(string tfm)
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
Hello, , World
Hello2, , World2
Hello, , World
Hello2, , World2
""");
testHostResult.AssertOutputContainsSummary(failed: 0, passed: 8, skipped: 0);
testHostResult.AssertOutputContainsSummary(failed: 0, passed: 12, skipped: 0);
}

public sealed class TestAssetFixture() : TestAssetFixtureBase(AcceptanceFixture.NuGetGlobalPackagesFolder)
Expand Down Expand Up @@ -97,11 +101,19 @@ public void TestMethod1(int p1, int p2, int p3, int p4, int p5, int p6, int p7,
[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)
public void TestMethod2(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}");
}
[DynamicData(nameof(DataTupleString3))]
[DynamicData(nameof(DataValueTupleString3))]
[TestMethod]
public void TestMethod3(string p1, string p2, string p3)
{
s_builder.AppendLine($"{p1}, {p2}, {p3}");
}
public static IEnumerable<Tuple<int, int, int, int, int, int, int, Tuple<int>>> DataTuple8 =>
[
(1, 2, 3, 4, 5, 6, 7, 8).ToTuple(),
Expand All @@ -125,6 +137,18 @@ public void TestMethod1(int p1, int p2, int p3, int p4, int p5, int p6, int p7,
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
(11, 12, 13, 14, 15, 16, 17, 18, 19, 20),
];
public static IEnumerable<Tuple<string, string, string>> DataTupleString3 =>
[
("Hello", (string)null, "World").ToTuple(),
("Hello2", (string)null, "World2").ToTuple(),
];
public static IEnumerable<ValueTuple<string, string, string>> DataValueTupleString3 =>
[
("Hello", null, "World"),
("Hello2", null, "World2"),
];
}
Expand Down

0 comments on commit 4a47388

Please sign in to comment.