Repository navigation
fix(core): format numeric display-name arguments with the invariant culture #7138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
81eb529
fix(core): format numeric display-name arguments with the invariant c…
thomhurst 8805f9e
fix(core): format Complex and NFloat display-name arguments invariantly
thomhurst 80b9e2f
docs(core): explain why IsNumeric uses an explicit type list
thomhurst 2aa5352
Merge remote-tracking branch 'origin/main' into fix/issue-7126-invari…
thomhurst 203cf4f
test(core): guard the numeric type list and expect invariant tuple ou…
thomhurst File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
tests/TUnit.Engine.Tests/Bug7126CultureInvariantDisplayNameTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| using Shouldly; | ||
| using TUnit.Engine.Tests.Enums; | ||
|
|
||
| namespace TUnit.Engine.Tests; | ||
|
|
||
| /// <summary> | ||
| /// End-to-end regression tests for https://github.com/thomhurst/TUnit/issues/7126. | ||
| /// Display names must format numeric arguments with the invariant culture, identically in | ||
| /// source-generated and reflection mode, whatever culture the test process runs under. | ||
| /// </summary> | ||
| public class Bug7126CultureInvariantDisplayNameTests(TestMode testMode) : InvokableTestBase(testMode) | ||
| { | ||
| [Test] | ||
| [Arguments("en-US")] | ||
| [Arguments("sv-SE")] | ||
| [Arguments("de-DE")] | ||
| public async Task Numeric_Arguments_Are_Displayed_Invariantly(string culture) | ||
| { | ||
| await RunTestsWithFilter( | ||
| "/*/*/Bug7126CultureInvariantDisplayNameTests/*", | ||
| [ | ||
| result => result.ResultSummary.Outcome.ShouldBe("Completed"), | ||
| result => result.ResultSummary.Counters.Total.ShouldBe(1), | ||
| result => result.ResultSummary.Counters.Passed.ShouldBe(1), | ||
| result => result.ResultSummary.Counters.Failed.ShouldBe(0), | ||
| result => result.Results.Single().TestName.ShouldBe("NumericArguments(1.5, -2, 2.25, 3.75)") | ||
| ], | ||
| new RunOptions().WithEnvironmentVariable("TUNIT_BUG_7126_CULTURE", culture)); | ||
| } | ||
| } |
44 changes: 44 additions & 0 deletions
44
tests/TUnit.TestProject/Bugs/7126/Bug7126CultureInvariantDisplayNameTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| using System.Globalization; | ||
| using System.Runtime.CompilerServices; | ||
| using TUnit.TestProject.Attributes; | ||
|
|
||
| namespace TUnit.TestProject.Bugs._7126; | ||
|
|
||
| /// <summary> | ||
| /// Repro for https://github.com/thomhurst/TUnit/issues/7126 — numeric arguments in display names | ||
| /// were formatted with the current culture, so under a decimal-comma culture (sv-SE, de-DE, ...) | ||
| /// <c>[Arguments(1.5, -2)]</c> became <c>Test(1,5, −2)</c>: ambiguous against the argument separator | ||
| /// and different from machine to machine. | ||
| /// TUnit.Engine.Tests sets <c>TUNIT_BUG_7126_CULTURE</c> to run this project under such a culture. | ||
| /// </summary> | ||
| [EngineTest(ExpectedResult.Pass)] | ||
| public class Bug7126CultureInvariantDisplayNameTests | ||
| { | ||
| internal const string CultureEnvironmentVariable = "TUNIT_BUG_7126_CULTURE"; | ||
|
|
||
| [ModuleInitializer] | ||
| internal static void ApplyRequestedCulture() | ||
| { | ||
| if (Environment.GetEnvironmentVariable(CultureEnvironmentVariable) is { Length: > 0 } cultureName) | ||
| { | ||
| var culture = CultureInfo.GetCultureInfo(cultureName); | ||
| CultureInfo.DefaultThreadCurrentCulture = culture; | ||
| CultureInfo.DefaultThreadCurrentUICulture = culture; | ||
| CultureInfo.CurrentCulture = culture; | ||
| CultureInfo.CurrentUICulture = culture; | ||
| } | ||
| } | ||
|
|
||
| [Test] | ||
| [Arguments(1.5, -2, 2.25f, 3.75)] | ||
| public async Task NumericArguments(double value, int count, float ratio, decimal amount) | ||
| { | ||
| if (Environment.GetEnvironmentVariable(CultureEnvironmentVariable) is { Length: > 0 } cultureName) | ||
| { | ||
| await Assert.That(CultureInfo.CurrentCulture.Name).IsEqualTo(cultureName); | ||
| } | ||
|
|
||
| await Assert.That(TestContext.Current!.Metadata.DisplayName) | ||
| .IsEqualTo("NumericArguments(1.5, -2, 2.25, 3.75)"); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| using System.Globalization; | ||
| using System.Numerics; | ||
| using TUnit.Core.Helpers; | ||
|
|
||
| namespace TUnit.UnitTests; | ||
|
|
||
| /// <summary> | ||
| /// Regression tests for https://github.com/thomhurst/TUnit/issues/7126: display-name arguments must not | ||
| /// depend on the current culture. | ||
| /// </summary> | ||
| public class ArgumentFormatterCultureTests | ||
| { | ||
| public static IEnumerable<Func<CultureInfo>> Cultures() | ||
| { | ||
| yield return () => CultureInfo.InvariantCulture; | ||
| yield return () => CultureInfo.GetCultureInfo("en-US"); | ||
| // Pin the decimal comma and U+2212 MINUS SIGN: sv-SE uses both under ICU, but not under NLS. | ||
| yield return () => WithDecimalCommaAndUnicodeMinus("sv-SE"); | ||
| yield return () => WithDecimalCommaAndUnicodeMinus("de-DE"); | ||
| } | ||
|
|
||
| [Test] | ||
| [MethodDataSource(nameof(Cultures))] | ||
| public async Task FormatArguments_Formats_Numbers_Invariantly(CultureInfo culture) | ||
| { | ||
| object?[] args = [1.5, -2, 2.25f, 3.75m, -1234567.891, (short)-7, 9_000_000_000L, new BigInteger(-42)]; | ||
|
|
||
| var result = WithCulture(culture, () => ArgumentFormatter.FormatArguments(args)); | ||
|
|
||
| await Assert.That(result).IsEqualTo("1.5, -2, 2.25, 3.75, -1234567.891, -7, 9000000000, -42"); | ||
| } | ||
|
|
||
| [Test] | ||
| [MethodDataSource(nameof(Cultures))] | ||
| public async Task Format_Formats_Numbers_Inside_Tuples_And_Collections_Invariantly(CultureInfo culture) | ||
| { | ||
| var (tuple, array) = WithCulture(culture, () => ( | ||
| ArgumentFormatter.Format((1.5, -2), []), | ||
| ArgumentFormatter.Format(new[] { 0.5, -0.25 }, []))); | ||
|
|
||
| await Assert.That(tuple).IsEqualTo("(1.5, -2)"); | ||
| await Assert.That(array).IsEqualTo("0.5, -0.25"); | ||
| } | ||
|
|
||
| [Test] | ||
| [MethodDataSource(nameof(Cultures))] | ||
| public async Task Format_With_Parameter_Type_Formats_Numbers_Invariantly(CultureInfo culture) | ||
| { | ||
| var result = WithCulture(culture, () => ArgumentFormatter.Format(-1.5m, typeof(decimal), [])); | ||
|
|
||
| await Assert.That(result).IsEqualTo("-1.5"); | ||
| } | ||
|
|
||
| #if NET | ||
| [Test] | ||
| [MethodDataSource(nameof(Cultures))] | ||
| public async Task FormatArguments_Formats_Modern_Numeric_Types_Invariantly(CultureInfo culture) | ||
| { | ||
| object?[] args = [(Half)(-1.5), (Int128)(-3), (UInt128)4, (nint)(-5)]; | ||
|
|
||
| var result = WithCulture(culture, () => ArgumentFormatter.FormatArguments(args)); | ||
|
|
||
| await Assert.That(result).IsEqualTo("-1.5, -3, 4, -5"); | ||
| } | ||
| #endif | ||
|
|
||
| [Test] | ||
| [MethodDataSource(nameof(Cultures))] | ||
| public async Task Custom_Formatters_Still_Take_Precedence(CultureInfo culture) | ||
| { | ||
| var result = WithCulture(culture, () => | ||
| ArgumentFormatter.Format(1.5, [o => o is double d ? d.ToString("F2", CultureInfo.CurrentCulture) : null])); | ||
|
|
||
| await Assert.That(result).IsEqualTo(1.5.ToString("F2", culture)); | ||
| } | ||
|
|
||
| [Test] | ||
| [MethodDataSource(nameof(Cultures))] | ||
| public async Task Non_Numeric_Primitives_And_Enums_Are_Unchanged(CultureInfo culture) | ||
| { | ||
| var result = WithCulture(culture, () => | ||
| ArgumentFormatter.FormatArguments([true, 'x', DayOfWeek.Monday, "a.b"])); | ||
|
|
||
| await Assert.That(result).IsEqualTo("True, x, Monday, a·b"); | ||
| } | ||
|
|
||
| private static CultureInfo WithDecimalCommaAndUnicodeMinus(string name) | ||
| { | ||
| var culture = (CultureInfo)CultureInfo.GetCultureInfo(name).Clone(); | ||
| culture.NumberFormat.NumberDecimalSeparator = ","; | ||
| culture.NumberFormat.NumberGroupSeparator = " "; | ||
| culture.NumberFormat.NegativeSign = "−"; | ||
| return culture; | ||
| } | ||
|
|
||
| private static T WithCulture<T>(CultureInfo culture, Func<T> action) | ||
| { | ||
| var original = CultureInfo.CurrentCulture; | ||
| CultureInfo.CurrentCulture = culture; | ||
| try | ||
| { | ||
| return action(); | ||
| } | ||
| finally | ||
| { | ||
| CultureInfo.CurrentCulture = original; | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.