25
25
using Xunit ;
26
26
using UnitsNet . Units ;
27
27
using Xunit . Abstractions ;
28
- #if WINDOWS_UWP
29
- using Culture = System . String ;
30
- #else
31
28
using System . Globalization ;
32
- using Culture = System . IFormatProvider ;
33
- #endif
34
29
35
30
namespace UnitsNet . Tests
36
31
{
@@ -41,6 +36,9 @@ namespace UnitsNet.Tests
41
36
public class UnitSystemTests
42
37
{
43
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" ) ;
44
42
45
43
public UnitSystemTests ( ITestOutputHelper output )
46
44
{
@@ -56,12 +54,7 @@ public UnitSystemTests(ITestOutputHelper output)
56
54
[ InlineData ( 0.115 , "0.12 m" ) ]
57
55
public void DefaultToStringFormatting ( double value , string expected )
58
56
{
59
- #if WINDOWS_UWP
60
- Culture cultureEnUs = "en-US" ;
61
- #else
62
- Culture cultureEnUs = new CultureInfo ( "en-US" ) ;
63
- #endif
64
- string actual = Length . FromMeters ( value ) . ToString ( LengthUnit . Meter , cultureEnUs ) ;
57
+ string actual = Length . FromMeters ( value ) . ToString ( LengthUnit . Meter , CultureAmerican ) ;
65
58
Assert . Equal ( expected , actual ) ;
66
59
}
67
60
@@ -85,11 +78,7 @@ private static IEnumerable<object> GetUnitTypesWithMissingAbbreviations<TUnitTyp
85
78
{
86
79
try
87
80
{
88
- #if WINDOWS_UWP
89
- unitSystem . GetDefaultAbbreviation ( unit . GetType ( ) , Convert . ToInt32 ( unit ) ) ;
90
- #else
91
81
unitSystem . GetDefaultAbbreviation ( unit ) ;
92
- #endif
93
82
}
94
83
catch
95
84
{
@@ -133,7 +122,7 @@ public void DecimalRadixPointCultureFormatting(string culture)
133
122
[ InlineData ( "es-MX" ) ]
134
123
public void CommaDigitGroupingCultureFormatting ( string cultureName )
135
124
{
136
- Culture culture = GetCulture ( cultureName ) ;
125
+ CultureInfo culture = GetCulture ( cultureName ) ;
137
126
Assert . Equal ( "1,111 m" , Length . FromMeters ( 1111 ) . ToString ( LengthUnit . Meter , culture ) ) ;
138
127
139
128
// Feet/Inch and Stone/Pound combinations are only used (customarily) in the US, UK and maybe Ireland - all English speaking countries.
@@ -154,14 +143,6 @@ public void SpaceDigitGroupingCultureFormatting(string culture)
154
143
Assert . Equal ( "1 111 m" , Length . FromMeters ( 1111 ) . ToString ( LengthUnit . Meter , GetCulture ( culture ) ) ) ;
155
144
}
156
145
157
- // Switzerland uses an apostrophe for digit grouping
158
- // [Ignore("Fails on Win 8.1 and Win10 due to a bug in .NET framework.")]
159
- // [InlineData("fr-CH")]
160
- // public void ApostropheDigitGroupingCultureFormatting(string culture)
161
- // {
162
- // Assert.Equal("1'111 m", Length.FromMeters(1111).ToString(LengthUnit.Meter, new CultureInfo(culture)));
163
- // }
164
-
165
146
// These cultures all use a decimal point in digit grouping
166
147
[ Theory ]
167
148
[ InlineData ( "de-DE" ) ]
@@ -174,7 +155,6 @@ public void DecimalPointDigitGroupingCultureFormatting(string culture)
174
155
Assert . Equal ( "1.111 m" , Length . FromMeters ( 1111 ) . ToString ( LengthUnit . Meter , GetCulture ( culture ) ) ) ;
175
156
}
176
157
177
- #if ! WINDOWS_UWP
178
158
[ Theory ]
179
159
[ InlineData ( "m^^2" , AreaUnit . SquareMeter ) ]
180
160
[ InlineData ( "cm^^2" , AreaUnit . SquareCentimeter ) ]
@@ -191,7 +171,6 @@ public void Parse_UnknownAbbreviationThrowsUnitNotFoundException()
191
171
{
192
172
Assert . Throws < UnitNotFoundException > ( ( ) => UnitSystem . Default . Parse < AreaUnit > ( "nonexistingunit" ) ) ;
193
173
}
194
- #endif
195
174
196
175
[ Theory ]
197
176
[ InlineData ( 1 , "1.1 m" ) ]
@@ -202,12 +181,7 @@ public void Parse_UnknownAbbreviationThrowsUnitNotFoundException()
202
181
[ InlineData ( 6 , "1.123457 m" ) ]
203
182
public void CustomNumberOfSignificantDigitsAfterRadixFormatting ( int significantDigitsAfterRadix , string expected )
204
183
{
205
- #if WINDOWS_UWP
206
- Culture cultureEnUs = "en-US" ;
207
- #else
208
- Culture cultureEnUs = new CultureInfo ( "en-US" ) ;
209
- #endif
210
- string actual = Length . FromMeters ( 1.123456789 ) . ToString ( LengthUnit . Meter , cultureEnUs , significantDigitsAfterRadix ) ;
184
+ string actual = Length . FromMeters ( 1.123456789 ) . ToString ( LengthUnit . Meter , CultureAmerican , significantDigitsAfterRadix ) ;
211
185
Assert . Equal ( expected , actual ) ;
212
186
}
213
187
@@ -222,12 +196,7 @@ public void CustomNumberOfSignificantDigitsAfterRadixFormatting(int significantD
222
196
public void RoundingErrorsWithSignificantDigitsAfterRadixFormatting ( double value ,
223
197
int maxSignificantDigitsAfterRadix , string expected )
224
198
{
225
- #if WINDOWS_UWP
226
- Culture cultureEnUs = "en-US" ;
227
- #else
228
- Culture cultureEnUs = new CultureInfo ( "en-US" ) ;
229
- #endif
230
- string actual = Length . FromMeters ( value ) . ToString ( LengthUnit . Meter , cultureEnUs , maxSignificantDigitsAfterRadix ) ;
199
+ string actual = Length . FromMeters ( value ) . ToString ( LengthUnit . Meter , CultureAmerican , maxSignificantDigitsAfterRadix ) ;
231
200
Assert . Equal ( expected , actual ) ;
232
201
}
233
202
@@ -239,12 +208,7 @@ public void RoundingErrorsWithSignificantDigitsAfterRadixFormatting(double value
239
208
[ InlineData ( 1.99e-4 , "1.99e-04 m" ) ]
240
209
public void ScientificNotationLowerInterval ( double value , string expected )
241
210
{
242
- #if WINDOWS_UWP
243
- Culture cultureEnUs = "en-US" ;
244
- #else
245
- Culture cultureEnUs = new CultureInfo ( "en-US" ) ;
246
- #endif
247
- string actual = Length . FromMeters ( value ) . ToString ( LengthUnit . Meter , cultureEnUs ) ;
211
+ string actual = Length . FromMeters ( value ) . ToString ( LengthUnit . Meter , CultureAmerican ) ;
248
212
Assert . Equal ( expected , actual ) ;
249
213
}
250
214
@@ -255,12 +219,7 @@ public void ScientificNotationLowerInterval(double value, string expected)
255
219
[ InlineData ( 999.99 , "999.99 m" ) ]
256
220
public void FixedPointNotationIntervalFormatting ( double value , string expected )
257
221
{
258
- #if WINDOWS_UWP
259
- Culture cultureEnUs = "en-US" ;
260
- #else
261
- Culture cultureEnUs = new CultureInfo ( "en-US" ) ;
262
- #endif
263
- string actual = Length . FromMeters ( value ) . ToString ( LengthUnit . Meter , cultureEnUs ) ;
222
+ string actual = Length . FromMeters ( value ) . ToString ( LengthUnit . Meter , CultureAmerican ) ;
264
223
Assert . Equal ( expected , actual ) ;
265
224
}
266
225
@@ -272,12 +231,7 @@ public void FixedPointNotationIntervalFormatting(double value, string expected)
272
231
[ InlineData ( 999999.99 , "999,999.99 m" ) ]
273
232
public void FixedPointNotationWithDigitGroupingIntervalFormatting ( double value , string expected )
274
233
{
275
- #if WINDOWS_UWP
276
- Culture cultureEnUs = "en-US" ;
277
- #else
278
- Culture cultureEnUs = new CultureInfo ( "en-US" ) ;
279
- #endif
280
- string actual = Length . FromMeters ( value ) . ToString ( LengthUnit . Meter , cultureEnUs ) ;
234
+ string actual = Length . FromMeters ( value ) . ToString ( LengthUnit . Meter , CultureAmerican ) ;
281
235
Assert . Equal ( expected , actual ) ;
282
236
}
283
237
@@ -288,12 +242,7 @@ public void FixedPointNotationWithDigitGroupingIntervalFormatting(double value,
288
242
[ InlineData ( double . MaxValue , "1.8e+308 m" ) ]
289
243
public void ScientificNotationUpperIntervalFormatting ( double value , string expected )
290
244
{
291
- #if WINDOWS_UWP
292
- Culture cultureEnUs = "en-US" ;
293
- #else
294
- Culture cultureEnUs = new CultureInfo ( "en-US" ) ;
295
- #endif
296
- string actual = Length . FromMeters ( value ) . ToString ( LengthUnit . Meter , cultureEnUs ) ;
245
+ string actual = Length . FromMeters ( value ) . ToString ( LengthUnit . Meter , CultureAmerican ) ;
297
246
Assert . Equal ( expected , actual ) ;
298
247
}
299
248
@@ -417,73 +366,56 @@ public void AllUnitsImplementToStringForInvariantCulture()
417
366
[ Fact ]
418
367
public void ToString_WithNorwegianCulture ( )
419
368
{
420
- #if WINDOWS_UWP
421
- Culture norwegian = "nb-NO" ;
422
- #else
423
- Culture norwegian = new CultureInfo ( "nb-NO" ) ;
424
- #endif
425
- Assert . Equal ( "1 °" , Angle . FromDegrees ( 1 ) . ToString ( AngleUnit . Degree , norwegian ) ) ;
426
- Assert . Equal ( "1 m²" , Area . FromSquareMeters ( 1 ) . ToString ( AreaUnit . SquareMeter , norwegian ) ) ;
427
- Assert . Equal ( "1 V" , ElectricPotential . FromVolts ( 1 ) . ToString ( ElectricPotentialUnit . Volt , norwegian ) ) ;
428
- Assert . Equal ( "1 m³/s" , Flow . FromCubicMetersPerSecond ( 1 ) . ToString ( FlowUnit . CubicMeterPerSecond , norwegian ) ) ;
429
- Assert . Equal ( "1 N" , Force . FromNewtons ( 1 ) . ToString ( ForceUnit . Newton , norwegian ) ) ;
430
- Assert . Equal ( "1 m" , Length . FromMeters ( 1 ) . ToString ( LengthUnit . Meter , norwegian ) ) ;
431
- Assert . Equal ( "1 kg" , Mass . FromKilograms ( 1 ) . ToString ( MassUnit . Kilogram , norwegian ) ) ;
432
- Assert . Equal ( "1 Pa" , Pressure . FromPascals ( 1 ) . ToString ( PressureUnit . Pascal , norwegian ) ) ;
433
- Assert . Equal ( "1 rad/s" , RotationalSpeed . FromRadiansPerSecond ( 1 ) . ToString ( RotationalSpeedUnit . RadianPerSecond , norwegian ) ) ;
434
- Assert . Equal ( "1 K" , Temperature . FromKelvins ( 1 ) . ToString ( TemperatureUnit . Kelvin , norwegian ) ) ;
435
- Assert . Equal ( "1 N·m" , Torque . FromNewtonMeters ( 1 ) . ToString ( TorqueUnit . NewtonMeter , norwegian ) ) ;
436
- Assert . Equal ( "1 m³" , Volume . FromCubicMeters ( 1 ) . ToString ( VolumeUnit . CubicMeter , norwegian ) ) ;
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 ) ) ;
437
381
}
438
382
439
383
[ Fact ]
440
384
public void ToString_WithRussianCulture ( )
441
385
{
442
- #if WINDOWS_UWP
443
- Culture russian = "ru-RU" ;
444
- #else
445
- Culture russian = new CultureInfo ( "ru-RU" ) ;
446
- #endif
447
- Assert . Equal ( "1 °" , Angle . FromDegrees ( 1 ) . ToString ( AngleUnit . Degree , russian ) ) ;
448
- Assert . Equal ( "1 м²" , Area . FromSquareMeters ( 1 ) . ToString ( AreaUnit . SquareMeter , russian ) ) ;
449
- Assert . Equal ( "1 В" , ElectricPotential . FromVolts ( 1 ) . ToString ( ElectricPotentialUnit . Volt , russian ) ) ;
450
- Assert . Equal ( "1 м³/с" , Flow . FromCubicMetersPerSecond ( 1 ) . ToString ( FlowUnit . CubicMeterPerSecond , russian ) ) ;
451
- Assert . Equal ( "1 Н" , Force . FromNewtons ( 1 ) . ToString ( ForceUnit . Newton , russian ) ) ;
452
- Assert . Equal ( "1 м" , Length . FromMeters ( 1 ) . ToString ( LengthUnit . Meter , russian ) ) ;
453
- Assert . Equal ( "1 кг" , Mass . FromKilograms ( 1 ) . ToString ( MassUnit . Kilogram , russian ) ) ;
454
- Assert . Equal ( "1 Па" , Pressure . FromPascals ( 1 ) . ToString ( PressureUnit . Pascal , russian ) ) ;
455
- Assert . Equal ( "1 рад/с" , RotationalSpeed . FromRadiansPerSecond ( 1 ) . ToString ( RotationalSpeedUnit . RadianPerSecond , russian ) ) ;
456
- Assert . Equal ( "1 K" , Temperature . FromKelvins ( 1 ) . ToString ( TemperatureUnit . Kelvin , russian ) ) ;
457
- Assert . Equal ( "1 Н·м" , Torque . FromNewtonMeters ( 1 ) . ToString ( TorqueUnit . NewtonMeter , russian ) ) ;
458
- Assert . Equal ( "1 м³" , Volume . FromCubicMeters ( 1 ) . ToString ( VolumeUnit . CubicMeter , russian ) ) ;
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 ) ) ;
459
398
}
460
399
461
400
[ Fact ]
462
401
public void GetDefaultAbbreviationFallsBackToDefaultStringIfNotSpecified ( )
463
402
{
464
- UnitSystem usUnits = UnitSystem . GetCached ( GetCulture ( "en-US" ) ) ;
465
-
466
- #if WINDOWS_UWP
467
- string abbreviation = usUnits . GetDefaultAbbreviation ( typeof ( CustomUnit ) , ( int ) CustomUnit . Unit1 ) ;
468
- Assert . Equal ( "(no abbreviation for CustomUnit with numeric value 1)" , abbreviation ) ;
469
- #else
403
+ UnitSystem usUnits = UnitSystem . GetCached ( CultureAmerican ) ;
470
404
string abbreviation = usUnits . GetDefaultAbbreviation ( CustomUnit . Unit1 ) ;
471
405
Assert . Equal ( "(no abbreviation for CustomUnit.Unit1)" , abbreviation ) ;
472
- #endif
473
406
}
474
407
475
- #if ! WINDOWS_UWP
476
408
[ Fact ]
477
409
public void GetDefaultAbbreviationFallsBackToUsEnglishCulture ( )
478
410
{
479
411
// CurrentCulture affects number formatting, such as comma or dot as decimal separator.
480
412
// CurrentUICulture affects localization, in this case the abbreviation.
481
413
// Zulu (South Africa)
482
- CultureInfo zuluCulture = new CultureInfo ( "zu-ZA" ) ;
414
+ var zuluCulture = new CultureInfo ( "zu-ZA" ) ;
483
415
UnitSystem zuluUnits = UnitSystem . GetCached ( zuluCulture ) ;
484
416
CultureInfo . CurrentCulture = CultureInfo . CurrentUICulture = zuluCulture ;
485
417
486
- UnitSystem usUnits = UnitSystem . GetCached ( new CultureInfo ( "en-US" ) ) ;
418
+ UnitSystem usUnits = UnitSystem . GetCached ( CultureAmerican ) ;
487
419
usUnits . MapUnitToAbbreviation ( CustomUnit . Unit1 , "US english abbreviation for Unit1" ) ;
488
420
489
421
// Act
@@ -492,19 +424,15 @@ public void GetDefaultAbbreviationFallsBackToUsEnglishCulture()
492
424
// Assert
493
425
Assert . Equal ( "US english abbreviation for Unit1" , abbreviation ) ;
494
426
}
495
- #endif
496
427
497
- #if ! WINDOWS_UWP
498
428
[ Fact ]
499
429
public void MapUnitToAbbreviation_AddCustomUnit_DoesNotOverrideDefaultAbbreviationForAlreadyMappedUnits ( )
500
430
{
501
- CultureInfo cultureInfo = new CultureInfo ( "en-US" ) ;
502
- UnitSystem unitSystem = UnitSystem . GetCached ( cultureInfo ) ;
431
+ UnitSystem unitSystem = UnitSystem . GetCached ( CultureAmerican ) ;
503
432
unitSystem . MapUnitToAbbreviation ( AreaUnit . SquareMeter , "m^2" ) ;
504
433
505
434
Assert . Equal ( "m²" , unitSystem . GetDefaultAbbreviation ( AreaUnit . SquareMeter ) ) ;
506
435
}
507
- #endif
508
436
509
437
[ Fact ]
510
438
public void NegativeInfinityFormatting ( )
@@ -518,7 +446,6 @@ public void NotANumberFormatting()
518
446
Assert . Equal ( "NaN m" , Length . FromMeters ( double . NaN ) . ToString ( ) ) ;
519
447
}
520
448
521
- #if ! WINDOWS_UWP
522
449
[ Fact ]
523
450
public void Parse_AmbiguousUnitsThrowsException ( )
524
451
{
@@ -530,7 +457,6 @@ public void Parse_AmbiguousUnitsThrowsException()
530
457
// Act 2
531
458
Assert . Throws < AmbiguousUnitParseException > ( ( ) => Volume . Parse ( "1 tsp" ) ) ;
532
459
}
533
- #endif
534
460
535
461
[ Fact ]
536
462
public void Parse_UnambiguousUnitsDoesNotThrow ( )
@@ -547,16 +473,11 @@ public void PositiveInfinityFormatting()
547
473
}
548
474
549
475
/// <summary>
550
- /// Convenience method to use the proper culture parameter type.
551
- /// The UWP lib uses culture name string instead of CultureInfo.
476
+ /// Convenience method to the proper culture parameter type.
552
477
/// </summary>
553
- private static Culture GetCulture ( string cultureName )
478
+ private static CultureInfo GetCulture ( string cultureName )
554
479
{
555
- #if WINDOWS_UWP
556
- return cultureName ;
557
- #else
558
480
return new CultureInfo ( cultureName ) ;
559
- #endif
560
481
}
561
482
}
562
- }
483
+ }
0 commit comments