@@ -31,7 +31,7 @@ internal class UnitTestBaseClassGenerator : GeneratorBase
3131 private readonly string _unitEnumName ;
3232
3333 /// <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 .
3535 /// </summary>
3636 private readonly string _baseUnitEnglishAbbreviation ;
3737
@@ -61,8 +61,13 @@ public UnitTestBaseClassGenerator(Quantity quantity)
6161
6262 /// <summary>
6363 /// 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".
6465 /// </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+ }
6671
6772 public override string Generate ( )
6873 {
@@ -464,7 +469,7 @@ public void ToString_ReturnsValueAndUnitAbbreviationInCurrentCulture()
464469 foreach ( var unit in _quantity . Units )
465470 {
466471 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());" ) ;
468473 }
469474 Writer . WL ( $@ "
470475 }}
@@ -483,7 +488,7 @@ public void ToString_WithSwedishCulture_ReturnsUnitAbbreviationForEnglishCulture
483488 foreach ( var unit in _quantity . Units )
484489 {
485490 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));" ) ;
487492 }
488493 Writer . WL ( $@ "
489494 }}
@@ -495,10 +500,10 @@ public void ToString_SFormat_FormatsNumberWithGivenDigitsAfterRadixForCurrentCul
495500 try
496501 {{
497502 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""));
502507 }}
503508 finally
504509 {{
@@ -510,10 +515,10 @@ public void ToString_SFormat_FormatsNumberWithGivenDigitsAfterRadixForCurrentCul
510515 public void ToString_SFormatAndCulture_FormatsNumberWithGivenDigitsAfterRadixForGivenCulture()
511516 {{
512517 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));
517522 }}
518523
519524
0 commit comments