@@ -31,7 +31,7 @@ internal class UnitTestBaseClassGenerator : GeneratorBase
31
31
private readonly string _unitEnumName ;
32
32
33
33
/// <summary>
34
- /// Example: "m" for Length quantity.
34
+ /// Example: " m" for Length quantity with leading whitespace or "" for Ratio quantity where base unit does not have an abbreviation .
35
35
/// </summary>
36
36
private readonly string _baseUnitEnglishAbbreviation ;
37
37
@@ -61,8 +61,13 @@ public UnitTestBaseClassGenerator(Quantity quantity)
61
61
62
62
/// <summary>
63
63
/// Gets the first en-US abbreviation for the unit -or- empty string if none is defined.
64
+ /// If a unit abbreviation exists, a leading whitespace is added to separate the number and the abbreviation like "1 m".
64
65
/// </summary>
65
- private static string GetEnglishAbbreviation ( Unit unit ) => unit . Localization . First ( l => l . Culture == "en-US" ) . Abbreviations . FirstOrDefault ( ) ?? "" ;
66
+ private static string GetEnglishAbbreviation ( Unit unit )
67
+ {
68
+ var unitAbbreviation = unit . Localization . First ( l => l . Culture == "en-US" ) . Abbreviations . FirstOrDefault ( ) ;
69
+ return string . IsNullOrEmpty ( unitAbbreviation ) ? "" : $ " { unitAbbreviation } ";
70
+ }
66
71
67
72
public override string Generate ( )
68
73
{
@@ -464,7 +469,7 @@ public void ToString_ReturnsValueAndUnitAbbreviationInCurrentCulture()
464
469
foreach ( var unit in _quantity . Units )
465
470
{
466
471
Writer . WL ( $@ "
467
- Assert.Equal(""1 { GetEnglishAbbreviation ( unit ) } "", new { _quantity . Name } (1, { GetUnitFullName ( unit ) } ).ToString());" ) ;
472
+ Assert.Equal(""1{ GetEnglishAbbreviation ( unit ) } "", new { _quantity . Name } (1, { GetUnitFullName ( unit ) } ).ToString());" ) ;
468
473
}
469
474
Writer . WL ( $@ "
470
475
}}
@@ -483,7 +488,7 @@ public void ToString_WithSwedishCulture_ReturnsUnitAbbreviationForEnglishCulture
483
488
foreach ( var unit in _quantity . Units )
484
489
{
485
490
Writer . WL ( $@ "
486
- Assert.Equal(""1 { GetEnglishAbbreviation ( unit ) } "", new { _quantity . Name } (1, { GetUnitFullName ( unit ) } ).ToString(swedishCulture));" ) ;
491
+ Assert.Equal(""1{ GetEnglishAbbreviation ( unit ) } "", new { _quantity . Name } (1, { GetUnitFullName ( unit ) } ).ToString(swedishCulture));" ) ;
487
492
}
488
493
Writer . WL ( $@ "
489
494
}}
@@ -495,10 +500,10 @@ public void ToString_SFormat_FormatsNumberWithGivenDigitsAfterRadixForCurrentCul
495
500
try
496
501
{{
497
502
CultureInfo.CurrentUICulture = CultureInfo.InvariantCulture;
498
- Assert.Equal(""0.1 { _baseUnitEnglishAbbreviation } "", new { _quantity . Name } (0.123456{ _numberSuffix } , { _baseUnitFullName } ).ToString(""s1""));
499
- Assert.Equal(""0.12 { _baseUnitEnglishAbbreviation } "", new { _quantity . Name } (0.123456{ _numberSuffix } , { _baseUnitFullName } ).ToString(""s2""));
500
- Assert.Equal(""0.123 { _baseUnitEnglishAbbreviation } "", new { _quantity . Name } (0.123456{ _numberSuffix } , { _baseUnitFullName } ).ToString(""s3""));
501
- Assert.Equal(""0.1235 { _baseUnitEnglishAbbreviation } "", new { _quantity . Name } (0.123456{ _numberSuffix } , { _baseUnitFullName } ).ToString(""s4""));
503
+ Assert.Equal(""0.1{ _baseUnitEnglishAbbreviation } "", new { _quantity . Name } (0.123456{ _numberSuffix } , { _baseUnitFullName } ).ToString(""s1""));
504
+ Assert.Equal(""0.12{ _baseUnitEnglishAbbreviation } "", new { _quantity . Name } (0.123456{ _numberSuffix } , { _baseUnitFullName } ).ToString(""s2""));
505
+ Assert.Equal(""0.123{ _baseUnitEnglishAbbreviation } "", new { _quantity . Name } (0.123456{ _numberSuffix } , { _baseUnitFullName } ).ToString(""s3""));
506
+ Assert.Equal(""0.1235{ _baseUnitEnglishAbbreviation } "", new { _quantity . Name } (0.123456{ _numberSuffix } , { _baseUnitFullName } ).ToString(""s4""));
502
507
}}
503
508
finally
504
509
{{
@@ -510,10 +515,10 @@ public void ToString_SFormat_FormatsNumberWithGivenDigitsAfterRadixForCurrentCul
510
515
public void ToString_SFormatAndCulture_FormatsNumberWithGivenDigitsAfterRadixForGivenCulture()
511
516
{{
512
517
var culture = CultureInfo.InvariantCulture;
513
- Assert.Equal(""0.1 { _baseUnitEnglishAbbreviation } "", new { _quantity . Name } (0.123456{ _numberSuffix } , { _baseUnitFullName } ).ToString(""s1"", culture));
514
- Assert.Equal(""0.12 { _baseUnitEnglishAbbreviation } "", new { _quantity . Name } (0.123456{ _numberSuffix } , { _baseUnitFullName } ).ToString(""s2"", culture));
515
- Assert.Equal(""0.123 { _baseUnitEnglishAbbreviation } "", new { _quantity . Name } (0.123456{ _numberSuffix } , { _baseUnitFullName } ).ToString(""s3"", culture));
516
- Assert.Equal(""0.1235 { _baseUnitEnglishAbbreviation } "", new { _quantity . Name } (0.123456{ _numberSuffix } , { _baseUnitFullName } ).ToString(""s4"", culture));
518
+ Assert.Equal(""0.1{ _baseUnitEnglishAbbreviation } "", new { _quantity . Name } (0.123456{ _numberSuffix } , { _baseUnitFullName } ).ToString(""s1"", culture));
519
+ Assert.Equal(""0.12{ _baseUnitEnglishAbbreviation } "", new { _quantity . Name } (0.123456{ _numberSuffix } , { _baseUnitFullName } ).ToString(""s2"", culture));
520
+ Assert.Equal(""0.123{ _baseUnitEnglishAbbreviation } "", new { _quantity . Name } (0.123456{ _numberSuffix } , { _baseUnitFullName } ).ToString(""s3"", culture));
521
+ Assert.Equal(""0.1235{ _baseUnitEnglishAbbreviation } "", new { _quantity . Name } (0.123456{ _numberSuffix } , { _baseUnitFullName } ).ToString(""s4"", culture));
517
522
}}
518
523
519
524
0 commit comments