Skip to content

Commit 4670911

Browse files
authored
Fix tests without explicit culture specification (#1257)
I fixed all the tests that didn't pass due to a non-English operating system. I didn't change the test logic. I didn't make any changes to the core code of the library, so this PR doesn't need a separate version. Also, I can't guarantee that I've fixed all cases of missing culture. For example, the test Assert.False("5 m" != "6 м") could return a false result not because 5 != 6, but because _meter_ is not a _метр_.
1 parent 23ab310 commit 4670911

11 files changed

+76
-59
lines changed

UnitsNet.Tests/BaseUnitsTests.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,23 @@ public void ExistsInWorksCorrectly()
132132
[Fact]
133133
public void ToStringGivesExpectedResult()
134134
{
135-
var siBaseUnits = new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second,
136-
ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela);
137-
138-
Assert.Equal("[Length]: m, [Mass]: kg, [Time]: s, [Current]: A, [Temperature]: K, [Amount]: mol, [LuminousIntensity]: cd", siBaseUnits.ToString());
135+
var siBaseUnits = new BaseUnits(LengthUnit.Meter,
136+
MassUnit.Kilogram,
137+
DurationUnit.Second,
138+
ElectricCurrentUnit.Ampere,
139+
TemperatureUnit.Kelvin,
140+
AmountOfSubstanceUnit.Mole,
141+
LuminousIntensityUnit.Candela);
142+
143+
var m = UnitAbbreviationsCache.Default.GetDefaultAbbreviation(LengthUnit.Meter);
144+
var kg = UnitAbbreviationsCache.Default.GetDefaultAbbreviation(MassUnit.Kilogram);
145+
var s = UnitAbbreviationsCache.Default.GetDefaultAbbreviation(DurationUnit.Second);
146+
var A = UnitAbbreviationsCache.Default.GetDefaultAbbreviation(ElectricCurrentUnit.Ampere);
147+
var K = UnitAbbreviationsCache.Default.GetDefaultAbbreviation(TemperatureUnit.Kelvin);
148+
var mol = UnitAbbreviationsCache.Default.GetDefaultAbbreviation(AmountOfSubstanceUnit.Mole);
149+
var cd = UnitAbbreviationsCache.Default.GetDefaultAbbreviation(LuminousIntensityUnit.Candela);
150+
151+
Assert.Equal($"[Length]: {m}, [Mass]: {kg}, [Time]: {s}, [Current]: {A}, [Temperature]: {K}, [Amount]: {mol}, [LuminousIntensity]: {cd}", siBaseUnits.ToString());
139152
}
140153

141154
[Fact]

UnitsNet.Tests/CustomCode/DurationTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ public void DurationTimesVolumeFlowEqualsVolume()
192192
[InlineData("1000 мсек", 1, "ru-RU")]
193193
public void DurationFromStringUsingMultipleAbbreviationsParsedCorrectly(string textValue, double expectedSeconds, string? culture = null)
194194
{
195-
var cultureInfo = culture == null ? null : new CultureInfo(culture);
195+
var cultureInfo = culture == null ? CultureInfo.InvariantCulture : new CultureInfo(culture);
196196

197197
AssertEx.EqualTolerance(expectedSeconds, Duration.Parse(textValue, cultureInfo).Seconds, SecondsTolerance);
198198
}

UnitsNet.Tests/CustomCode/LengthTests.cs

Lines changed: 3 additions & 2 deletions
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;
5+
using System.Globalization;
56
using UnitsNet.Units;
67
using Xunit;
78

@@ -144,15 +145,15 @@ public void LengthTimesSpecificWeightEqualsPressure()
144145
public void ToStringReturnsCorrectNumberAndUnitWithDefaultUnitWhichIsMeter()
145146
{
146147
var meter = Length.FromMeters(5);
147-
string meterString = meter.ToString();
148+
string meterString = meter.ToString(CultureInfo.InvariantCulture);
148149
Assert.Equal("5 m", meterString);
149150
}
150151

151152
[Fact]
152153
public void ToStringReturnsCorrectNumberAndUnitWithCentimeterAsDefualtUnit()
153154
{
154155
var value = Length.From(2, LengthUnit.Centimeter);
155-
string valueString = value.ToString();
156+
string valueString = value.ToString(CultureInfo.InvariantCulture);
156157
Assert.Equal("2 cm", valueString);
157158
}
158159

UnitsNet.Tests/CustomCode/ParseTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ public void ParseWithCultureUsingDotAsThousandSeparators(string s, double expect
9696
[Fact]
9797
public void ParseMultiWordAbbreviations()
9898
{
99-
Assert.Equal(Mass.FromShortTons(333), Mass.Parse("333 short tn"));
100-
Assert.Equal(Mass.FromLongTons(333), Mass.Parse("333 long tn"));
99+
Assert.Equal(Mass.FromShortTons(333), Mass.Parse("333 short tn", CultureInfo.InvariantCulture));
100+
Assert.Equal(Mass.FromLongTons(333), Mass.Parse("333 long tn", CultureInfo.InvariantCulture));
101101
}
102102

103103
[Theory]

UnitsNet.Tests/CustomCode/StonePoundsTests.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,12 @@ public void StonePoundsRoundTrip()
3131
}
3232

3333
[Fact]
34-
public void StonePoundsToString_FormatsNumberInDefaultCulture()
34+
public void StonePoundsToString_FormatsNumberInCurrentCulture()
3535
{
36-
Mass m = Mass.FromStonePounds(3500, 1);
37-
StonePounds stonePounds = m.StonePounds;
38-
string numberInCurrentCulture = 3500.ToString("n0", CultureInfo.CurrentCulture); // Varies between machines, can't hard code it
36+
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("en-US");
37+
StonePounds stonePounds = Mass.FromStonePounds(3500, 1).StonePounds;
3938

40-
Assert.Equal($"{numberInCurrentCulture} st 1 lb", stonePounds.ToString());
39+
Assert.Equal("3,500 st 1 lb", stonePounds.ToString());
4140
}
4241

4342
// These cultures use a thin space in digit grouping

UnitsNet.Tests/QuantityIFormattableTests.cs

Lines changed: 5 additions & 4 deletions
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;
5+
using System.Globalization;
56
using Xunit;
67

78
namespace UnitsNet.Tests
@@ -26,11 +27,11 @@ public void EmptyOrNullFormatStringEqualsGFormat()
2627
[Fact]
2728
public void AFormatGetsAbbreviations()
2829
{
29-
Assert.Equal(UnitAbbreviationsCache.Default.GetDefaultAbbreviation(MyLength.Unit), MyLength.ToString("a"));
30-
Assert.Equal(UnitAbbreviationsCache.Default.GetDefaultAbbreviation(MyLength.Unit), MyLength.ToString("a0"));
30+
Assert.Equal(UnitAbbreviationsCache.Default.GetDefaultAbbreviation(MyLength.Unit, CultureInfo.InvariantCulture), MyLength.ToString("a", CultureInfo.InvariantCulture));
31+
Assert.Equal(UnitAbbreviationsCache.Default.GetDefaultAbbreviation(MyLength.Unit, CultureInfo.InvariantCulture), MyLength.ToString("a0", CultureInfo.InvariantCulture));
3132

32-
Assert.Equal(UnitAbbreviationsCache.Default.GetUnitAbbreviations(MyLength.Unit)[1], MyLength.ToString("a1"));
33-
Assert.Equal(UnitAbbreviationsCache.Default.GetUnitAbbreviations(MyLength.Unit)[2], MyLength.ToString("a2"));
33+
Assert.Equal(UnitAbbreviationsCache.Default.GetUnitAbbreviations(MyLength.Unit, CultureInfo.InvariantCulture)[1], MyLength.ToString("a1", CultureInfo.InvariantCulture));
34+
Assert.Equal(UnitAbbreviationsCache.Default.GetUnitAbbreviations(MyLength.Unit, CultureInfo.InvariantCulture)[2], MyLength.ToString("a2", CultureInfo.InvariantCulture));
3435
}
3536

3637
[Fact]

UnitsNet.Tests/QuantityTest.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Linq;
88
using UnitsNet.Units;
99
using Xunit;
10+
using static System.Globalization.CultureInfo;
1011

1112
namespace UnitsNet.Tests
1213
{
@@ -103,9 +104,9 @@ public void TryGetUnitInfo_ReturnsFalseIfNotFound()
103104
[Fact]
104105
public void Parse_GivenValueAndUnit_ReturnsQuantity()
105106
{
106-
Assert.Equal(Length.FromCentimeters(3), Quantity.Parse(CultureInfo.InvariantCulture, typeof(Length), "3 cm"));
107-
Assert.Equal(Mass.FromTonnes(3), Quantity.Parse(CultureInfo.InvariantCulture, typeof(Mass), "03t"));
108-
Assert.Equal(Pressure.FromMegabars(3), Quantity.Parse(CultureInfo.InvariantCulture, typeof(Pressure), "3.0 Mbar"));
107+
Assert.Equal(Length.FromCentimeters(3), Quantity.Parse(InvariantCulture, typeof(Length), "3 cm"));
108+
Assert.Equal(Mass.FromTonnes(3), Quantity.Parse(InvariantCulture, typeof(Mass), "03t"));
109+
Assert.Equal(Pressure.FromMegabars(3), Quantity.Parse(InvariantCulture, typeof(Pressure), "3.0 Mbar"));
109110
}
110111

111112
[Fact]
@@ -135,30 +136,30 @@ public void TryFrom_GivenValueAndUnit_ReturnsQuantity()
135136
[Fact]
136137
public void TryParse_GivenInvalidQuantityType_ReturnsFalseAndNullQuantity()
137138
{
138-
Assert.False(Quantity.TryParse(typeof(DummyIQuantity), "3.0 cm", out IQuantity? parsedLength));
139+
Assert.False(Quantity.TryParse(InvariantCulture, typeof(DummyIQuantity), "3.0 cm", out IQuantity? parsedLength));
139140
Assert.Null(parsedLength);
140141
}
141142

142143
[Fact]
143144
public void TryParse_GivenInvalidString_ReturnsFalseAndNullQuantity()
144145
{
145-
Assert.False(Quantity.TryParse(typeof(Length), "x cm", out IQuantity? parsedLength));
146+
Assert.False(Quantity.TryParse(InvariantCulture, typeof(Length), "x cm", out IQuantity? parsedLength));
146147
Assert.Null(parsedLength);
147148

148-
Assert.False(Quantity.TryParse(typeof(Mass), "xt", out IQuantity? parsedMass));
149+
Assert.False(Quantity.TryParse(InvariantCulture, typeof(Mass), "xt", out IQuantity? parsedMass));
149150
Assert.Null(parsedMass);
150151

151-
Assert.False(Quantity.TryParse(typeof(Pressure), "foo", out IQuantity? parsedPressure));
152+
Assert.False(Quantity.TryParse(InvariantCulture, typeof(Pressure), "foo", out IQuantity? parsedPressure));
152153
Assert.Null(parsedPressure);
153154
}
154155

155156
[Fact]
156157
public void TryParse_GivenValueAndUnit_ReturnsQuantity()
157158
{
158-
Assert.True(Quantity.TryParse(typeof(Length), "3 cm", out IQuantity? parsedLength));
159+
Assert.True(Quantity.TryParse(InvariantCulture, typeof(Length), "3 cm", out IQuantity? parsedLength));
159160
Assert.Equal(Length.FromCentimeters(3), parsedLength);
160161

161-
Assert.True(Quantity.TryParse(typeof(Mass), "03t", out IQuantity? parsedMass));
162+
Assert.True(Quantity.TryParse(InvariantCulture, typeof(Mass), "03t", out IQuantity? parsedMass));
162163
Assert.Equal(Mass.FromTonnes(3), parsedMass);
163164

164165
Assert.True(Quantity.TryParse(NumberFormatInfo.InvariantInfo, typeof(Pressure), "3.0 Mbar", out IQuantity? parsedPressure));

UnitsNet.Tests/QuantityTypeConverterTest.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public void ConvertTo_GivenStringType_ReturnsQuantityString()
160160

161161
var convertedQuantity = (string?)converter.ConvertTo(length, typeof(string));
162162

163-
Assert.Equal("1 m", convertedQuantity);
163+
Assert.Equal(Length.FromMeters(1).ToString(), convertedQuantity);
164164
}
165165

166166
[Fact]
@@ -242,10 +242,12 @@ public void ConvertTo_GivenSomeQuantityAndContextWithDisplayAsUnitAttributes_Ret
242242
});
243243
Length length = Length.FromMeters(1);
244244

245-
string convertedQuantityDefaultCulture = (string)converter.ConvertTo(length, typeof(string))!;
245+
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("en-US");
246+
247+
string convertedQuantityCurrentCulture = (string)converter.ConvertTo(length, typeof(string))!;
246248
string convertedQuantitySpecificCulture = (string)converter.ConvertTo(context, Culture, length, typeof(string))!;
247249

248-
Assert.Equal("1 m", convertedQuantityDefaultCulture);
250+
Assert.Equal("1 m", convertedQuantityCurrentCulture);
249251
Assert.Equal("10 dm", convertedQuantitySpecificCulture);
250252
}
251253

UnitsNet.Tests/UnitAbbreviationsCacheTests.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -152,21 +152,21 @@ public void ScientificNotationUpperIntervalFormatting(double value, string expec
152152
[Fact]
153153
public void AllUnitsImplementToStringForInvariantCulture()
154154
{
155-
Assert.Equal("1 °", Angle.FromDegrees(1).ToString());
156-
Assert.Equal("1 m²", Area.FromSquareMeters(1).ToString());
157-
Assert.Equal("1 V", ElectricPotential.FromVolts(1).ToString());
158-
Assert.Equal("1 N", Force.FromNewtons(1).ToString());
159-
Assert.Equal("1 m", Length.FromMeters(1).ToString());
160-
Assert.Equal("1 kg", Mass.FromKilograms(1).ToString());
161-
Assert.Equal("1 Pa", Pressure.FromPascals(1).ToString());
162-
Assert.Equal("1 rad/s", RotationalSpeed.FromRadiansPerSecond(1).ToString());
163-
Assert.Equal("1 K", Temperature.FromKelvins(1).ToString());
164-
Assert.Equal("1 N·m", Torque.FromNewtonMeters(1).ToString());
165-
Assert.Equal("1 m³", Volume.FromCubicMeters(1).ToString());
166-
Assert.Equal("1 m³/s", VolumeFlow.FromCubicMetersPerSecond(1).ToString());
167-
168-
Assert.Equal("2 ft 3 in", Length.FromFeetInches(2, 3).FeetInches.ToString());
169-
Assert.Equal("3 st 7 lb", Mass.FromStonePounds(3, 7).StonePounds.ToString());
155+
Assert.Equal("1 °", Angle.FromDegrees(1).ToString(CultureInfo.InvariantCulture));
156+
Assert.Equal("1 m²", Area.FromSquareMeters(1).ToString(CultureInfo.InvariantCulture));
157+
Assert.Equal("1 V", ElectricPotential.FromVolts(1).ToString(CultureInfo.InvariantCulture));
158+
Assert.Equal("1 N", Force.FromNewtons(1).ToString(CultureInfo.InvariantCulture));
159+
Assert.Equal("1 m", Length.FromMeters(1).ToString(CultureInfo.InvariantCulture));
160+
Assert.Equal("1 kg", Mass.FromKilograms(1).ToString(CultureInfo.InvariantCulture));
161+
Assert.Equal("1 Pa", Pressure.FromPascals(1).ToString(CultureInfo.InvariantCulture));
162+
Assert.Equal("1 rad/s", RotationalSpeed.FromRadiansPerSecond(1).ToString(CultureInfo.InvariantCulture));
163+
Assert.Equal("1 K", Temperature.FromKelvins(1).ToString(CultureInfo.InvariantCulture));
164+
Assert.Equal("1 N·m", Torque.FromNewtonMeters(1).ToString(CultureInfo.InvariantCulture));
165+
Assert.Equal("1 m³", Volume.FromCubicMeters(1).ToString(CultureInfo.InvariantCulture));
166+
Assert.Equal("1 m³/s", VolumeFlow.FromCubicMetersPerSecond(1).ToString(CultureInfo.InvariantCulture));
167+
168+
Assert.Equal("2 ft 3 in", Length.FromFeetInches(2, 3).FeetInches.ToString(CultureInfo.InvariantCulture));
169+
Assert.Equal("3 st 7 lb", Mass.FromStonePounds(3, 7).StonePounds.ToString(CultureInfo.InvariantCulture));
170170
}
171171

172172
[Fact]
@@ -244,7 +244,7 @@ public void MapUnitToAbbreviation_AddCustomUnit_DoesNotOverrideDefaultAbbreviati
244244
var cache = new UnitAbbreviationsCache();
245245
cache.MapUnitToAbbreviation(AreaUnit.SquareMeter, AmericanCulture, "m^2");
246246

247-
Assert.Equal("m²", cache.GetDefaultAbbreviation(AreaUnit.SquareMeter));
247+
Assert.Equal("m²", cache.GetDefaultAbbreviation(AreaUnit.SquareMeter, AmericanCulture));
248248
}
249249

250250
[Fact]

UnitsNet.Tests/UnitConverterTest.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed under MIT No Attribution, see LICENSE file at the root.
22
// Copyright 2013 Andreas Gullberg Larsen ([email protected]). Maintained at https://github.com/angularsen/UnitsNet.
33

4+
using System.Globalization;
45
using UnitsNet.Tests.CustomQuantities;
56
using UnitsNet.Units;
67
using Xunit;
@@ -208,24 +209,23 @@ public void ConvertByAbbreviation_ThrowsUnitNotFoundExceptionOnUnknownFromOrToUn
208209
}
209210

210211
[Theory]
211-
[InlineData(1, "UnknownQuantity", "m", "cm")]
212-
[InlineData(1, "Length", "UnknownFromUnit", "cm")]
213-
[InlineData(1, "Length", "m", "UnknownToUnit")]
214-
public void TryConvertByAbbreviation_ReturnsFalseForInvalidInput(double inputValue, string quantityTypeName, string fromUnit, string toUnit)
212+
[InlineData(1, "UnknownQuantity", "m", "cm", "en-US")]
213+
[InlineData(1, "Length", "UnknownFromUnit", "cm", "en-US")]
214+
[InlineData(1, "Length", "m", "UnknownToUnit", "en-US")]
215+
public void TryConvertByAbbreviation_ReturnsFalseForInvalidInput(double inputValue, string quantityTypeName, string fromUnit, string toUnit, string culture)
215216
{
216-
Assert.False(UnitConverter.TryConvertByAbbreviation(inputValue, quantityTypeName, fromUnit, toUnit, out double result));
217+
Assert.False(UnitConverter.TryConvertByAbbreviation(inputValue, quantityTypeName, fromUnit, toUnit, out double result, culture));
217218
Assert.Equal(0, result);
218219
}
219220

220221
[Theory]
221-
[InlineData(0, 0, "Length", "m", "cm")]
222-
[InlineData(100, 1, "Length", "m", "cm")]
223-
[InlineData(1, 1000, "Mass", "g", "kg")]
224-
[InlineData(1000, 1, "ElectricCurrent", "kA", "A")]
225-
public void TryConvertByAbbreviation_ReturnsTrueOnSuccessAndOutputsResult(double expectedValue, double inputValue, string quantityTypeName, string fromUnit,
226-
string toUnit)
222+
[InlineData(0, 0, "Length", "m", "cm", "en-US")]
223+
[InlineData(100, 1, "Length", "m", "cm", "en-US")]
224+
[InlineData(1, 1000, "Mass", "g", "kg", "en-US")]
225+
[InlineData(1000, 1, "ElectricCurrent", "kA", "A", "en-US")]
226+
public void TryConvertByAbbreviation_ReturnsTrueOnSuccessAndOutputsResult(double expectedValue, double inputValue, string quantityTypeName, string fromUnit, string toUnit, string culture)
227227
{
228-
Assert.True(UnitConverter.TryConvertByAbbreviation(inputValue, quantityTypeName, fromUnit, toUnit, out double result), "TryConvertByAbbreviation() return value.");
228+
Assert.True(UnitConverter.TryConvertByAbbreviation(inputValue, quantityTypeName, fromUnit, toUnit, out double result, culture), "TryConvertByAbbreviation() return value.");
229229
Assert.Equal(expectedValue, result);
230230
}
231231
}

0 commit comments

Comments
 (0)