Skip to content

Commit 4a47388

Browse files
Fix tuple regression before it's shipped :) (#4678)
Co-authored-by: Amaury Levé <[email protected]>
1 parent b710679 commit 4a47388

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

src/TestFramework/TestFramework/Attributes/DataSource/DynamicDataOperations.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,13 @@ private static bool TryHandleTupleDataSource(object data, List<object[]> objects
202202
return true;
203203
}
204204

205+
static object GetFieldOrProperty(Type type, object data, string fieldOrPropertyName)
206+
// ValueTuple is a value type, and uses fields for Items.
207+
// Tuple is a reference type, and uses properties for Items.
208+
=> type.IsValueType
209+
? type.GetField(fieldOrPropertyName).GetValue(data)
210+
: type.GetProperty(fieldOrPropertyName).GetValue(data);
211+
205212
static void ProcessTuple(object data, object[] array, int startingIndex)
206213
{
207214
Type type = data.GetType();
@@ -211,13 +218,11 @@ static void ProcessTuple(object data, object[] array, int startingIndex)
211218
if (i != 7)
212219
{
213220
// Note: ItemN are properties on Tuple, but are fields on ValueTuple
214-
array[startingIndex + i] = type.GetField($"Item{i + 1}")?.GetValue(data)
215-
?? type.GetProperty($"Item{i + 1}").GetValue(data);
221+
array[startingIndex + i] = GetFieldOrProperty(type, data, $"Item{i + 1}");
216222
continue;
217223
}
218224

219-
object rest = type.GetProperty("Rest")?.GetValue(data) ??
220-
type.GetField("Rest").GetValue(data)!;
225+
object rest = GetFieldOrProperty(type, data, "Rest");
221226
if (IsTupleOrValueTuple(rest, out _))
222227
{
223228
ProcessTuple(rest, array, startingIndex + 7);

test/IntegrationTests/MSTest.Acceptance.IntegrationTests/TupleDynamicDataTests.cs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft Corporation. All rights reserved.
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

44
using Microsoft.Testing.Platform.Acceptance.IntegrationTests;
@@ -28,8 +28,12 @@ public async Task CanUseLongTuplesAndValueTuplesForAllFrameworks(string tfm)
2828
11, 12, 13, 14, 15, 16, 17, 18, 19, 20
2929
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
3030
11, 12, 13, 14, 15, 16, 17, 18, 19, 20
31+
Hello, , World
32+
Hello2, , World2
33+
Hello, , World
34+
Hello2, , World2
3135
""");
32-
testHostResult.AssertOutputContainsSummary(failed: 0, passed: 8, skipped: 0);
36+
testHostResult.AssertOutputContainsSummary(failed: 0, passed: 12, skipped: 0);
3337
}
3438

3539
public sealed class TestAssetFixture() : TestAssetFixtureBase(AcceptanceFixture.NuGetGlobalPackagesFolder)
@@ -97,11 +101,19 @@ public void TestMethod1(int p1, int p2, int p3, int p4, int p5, int p6, int p7,
97101
[DynamicData(nameof(DataTuple10))]
98102
[DynamicData(nameof(DataValueTuple10))]
99103
[TestMethod]
100-
public void TestMethod1(int p1, int p2, int p3, int p4, int p5, int p6, int p7, int p8, int p9, int p10)
104+
public void TestMethod2(int p1, int p2, int p3, int p4, int p5, int p6, int p7, int p8, int p9, int p10)
101105
{
102106
s_builder.AppendLine($"{p1}, {p2}, {p3}, {p4}, {p5}, {p6}, {p7}, {p8}, {p9}, {p10}");
103107
}
104108
109+
[DynamicData(nameof(DataTupleString3))]
110+
[DynamicData(nameof(DataValueTupleString3))]
111+
[TestMethod]
112+
public void TestMethod3(string p1, string p2, string p3)
113+
{
114+
s_builder.AppendLine($"{p1}, {p2}, {p3}");
115+
}
116+
105117
public static IEnumerable<Tuple<int, int, int, int, int, int, int, Tuple<int>>> DataTuple8 =>
106118
[
107119
(1, 2, 3, 4, 5, 6, 7, 8).ToTuple(),
@@ -125,6 +137,18 @@ public void TestMethod1(int p1, int p2, int p3, int p4, int p5, int p6, int p7,
125137
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
126138
(11, 12, 13, 14, 15, 16, 17, 18, 19, 20),
127139
];
140+
141+
public static IEnumerable<Tuple<string, string, string>> DataTupleString3 =>
142+
[
143+
("Hello", (string)null, "World").ToTuple(),
144+
("Hello2", (string)null, "World2").ToTuple(),
145+
];
146+
147+
public static IEnumerable<ValueTuple<string, string, string>> DataValueTupleString3 =>
148+
[
149+
("Hello", null, "World"),
150+
("Hello2", null, "World2"),
151+
];
128152
}
129153
130154

0 commit comments

Comments
 (0)