Skip to content

Commit 2a50ae9

Browse files
authored
Format fallback parameter names with the invariant culture (#1897)
parameterToNameLookup pins invariant formatting for the common types, but not sbyte, Int128, UInt128, BigInteger, nint or nuint. Those fell through to parameter.ToString(), which uses the ambient culture: under sv-SE with ICU ((sbyte)-5).ToString() is '\u22125' with U+2212 MINUS SIGN, so a theory parameterised on a negative value of one of those types named its received and verified files differently than the same test on CI. Fixed at the fallback rather than by extending the type list, so any formattable type is covered instead of only the ones someone remembered to add.
1 parent 9358598 commit 2a50ae9

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

src/Verify.Tests/Naming/NameForParameterTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,28 @@ public void CollectionItemPathFriendlyFalseNotCleaned()
3333
Assert.Contains("a/b", name);
3434
}
3535

36+
// Types not in parameterToNameLookup fall back to formatting the value, which must
37+
// not vary by machine culture. sv-SE renders a negative number with U+2212 MINUS
38+
// SIGN under ICU, so a theory case would name its files differently than on CI.
39+
[Theory]
40+
[InlineData("sv-SE")]
41+
[InlineData("en-US")]
42+
public void NegativeSbyteIsInvariant(string cultureName)
43+
{
44+
var original = CultureInfo.CurrentCulture;
45+
CultureInfo.CurrentCulture = new(cultureName);
46+
try
47+
{
48+
Assert.Equal(
49+
"-5",
50+
VerifierSettings.GetNameForParameter((sbyte) -5, counter: CounterBuilder.Empty()));
51+
}
52+
finally
53+
{
54+
CultureInfo.CurrentCulture = original;
55+
}
56+
}
57+
3658
[Fact]
3759
public Task Int() =>
3860
Verify(VerifierSettings.GetNameForParameter(10, counter: CounterBuilder.Empty()));

src/Verify/Naming/VerifierSettings.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,13 @@ public static void AppendParameter(object? parameter, StringBuilder builder, boo
162162
continue;
163163
}
164164

165-
var nameForParameter = parameter.ToString();
165+
// Invariant for anything formattable, so a file name never varies by machine
166+
// culture. parameterToNameLookup covers the common types, but not sbyte,
167+
// Int128, UInt128, BigInteger, nint or nuint, and under sv-SE a negative
168+
// value of any of those renders with U+2212 MINUS SIGN instead of '-'.
169+
var nameForParameter = parameter is IFormattable formattable
170+
? formattable.ToString(null, Culture.InvariantCulture)
171+
: parameter.ToString();
166172
// ReSharper disable once ConditionIsAlwaysTrueOrFalse
167173
if (nameForParameter is null)
168174
{

0 commit comments

Comments
 (0)