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
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,155 @@ internal static class CustomDisplayNameTests_MethodDataSourceTest_ModuleInitiali
}


// ===== FILE SEPARATOR =====

// <auto-generated/>
#pragma warning disable

// <auto-generated/>
#pragma warning disable
#nullable enable
namespace TUnit.Generated;
internal sealed class CustomDisplayNameTests_TestParameterNamePrefixBug_TestSource_GUID : global::TUnit.Core.Interfaces.SourceGenerator.ITestSource
{
public async global::System.Collections.Generic.IAsyncEnumerable<global::TUnit.Core.TestMetadata> GetTestsAsync(string testSessionId, [global::System.Runtime.CompilerServices.EnumeratorCancellation] global::System.Threading.CancellationToken cancellationToken = default)
{
var metadata = new global::TUnit.Core.TestMetadata<global::TUnit.TestProject.CustomDisplayNameTests>
{
TestName = "TestParameterNamePrefixBug",
TestClassType = typeof(global::TUnit.TestProject.CustomDisplayNameTests),
TestMethodName = "TestParameterNamePrefixBug",
Dependencies = global::System.Array.Empty<global::TUnit.Core.TestDependency>(),
AttributeFactory = () =>
[
new global::TUnit.Core.TestAttribute(),
new global::TUnit.Core.DisplayNameAttribute("Test this($someValue, $someValueType)"),
new global::TUnit.Core.ArgumentsAttribute(100, "Type1"),
new global::TUnit.Core.ArgumentsAttribute(200, "Type2"),
new global::TUnit.Core.ArgumentsAttribute(300, "Type1"),
new global::TUnit.TestProject.Attributes.EngineTest(global::TUnit.TestProject.Attributes.ExpectedResult.Pass)
],
DataSources = new global::TUnit.Core.IDataSourceAttribute[]
{
new global::TUnit.Core.ArgumentsAttribute(100, "Type1"),
new global::TUnit.Core.ArgumentsAttribute(200, "Type2"),
new global::TUnit.Core.ArgumentsAttribute(300, "Type1"),
},
ClassDataSources = new global::TUnit.Core.IDataSourceAttribute[]
{
},
PropertyDataSources = new global::TUnit.Core.PropertyDataSource[]
{
},
PropertyInjections = new global::TUnit.Core.PropertyInjectionData[]
{
},
InheritanceDepth = 0,
FilePath = @"",
LineNumber = 45,
MethodMetadata = new global::TUnit.Core.MethodMetadata
{
Type = typeof(global::TUnit.TestProject.CustomDisplayNameTests),
TypeReference = global::TUnit.Core.TypeReference.CreateConcrete("TUnit.TestProject.CustomDisplayNameTests, TestsBase`1"),
Name = "TestParameterNamePrefixBug",
GenericTypeCount = 0,
ReturnType = typeof(global::System.Threading.Tasks.Task),
ReturnTypeReference = global::TUnit.Core.TypeReference.CreateConcrete("System.Threading.Tasks.Task, System.Private.CoreLib"),
Parameters = new global::TUnit.Core.ParameterMetadata[]
{
new global::TUnit.Core.ParameterMetadata(typeof(int))
{
Name = "someValue",
TypeReference = global::TUnit.Core.TypeReference.CreateConcrete("int, System.Private.CoreLib"),
IsNullable = false,
ReflectionInfo = typeof(global::TUnit.TestProject.CustomDisplayNameTests).GetMethod("TestParameterNamePrefixBug", global::System.Reflection.BindingFlags.Public | global::System.Reflection.BindingFlags.Instance, null, new global::System.Type[] { typeof(int), typeof(string) }, null)!.GetParameters()[0]
},
new global::TUnit.Core.ParameterMetadata(typeof(string))
{
Name = "someValueType",
TypeReference = global::TUnit.Core.TypeReference.CreateConcrete("string, System.Private.CoreLib"),
IsNullable = false,
ReflectionInfo = typeof(global::TUnit.TestProject.CustomDisplayNameTests).GetMethod("TestParameterNamePrefixBug", global::System.Reflection.BindingFlags.Public | global::System.Reflection.BindingFlags.Instance, null, new global::System.Type[] { typeof(int), typeof(string) }, null)!.GetParameters()[1]
}
},
Class = global::TUnit.Core.ClassMetadata.GetOrAdd("TestsBase`1:global::TUnit.TestProject.CustomDisplayNameTests", () =>
{
var classMetadata = new global::TUnit.Core.ClassMetadata
{
Type = typeof(global::TUnit.TestProject.CustomDisplayNameTests),
TypeReference = global::TUnit.Core.TypeReference.CreateConcrete("TUnit.TestProject.CustomDisplayNameTests, TestsBase`1"),
Name = "CustomDisplayNameTests",
Namespace = "TUnit.TestProject",
Assembly = global::TUnit.Core.AssemblyMetadata.GetOrAdd("TestsBase`1", () => new global::TUnit.Core.AssemblyMetadata { Name = "TestsBase`1" }),
Parameters = global::System.Array.Empty<global::TUnit.Core.ParameterMetadata>(),
Properties = new global::TUnit.Core.PropertyMetadata[]
{
new global::TUnit.Core.PropertyMetadata
{
ReflectionInfo = typeof(global::TUnit.TestProject.CustomDisplayNameTests).GetProperty("Order"),
Type = typeof(int),
Name = "Order",
IsStatic = false,
IsNullable = false,
Getter = o => ((global::TUnit.TestProject.CustomDisplayNameTests)o).Order,
ClassMetadata = null!,
ContainingTypeMetadata = null!
}
},
Parent = null
};
// Set ClassMetadata and ContainingTypeMetadata references on properties to avoid circular dependency
foreach (var prop in classMetadata.Properties)
{
prop.ClassMetadata = classMetadata;
prop.ContainingTypeMetadata = classMetadata;
}
return classMetadata;
})
},
InstanceFactory = (typeArgs, args) => new global::TUnit.TestProject.CustomDisplayNameTests(),
TestInvoker = async (instance, args) =>
{
var typedInstance = (global::TUnit.TestProject.CustomDisplayNameTests)instance;
var context = global::TUnit.Core.TestContext.Current;
// Invoke with only the arguments that were provided
switch (args.Length)
{
case 2:
await typedInstance.TestParameterNamePrefixBug(TUnit.Core.Helpers.CastHelper.Cast<int>(args[0]), TUnit.Core.Helpers.CastHelper.Cast<string>(args[1]));
break;
default:
throw new global::System.ArgumentException($"Expected exactly 2 arguments, but got {args.Length}");
}
},
InvokeTypedTest = async (instance, args, cancellationToken) =>
{
// Invoke with only the arguments that were provided
switch (args.Length)
{
case 2:
await instance.TestParameterNamePrefixBug(TUnit.Core.Helpers.CastHelper.Cast<int>(args[0]), TUnit.Core.Helpers.CastHelper.Cast<string>(args[1]));
break;
default:
throw new global::System.ArgumentException($"Expected exactly 2 arguments, but got {args.Length}");
}
},
};
metadata.UseRuntimeDataGeneration(testSessionId);
yield return metadata;
yield break;
}
}
internal static class CustomDisplayNameTests_TestParameterNamePrefixBug_ModuleInitializer_GUID
{
[global::System.Runtime.CompilerServices.ModuleInitializer]
public static void Initialize()
{
global::TUnit.Core.SourceRegistrar.Register(typeof(global::TUnit.TestProject.CustomDisplayNameTests), new CustomDisplayNameTests_TestParameterNamePrefixBug_TestSource_GUID());
}
}


// ===== FILE SEPARATOR =====

// <auto-generated/>
Expand Down Expand Up @@ -561,7 +710,7 @@ internal sealed class CustomDisplayNameTests_PasswordTest_TestSource_GUID : glob
},
InheritanceDepth = 0,
FilePath = @"",
LineNumber = 45,
LineNumber = 65,
MethodMetadata = new global::TUnit.Core.MethodMetadata
{
Type = typeof(global::TUnit.TestProject.CustomDisplayNameTests),
Expand Down Expand Up @@ -697,7 +846,7 @@ internal sealed class CustomDisplayNameTests_SameClassConstantTest_TestSource_GU
},
InheritanceDepth = 0,
FilePath = @"",
LineNumber = 52,
LineNumber = 72,
MethodMetadata = new global::TUnit.Core.MethodMetadata
{
Type = typeof(global::TUnit.TestProject.CustomDisplayNameTests),
Expand Down Expand Up @@ -808,7 +957,7 @@ internal sealed class CustomDisplayNameTests_DifferentClassConstantTest_TestSour
},
InheritanceDepth = 0,
FilePath = @"",
LineNumber = 59,
LineNumber = 79,
MethodMetadata = new global::TUnit.Core.MethodMetadata
{
Type = typeof(global::TUnit.TestProject.CustomDisplayNameTests),
Expand Down Expand Up @@ -919,7 +1068,7 @@ internal sealed class CustomDisplayNameTests_NestedClassConstantTest_TestSource_
},
InheritanceDepth = 0,
FilePath = @"",
LineNumber = 66,
LineNumber = 86,
MethodMetadata = new global::TUnit.Core.MethodMetadata
{
Type = typeof(global::TUnit.TestProject.CustomDisplayNameTests),
Expand Down
6 changes: 4 additions & 2 deletions TUnit.Core/Attributes/TestMetadata/DisplayNameAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ protected override string FormatDisplayName(DiscoveredTestContext context)
var methodParameters = testDetails
.MethodMetadata
.Parameters
.Zip(testDetails.TestMethodArguments, (parameterInfo, testArgument) => (ParameterInfo: parameterInfo, TestArgument: testArgument));
.Zip(testDetails.TestMethodArguments, (parameterInfo, testArgument) => (ParameterInfo: parameterInfo, TestArgument: testArgument))
.OrderByDescending(p => p.ParameterInfo.Name?.Length ?? 0); // Sort by name length descending to avoid prefix issues

foreach (var parameter in methodParameters)
{
Expand All @@ -64,7 +65,8 @@ protected override string FormatDisplayName(DiscoveredTestContext context)
.MethodMetadata
.Class
.Parameters
.Zip(testDetails.TestClassArguments, (parameterInfo, testArgument) => (ParameterInfo: parameterInfo, TestArgument: testArgument));
.Zip(testDetails.TestClassArguments, (parameterInfo, testArgument) => (ParameterInfo: parameterInfo, TestArgument: testArgument))
.OrderByDescending(p => p.ParameterInfo.Name?.Length ?? 0); // Sort by name length descending to avoid prefix issues

foreach (var parameter in classParameters)
{
Expand Down
20 changes: 20 additions & 0 deletions TUnit.TestProject/CustomDisplayNameTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,26 @@ public async Task MethodDataSourceTest(string foo)
await Assert.That(TestContext.Current!.GetDisplayName()).IsEqualTo("Test using MethodDataSource");
}

[Test]
[DisplayName("Test this($someValue, $someValueType)")]
[Arguments(100, "Type1")]
[Arguments(200, "Type2")]
[Arguments(300, "Type1")]
public async Task TestParameterNamePrefixBug(int someValue, string someValueType)
{
var displayName = TestContext.Current!.GetDisplayName();
// This should produce:
// Test this(100, Type1), Test this(200, Type2), Test this(300, Type1)
// But currently produces:
// Test this(100, 100Type), Test this(200, 200Type), Test this(300, 300Type)
await Assert.That(displayName)
.IsEqualTo("Test this(100, Type1)")
.Or
.IsEqualTo("Test this(200, Type2)")
.Or
.IsEqualTo("Test this(300, Type1)");
}

[Test]
[MyGenerator]
public async Task PasswordTest(string password)
Expand Down
Loading