@@ -38,7 +38,7 @@ internal class UnitTestBaseClassGenerator : GeneratorBase
38
38
private readonly Dictionary < string , Unit > _unitSystemUnits = new Dictionary < string , Unit > ( ) ;
39
39
40
40
/// <summary>
41
- /// Example: "m" for Length quantity.
41
+ /// Example: " m" for Length quantity with leading whitespace or "" for Ratio quantity where base unit does not have an abbreviation .
42
42
/// </summary>
43
43
private readonly string _baseUnitEnglishAbbreviation ;
44
44
@@ -76,8 +76,13 @@ public UnitTestBaseClassGenerator(Quantity quantity)
76
76
77
77
/// <summary>
78
78
/// Gets the first en-US abbreviation for the unit -or- empty string if none is defined.
79
+ /// If a unit abbreviation exists, a leading whitespace is added to separate the number and the abbreviation like "1 m".
79
80
/// </summary>
80
- private static string GetEnglishAbbreviation ( Unit unit ) => unit . Localization . First ( l => l . Culture == "en-US" ) . Abbreviations . FirstOrDefault ( ) ?? "" ;
81
+ private static string GetEnglishAbbreviation ( Unit unit )
82
+ {
83
+ var unitAbbreviation = unit . Localization . First ( l => l . Culture == "en-US" ) . Abbreviations . FirstOrDefault ( ) ;
84
+ return string . IsNullOrEmpty ( unitAbbreviation ) ? "" : $ " { unitAbbreviation } ";
85
+ }
81
86
82
87
public override string Generate ( )
83
88
{
@@ -522,7 +527,7 @@ public void ToString_ReturnsValueAndUnitAbbreviationInCurrentCulture()
522
527
foreach ( var unit in _quantity . Units )
523
528
{
524
529
Writer . WL ( $@ "
525
- Assert.Equal(""1 { GetEnglishAbbreviation ( unit ) } "", new { _quantity . Name } (1, { GetUnitFullName ( unit ) } ).ToString());" ) ;
530
+ Assert.Equal(""1{ GetEnglishAbbreviation ( unit ) } "", new { _quantity . Name } (1, { GetUnitFullName ( unit ) } ).ToString());" ) ;
526
531
}
527
532
Writer . WL ( $@ "
528
533
}}
@@ -541,7 +546,7 @@ public void ToString_WithSwedishCulture_ReturnsUnitAbbreviationForEnglishCulture
541
546
foreach ( var unit in _quantity . Units )
542
547
{
543
548
Writer . WL ( $@ "
544
- Assert.Equal(""1 { GetEnglishAbbreviation ( unit ) } "", new { _quantity . Name } (1, { GetUnitFullName ( unit ) } ).ToString(swedishCulture));" ) ;
549
+ Assert.Equal(""1{ GetEnglishAbbreviation ( unit ) } "", new { _quantity . Name } (1, { GetUnitFullName ( unit ) } ).ToString(swedishCulture));" ) ;
545
550
}
546
551
Writer . WL ( $@ "
547
552
}}
@@ -553,10 +558,10 @@ public void ToString_SFormat_FormatsNumberWithGivenDigitsAfterRadixForCurrentCul
553
558
try
554
559
{{
555
560
CultureInfo.CurrentUICulture = CultureInfo.InvariantCulture;
556
- Assert.Equal(""0.1 { _baseUnitEnglishAbbreviation } "", new { _quantity . Name } (0.123456{ _numberSuffix } , { _baseUnitFullName } ).ToString(""s1""));
557
- Assert.Equal(""0.12 { _baseUnitEnglishAbbreviation } "", new { _quantity . Name } (0.123456{ _numberSuffix } , { _baseUnitFullName } ).ToString(""s2""));
558
- Assert.Equal(""0.123 { _baseUnitEnglishAbbreviation } "", new { _quantity . Name } (0.123456{ _numberSuffix } , { _baseUnitFullName } ).ToString(""s3""));
559
- Assert.Equal(""0.1235 { _baseUnitEnglishAbbreviation } "", new { _quantity . Name } (0.123456{ _numberSuffix } , { _baseUnitFullName } ).ToString(""s4""));
561
+ Assert.Equal(""0.1{ _baseUnitEnglishAbbreviation } "", new { _quantity . Name } (0.123456{ _numberSuffix } , { _baseUnitFullName } ).ToString(""s1""));
562
+ Assert.Equal(""0.12{ _baseUnitEnglishAbbreviation } "", new { _quantity . Name } (0.123456{ _numberSuffix } , { _baseUnitFullName } ).ToString(""s2""));
563
+ Assert.Equal(""0.123{ _baseUnitEnglishAbbreviation } "", new { _quantity . Name } (0.123456{ _numberSuffix } , { _baseUnitFullName } ).ToString(""s3""));
564
+ Assert.Equal(""0.1235{ _baseUnitEnglishAbbreviation } "", new { _quantity . Name } (0.123456{ _numberSuffix } , { _baseUnitFullName } ).ToString(""s4""));
560
565
}}
561
566
finally
562
567
{{
@@ -568,10 +573,10 @@ public void ToString_SFormat_FormatsNumberWithGivenDigitsAfterRadixForCurrentCul
568
573
public void ToString_SFormatAndCulture_FormatsNumberWithGivenDigitsAfterRadixForGivenCulture()
569
574
{{
570
575
var culture = CultureInfo.InvariantCulture;
571
- Assert.Equal(""0.1 { _baseUnitEnglishAbbreviation } "", new { _quantity . Name } (0.123456{ _numberSuffix } , { _baseUnitFullName } ).ToString(""s1"", culture));
572
- Assert.Equal(""0.12 { _baseUnitEnglishAbbreviation } "", new { _quantity . Name } (0.123456{ _numberSuffix } , { _baseUnitFullName } ).ToString(""s2"", culture));
573
- Assert.Equal(""0.123 { _baseUnitEnglishAbbreviation } "", new { _quantity . Name } (0.123456{ _numberSuffix } , { _baseUnitFullName } ).ToString(""s3"", culture));
574
- Assert.Equal(""0.1235 { _baseUnitEnglishAbbreviation } "", new { _quantity . Name } (0.123456{ _numberSuffix } , { _baseUnitFullName } ).ToString(""s4"", culture));
576
+ Assert.Equal(""0.1{ _baseUnitEnglishAbbreviation } "", new { _quantity . Name } (0.123456{ _numberSuffix } , { _baseUnitFullName } ).ToString(""s1"", culture));
577
+ Assert.Equal(""0.12{ _baseUnitEnglishAbbreviation } "", new { _quantity . Name } (0.123456{ _numberSuffix } , { _baseUnitFullName } ).ToString(""s2"", culture));
578
+ Assert.Equal(""0.123{ _baseUnitEnglishAbbreviation } "", new { _quantity . Name } (0.123456{ _numberSuffix } , { _baseUnitFullName } ).ToString(""s3"", culture));
579
+ Assert.Equal(""0.1235{ _baseUnitEnglishAbbreviation } "", new { _quantity . Name } (0.123456{ _numberSuffix } , { _baseUnitFullName } ).ToString(""s4"", culture));
575
580
}}
576
581
577
582
0 commit comments