diff --git a/src/BenchmarkDotNet/Exporters/XUnitNameProvider.cs b/src/BenchmarkDotNet/Exporters/XUnitNameProvider.cs index 63dc6b90cc..1c18ef4346 100644 --- a/src/BenchmarkDotNet/Exporters/XUnitNameProvider.cs +++ b/src/BenchmarkDotNet/Exporters/XUnitNameProvider.cs @@ -98,7 +98,7 @@ private static string GetArgument(object argumentValue, Type argumentType) return GetArgument(array[0], argumentType); if (argumentValue is string text) - return $"\"{text}\""; + return $"\"{EscapeWhitespaces(text)}\""; if (argumentValue is char character) return $"'{character}'"; if (argumentValue is DateTime time) @@ -136,5 +136,9 @@ private static string GetArray(IEnumerable collection) return buffer.ToString(); } + + private static string EscapeWhitespaces(string text) + => text.Replace("\t", "\\t") + .Replace("\r\n", "\\r\\n"); } } diff --git a/tests/BenchmarkDotNet.Tests/XUnitNameProviderTests.cs b/tests/BenchmarkDotNet.Tests/XUnitNameProviderTests.cs index 60c77b9b47..1b4c439627 100644 --- a/tests/BenchmarkDotNet.Tests/XUnitNameProviderTests.cs +++ b/tests/BenchmarkDotNet.Tests/XUnitNameProviderTests.cs @@ -70,6 +70,10 @@ public void ArraysAreSupported() [Fact] public void UnicodeIsSupported() => AssertBenchmarkName("BenchmarkDotNet.Tests.WithCrazyUnicodeCharacters.Method(arg1: \"" + "FOO" + "\", arg2: \""+ "\u03C3" + "\", arg3: \"" + "x\u0305" + "\")"); + + [Fact] + public void TabsAndEnters() + => AssertBenchmarkName("BenchmarkDotNet.Tests.WithTabAndEnter.Method(tab: \"1\\t2\", enter: \"3\\r\\n4\")"); [Fact] public void VeryLongArraysAreSupported() @@ -197,6 +201,13 @@ public IEnumerable Data() yield return new object[] { "FOO", "\u03C3", "x\u0305" }; // https://github.com/Microsoft/xunit-performance/blob/f1d1d62a934694d8cd19063e60e04c590711d904/tests/simpleharness/Program.cs#L29 } } + + public class WithTabAndEnter + { + [Benchmark] + [Arguments("1\t2", "3\r\n4")] + public void Method(string tab, string enter) { } + } public class WithBigArray {