@@ -17,9 +17,40 @@ public class UnitAbbreviationsCacheTests
1717 {
1818 private static readonly CultureInfo AmericanCulture = CultureInfo . GetCultureInfo ( "en-US" ) ;
1919 private static readonly CultureInfo NorwegianCulture = CultureInfo . GetCultureInfo ( "nb-NO" ) ;
20+
21+ [ Fact ]
22+ public void EmptyConstructor_ReturnsAnAbbreviationCacheWithDefaultQuantityInfoLookup ( )
23+ {
24+ var unitAbbreviationCache = new UnitAbbreviationsCache ( ) ;
25+
26+ Assert . Equal ( UnitsNetSetup . Default . QuantityInfoLookup , unitAbbreviationCache . Quantities ) ;
27+ Assert . Equal ( "g" , unitAbbreviationCache . GetUnitAbbreviations ( MassUnit . Gram , AmericanCulture ) [ 0 ] ) ;
28+ Assert . Throws < UnitNotFoundException > ( ( ) => unitAbbreviationCache . GetUnitAbbreviations ( HowMuchUnit . Some ) ) ;
29+ }
2030
2131 [ Fact ]
22- public void UnitAbbreviationsCacheDefaultReturnsUnitsNetSetupDefaultUnitAbbreviations ( )
32+ public void Constructor_WithQuantities_ReturnsAnAbbreviationCacheWithNewQuantityInfoLookup ( )
33+ {
34+ var unitAbbreviationCache = new UnitAbbreviationsCache ( [ Mass . Info , HowMuch . Info ] ) ;
35+
36+ Assert . NotEqual ( UnitsNetSetup . Default . QuantityInfoLookup , unitAbbreviationCache . Quantities ) ;
37+ Assert . Equal ( "g" , unitAbbreviationCache . GetUnitAbbreviations ( MassUnit . Gram , AmericanCulture ) [ 0 ] ) ;
38+ Assert . Empty ( unitAbbreviationCache . GetUnitAbbreviations ( HowMuchUnit . Some , AmericanCulture ) ) ;
39+ Assert . Throws < UnitNotFoundException > ( ( ) => unitAbbreviationCache . GetUnitAbbreviations ( LengthUnit . Meter ) ) ;
40+ }
41+
42+ [ Fact ]
43+ public void CreateDefault_ReturnsAnAbbreviationCacheWithDefaultQuantityInfoLookup ( )
44+ {
45+ var unitAbbreviationCache = UnitAbbreviationsCache . CreateDefault ( ) ;
46+
47+ Assert . Equal ( UnitsNetSetup . Default . QuantityInfoLookup , unitAbbreviationCache . Quantities ) ;
48+ Assert . Equal ( "g" , unitAbbreviationCache . GetUnitAbbreviations ( MassUnit . Gram , AmericanCulture ) [ 0 ] ) ;
49+ Assert . Throws < UnitNotFoundException > ( ( ) => unitAbbreviationCache . GetUnitAbbreviations ( HowMuchUnit . Some ) ) ;
50+ }
51+
52+ [ Fact ]
53+ public void UnitAbbreviationsCache_Default_ReturnsInstanceFromUnitsNetSetup ( )
2354 {
2455 Assert . Equal ( UnitsNetSetup . Default . UnitAbbreviations , UnitAbbreviationsCache . Default ) ;
2556 }
@@ -34,7 +65,7 @@ public void GetUnitAbbreviationsThrowsUnitNotFoundExceptionIfNoneExist()
3465 }
3566
3667 [ Fact ]
37- public void GetUnitAbbreviationsReturnsTheExpectedAbbreviationWhenConstructedWithTheSpecificQuantityInfo ( )
68+ public void GetUnitAbbreviationReturnsTheExpectedAbbreviationWhenConstructedWithTheSpecificQuantityInfo ( )
3869 {
3970 Assert . Multiple ( checks :
4071 [
@@ -43,6 +74,13 @@ public void GetUnitAbbreviationsReturnsTheExpectedAbbreviationWhenConstructedWit
4374 ] ) ;
4475 }
4576
77+ [ Fact ]
78+ public void GetUnitAbbreviationsReturnsTheExpectedAbbreviationWhenConstructedWithTheSpecificQuantityInfo ( )
79+ {
80+ var unitAbbreviationCache = new UnitAbbreviationsCache ( [ Mass . Info ] ) ;
81+ Assert . Equal ( "g" , unitAbbreviationCache . GetUnitAbbreviations ( MassUnit . Gram , AmericanCulture ) [ 0 ] ) ;
82+ }
83+
4684 [ Fact ]
4785 public void GetDefaultAbbreviationReturnsTheExpectedAbbreviationWhenConstructedWithTheSpecificQuantityInfo ( )
4886 {
@@ -71,6 +109,24 @@ public void GetDefaultAbbreviationFallsBackToUsEnglishCulture()
71109 Assert . Equal ( "US english abbreviation for Unit1" , abbreviation ) ;
72110 }
73111
112+ [ Fact ]
113+ public void GetDefaultAbbreviationFallsBackToInvariantCulture ( )
114+ {
115+ // CurrentCulture affects number formatting, such as comma or dot as decimal separator.
116+ // CurrentCulture also affects localization of unit abbreviations.
117+ // Zulu (South Africa)
118+ var zuluCulture = CultureInfo . GetCultureInfo ( "zu-ZA" ) ;
119+
120+ var abbreviationsCache = new UnitAbbreviationsCache ( [ HowMuch . Info ] ) ;
121+ abbreviationsCache . MapUnitToAbbreviation ( HowMuchUnit . AShitTon , CultureInfo . InvariantCulture , "Invariant abbreviation for Unit1" ) ;
122+
123+ // Act
124+ string abbreviation = abbreviationsCache . GetDefaultAbbreviation ( HowMuchUnit . AShitTon , zuluCulture ) ;
125+
126+ // Assert
127+ Assert . Equal ( "Invariant abbreviation for Unit1" , abbreviation ) ;
128+ }
129+
74130 [ Fact ]
75131 public void GetDefaultAbbreviationThrowsUnitNotFoundExceptionIfNoneExist ( )
76132 {
@@ -87,9 +143,22 @@ public void GetDefaultAbbreviation_ForUnitWithoutAbbreviations_ThrowsInvalidOper
87143 }
88144
89145 [ Fact ]
90- public void GetAbbreviationsThrowsArgumentNullExceptionWhenGivenANullUnitInfo ( )
146+ public void GetAllUnitAbbreviationsForQuantity_WithQuantityWithoutAbbreviations_ReturnsEmpty ( )
147+ {
148+ var unitAbbreviationsCache = new UnitAbbreviationsCache ( [ HowMuch . Info ] ) ;
149+ Assert . Empty ( unitAbbreviationsCache . GetAllUnitAbbreviationsForQuantity ( typeof ( HowMuchUnit ) ) ) ;
150+ }
151+
152+ [ Fact ]
153+ public void GetAllUnitAbbreviationsForQuantity_WithInvalidUnitType_ThrowsArgumentException ( )
154+ {
155+ Assert . Throws < ArgumentException > ( ( ) => UnitAbbreviationsCache . Default . GetAllUnitAbbreviationsForQuantity ( typeof ( DateTime ) ) ) ;
156+ }
157+
158+ [ Fact ]
159+ public void GetAllUnitAbbreviationsForQuantity_WithUnknownUnitType_ThrowsUnitNotFoundException ( )
91160 {
92- Assert . Throws < ArgumentNullException > ( ( ) => new UnitAbbreviationsCache ( ) . GetAbbreviations ( null ! ) ) ;
161+ Assert . Throws < UnitNotFoundException > ( ( ) => UnitAbbreviationsCache . Default . GetAllUnitAbbreviationsForQuantity ( typeof ( HowMuchUnit ) ) ) ;
93162 }
94163
95164 [ Fact ]
@@ -119,15 +188,6 @@ public void MapUnitToAbbreviation_AddCustomUnit_DoesNotOverrideDefaultAbbreviati
119188 Assert . Equal ( "m²" , cache . GetDefaultAbbreviation ( AreaUnit . SquareMeter , AmericanCulture ) ) ;
120189 }
121190
122- [ Fact ]
123- public void MapUnitToDefaultAbbreviation_GivenUnitAndCulture_SetsDefaultAbbreviationForUnitAndCulture ( )
124- {
125- var cache = new UnitAbbreviationsCache ( ) ;
126- cache . MapUnitToDefaultAbbreviation ( AreaUnit . SquareMeter , AmericanCulture , "m^2" ) ;
127-
128- Assert . Equal ( "m^2" , cache . GetDefaultAbbreviation ( AreaUnit . SquareMeter , AmericanCulture ) ) ;
129- }
130-
131191 [ Fact ]
132192 public void MapUnitToDefaultAbbreviation_GivenUnitAndNoCulture_SetsDefaultAbbreviationForUnitForCurrentCulture ( )
133193 {
@@ -141,15 +201,21 @@ public void MapUnitToDefaultAbbreviation_GivenUnitAndNoCulture_SetsDefaultAbbrev
141201 }
142202
143203 [ Fact ]
144- public void MapUnitToDefaultAbbreviation_GivenUnitTypeAndValue_SetsDefaultAbbreviationForUnitForCurrentCulture ( )
204+ public void MapUnitToDefaultAbbreviation_GivenUnitAndCulture_SetsDefaultAbbreviationForUnitAndCulture ( )
145205 {
146- using var cultureScope = new CultureScope ( NorwegianCulture ) ;
147- var cache = new UnitAbbreviationsCache ( [ Mass . Info ] ) ;
206+ var cache = new UnitAbbreviationsCache ( [ Area . Info ] ) ;
207+ cache . MapUnitToDefaultAbbreviation ( AreaUnit . SquareMeter , AmericanCulture , "m^2" ) ;
148208
149- cache . MapUnitToDefaultAbbreviation ( typeof ( MassUnit ) , ( int ) MassUnit . Gram , null , "zz" ) ;
209+ Assert . Equal ( "m^2" , cache . GetDefaultAbbreviation ( AreaUnit . SquareMeter , AmericanCulture ) ) ;
210+ }
150211
151- Assert . Equal ( "zz" , cache . GetDefaultAbbreviation ( MassUnit . Gram ) ) ;
152- Assert . Equal ( "g" , cache . GetDefaultAbbreviation ( MassUnit . Gram , AmericanCulture ) ) ;
212+ [ Fact ]
213+ public void MapUnitToDefaultAbbreviation_GivenUnitTypeValueAndCulture_SetsDefaultAbbreviationForUnitAndCulture ( )
214+ {
215+ var cache = new UnitAbbreviationsCache ( [ Area . Info ] ) ;
216+ cache . MapUnitToDefaultAbbreviation ( typeof ( AreaUnit ) , ( int ) AreaUnit . SquareMeter , AmericanCulture , "m^2" ) ;
217+
218+ Assert . Equal ( "m^2" , cache . GetDefaultAbbreviation ( AreaUnit . SquareMeter , AmericanCulture ) ) ;
153219 }
154220
155221 [ Fact ]
@@ -214,5 +280,40 @@ public void MapAndLookup_WithSpecificEnumType()
214280 unitAbbreviationsCache . MapUnitToDefaultAbbreviation ( HowMuchUnit . Some , "sm" ) ;
215281 Assert . Equal ( "sm" , unitAbbreviationsCache . GetDefaultAbbreviation ( HowMuchUnit . Some ) ) ;
216282 }
283+
284+ /// <inheritdoc cref="MapAndLookup_WithSpecificEnumType"/>
285+ [ Fact ]
286+ public void MapAndLookup_WithEnumType ( )
287+ {
288+ Enum valueAsEnumType = HowMuchUnit . Some ;
289+ var unitAbbreviationsCache = new UnitAbbreviationsCache ( [ HowMuch . Info ] ) ;
290+ unitAbbreviationsCache . MapUnitToDefaultAbbreviation ( valueAsEnumType , "sm" ) ;
291+ Assert . Equal ( "sm" , unitAbbreviationsCache . GetDefaultAbbreviation ( valueAsEnumType ) ) ;
292+ }
293+
294+ /// <inheritdoc cref="MapAndLookup_WithSpecificEnumType"/>
295+ [ Fact ]
296+ public void MapAndLookup_MapWithSpecificEnumType_LookupWithEnumType ( )
297+ {
298+ var unitAbbreviationsCache = new UnitAbbreviationsCache ( [ HowMuch . Info ] ) ;
299+ unitAbbreviationsCache . MapUnitToDefaultAbbreviation ( HowMuchUnit . Some , "sm" ) ;
300+ Assert . Equal ( "sm" , unitAbbreviationsCache . GetDefaultAbbreviation ( ( Enum ) HowMuchUnit . Some ) ) ;
301+ }
302+
303+ [ Fact ]
304+ public void MapUnitToAbbreviation_WithUnknownUnit_ThrowsUnitNotFoundException ( )
305+ {
306+ var unitAbbreviationCache = new UnitAbbreviationsCache ( [ Mass . Info ] ) ;
307+ Assert . Throws < UnitNotFoundException > ( ( ) => unitAbbreviationCache . MapUnitToAbbreviation ( HowMuchUnit . Some , "nothing" ) ) ;
308+ Assert . Throws < UnitNotFoundException > ( ( ) => unitAbbreviationCache . MapUnitToAbbreviation ( LengthUnit . Centimeter , "nothing" ) ) ;
309+ }
310+
311+ [ Fact ]
312+ public void MapUnitToDefaultAbbreviation_WithUnknownUnit_ThrowsUnitNotFoundException ( )
313+ {
314+ var unitAbbreviationCache = new UnitAbbreviationsCache ( [ Mass . Info ] ) ;
315+ Assert . Throws < UnitNotFoundException > ( ( ) => unitAbbreviationCache . MapUnitToDefaultAbbreviation ( HowMuchUnit . Some , "nothing" ) ) ;
316+ Assert . Throws < UnitNotFoundException > ( ( ) => unitAbbreviationCache . MapUnitToDefaultAbbreviation ( LengthUnit . AstronomicalUnit , "nothing" ) ) ;
317+ }
217318 }
218319}
0 commit comments