@@ -36,9 +36,13 @@ namespace UnitsNet.Tests
36
36
public class UnitSystemTests
37
37
{
38
38
private readonly ITestOutputHelper _output ;
39
- private static CultureInfo CultureAmerican => new CultureInfo ( "en-US" ) ;
40
- private static CultureInfo CultureRussian => new CultureInfo ( "ru-RU" ) ;
41
- private static CultureInfo CultureNorwegian => new CultureInfo ( "nb-NO" ) ;
39
+ private const string AmericanCultureName = "en-US" ;
40
+ private const string RussianCultureName = "ru-RU" ;
41
+ private const string NorwegianCultureName = "nb-NO" ;
42
+
43
+ private static readonly IFormatProvider AmericanCulture = new CultureInfo ( AmericanCultureName ) ;
44
+ private static readonly IFormatProvider NorwegianCulture = new CultureInfo ( NorwegianCultureName ) ;
45
+ private static readonly IFormatProvider RussianCulture = new CultureInfo ( RussianCultureName ) ;
42
46
43
47
public UnitSystemTests ( ITestOutputHelper output )
44
48
{
@@ -54,7 +58,7 @@ public UnitSystemTests(ITestOutputHelper output)
54
58
[ InlineData ( 0.115 , "0.12 m" ) ]
55
59
public void DefaultToStringFormatting ( double value , string expected )
56
60
{
57
- string actual = Length . FromMeters ( value ) . ToString ( LengthUnit . Meter , CultureAmerican ) ;
61
+ string actual = Length . FromMeters ( value ) . ToString ( LengthUnit . Meter , AmericanCulture ) ;
58
62
Assert . Equal ( expected , actual ) ;
59
63
}
60
64
@@ -181,7 +185,7 @@ public void Parse_UnknownAbbreviationThrowsUnitNotFoundException()
181
185
[ InlineData ( 6 , "1.123457 m" ) ]
182
186
public void CustomNumberOfSignificantDigitsAfterRadixFormatting ( int significantDigitsAfterRadix , string expected )
183
187
{
184
- string actual = Length . FromMeters ( 1.123456789 ) . ToString ( LengthUnit . Meter , CultureAmerican , significantDigitsAfterRadix ) ;
188
+ string actual = Length . FromMeters ( 1.123456789 ) . ToString ( LengthUnit . Meter , AmericanCulture , significantDigitsAfterRadix ) ;
185
189
Assert . Equal ( expected , actual ) ;
186
190
}
187
191
@@ -196,7 +200,7 @@ public void CustomNumberOfSignificantDigitsAfterRadixFormatting(int significantD
196
200
public void RoundingErrorsWithSignificantDigitsAfterRadixFormatting ( double value ,
197
201
int maxSignificantDigitsAfterRadix , string expected )
198
202
{
199
- string actual = Length . FromMeters ( value ) . ToString ( LengthUnit . Meter , CultureAmerican , maxSignificantDigitsAfterRadix ) ;
203
+ string actual = Length . FromMeters ( value ) . ToString ( LengthUnit . Meter , AmericanCulture , maxSignificantDigitsAfterRadix ) ;
200
204
Assert . Equal ( expected , actual ) ;
201
205
}
202
206
@@ -208,7 +212,7 @@ public void RoundingErrorsWithSignificantDigitsAfterRadixFormatting(double value
208
212
[ InlineData ( 1.99e-4 , "1.99e-04 m" ) ]
209
213
public void ScientificNotationLowerInterval ( double value , string expected )
210
214
{
211
- string actual = Length . FromMeters ( value ) . ToString ( LengthUnit . Meter , CultureAmerican ) ;
215
+ string actual = Length . FromMeters ( value ) . ToString ( LengthUnit . Meter , AmericanCulture ) ;
212
216
Assert . Equal ( expected , actual ) ;
213
217
}
214
218
@@ -219,7 +223,7 @@ public void ScientificNotationLowerInterval(double value, string expected)
219
223
[ InlineData ( 999.99 , "999.99 m" ) ]
220
224
public void FixedPointNotationIntervalFormatting ( double value , string expected )
221
225
{
222
- string actual = Length . FromMeters ( value ) . ToString ( LengthUnit . Meter , CultureAmerican ) ;
226
+ string actual = Length . FromMeters ( value ) . ToString ( LengthUnit . Meter , AmericanCulture ) ;
223
227
Assert . Equal ( expected , actual ) ;
224
228
}
225
229
@@ -231,7 +235,7 @@ public void FixedPointNotationIntervalFormatting(double value, string expected)
231
235
[ InlineData ( 999999.99 , "999,999.99 m" ) ]
232
236
public void FixedPointNotationWithDigitGroupingIntervalFormatting ( double value , string expected )
233
237
{
234
- string actual = Length . FromMeters ( value ) . ToString ( LengthUnit . Meter , CultureAmerican ) ;
238
+ string actual = Length . FromMeters ( value ) . ToString ( LengthUnit . Meter , AmericanCulture ) ;
235
239
Assert . Equal ( expected , actual ) ;
236
240
}
237
241
@@ -242,7 +246,7 @@ public void FixedPointNotationWithDigitGroupingIntervalFormatting(double value,
242
246
[ InlineData ( double . MaxValue , "1.8e+308 m" ) ]
243
247
public void ScientificNotationUpperIntervalFormatting ( double value , string expected )
244
248
{
245
- string actual = Length . FromMeters ( value ) . ToString ( LengthUnit . Meter , CultureAmerican ) ;
249
+ string actual = Length . FromMeters ( value ) . ToString ( LengthUnit . Meter , AmericanCulture ) ;
246
250
Assert . Equal ( expected , actual ) ;
247
251
}
248
252
@@ -366,41 +370,41 @@ public void AllUnitsImplementToStringForInvariantCulture()
366
370
[ Fact ]
367
371
public void ToString_WithNorwegianCulture ( )
368
372
{
369
- Assert . Equal ( "1 °" , Angle . FromDegrees ( 1 ) . ToString ( AngleUnit . Degree , CultureNorwegian ) ) ;
370
- Assert . Equal ( "1 m²" , Area . FromSquareMeters ( 1 ) . ToString ( AreaUnit . SquareMeter , CultureNorwegian ) ) ;
371
- Assert . Equal ( "1 V" , ElectricPotential . FromVolts ( 1 ) . ToString ( ElectricPotentialUnit . Volt , CultureNorwegian ) ) ;
372
- Assert . Equal ( "1 m³/s" , Flow . FromCubicMetersPerSecond ( 1 ) . ToString ( FlowUnit . CubicMeterPerSecond , CultureNorwegian ) ) ;
373
- Assert . Equal ( "1 N" , Force . FromNewtons ( 1 ) . ToString ( ForceUnit . Newton , CultureNorwegian ) ) ;
374
- Assert . Equal ( "1 m" , Length . FromMeters ( 1 ) . ToString ( LengthUnit . Meter , CultureNorwegian ) ) ;
375
- Assert . Equal ( "1 kg" , Mass . FromKilograms ( 1 ) . ToString ( MassUnit . Kilogram , CultureNorwegian ) ) ;
376
- Assert . Equal ( "1 Pa" , Pressure . FromPascals ( 1 ) . ToString ( PressureUnit . Pascal , CultureNorwegian ) ) ;
377
- Assert . Equal ( "1 rad/s" , RotationalSpeed . FromRadiansPerSecond ( 1 ) . ToString ( RotationalSpeedUnit . RadianPerSecond , CultureNorwegian ) ) ;
378
- Assert . Equal ( "1 K" , Temperature . FromKelvins ( 1 ) . ToString ( TemperatureUnit . Kelvin , CultureNorwegian ) ) ;
379
- Assert . Equal ( "1 N·m" , Torque . FromNewtonMeters ( 1 ) . ToString ( TorqueUnit . NewtonMeter , CultureNorwegian ) ) ;
380
- Assert . Equal ( "1 m³" , Volume . FromCubicMeters ( 1 ) . ToString ( VolumeUnit . CubicMeter , CultureNorwegian ) ) ;
373
+ Assert . Equal ( "1 °" , Angle . FromDegrees ( 1 ) . ToString ( AngleUnit . Degree , NorwegianCulture ) ) ;
374
+ Assert . Equal ( "1 m²" , Area . FromSquareMeters ( 1 ) . ToString ( AreaUnit . SquareMeter , NorwegianCulture ) ) ;
375
+ Assert . Equal ( "1 V" , ElectricPotential . FromVolts ( 1 ) . ToString ( ElectricPotentialUnit . Volt , NorwegianCulture ) ) ;
376
+ Assert . Equal ( "1 m³/s" , VolumeFlow . FromCubicMetersPerSecond ( 1 ) . ToString ( VolumeFlowUnit . CubicMeterPerSecond , NorwegianCulture ) ) ;
377
+ Assert . Equal ( "1 N" , Force . FromNewtons ( 1 ) . ToString ( ForceUnit . Newton , NorwegianCulture ) ) ;
378
+ Assert . Equal ( "1 m" , Length . FromMeters ( 1 ) . ToString ( LengthUnit . Meter , NorwegianCulture ) ) ;
379
+ Assert . Equal ( "1 kg" , Mass . FromKilograms ( 1 ) . ToString ( MassUnit . Kilogram , NorwegianCulture ) ) ;
380
+ Assert . Equal ( "1 Pa" , Pressure . FromPascals ( 1 ) . ToString ( PressureUnit . Pascal , NorwegianCulture ) ) ;
381
+ Assert . Equal ( "1 rad/s" , RotationalSpeed . FromRadiansPerSecond ( 1 ) . ToString ( RotationalSpeedUnit . RadianPerSecond , NorwegianCulture ) ) ;
382
+ Assert . Equal ( "1 K" , Temperature . FromKelvins ( 1 ) . ToString ( TemperatureUnit . Kelvin , NorwegianCulture ) ) ;
383
+ Assert . Equal ( "1 N·m" , Torque . FromNewtonMeters ( 1 ) . ToString ( TorqueUnit . NewtonMeter , NorwegianCulture ) ) ;
384
+ Assert . Equal ( "1 m³" , Volume . FromCubicMeters ( 1 ) . ToString ( VolumeUnit . CubicMeter , NorwegianCulture ) ) ;
381
385
}
382
386
383
387
[ Fact ]
384
388
public void ToString_WithRussianCulture ( )
385
389
{
386
- Assert . Equal ( "1 °" , Angle . FromDegrees ( 1 ) . ToString ( AngleUnit . Degree , CultureRussian ) ) ;
387
- Assert . Equal ( "1 м²" , Area . FromSquareMeters ( 1 ) . ToString ( AreaUnit . SquareMeter , CultureRussian ) ) ;
388
- Assert . Equal ( "1 В" , ElectricPotential . FromVolts ( 1 ) . ToString ( ElectricPotentialUnit . Volt , CultureRussian ) ) ;
389
- Assert . Equal ( "1 м³/с" , Flow . FromCubicMetersPerSecond ( 1 ) . ToString ( FlowUnit . CubicMeterPerSecond , CultureRussian ) ) ;
390
- Assert . Equal ( "1 Н" , Force . FromNewtons ( 1 ) . ToString ( ForceUnit . Newton , CultureRussian ) ) ;
391
- Assert . Equal ( "1 м" , Length . FromMeters ( 1 ) . ToString ( LengthUnit . Meter , CultureRussian ) ) ;
392
- Assert . Equal ( "1 кг" , Mass . FromKilograms ( 1 ) . ToString ( MassUnit . Kilogram , CultureRussian ) ) ;
393
- Assert . Equal ( "1 Па" , Pressure . FromPascals ( 1 ) . ToString ( PressureUnit . Pascal , CultureRussian ) ) ;
394
- Assert . Equal ( "1 рад/с" , RotationalSpeed . FromRadiansPerSecond ( 1 ) . ToString ( RotationalSpeedUnit . RadianPerSecond , CultureRussian ) ) ;
395
- Assert . Equal ( "1 K" , Temperature . FromKelvins ( 1 ) . ToString ( TemperatureUnit . Kelvin , CultureRussian ) ) ;
396
- Assert . Equal ( "1 Н·м" , Torque . FromNewtonMeters ( 1 ) . ToString ( TorqueUnit . NewtonMeter , CultureRussian ) ) ;
397
- Assert . Equal ( "1 м³" , Volume . FromCubicMeters ( 1 ) . ToString ( VolumeUnit . CubicMeter , CultureRussian ) ) ;
390
+ Assert . Equal ( "1 °" , Angle . FromDegrees ( 1 ) . ToString ( AngleUnit . Degree , RussianCulture ) ) ;
391
+ Assert . Equal ( "1 м²" , Area . FromSquareMeters ( 1 ) . ToString ( AreaUnit . SquareMeter , RussianCulture ) ) ;
392
+ Assert . Equal ( "1 В" , ElectricPotential . FromVolts ( 1 ) . ToString ( ElectricPotentialUnit . Volt , RussianCulture ) ) ;
393
+ Assert . Equal ( "1 м³/с" , VolumeFlow . FromCubicMetersPerSecond ( 1 ) . ToString ( VolumeFlowUnit . CubicMeterPerSecond , RussianCulture ) ) ;
394
+ Assert . Equal ( "1 Н" , Force . FromNewtons ( 1 ) . ToString ( ForceUnit . Newton , RussianCulture ) ) ;
395
+ Assert . Equal ( "1 м" , Length . FromMeters ( 1 ) . ToString ( LengthUnit . Meter , RussianCulture ) ) ;
396
+ Assert . Equal ( "1 кг" , Mass . FromKilograms ( 1 ) . ToString ( MassUnit . Kilogram , RussianCulture ) ) ;
397
+ Assert . Equal ( "1 Па" , Pressure . FromPascals ( 1 ) . ToString ( PressureUnit . Pascal , RussianCulture ) ) ;
398
+ Assert . Equal ( "1 рад/с" , RotationalSpeed . FromRadiansPerSecond ( 1 ) . ToString ( RotationalSpeedUnit . RadianPerSecond , RussianCulture ) ) ;
399
+ Assert . Equal ( "1 K" , Temperature . FromKelvins ( 1 ) . ToString ( TemperatureUnit . Kelvin , RussianCulture ) ) ;
400
+ Assert . Equal ( "1 Н·м" , Torque . FromNewtonMeters ( 1 ) . ToString ( TorqueUnit . NewtonMeter , RussianCulture ) ) ;
401
+ Assert . Equal ( "1 м³" , Volume . FromCubicMeters ( 1 ) . ToString ( VolumeUnit . CubicMeter , RussianCulture ) ) ;
398
402
}
399
403
400
404
[ Fact ]
401
405
public void GetDefaultAbbreviationFallsBackToDefaultStringIfNotSpecified ( )
402
406
{
403
- UnitSystem usUnits = UnitSystem . GetCached ( CultureAmerican ) ;
407
+ UnitSystem usUnits = UnitSystem . GetCached ( AmericanCultureName ) ;
404
408
string abbreviation = usUnits . GetDefaultAbbreviation ( CustomUnit . Unit1 ) ;
405
409
Assert . Equal ( "(no abbreviation for CustomUnit.Unit1)" , abbreviation ) ;
406
410
}
@@ -415,7 +419,7 @@ public void GetDefaultAbbreviationFallsBackToUsEnglishCulture()
415
419
UnitSystem zuluUnits = UnitSystem . GetCached ( zuluCulture ) ;
416
420
CultureInfo . CurrentCulture = CultureInfo . CurrentUICulture = zuluCulture ;
417
421
418
- UnitSystem usUnits = UnitSystem . GetCached ( CultureAmerican ) ;
422
+ UnitSystem usUnits = UnitSystem . GetCached ( AmericanCultureName ) ;
419
423
usUnits . MapUnitToAbbreviation ( CustomUnit . Unit1 , "US english abbreviation for Unit1" ) ;
420
424
421
425
// Act
@@ -428,7 +432,7 @@ public void GetDefaultAbbreviationFallsBackToUsEnglishCulture()
428
432
[ Fact ]
429
433
public void MapUnitToAbbreviation_AddCustomUnit_DoesNotOverrideDefaultAbbreviationForAlreadyMappedUnits ( )
430
434
{
431
- UnitSystem unitSystem = UnitSystem . GetCached ( CultureAmerican ) ;
435
+ UnitSystem unitSystem = UnitSystem . GetCached ( AmericanCultureName ) ;
432
436
unitSystem . MapUnitToAbbreviation ( AreaUnit . SquareMeter , "m^2" ) ;
433
437
434
438
Assert . Equal ( "m²" , unitSystem . GetDefaultAbbreviation ( AreaUnit . SquareMeter ) ) ;
0 commit comments