From 0fc72c26883ee6f34f9b19a01ce8621afbbde4e2 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Sat, 20 Jul 2024 22:52:26 +1000 Subject: [PATCH] Breaking: Respect NUnit class level parameters and drop support for custom TestName (#1249) --- docs/mdsource/parameterised.source.md | 5 +- docs/parameterised.md | 5 +- src/Benchmarks/FileNameCleanerBenchmarks.cs | 10 -- ...eWithNamespace(Value1,1).Test.verified.txt | 0 ...ams(1).WithMethodLevel_arg2=3.verified.txt | 1 - ...ams(1).WithMethodLevel_arg2=4.verified.txt | 1 - ...velParams(2).IgnoreParameters.verified.txt | 1 - .../ClassLevelParams(2).Simple.verified.txt | 1 - ...rams(2).WithMethodLeve_arg2=3.verified.txt | 1 - ...rams(2).WithMethodLeve_arg2=4.verified.txt | 1 - ...ams(2).WithMethodLevel_arg2=3.verified.txt | 1 - ...ams(2).WithMethodLevel_arg2=4.verified.txt | 1 - ...LevelParams.IgnoreParameters.verified.txt} | 0 ...assLevelParams.Simple_arg1=1.verified.txt} | 0 ...lassLevelParams.Simple_arg1=2.verified.txt | 1 + ...ithMethodLevel_arg1=1_arg2=3.verified.txt} | 0 ...ithMethodLevel_arg1=1_arg2=4.verified.txt} | 0 ...WithMethodLevel_arg1=2_arg2=3.verified.txt | 1 + ...WithMethodLevel_arg1=2_arg2=4.verified.txt | 1 + ...rsSample.Double(1.1d)_arg=1.1.verified.txt | 1 + ...ersSample.Float(1.1f)_arg=1.1.verified.txt | 1 + ...e2_arg2=2_arg3=FromTestCase2.verified.txt} | 0 ...sage.Test_arg1=Value1_arg2=1.verified.txt} | 0 ...sage.Test_arg1=Value2_arg2=2.verified.txt} | 0 ...pace.Test_arg1=Value1_arg2=1.verified.txt} | 0 ...pace.Test_arg1=Value2_arg2=2.verified.txt} | 0 ...InvalidChars_TextForParameter.verified.txt | 1 + ...ameAndInvalidChars_arg=Value1.verified.txt | 1 + ...CaseWithName_TextForParameter.verified.txt | 1 + ...s.TestCaseWithName_arg=Value1.verified.txt | 1 + src/Verify.NUnit/GlobalUsings.cs | 2 + src/Verify.NUnit/Verifier.cs | 127 +++++++++++++----- ...s.ReplaceInvalidFileNameChars.verified.txt | 1 - .../FileNameCleanerBenchmarksTests.cs | 6 - src/Verify/FileNameCleaner.cs | 27 ---- 35 files changed, 116 insertions(+), 84 deletions(-) delete mode 100644 src/Benchmarks/FileNameCleanerBenchmarks.cs delete mode 100644 src/Verify.NUnit.Tests/MyNamespace.TestFixtureSourceUsageWithNamespace(Value1,1).Test.verified.txt delete mode 100644 src/Verify.NUnit.Tests/Snippets/ClassLevelParams(1).WithMethodLevel_arg2=3.verified.txt delete mode 100644 src/Verify.NUnit.Tests/Snippets/ClassLevelParams(1).WithMethodLevel_arg2=4.verified.txt delete mode 100644 src/Verify.NUnit.Tests/Snippets/ClassLevelParams(2).IgnoreParameters.verified.txt delete mode 100644 src/Verify.NUnit.Tests/Snippets/ClassLevelParams(2).Simple.verified.txt delete mode 100644 src/Verify.NUnit.Tests/Snippets/ClassLevelParams(2).WithMethodLeve_arg2=3.verified.txt delete mode 100644 src/Verify.NUnit.Tests/Snippets/ClassLevelParams(2).WithMethodLeve_arg2=4.verified.txt delete mode 100644 src/Verify.NUnit.Tests/Snippets/ClassLevelParams(2).WithMethodLevel_arg2=3.verified.txt delete mode 100644 src/Verify.NUnit.Tests/Snippets/ClassLevelParams(2).WithMethodLevel_arg2=4.verified.txt rename src/Verify.NUnit.Tests/Snippets/{ClassLevelParams(1).IgnoreParameters.verified.txt => ClassLevelParams.IgnoreParameters.verified.txt} (100%) rename src/Verify.NUnit.Tests/Snippets/{ClassLevelParams(1).Simple.verified.txt => ClassLevelParams.Simple_arg1=1.verified.txt} (100%) create mode 100644 src/Verify.NUnit.Tests/Snippets/ClassLevelParams.Simple_arg1=2.verified.txt rename src/Verify.NUnit.Tests/Snippets/{ClassLevelParams(1).WithMethodLeve_arg2=3.verified.txt => ClassLevelParams.WithMethodLevel_arg1=1_arg2=3.verified.txt} (100%) rename src/Verify.NUnit.Tests/Snippets/{ClassLevelParams(1).WithMethodLeve_arg2=4.verified.txt => ClassLevelParams.WithMethodLevel_arg1=1_arg2=4.verified.txt} (100%) create mode 100644 src/Verify.NUnit.Tests/Snippets/ClassLevelParams.WithMethodLevel_arg1=2_arg2=3.verified.txt create mode 100644 src/Verify.NUnit.Tests/Snippets/ClassLevelParams.WithMethodLevel_arg1=2_arg2=4.verified.txt create mode 100644 src/Verify.NUnit.Tests/Snippets/ParametersSample.Double(1.1d)_arg=1.1.verified.txt create mode 100644 src/Verify.NUnit.Tests/Snippets/ParametersSample.Float(1.1f)_arg=1.1.verified.txt rename src/Verify.NUnit.Tests/{TestFixtureSourceAndTestCaseUsage(Value2,2).Test_arg3=FromTestCase2.verified.txt => TestFixtureSourceAndTestCaseUsage.Test_arg1=Value2_arg2=2_arg3=FromTestCase2.verified.txt} (100%) rename src/Verify.NUnit.Tests/{TestFixtureSourceUsage(Value1,1).Test.verified.txt => TestFixtureSourceUsage.Test_arg1=Value1_arg2=1.verified.txt} (100%) rename src/Verify.NUnit.Tests/{TestFixtureSourceUsage(Value2,2).Test.verified.txt => TestFixtureSourceUsage.Test_arg1=Value2_arg2=2.verified.txt} (100%) rename src/Verify.NUnit.Tests/{TestFixtureSourceUsageWithNamespace(Value1,1).Test.verified.txt => TestFixtureSourceUsageWithNamespace.Test_arg1=Value1_arg2=1.verified.txt} (100%) rename src/Verify.NUnit.Tests/{TestFixtureSourceUsageWithNamespace(Value2,2).Test.verified.txt => TestFixtureSourceUsageWithNamespace.Test_arg1=Value2_arg2=2.verified.txt} (100%) create mode 100644 src/Verify.NUnit.Tests/Tests.TestCaseWithNameAndInvalidChars_TextForParameter.verified.txt create mode 100644 src/Verify.NUnit.Tests/Tests.TestCaseWithNameAndInvalidChars_arg=Value1.verified.txt create mode 100644 src/Verify.NUnit.Tests/Tests.TestCaseWithName_TextForParameter.verified.txt create mode 100644 src/Verify.NUnit.Tests/Tests.TestCaseWithName_arg=Value1.verified.txt delete mode 100644 src/Verify.Tests/FileNameCleanerBenchmarksTests.ReplaceInvalidFileNameChars.verified.txt delete mode 100644 src/Verify.Tests/FileNameCleanerBenchmarksTests.cs diff --git a/docs/mdsource/parameterised.source.md b/docs/mdsource/parameterised.source.md index d639dfa4f4..e1c284cd2b 100644 --- a/docs/mdsource/parameterised.source.md +++ b/docs/mdsource/parameterised.source.md @@ -47,7 +47,10 @@ When using a [TestFixtureSource](https://docs.nunit.org/articles/nunit/writing-t snippet: TestFixtureSourceUsage.cs -Produces `TestFixtureSourceUsage(Value1,1).Test.verified.txt` and `TestFixtureSourceUsage(Value2,2).Test.verified.txt`. +Produces: + + * `TestFixtureSourceUsage.Test_arg1=Value1_arg2=1.verified.txt` + * `TestFixtureSourceUsage.Test_arg1=Value2_arg2=2.verified.txt` ## xUnit diff --git a/docs/parameterised.md b/docs/parameterised.md index 3f6a78c772..d5367558ce 100644 --- a/docs/parameterised.md +++ b/docs/parameterised.md @@ -121,7 +121,10 @@ public class TestFixtureSourceUsage(string arg1, int arg2) snippet source | anchor -Produces `TestFixtureSourceUsage(Value1,1).Test.verified.txt` and `TestFixtureSourceUsage(Value2,2).Test.verified.txt`. +Produces: + + * `TestFixtureSourceUsage.Test_arg1=Value1_arg2=1.verified.txt` + * `TestFixtureSourceUsage.Test_arg1=Value2_arg2=2.verified.txt` ## xUnit diff --git a/src/Benchmarks/FileNameCleanerBenchmarks.cs b/src/Benchmarks/FileNameCleanerBenchmarks.cs deleted file mode 100644 index 0b88434e24..0000000000 --- a/src/Benchmarks/FileNameCleanerBenchmarks.cs +++ /dev/null @@ -1,10 +0,0 @@ -[MemoryDiagnoser(false)] -public class FileNameCleanerBenchmarks -{ - [Benchmark] - public void ReplaceInvalidFileNameChars() - { - "Ant apple | The Bear Fox > Theater".ReplaceInvalidFileNameChars(); - "Ant apple The Bear Fox Theater".ReplaceInvalidFileNameChars(); - } -} \ No newline at end of file diff --git a/src/Verify.NUnit.Tests/MyNamespace.TestFixtureSourceUsageWithNamespace(Value1,1).Test.verified.txt b/src/Verify.NUnit.Tests/MyNamespace.TestFixtureSourceUsageWithNamespace(Value1,1).Test.verified.txt deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/src/Verify.NUnit.Tests/Snippets/ClassLevelParams(1).WithMethodLevel_arg2=3.verified.txt b/src/Verify.NUnit.Tests/Snippets/ClassLevelParams(1).WithMethodLevel_arg2=3.verified.txt deleted file mode 100644 index 3ee291f7bd..0000000000 --- a/src/Verify.NUnit.Tests/Snippets/ClassLevelParams(1).WithMethodLevel_arg2=3.verified.txt +++ /dev/null @@ -1 +0,0 @@ -Value \ No newline at end of file diff --git a/src/Verify.NUnit.Tests/Snippets/ClassLevelParams(1).WithMethodLevel_arg2=4.verified.txt b/src/Verify.NUnit.Tests/Snippets/ClassLevelParams(1).WithMethodLevel_arg2=4.verified.txt deleted file mode 100644 index 3ee291f7bd..0000000000 --- a/src/Verify.NUnit.Tests/Snippets/ClassLevelParams(1).WithMethodLevel_arg2=4.verified.txt +++ /dev/null @@ -1 +0,0 @@ -Value \ No newline at end of file diff --git a/src/Verify.NUnit.Tests/Snippets/ClassLevelParams(2).IgnoreParameters.verified.txt b/src/Verify.NUnit.Tests/Snippets/ClassLevelParams(2).IgnoreParameters.verified.txt deleted file mode 100644 index 3ee291f7bd..0000000000 --- a/src/Verify.NUnit.Tests/Snippets/ClassLevelParams(2).IgnoreParameters.verified.txt +++ /dev/null @@ -1 +0,0 @@ -Value \ No newline at end of file diff --git a/src/Verify.NUnit.Tests/Snippets/ClassLevelParams(2).Simple.verified.txt b/src/Verify.NUnit.Tests/Snippets/ClassLevelParams(2).Simple.verified.txt deleted file mode 100644 index 3ee291f7bd..0000000000 --- a/src/Verify.NUnit.Tests/Snippets/ClassLevelParams(2).Simple.verified.txt +++ /dev/null @@ -1 +0,0 @@ -Value \ No newline at end of file diff --git a/src/Verify.NUnit.Tests/Snippets/ClassLevelParams(2).WithMethodLeve_arg2=3.verified.txt b/src/Verify.NUnit.Tests/Snippets/ClassLevelParams(2).WithMethodLeve_arg2=3.verified.txt deleted file mode 100644 index 3ee291f7bd..0000000000 --- a/src/Verify.NUnit.Tests/Snippets/ClassLevelParams(2).WithMethodLeve_arg2=3.verified.txt +++ /dev/null @@ -1 +0,0 @@ -Value \ No newline at end of file diff --git a/src/Verify.NUnit.Tests/Snippets/ClassLevelParams(2).WithMethodLeve_arg2=4.verified.txt b/src/Verify.NUnit.Tests/Snippets/ClassLevelParams(2).WithMethodLeve_arg2=4.verified.txt deleted file mode 100644 index 3ee291f7bd..0000000000 --- a/src/Verify.NUnit.Tests/Snippets/ClassLevelParams(2).WithMethodLeve_arg2=4.verified.txt +++ /dev/null @@ -1 +0,0 @@ -Value \ No newline at end of file diff --git a/src/Verify.NUnit.Tests/Snippets/ClassLevelParams(2).WithMethodLevel_arg2=3.verified.txt b/src/Verify.NUnit.Tests/Snippets/ClassLevelParams(2).WithMethodLevel_arg2=3.verified.txt deleted file mode 100644 index 3ee291f7bd..0000000000 --- a/src/Verify.NUnit.Tests/Snippets/ClassLevelParams(2).WithMethodLevel_arg2=3.verified.txt +++ /dev/null @@ -1 +0,0 @@ -Value \ No newline at end of file diff --git a/src/Verify.NUnit.Tests/Snippets/ClassLevelParams(2).WithMethodLevel_arg2=4.verified.txt b/src/Verify.NUnit.Tests/Snippets/ClassLevelParams(2).WithMethodLevel_arg2=4.verified.txt deleted file mode 100644 index 3ee291f7bd..0000000000 --- a/src/Verify.NUnit.Tests/Snippets/ClassLevelParams(2).WithMethodLevel_arg2=4.verified.txt +++ /dev/null @@ -1 +0,0 @@ -Value \ No newline at end of file diff --git a/src/Verify.NUnit.Tests/Snippets/ClassLevelParams(1).IgnoreParameters.verified.txt b/src/Verify.NUnit.Tests/Snippets/ClassLevelParams.IgnoreParameters.verified.txt similarity index 100% rename from src/Verify.NUnit.Tests/Snippets/ClassLevelParams(1).IgnoreParameters.verified.txt rename to src/Verify.NUnit.Tests/Snippets/ClassLevelParams.IgnoreParameters.verified.txt diff --git a/src/Verify.NUnit.Tests/Snippets/ClassLevelParams(1).Simple.verified.txt b/src/Verify.NUnit.Tests/Snippets/ClassLevelParams.Simple_arg1=1.verified.txt similarity index 100% rename from src/Verify.NUnit.Tests/Snippets/ClassLevelParams(1).Simple.verified.txt rename to src/Verify.NUnit.Tests/Snippets/ClassLevelParams.Simple_arg1=1.verified.txt diff --git a/src/Verify.NUnit.Tests/Snippets/ClassLevelParams.Simple_arg1=2.verified.txt b/src/Verify.NUnit.Tests/Snippets/ClassLevelParams.Simple_arg1=2.verified.txt new file mode 100644 index 0000000000..5f282702bb --- /dev/null +++ b/src/Verify.NUnit.Tests/Snippets/ClassLevelParams.Simple_arg1=2.verified.txt @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/src/Verify.NUnit.Tests/Snippets/ClassLevelParams(1).WithMethodLeve_arg2=3.verified.txt b/src/Verify.NUnit.Tests/Snippets/ClassLevelParams.WithMethodLevel_arg1=1_arg2=3.verified.txt similarity index 100% rename from src/Verify.NUnit.Tests/Snippets/ClassLevelParams(1).WithMethodLeve_arg2=3.verified.txt rename to src/Verify.NUnit.Tests/Snippets/ClassLevelParams.WithMethodLevel_arg1=1_arg2=3.verified.txt diff --git a/src/Verify.NUnit.Tests/Snippets/ClassLevelParams(1).WithMethodLeve_arg2=4.verified.txt b/src/Verify.NUnit.Tests/Snippets/ClassLevelParams.WithMethodLevel_arg1=1_arg2=4.verified.txt similarity index 100% rename from src/Verify.NUnit.Tests/Snippets/ClassLevelParams(1).WithMethodLeve_arg2=4.verified.txt rename to src/Verify.NUnit.Tests/Snippets/ClassLevelParams.WithMethodLevel_arg1=1_arg2=4.verified.txt diff --git a/src/Verify.NUnit.Tests/Snippets/ClassLevelParams.WithMethodLevel_arg1=2_arg2=3.verified.txt b/src/Verify.NUnit.Tests/Snippets/ClassLevelParams.WithMethodLevel_arg1=2_arg2=3.verified.txt new file mode 100644 index 0000000000..5f282702bb --- /dev/null +++ b/src/Verify.NUnit.Tests/Snippets/ClassLevelParams.WithMethodLevel_arg1=2_arg2=3.verified.txt @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/src/Verify.NUnit.Tests/Snippets/ClassLevelParams.WithMethodLevel_arg1=2_arg2=4.verified.txt b/src/Verify.NUnit.Tests/Snippets/ClassLevelParams.WithMethodLevel_arg1=2_arg2=4.verified.txt new file mode 100644 index 0000000000..5f282702bb --- /dev/null +++ b/src/Verify.NUnit.Tests/Snippets/ClassLevelParams.WithMethodLevel_arg1=2_arg2=4.verified.txt @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/src/Verify.NUnit.Tests/Snippets/ParametersSample.Double(1.1d)_arg=1.1.verified.txt b/src/Verify.NUnit.Tests/Snippets/ParametersSample.Double(1.1d)_arg=1.1.verified.txt new file mode 100644 index 0000000000..8ab02375ee --- /dev/null +++ b/src/Verify.NUnit.Tests/Snippets/ParametersSample.Double(1.1d)_arg=1.1.verified.txt @@ -0,0 +1 @@ +1.1 \ No newline at end of file diff --git a/src/Verify.NUnit.Tests/Snippets/ParametersSample.Float(1.1f)_arg=1.1.verified.txt b/src/Verify.NUnit.Tests/Snippets/ParametersSample.Float(1.1f)_arg=1.1.verified.txt new file mode 100644 index 0000000000..8ab02375ee --- /dev/null +++ b/src/Verify.NUnit.Tests/Snippets/ParametersSample.Float(1.1f)_arg=1.1.verified.txt @@ -0,0 +1 @@ +1.1 \ No newline at end of file diff --git a/src/Verify.NUnit.Tests/TestFixtureSourceAndTestCaseUsage(Value2,2).Test_arg3=FromTestCase2.verified.txt b/src/Verify.NUnit.Tests/TestFixtureSourceAndTestCaseUsage.Test_arg1=Value2_arg2=2_arg3=FromTestCase2.verified.txt similarity index 100% rename from src/Verify.NUnit.Tests/TestFixtureSourceAndTestCaseUsage(Value2,2).Test_arg3=FromTestCase2.verified.txt rename to src/Verify.NUnit.Tests/TestFixtureSourceAndTestCaseUsage.Test_arg1=Value2_arg2=2_arg3=FromTestCase2.verified.txt diff --git a/src/Verify.NUnit.Tests/TestFixtureSourceUsage(Value1,1).Test.verified.txt b/src/Verify.NUnit.Tests/TestFixtureSourceUsage.Test_arg1=Value1_arg2=1.verified.txt similarity index 100% rename from src/Verify.NUnit.Tests/TestFixtureSourceUsage(Value1,1).Test.verified.txt rename to src/Verify.NUnit.Tests/TestFixtureSourceUsage.Test_arg1=Value1_arg2=1.verified.txt diff --git a/src/Verify.NUnit.Tests/TestFixtureSourceUsage(Value2,2).Test.verified.txt b/src/Verify.NUnit.Tests/TestFixtureSourceUsage.Test_arg1=Value2_arg2=2.verified.txt similarity index 100% rename from src/Verify.NUnit.Tests/TestFixtureSourceUsage(Value2,2).Test.verified.txt rename to src/Verify.NUnit.Tests/TestFixtureSourceUsage.Test_arg1=Value2_arg2=2.verified.txt diff --git a/src/Verify.NUnit.Tests/TestFixtureSourceUsageWithNamespace(Value1,1).Test.verified.txt b/src/Verify.NUnit.Tests/TestFixtureSourceUsageWithNamespace.Test_arg1=Value1_arg2=1.verified.txt similarity index 100% rename from src/Verify.NUnit.Tests/TestFixtureSourceUsageWithNamespace(Value1,1).Test.verified.txt rename to src/Verify.NUnit.Tests/TestFixtureSourceUsageWithNamespace.Test_arg1=Value1_arg2=1.verified.txt diff --git a/src/Verify.NUnit.Tests/TestFixtureSourceUsageWithNamespace(Value2,2).Test.verified.txt b/src/Verify.NUnit.Tests/TestFixtureSourceUsageWithNamespace.Test_arg1=Value2_arg2=2.verified.txt similarity index 100% rename from src/Verify.NUnit.Tests/TestFixtureSourceUsageWithNamespace(Value2,2).Test.verified.txt rename to src/Verify.NUnit.Tests/TestFixtureSourceUsageWithNamespace.Test_arg1=Value2_arg2=2.verified.txt diff --git a/src/Verify.NUnit.Tests/Tests.TestCaseWithNameAndInvalidChars_TextForParameter.verified.txt b/src/Verify.NUnit.Tests/Tests.TestCaseWithNameAndInvalidChars_TextForParameter.verified.txt new file mode 100644 index 0000000000..39d0344b5c --- /dev/null +++ b/src/Verify.NUnit.Tests/Tests.TestCaseWithNameAndInvalidChars_TextForParameter.verified.txt @@ -0,0 +1 @@ +Value1 \ No newline at end of file diff --git a/src/Verify.NUnit.Tests/Tests.TestCaseWithNameAndInvalidChars_arg=Value1.verified.txt b/src/Verify.NUnit.Tests/Tests.TestCaseWithNameAndInvalidChars_arg=Value1.verified.txt new file mode 100644 index 0000000000..5f282702bb --- /dev/null +++ b/src/Verify.NUnit.Tests/Tests.TestCaseWithNameAndInvalidChars_arg=Value1.verified.txt @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/src/Verify.NUnit.Tests/Tests.TestCaseWithName_TextForParameter.verified.txt b/src/Verify.NUnit.Tests/Tests.TestCaseWithName_TextForParameter.verified.txt new file mode 100644 index 0000000000..39d0344b5c --- /dev/null +++ b/src/Verify.NUnit.Tests/Tests.TestCaseWithName_TextForParameter.verified.txt @@ -0,0 +1 @@ +Value1 \ No newline at end of file diff --git a/src/Verify.NUnit.Tests/Tests.TestCaseWithName_arg=Value1.verified.txt b/src/Verify.NUnit.Tests/Tests.TestCaseWithName_arg=Value1.verified.txt new file mode 100644 index 0000000000..5f282702bb --- /dev/null +++ b/src/Verify.NUnit.Tests/Tests.TestCaseWithName_arg=Value1.verified.txt @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/src/Verify.NUnit/GlobalUsings.cs b/src/Verify.NUnit/GlobalUsings.cs index 0a6e81330c..f743e7df1e 100644 --- a/src/Verify.NUnit/GlobalUsings.cs +++ b/src/Verify.NUnit/GlobalUsings.cs @@ -3,3 +3,5 @@ global using NUnit.Framework.Internal; global using NUnit.Framework.Interfaces; global using VerifyTests; + +global using TestAdapter = NUnit.Framework.TestContext.TestAdapter; diff --git a/src/Verify.NUnit/Verifier.cs b/src/Verify.NUnit/Verifier.cs index 9fc681c84c..1dca4e29e0 100644 --- a/src/Verify.NUnit/Verifier.cs +++ b/src/Verify.NUnit/Verifier.cs @@ -31,63 +31,130 @@ static InnerVerifier BuildVerifier(string sourceFile, VerifySettings settings, b throw new("TestContext.CurrentContext.Test.Method is null. Verify can only be used from within a test method."); } - if (!settings.HasParameters && - adapter.Arguments.Length > 0) + var method = testMethod.MethodInfo; + var type = testMethod.TypeInfo.Type; + + IReadOnlyList? parameterNames; + if (settings.HasParameters) { - settings.SetParameters(adapter.Arguments); + parameterNames = GetParameterNames(adapter); } - - var customName = !adapter.FullName.StartsWith($"{testMethod.TypeInfo.FullName}.{testMethod.Name}"); - if (customName) + else { - - settings.typeName ??= adapter.GetTypeName(); - - settings.methodName ??= adapter.GetMethodName(); + var (names, values) = GetParameterInfo(adapter); + settings.SetParameters(values); + parameterNames = names; } - var type = testMethod.TypeInfo.Type; VerifierSettings.AssignTargetAssembly(type.Assembly); - var method = testMethod.MethodInfo; - var pathInfo = GetPathInfo(sourceFile, type, method); return new( sourceFile, settings, type.NameWithParent(), method.Name, - method.ParameterNames(), + parameterNames, pathInfo); } - static string GetMethodName(this TestContext.TestAdapter adapter) + static (IReadOnlyList? names, object?[] values) GetParameterInfo(TestAdapter adapter) { - var name = adapter.Name; - var indexOf = name.IndexOf('('); + var method = adapter.Method!; - if (indexOf != -1) + var methodParameterNames = method.MethodInfo.ParameterNames(); + + var parent = GetParent(adapter); + + if (parent == null) { - name = name[..indexOf]; + return (methodParameterNames, adapter.Arguments); } - return name.ReplaceInvalidFileNameChars(); + var argumentsLength = parent.Arguments.Length; + if (argumentsLength == 0) + { + return (methodParameterNames, adapter.Arguments); + } + + var names = GetConstructorParameterNames(method.TypeInfo.Type, argumentsLength); + if (methodParameterNames == null) + { + return (names.ToList(), parent.Arguments); + } + + return ( + [.. names, .. methodParameterNames], + [.. parent.Arguments, .. adapter.Arguments]); } - static string GetTypeName(this TestContext.TestAdapter adapter) + static IReadOnlyList? GetParameterNames(TestAdapter adapter) { - var fullName = adapter.FullName.AsSpan(); - var fullNameLength = fullName.Length - (adapter.Name.Length + 1); - var typeName = fullName[..fullNameLength]; - var typeInfo = adapter.Method!.TypeInfo; - // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract - if (typeInfo.Namespace is not null) + var method = adapter.Method!; + + var methodParameterNames = method.MethodInfo.ParameterNames(); + + var parent = GetParent(adapter); + + if (parent == null) + { + return methodParameterNames; + } + + var names = GetConstructorParameterNames(method.TypeInfo.Type, parent.Arguments.Length); + if (methodParameterNames == null) + { + return names.ToList(); + } + + return [.. names, .. methodParameterNames]; + } + + static ITest? GetParent(TestAdapter adapter) + { + var test = GetTest(adapter); + var parent = test.Parent; + if (parent is ParameterizedMethodSuite methodSuite) + { + return methodSuite.Parent; + } + + return parent; + } + + static Test GetTest(TestAdapter adapter) + { + var field = adapter + .GetType() + .GetField("_test", BindingFlags.Instance | BindingFlags.NonPublic)!; + return (Test) field.GetValue(adapter)!; + } + + static IEnumerable GetConstructorParameterNames(Type type, int argumentsLength) + { + IEnumerable? names = null; + foreach (var constructor in type.GetConstructors(BindingFlags.Instance | BindingFlags.Public)) + { + var parameters = constructor.GetParameters(); + if (parameters.Length != argumentsLength) + { + continue; + } + + if (names != null) + { + throw new($"Found multiple constructors with {argumentsLength} parameters. Unable to derive names of parameters. Instead use UseParameters to pass in explicit parameter."); + } + + names = parameters.Select(_ => _.Name!); + } + + if (names == null) { - typeName = typeName[(typeInfo.Namespace.Length + 1)..]; + throw new($"Could not find constructor with {argumentsLength} parameters."); } - return typeName.ToString().Replace("\"", "") - .ReplaceInvalidFileNameChars(); + return names; } static SettingsTask Verify( diff --git a/src/Verify.Tests/FileNameCleanerBenchmarksTests.ReplaceInvalidFileNameChars.verified.txt b/src/Verify.Tests/FileNameCleanerBenchmarksTests.ReplaceInvalidFileNameChars.verified.txt deleted file mode 100644 index 6bc7ce878a..0000000000 --- a/src/Verify.Tests/FileNameCleanerBenchmarksTests.ReplaceInvalidFileNameChars.verified.txt +++ /dev/null @@ -1 +0,0 @@ -Ant apple - The Bear Fox - Theater \ No newline at end of file diff --git a/src/Verify.Tests/FileNameCleanerBenchmarksTests.cs b/src/Verify.Tests/FileNameCleanerBenchmarksTests.cs deleted file mode 100644 index 77db096977..0000000000 --- a/src/Verify.Tests/FileNameCleanerBenchmarksTests.cs +++ /dev/null @@ -1,6 +0,0 @@ -public class FileNameCleanerBenchmarksTests -{ - [Fact] - public Task ReplaceInvalidFileNameChars() => - Verify("Ant apple | The Bear Fox > Theater".ReplaceInvalidFileNameChars()); -} \ No newline at end of file diff --git a/src/Verify/FileNameCleaner.cs b/src/Verify/FileNameCleaner.cs index 69a3173ae3..67b304635b 100644 --- a/src/Verify/FileNameCleaner.cs +++ b/src/Verify/FileNameCleaner.cs @@ -49,33 +49,6 @@ static SearchValues invalidFileNameSearchValues = SearchValues.Create(invalidFileNameChars); #endif - public static string ReplaceInvalidFileNameChars(this string value) - { - var span = value.AsSpan(); - - var index = IndexOfInvalidChar(span); - - if (index == -1) - { - return value; - } - - Span target = stackalloc char[value.Length]; - span.CopyTo(target); - - target[index] = '-'; - index++; - for (; index < target.Length; index++) - { - if (IsInvalid(target[index])) - { - target[index] = '-'; - } - } - - return target.ToString(); - } - static int IndexOfInvalidChar(CharSpan span) => #if NET8_0_OR_GREATER span.IndexOfAny(invalidFileNameSearchValues);