Skip to content
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
48 changes: 48 additions & 0 deletions Cpp2IL.Core.Tests/GenericTypeArgsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.Linq;
using Cpp2IL.Core.Model.Contexts;
using Cpp2IL.Core.Utils;

namespace Cpp2IL.Core.Tests;


public class GenericTypeArgsTests
{
private ApplicationAnalysisContext _ctx = null!;

[SetUp]
public void Setup()
{
Cpp2IlApi.ResetInternalState();
_ctx = TestGameLoader.LoadSimple2022Game();
}

// Generic-instance types actually referenced in the fixture (field types — List<T>, Dictionary<K,V>, …).
private System.Collections.Generic.List<GenericInstanceTypeAnalysisContext> GenericInstances()
=> _ctx.Assemblies.SelectMany(a => a.Types)
.SelectMany(t => t.Fields.Select(f => f.FieldType))
.OfType<GenericInstanceTypeAnalysisContext>()
.ToList();

[Test]
public void Fixture_has_generic_instances()
{
Assert.That(GenericInstances(), Is.Not.Empty);
}

[Test]
public void GetTypeName_appends_the_argument_list()
{
foreach (var gi in GenericInstances())
{
var name = CsFileUtils.GetTypeName(gi);

Assert.That(name, Does.Contain("<").And.Contain(">"), name);
Assert.That(name, Does.Not.Contain("`"),
$"CLR generic arity marker should not be rendered: {name}");

// the argument text is exactly each argument's own GetTypeName, comma-joined
var expectedArgs = string.Join(", ", gi.GenericArguments.Select(CsFileUtils.GetTypeName));
Assert.That(name, Does.EndWith("<" + expectedArgs + ">"), name);
}
}
}
6 changes: 5 additions & 1 deletion Cpp2IL.Core/Utils/CsFileUtils.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.CodeDom.Compiler;
using System.Linq;
using System.Reflection;
using System.Text;
using Cpp2IL.Core.Logging;
Expand Down Expand Up @@ -368,7 +369,10 @@ public static string GetTypeName(TypeAnalysisContext type)
{
var genericTypeName = GetTypeName(genericInstanceType.GenericType);
var backTickIndex = genericTypeName.LastIndexOf('`');
return backTickIndex > 0 ? genericTypeName[..backTickIndex] : genericTypeName;
var baseName = backTickIndex > 0 ? genericTypeName[..backTickIndex] : genericTypeName;
return genericInstanceType.GenericArguments.Count > 0
? baseName + "<" + string.Join(", ", genericInstanceType.GenericArguments.Select(GetTypeName)) + ">"
: baseName;
}

if (type.Namespace is "System")
Expand Down
Loading