@@ -17,9 +17,40 @@ public class UnitAbbreviationsCacheTests
17
17
{
18
18
private static readonly CultureInfo AmericanCulture = CultureInfo . GetCultureInfo ( "en-US" ) ;
19
19
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
+ }
20
30
21
31
[ 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 ( )
23
54
{
24
55
Assert . Equal ( UnitsNetSetup . Default . UnitAbbreviations , UnitAbbreviationsCache . Default ) ;
25
56
}
@@ -34,7 +65,7 @@ public void GetUnitAbbreviationsThrowsUnitNotFoundExceptionIfNoneExist()
34
65
}
35
66
36
67
[ Fact ]
37
- public void GetUnitAbbreviationsReturnsTheExpectedAbbreviationWhenConstructedWithTheSpecificQuantityInfo ( )
68
+ public void GetUnitAbbreviationReturnsTheExpectedAbbreviationWhenConstructedWithTheSpecificQuantityInfo ( )
38
69
{
39
70
Assert . Multiple ( checks :
40
71
[
@@ -43,6 +74,13 @@ public void GetUnitAbbreviationsReturnsTheExpectedAbbreviationWhenConstructedWit
43
74
] ) ;
44
75
}
45
76
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
+
46
84
[ Fact ]
47
85
public void GetDefaultAbbreviationReturnsTheExpectedAbbreviationWhenConstructedWithTheSpecificQuantityInfo ( )
48
86
{
@@ -71,6 +109,24 @@ public void GetDefaultAbbreviationFallsBackToUsEnglishCulture()
71
109
Assert . Equal ( "US english abbreviation for Unit1" , abbreviation ) ;
72
110
}
73
111
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
+
74
130
[ Fact ]
75
131
public void GetDefaultAbbreviationThrowsUnitNotFoundExceptionIfNoneExist ( )
76
132
{
@@ -87,9 +143,22 @@ public void GetDefaultAbbreviation_ForUnitWithoutAbbreviations_ThrowsInvalidOper
87
143
}
88
144
89
145
[ 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 ( )
91
160
{
92
- Assert . Throws < ArgumentNullException > ( ( ) => new UnitAbbreviationsCache ( ) . GetAbbreviations ( null ! ) ) ;
161
+ Assert . Throws < UnitNotFoundException > ( ( ) => UnitAbbreviationsCache . Default . GetAllUnitAbbreviationsForQuantity ( typeof ( HowMuchUnit ) ) ) ;
93
162
}
94
163
95
164
[ Fact ]
@@ -119,15 +188,6 @@ public void MapUnitToAbbreviation_AddCustomUnit_DoesNotOverrideDefaultAbbreviati
119
188
Assert . Equal ( "m²" , cache . GetDefaultAbbreviation ( AreaUnit . SquareMeter , AmericanCulture ) ) ;
120
189
}
121
190
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
-
131
191
[ Fact ]
132
192
public void MapUnitToDefaultAbbreviation_GivenUnitAndNoCulture_SetsDefaultAbbreviationForUnitForCurrentCulture ( )
133
193
{
@@ -141,15 +201,21 @@ public void MapUnitToDefaultAbbreviation_GivenUnitAndNoCulture_SetsDefaultAbbrev
141
201
}
142
202
143
203
[ Fact ]
144
- public void MapUnitToDefaultAbbreviation_GivenUnitTypeAndValue_SetsDefaultAbbreviationForUnitForCurrentCulture ( )
204
+ public void MapUnitToDefaultAbbreviation_GivenUnitAndCulture_SetsDefaultAbbreviationForUnitAndCulture ( )
145
205
{
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" ) ;
148
208
149
- cache . MapUnitToDefaultAbbreviation ( typeof ( MassUnit ) , ( int ) MassUnit . Gram , null , "zz" ) ;
209
+ Assert . Equal ( "m^2" , cache . GetDefaultAbbreviation ( AreaUnit . SquareMeter , AmericanCulture ) ) ;
210
+ }
150
211
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 ) ) ;
153
219
}
154
220
155
221
[ Fact ]
@@ -214,5 +280,40 @@ public void MapAndLookup_WithSpecificEnumType()
214
280
unitAbbreviationsCache . MapUnitToDefaultAbbreviation ( HowMuchUnit . Some , "sm" ) ;
215
281
Assert . Equal ( "sm" , unitAbbreviationsCache . GetDefaultAbbreviation ( HowMuchUnit . Some ) ) ;
216
282
}
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
+ }
217
318
}
218
319
}
0 commit comments