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 TypeExtensions.ToTypeString method Exception. #639

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
34 changes: 28 additions & 6 deletions src/CommunityToolkit.Diagnostics/Extensions/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,18 @@ private static string FormatDisplayString(Type type, int genericTypeOffset, Read
genericTypeDefinition == typeof(ValueTuple<,,,,,,>) ||
genericTypeDefinition == typeof(ValueTuple<,,,,,,,>))
{
IEnumerable<string> formattedTypes = FormatDisplayStringForAllTypes(type.GetGenericArguments());
Type[] tupleArguments = type.GetGenericArguments();

// If the tuple is using open generics, format in the form (,,,) to match other generic type
// definitions. Note that it's not possible to have a mix of generic type parameters and
// concrete type arguments, so we just need to check the first one to know what to do here.
if (tupleArguments[0].IsGenericParameter)
{
return $"({new string(',', tupleArguments.Length - 1)})";
}

// If the tuple type is constructed, format it normally in the (T1, T2, ..., TN) format
IEnumerable<string> formattedTypes = FormatDisplayStringForAllTypes(tupleArguments);

return $"({string.Join(", ", formattedTypes)})";
}
Expand All @@ -147,10 +158,19 @@ private static string FormatDisplayString(Type type, int genericTypeOffset, Read
int genericArgumentsCount = int.Parse(tokens[1]);
int typeArgumentsOffset = typeArguments.Length - genericTypeOffset - genericArgumentsCount;
Type[] currentTypeArguments = typeArguments.Slice(typeArgumentsOffset, genericArgumentsCount).ToArray();
IEnumerable<string> formattedTypes = FormatDisplayStringForAllTypes(currentTypeArguments);

// Standard generic types are displayed as Foo<T>
displayName = $"{tokens[0]}<{string.Join(", ", formattedTypes)}>";
// Special case generic type parameters (same as with tuples)
if (currentTypeArguments[0].IsGenericParameter)
{
displayName = $"{tokens[0]}<{new string(',', currentTypeArguments.Length - 1)}>";
}
else
{
IEnumerable<string> formattedTypes = FormatDisplayStringForAllTypes(currentTypeArguments);

// Standard generic types are displayed as Foo<T>
displayName = $"{tokens[0]}<{string.Join(", ", formattedTypes)}>";
}

// Track the current offset for the shared generic arguments list
genericTypeOffset += genericArgumentsCount;
Expand All @@ -161,8 +181,10 @@ private static string FormatDisplayString(Type type, int genericTypeOffset, Read
displayName = type.Name;
}

// If the type is nested, recursively format the hierarchy as well
if (type.IsNested)
// If the type is nested, recursively format the hierarchy as well, unless the type is a generic type parameter. In that case,
// the declaring type would return the parent class that defined the generic type parameter. However, the current invocation of
// FormatDisplayString has already been invoked recursively while trying to format the parent class, so we need to stop here.
if (type.IsNested && !type.IsGenericParameter)
{
return $"{FormatDisplayString(type.DeclaringType!, genericTypeOffset, typeArguments)}.{displayName}";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,26 @@ public void Test_TypeExtensions_GenericTypes(string name, Type type)
Assert.AreEqual(name, type.ToTypeString());
}

[TestMethod]
[DataRow("System.Span<>", typeof(Span<>))]
[DataRow("System.Collections.Generic.List<>", typeof(List<>))]
[DataRow("System.Collections.Generic.Dictionary<,>", typeof(Dictionary<,>))]
[DataRow("(,)", typeof(ValueTuple<,>))]
[DataRow("(,,,,,)", typeof(ValueTuple<,,,,,>))]
[DataRow("CommunityToolkit.Diagnostics.UnitTests.Extensions.Test_TypeExtensions.Animal.Rabbit<>.Foo<>", typeof(Animal.Rabbit<>.Foo<>))]
[DataRow("CommunityToolkit.Diagnostics.UnitTests.Extensions.Test_TypeExtensions.Animal.Llama<,>.Foo<>", typeof(Animal.Llama<,>.Foo<>))]
public void Test_TypeExtensions_OpenGenericTypes(string name, Type type)
{
Assert.AreEqual(name, type.ToTypeString());
}

[TestMethod]
[DataRow("CommunityToolkit.Diagnostics.UnitTests.Extensions.Test_TypeExtensions.Animal", typeof(Animal))]
[DataRow("CommunityToolkit.Diagnostics.UnitTests.Extensions.Test_TypeExtensions.Animal.Cat", typeof(Animal.Cat))]
[DataRow("CommunityToolkit.Diagnostics.UnitTests.Extensions.Test_TypeExtensions.Animal.Dog", typeof(Animal.Dog))]
[DataRow("CommunityToolkit.Diagnostics.UnitTests.Extensions.Test_TypeExtensions.Animal.Rabbit<int?>", typeof(Animal.Rabbit<int?>))]
[DataRow("CommunityToolkit.Diagnostics.UnitTests.Extensions.Test_TypeExtensions.Animal.Rabbit<string>", typeof(Animal.Rabbit<string>))]
[DataRow("CommunityToolkit.Diagnostics.UnitTests.Extensions.Test_TypeExtensions.Animal.Rabbit<CommunityToolkit.Diagnostics.UnitTests.Extensions.Test_TypeExtensions.Animal.Dog>", typeof(Animal.Rabbit<Animal.Dog>))]
[DataRow("CommunityToolkit.Diagnostics.UnitTests.Extensions.Test_TypeExtensions.Animal.Rabbit<int>.Foo", typeof(Animal.Rabbit<int>.Foo))]
[DataRow("CommunityToolkit.Diagnostics.UnitTests.Extensions.Test_TypeExtensions.Animal.Rabbit<(string, int)?>.Foo", typeof(Animal.Rabbit<(string, int)?>.Foo))]
[DataRow("CommunityToolkit.Diagnostics.UnitTests.Extensions.Test_TypeExtensions.Animal.Rabbit<int>.Foo<string>", typeof(Animal.Rabbit<int>.Foo<string>))]
Expand Down