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
Changes from 1 commit
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
Loading