Skip to content

Commit 4e14b32

Browse files
committed
♻️🧪Use CultureScope in tests to revert CurrentCulture changes
Use `CultureScope` instead of try-catch, simpler syntax. Add `CultureScope` to some test cases that previously did not revert their changes to CurrentCulture, to avoid flaky tests. # Conflicts: # CodeGen/Generators/UnitsNetGen/UnitTestBaseClassGenerator.cs # UnitsNet.Tests/Helpers/CultureScope.cs
1 parent aec9243 commit 4e14b32

File tree

131 files changed

+2515
-4420
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

131 files changed

+2515
-4420
lines changed

CodeGen/Generators/UnitsNetGen/UnitTestBaseClassGenerator.cs

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -822,20 +822,13 @@ public void BaseDimensionsShouldNeverBeNull()
822822
[Fact]
823823
public void ToString_ReturnsValueAndUnitAbbreviationInCurrentCulture()
824824
{{
825-
var prevCulture = Thread.CurrentThread.CurrentCulture;
826-
Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo(""en-US"");
827-
try {{");
825+
using var _ = new CultureScope(""en-US"");");
828826
foreach (var unit in _quantity.Units)
829827
{
830828
Writer.WL($@"
831-
Assert.Equal(""1{GetEnglishAbbreviation(unit)}"", new {_quantity.Name}(1, {GetUnitFullName(unit)}).ToString());");
829+
Assert.Equal(""1{GetEnglishAbbreviation(unit)}"", new {_quantity.Name}(1, {GetUnitFullName(unit)}).ToString());");
832830
}
833831
Writer.WL($@"
834-
}}
835-
finally
836-
{{
837-
Thread.CurrentThread.CurrentCulture = prevCulture;
838-
}}
839832
}}
840833
841834
[Fact]
@@ -855,19 +848,11 @@ public void ToString_WithSwedishCulture_ReturnsUnitAbbreviationForEnglishCulture
855848
[Fact]
856849
public void ToString_SFormat_FormatsNumberWithGivenDigitsAfterRadixForCurrentCulture()
857850
{{
858-
var oldCulture = CultureInfo.CurrentCulture;
859-
try
860-
{{
861-
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
862-
Assert.Equal(""0.1{_baseUnitEnglishAbbreviation}"", new {_quantity.Name}(0.123456, {_baseUnitFullName}).ToString(""s1""));
863-
Assert.Equal(""0.12{_baseUnitEnglishAbbreviation}"", new {_quantity.Name}(0.123456, {_baseUnitFullName}).ToString(""s2""));
864-
Assert.Equal(""0.123{_baseUnitEnglishAbbreviation}"", new {_quantity.Name}(0.123456, {_baseUnitFullName}).ToString(""s3""));
865-
Assert.Equal(""0.1235{_baseUnitEnglishAbbreviation}"", new {_quantity.Name}(0.123456, {_baseUnitFullName}).ToString(""s4""));
866-
}}
867-
finally
868-
{{
869-
CultureInfo.CurrentCulture = oldCulture;
870-
}}
851+
var _ = new CultureScope(CultureInfo.InvariantCulture);
852+
Assert.Equal(""0.1{_baseUnitEnglishAbbreviation}"", new {_quantity.Name}(0.123456, {_baseUnitFullName}).ToString(""s1""));
853+
Assert.Equal(""0.12{_baseUnitEnglishAbbreviation}"", new {_quantity.Name}(0.123456, {_baseUnitFullName}).ToString(""s2""));
854+
Assert.Equal(""0.123{_baseUnitEnglishAbbreviation}"", new {_quantity.Name}(0.123456, {_baseUnitFullName}).ToString(""s3""));
855+
Assert.Equal(""0.1235{_baseUnitEnglishAbbreviation}"", new {_quantity.Name}(0.123456, {_baseUnitFullName}).ToString(""s4""));
871856
}}
872857
873858
[Fact]

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ var oneKg = Mass.FromKilograms(1);
9898

9999
// ToString() uses CurrentCulture for abbreviation language number formatting. This is consistent with the behavior of the .NET Framework,
100100
// where DateTime.ToString() uses CurrentCulture for the whole string, likely because mixing an english date format with a russian month name might be confusing.
101-
Thread.CurrentThread.CurrentCulture = russian;
101+
CultureInfo.CurrentCulture = russian;
102102
string kgRu = oneKg.ToString(); // "1 кг"
103103
104104
// ToString() with specific culture and custom string format pattern
@@ -362,7 +362,7 @@ double convertedValue = UnitConverter.Convert(
362362
### Example: WPF app using IValueConverter to parse input
363363

364364
Src: [Samples/MvvmSample.Wpf](https://github.com/angularsen/UnitsNet/tree/master/Samples/MvvmSample.Wpf)
365-
365+
366366
![wpfmvvmsample_219w](https://user-images.githubusercontent.com/787816/34913417-094332e2-f8fd-11e7-9d8a-92db105fbbc9.png)
367367
368368
The purpose of this app is to show how to create an `IValueConverter` in order to bind XAML to quantities.

UnitsNet.Tests/CustomCode/StonePoundsTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Copyright 2013 Andreas Gullberg Larsen ([email protected]). Maintained at https://github.com/angularsen/UnitsNet.
33

44
using System.Globalization;
5+
using UnitsNet.Tests.Helpers;
56
using Xunit;
67

78
namespace UnitsNet.Tests
@@ -33,7 +34,7 @@ public void StonePoundsRoundTrip()
3334
[Fact]
3435
public void StonePoundsToString_FormatsNumberInCurrentCulture()
3536
{
36-
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("en-US");
37+
using var _ = new CultureScope("en-US");
3738
StonePounds stonePounds = Mass.FromStonePounds(3500, 1).StonePounds;
3839

3940
Assert.Equal("3,500 st 1 lb", stonePounds.ToString());

UnitsNet.Tests/GeneratedCode/TestsBase/AbsorbedDoseOfIonizingRadiationTestsBase.g.cs

Lines changed: 22 additions & 37 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnitsNet.Tests/GeneratedCode/TestsBase/AccelerationTestsBase.g.cs

Lines changed: 20 additions & 35 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)