Skip to content

Commit

Permalink
escape tabs and enters in the exported benchmark id (to keep it in sy…
Browse files Browse the repository at this point in the history
…nc with xunit conventions), #701
  • Loading branch information
adamsitnik committed Jun 17, 2018
1 parent 9a9648c commit 5716c14
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/BenchmarkDotNet/Exporters/XUnitNameProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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");
}
}
11 changes: 11 additions & 0 deletions tests/BenchmarkDotNet.Tests/XUnitNameProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ public void ArraysAreSupported()
[Fact]
public void UnicodeIsSupported()
=> AssertBenchmarkName<WithCrazyUnicodeCharacters>("BenchmarkDotNet.Tests.WithCrazyUnicodeCharacters.Method(arg1: \"" + "FOO" + "\", arg2: \""+ "\u03C3" + "\", arg3: \"" + "x\u0305" + "\")");

[Fact]
public void TabsAndEnters()
=> AssertBenchmarkName<WithTabAndEnter>("BenchmarkDotNet.Tests.WithTabAndEnter.Method(tab: \"1\\t2\", enter: \"3\\r\\n4\")");

[Fact]
public void VeryLongArraysAreSupported()
Expand Down Expand Up @@ -197,6 +201,13 @@ public IEnumerable<object[]> 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
{
Expand Down

0 comments on commit 5716c14

Please sign in to comment.