-
Notifications
You must be signed in to change notification settings - Fork 412
Expand file tree
/
Copy pathUnitAbbreviationsCacheTests.cs
More file actions
332 lines (275 loc) · 16.1 KB
/
Copy pathUnitAbbreviationsCacheTests.cs
File metadata and controls
332 lines (275 loc) · 16.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// Licensed under MIT No Attribution, see LICENSE file at the root.
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
using System.Globalization;
using System.Linq;
using UnitsNet.Tests.CustomQuantities;
using UnitsNet.Tests.Helpers;
using UnitsNet.Units;
using Xunit;
namespace UnitsNet.Tests
{
// Disable parallelization due to manipulating global state, like UnitsNetSetup.Default.UnitAbbreviations.MapUnitToDefaultAbbreviation().
[Collection(nameof(DisableParallelizationCollectionFixture))]
public class UnitAbbreviationsCacheTests
{
private static readonly CultureInfo AmericanCulture = CultureInfo.GetCultureInfo("en-US");
private static readonly CultureInfo NorwegianCulture = CultureInfo.GetCultureInfo("nb-NO");
[Fact]
public void EmptyConstructor_ReturnsAnAbbreviationCacheWithDefaultQuantityInfoLookup()
{
var unitAbbreviationCache = new UnitAbbreviationsCache();
Assert.Equal(UnitsNetSetup.Default.Quantities, unitAbbreviationCache.Quantities);
Assert.Equal("g", unitAbbreviationCache.GetUnitAbbreviations(MassUnit.Gram, AmericanCulture)[0]);
Assert.Throws<UnitNotFoundException>(() => unitAbbreviationCache.GetUnitAbbreviations(HowMuchUnit.Some));
}
[Fact]
public void Constructor_WithQuantities_ReturnsAnAbbreviationCacheWithNewQuantityInfoLookup()
{
var unitAbbreviationCache = new UnitAbbreviationsCache([Mass.Info, HowMuch.Info]);
Assert.NotEqual(UnitsNetSetup.Default.Quantities, unitAbbreviationCache.Quantities);
Assert.Equal("g", unitAbbreviationCache.GetUnitAbbreviations(MassUnit.Gram, AmericanCulture)[0]);
Assert.Empty(unitAbbreviationCache.GetUnitAbbreviations(HowMuchUnit.Some, AmericanCulture));
Assert.Throws<UnitNotFoundException>(() => unitAbbreviationCache.GetUnitAbbreviations(LengthUnit.Meter));
}
[Fact]
public void CreateDefault_ReturnsAnAbbreviationCacheWithDefaultQuantityInfoLookup()
{
var unitAbbreviationCache = UnitAbbreviationsCache.CreateDefault();
Assert.Equal(UnitsNetSetup.Default.Quantities, unitAbbreviationCache.Quantities);
Assert.Equal("g", unitAbbreviationCache.GetUnitAbbreviations(MassUnit.Gram, AmericanCulture)[0]);
Assert.Throws<UnitNotFoundException>(() => unitAbbreviationCache.GetUnitAbbreviations(HowMuchUnit.Some));
}
[Fact]
public void UnitAbbreviationsCache_Default_ReturnsInstanceFromUnitsNetSetup()
{
Assert.Equal(UnitsNetSetup.Default.UnitAbbreviations, UnitAbbreviationsCache.Default);
}
[Fact]
public void GetUnitAbbreviationsThrowsUnitNotFoundExceptionIfNoneExist()
{
Assert.Multiple(checks: [
() => Assert.Throws<UnitNotFoundException>(() => new UnitAbbreviationsCache().GetUnitAbbreviations(HowMuchUnit.AShitTon)),
() => Assert.Throws<UnitNotFoundException>(() => new UnitAbbreviationsCache().GetUnitAbbreviations(typeof(HowMuchUnit), (int)HowMuchUnit.AShitTon))
]);
}
[Fact]
public void GetUnitAbbreviationReturnsTheExpectedAbbreviationWhenConstructedWithTheSpecificQuantityInfo()
{
Assert.Multiple(checks:
[
() => { Assert.Equal("g", new UnitAbbreviationsCache([Mass.Info]).GetUnitAbbreviations(MassUnit.Gram, AmericanCulture)[0]); },
() => { Assert.Equal("g", new UnitAbbreviationsCache([Mass.Info]).GetUnitAbbreviations(typeof(MassUnit), (int)MassUnit.Gram, AmericanCulture)[0]); }
]);
}
[Fact]
public void GetUnitAbbreviationsReturnsTheExpectedAbbreviationWhenConstructedWithTheSpecificQuantityInfo()
{
var unitAbbreviationCache = new UnitAbbreviationsCache([Mass.Info]);
Assert.Equal("g", unitAbbreviationCache.GetUnitAbbreviations(MassUnit.Gram, AmericanCulture)[0]);
}
[Fact]
public void GetDefaultAbbreviationReturnsTheExpectedAbbreviationWhenConstructedWithTheSpecificQuantityInfo()
{
Assert.Multiple(checks:
[
() => { Assert.Equal("g", new UnitAbbreviationsCache([Mass.Info]).GetDefaultAbbreviation(MassUnit.Gram, AmericanCulture)); },
() => { Assert.Equal("g", new UnitAbbreviationsCache([Mass.Info]).GetDefaultAbbreviation(typeof(MassUnit), (int)MassUnit.Gram, AmericanCulture)); },
() => { Assert.Equal("g", new UnitAbbreviationsCache([Mass.Info]).GetDefaultAbbreviation(Mass.Info[MassUnit.Gram])); }
]);
}
[Fact]
public void GetDefaultAbbreviationFallsBackToUsEnglishCulture()
{
// CurrentCulture affects number formatting, such as comma or dot as decimal separator.
// CurrentCulture also affects localization of unit abbreviations.
// Zulu (South Africa)
var zuluCulture = CultureInfo.GetCultureInfo("zu-ZA");
var abbreviationsCache = new UnitAbbreviationsCache([HowMuch.Info]);
abbreviationsCache.MapUnitToAbbreviation(HowMuchUnit.AShitTon, AmericanCulture, "US english abbreviation for Unit1");
// Act
string abbreviation = abbreviationsCache.GetDefaultAbbreviation(HowMuchUnit.AShitTon, zuluCulture);
// Assert
Assert.Equal("US english abbreviation for Unit1", abbreviation);
}
[Fact]
public void GetDefaultAbbreviationFallsBackToInvariantCulture()
{
// CurrentCulture affects number formatting, such as comma or dot as decimal separator.
// CurrentCulture also affects localization of unit abbreviations.
// Zulu (South Africa)
var zuluCulture = CultureInfo.GetCultureInfo("zu-ZA");
var abbreviationsCache = new UnitAbbreviationsCache([HowMuch.Info]);
abbreviationsCache.MapUnitToAbbreviation(HowMuchUnit.AShitTon, CultureInfo.InvariantCulture, "Invariant abbreviation for Unit1");
// Act
string abbreviation = abbreviationsCache.GetDefaultAbbreviation(HowMuchUnit.AShitTon, zuluCulture);
// Assert
Assert.Equal("Invariant abbreviation for Unit1", abbreviation);
}
[Fact]
public void GetDefaultAbbreviation_WithNullUnitType_ThrowsArgumentNullException()
{
Assert.Throws<ArgumentNullException>(() => UnitAbbreviationsCache.Default.GetDefaultAbbreviation(null!, 1));
}
[Fact]
public void GetDefaultAbbreviationThrowsUnitNotFoundExceptionIfNoneExist()
{
Assert.Multiple(checks: [
() => Assert.Throws<UnitNotFoundException>(() => new UnitAbbreviationsCache().GetDefaultAbbreviation(HowMuchUnit.AShitTon)),
() => Assert.Throws<UnitNotFoundException>(() => new UnitAbbreviationsCache().GetDefaultAbbreviation(typeof(HowMuchUnit), (int)HowMuchUnit.AShitTon))
]);
}
[Fact]
public void GetDefaultAbbreviation_ForUnitWithoutAbbreviations_ThrowsInvalidOperationException()
{
Assert.Throws<InvalidOperationException>(() => new UnitAbbreviationsCache([HowMuch.Info]).GetDefaultAbbreviation(HowMuchUnit.AShitTon));
}
[Fact]
public void GetAllUnitAbbreviationsForQuantity_WithQuantityWithoutAbbreviations_ReturnsEmpty()
{
var unitAbbreviationsCache = new UnitAbbreviationsCache([HowMuch.Info]);
Assert.Empty(unitAbbreviationsCache.GetAllUnitAbbreviationsForQuantity(typeof(HowMuchUnit)));
}
[Fact]
public void GetAllUnitAbbreviationsForQuantity_WithNullUnitType_ThrowsArgumentNullException()
{
Assert.Throws<ArgumentNullException>(() => UnitAbbreviationsCache.Default.GetAllUnitAbbreviationsForQuantity(null!));
}
[Fact]
public void GetAllUnitAbbreviationsForQuantity_WithInvalidUnitType_ThrowsArgumentException()
{
Assert.Throws<ArgumentException>(() => UnitAbbreviationsCache.Default.GetAllUnitAbbreviationsForQuantity(typeof(DateTime)));
}
[Fact]
public void GetAllUnitAbbreviationsForQuantity_WithUnknownUnitType_ThrowsUnitNotFoundException()
{
Assert.Throws<UnitNotFoundException>(() => UnitAbbreviationsCache.Default.GetAllUnitAbbreviationsForQuantity(typeof(HowMuchUnit)));
}
[Fact]
public void MapUnitToAbbreviation_DoesNotAffectOtherCacheInstances()
{
var culture = AmericanCulture;
var unit = AreaUnit.SquareMeter;
var cache1 = UnitAbbreviationsCache.CreateDefault();
cache1.MapUnitToAbbreviation(unit, culture, "m^2");
var cache2 = UnitAbbreviationsCache.CreateDefault();
cache2.MapUnitToAbbreviation(unit, culture, "m2");
Assert.Equal(new[] { "m²", "m^2" }, cache1.GetUnitAbbreviations(unit, culture));
Assert.Equal(new[] { "m²", "m2" }, cache2.GetUnitAbbreviations(unit, culture));
Assert.Equal("m²", cache1.GetDefaultAbbreviation(unit, culture));
Assert.Equal("m²", cache2.GetDefaultAbbreviation(unit, culture));
}
[Fact]
public void MapUnitToAbbreviation_AddCustomUnit_DoesNotOverrideDefaultAbbreviationForAlreadyMappedUnits()
{
var cache = UnitAbbreviationsCache.CreateDefault();
cache.MapUnitToAbbreviation(AreaUnit.SquareMeter, AmericanCulture, "m^2");
Assert.Equal("m²", cache.GetDefaultAbbreviation(AreaUnit.SquareMeter, AmericanCulture));
}
[Fact]
public void MapUnitToDefaultAbbreviation_GivenUnitAndNoCulture_SetsDefaultAbbreviationForUnitForCurrentCulture()
{
using var cultureScope = new CultureScope(NorwegianCulture);
var cache = new UnitAbbreviationsCache([Mass.Info]);
cache.MapUnitToDefaultAbbreviation(MassUnit.Gram, "zz");
Assert.Equal("zz", cache.GetDefaultAbbreviation(MassUnit.Gram));
Assert.Equal("g", cache.GetDefaultAbbreviation(MassUnit.Gram, AmericanCulture));
}
[Fact]
public void MapUnitToDefaultAbbreviation_GivenUnitAndCulture_SetsDefaultAbbreviationForUnitAndCulture()
{
var cache = new UnitAbbreviationsCache([Area.Info]);
cache.MapUnitToDefaultAbbreviation(AreaUnit.SquareMeter, AmericanCulture, "m^2");
Assert.Equal("m^2", cache.GetDefaultAbbreviation(AreaUnit.SquareMeter, AmericanCulture));
}
[Fact]
public void MapUnitToDefaultAbbreviation_GivenUnitTypeValueAndCulture_SetsDefaultAbbreviationForUnitAndCulture()
{
var cache = new UnitAbbreviationsCache([Area.Info]);
cache.MapUnitToDefaultAbbreviation(typeof(AreaUnit), (int)AreaUnit.SquareMeter, AmericanCulture, "m^2");
Assert.Equal("m^2", cache.GetDefaultAbbreviation(AreaUnit.SquareMeter, AmericanCulture));
}
[Fact]
public void MapUnitToDefaultAbbreviation_GivenCustomAbbreviation_SetsAbbreviationUsedByQuantityToString()
{
// Use a distinct culture here so that we don't mess up other tests that may rely on the default cache.
var newZealandCulture = CultureInfo.GetCultureInfo("en-NZ");
UnitsNetSetup.Default.UnitAbbreviations.MapUnitToDefaultAbbreviation(AreaUnit.SquareMeter, newZealandCulture, "m^2");
Assert.Equal("1 m^2", Area.FromSquareMeters(1).ToString(newZealandCulture));
}
[Fact]
public void MapUnitToAbbreviation_GivenUnitTypeAndValue_AddsTheAbbreviationForUnitForCurrentCulture()
{
using var cultureScope = new CultureScope(NorwegianCulture);
var cache = new UnitAbbreviationsCache([Mass.Info]);
cache.MapUnitToAbbreviation(typeof(MassUnit), (int)MassUnit.Gram, null, "zz");
Assert.Equal("zz", cache.GetUnitAbbreviations(MassUnit.Gram).Last());
Assert.Equal("g", cache.GetDefaultAbbreviation(MassUnit.Gram, AmericanCulture));
Assert.DoesNotContain("zz", cache.GetUnitAbbreviations(MassUnit.Gram, AmericanCulture));
}
[Fact]
public void MapUnitToAbbreviation_DoesNotInsertDuplicates()
{
var cache = new UnitAbbreviationsCache([HowMuch.Info]);
cache.MapUnitToAbbreviation(HowMuchUnit.Some, "sm");
cache.MapUnitToAbbreviation(HowMuchUnit.Some, "sm");
cache.MapUnitToAbbreviation(HowMuchUnit.Some, "sm");
Assert.Equal("sm", cache.GetDefaultAbbreviation(HowMuchUnit.Some));
Assert.Equal(new[] { "sm" }, cache.GetUnitAbbreviations(HowMuchUnit.Some));
Assert.Equal(new[] { "sm" }, cache.GetAllUnitAbbreviationsForQuantity(typeof(HowMuchUnit)));
}
[Fact]
public void MapUnitToDefaultAbbreviation_Twice_SetsNewDefaultAndKeepsOrderOfExistingAbbreviations()
{
var cache = new UnitAbbreviationsCache([HowMuch.Info]);
cache.MapUnitToAbbreviation(HowMuchUnit.Some, "sm");
cache.MapUnitToDefaultAbbreviation(HowMuchUnit.Some, "1st default");
cache.MapUnitToDefaultAbbreviation(HowMuchUnit.Some, "2nd default");
Assert.Equal("2nd default", cache.GetDefaultAbbreviation(HowMuchUnit.Some));
Assert.Equal(new[] { "2nd default", "1st default", "sm" }, cache.GetUnitAbbreviations(HowMuchUnit.Some));
}
/// <summary>
/// Test that lookup works when specifying unit enum value both as cast to <see cref="Enum"/> and as specific enum value type.
/// We have had subtle bugs when <see cref="Enum"/> is passed to generic methods with constraint TUnitEnum : Enum,
/// which the Enum type satisfies, but trying to use typeof(TUnitEnum) no longer represent the actual enum type so unitEnumValue.GetType() should be used.
/// </summary>
[Fact]
public void MapAndLookup_WithSpecificEnumType()
{
var unitAbbreviationsCache = new UnitAbbreviationsCache([HowMuch.Info]);
unitAbbreviationsCache.MapUnitToDefaultAbbreviation(HowMuchUnit.Some, "sm");
Assert.Equal("sm", unitAbbreviationsCache.GetDefaultAbbreviation(HowMuchUnit.Some));
}
/// <inheritdoc cref="MapAndLookup_WithSpecificEnumType"/>
[Fact]
public void MapAndLookup_WithEnumType()
{
Enum valueAsEnumType = HowMuchUnit.Some;
var unitAbbreviationsCache = new UnitAbbreviationsCache([HowMuch.Info]);
unitAbbreviationsCache.MapUnitToDefaultAbbreviation(valueAsEnumType, "sm");
Assert.Equal("sm", unitAbbreviationsCache.GetDefaultAbbreviation(valueAsEnumType));
}
/// <inheritdoc cref="MapAndLookup_WithSpecificEnumType"/>
[Fact]
public void MapAndLookup_MapWithSpecificEnumType_LookupWithEnumType()
{
var unitAbbreviationsCache = new UnitAbbreviationsCache([HowMuch.Info]);
unitAbbreviationsCache.MapUnitToDefaultAbbreviation(HowMuchUnit.Some, "sm");
Assert.Equal("sm", unitAbbreviationsCache.GetDefaultAbbreviation((Enum)HowMuchUnit.Some));
}
[Fact]
public void MapUnitToAbbreviation_WithUnknownUnit_ThrowsUnitNotFoundException()
{
var unitAbbreviationCache = new UnitAbbreviationsCache([Mass.Info]);
Assert.Throws<UnitNotFoundException>(() => unitAbbreviationCache.MapUnitToAbbreviation(HowMuchUnit.Some, "nothing"));
Assert.Throws<UnitNotFoundException>(() => unitAbbreviationCache.MapUnitToAbbreviation(LengthUnit.Centimeter, "nothing"));
}
[Fact]
public void MapUnitToDefaultAbbreviation_WithUnknownUnit_ThrowsUnitNotFoundException()
{
var unitAbbreviationCache = new UnitAbbreviationsCache([Mass.Info]);
Assert.Throws<UnitNotFoundException>(() => unitAbbreviationCache.MapUnitToDefaultAbbreviation(HowMuchUnit.Some, "nothing"));
Assert.Throws<UnitNotFoundException>(() => unitAbbreviationCache.MapUnitToDefaultAbbreviation(LengthUnit.AstronomicalUnit, "nothing"));
}
}
}