-
Notifications
You must be signed in to change notification settings - Fork 407
Expand file tree
/
Copy pathUnitAbbreviationsCacheTests.cs
More file actions
329 lines (290 loc) · 15.8 KB
/
UnitAbbreviationsCacheTests.cs
File metadata and controls
329 lines (290 loc) · 15.8 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
// 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.Collections.Generic;
using System.Globalization;
using System.Linq;
using UnitsNet.Units;
using Xunit;
using Xunit.Abstractions;
namespace UnitsNet.Tests
{
[Collection(nameof(UnitAbbreviationsCacheFixture))]
public class UnitAbbreviationsCacheTests
{
private readonly ITestOutputHelper _output;
private const string AmericanCultureName = "en-US";
private const string RussianCultureName = "ru-RU";
private const string NorwegianCultureName = "nb-NO";
private static readonly IFormatProvider AmericanCulture = new CultureInfo(AmericanCultureName);
private static readonly IFormatProvider NorwegianCulture = new CultureInfo(NorwegianCultureName);
private static readonly IFormatProvider RussianCulture = new CultureInfo(RussianCultureName);
public UnitAbbreviationsCacheTests(ITestOutputHelper output)
{
_output = output;
}
// The default, parameterless ToString() method uses 2 sigifnificant digits after the radix point.
[Theory]
[InlineData(0, "0 m")]
[InlineData(0.1, "0.1 m")]
[InlineData(0.11, "0.11 m")]
[InlineData(0.111234, "0.11 m")]
[InlineData(0.115, "0.12 m")]
public void DefaultToStringFormatting(double value, string expected)
{
string actual = Length.FromMeters(value).ToUnit(LengthUnit.Meter).ToString(AmericanCulture);
Assert.Equal(expected, actual);
}
private enum CustomUnit
{
// ReSharper disable UnusedMember.Local
Undefined = 0,
Unit1,
Unit2
// ReSharper restore UnusedMember.Local
}
// These cultures all use a comma for the radix point
[Theory]
[InlineData("de-DE")]
[InlineData("da-DK")]
[InlineData("es-AR")]
[InlineData("es-ES")]
[InlineData("it-IT")]
public void CommaRadixPointCultureFormatting(string culture)
{
Assert.Equal("0,12 m", Length.FromMeters(0.12).ToUnit(LengthUnit.Meter).ToString(GetCulture(culture)));
}
// These cultures all use a decimal point for the radix point
[Theory]
[InlineData("en-CA")]
[InlineData("en-US")]
[InlineData("ar-EG")]
[InlineData("en-GB")]
[InlineData("es-MX")]
public void DecimalRadixPointCultureFormatting(string culture)
{
Assert.Equal("0.12 m", Length.FromMeters(0.12).ToUnit(LengthUnit.Meter).ToString(GetCulture(culture)));
}
// These cultures all use a comma in digit grouping
[Theory]
[InlineData("en-CA")]
[InlineData("en-US")]
[InlineData("ar-EG")]
[InlineData("en-GB")]
[InlineData("es-MX")]
public void CommaDigitGroupingCultureFormatting(string cultureName)
{
CultureInfo culture = GetCulture(cultureName);
Assert.Equal("1,111 m", Length.FromMeters(1111).ToUnit(LengthUnit.Meter).ToString(culture));
// Feet/Inch and Stone/Pound combinations are only used (customarily) in the US, UK and maybe Ireland - all English speaking countries.
// FeetInches returns a whole number of feet, with the remainder expressed (rounded) in inches. Same for SonePounds.
Assert.Equal("2,222 ft 3 in",
Length.FromFeetInches(2222, 3).FeetInches.ToString(culture));
Assert.Equal("3,333 st 7 lb",
Mass.FromStonePounds(3333, 7).StonePounds.ToString(culture));
}
// These cultures use a thin space in digit grouping
[Theory]
[InlineData("nn-NO")]
[InlineData("fr-FR")]
public void SpaceDigitGroupingCultureFormatting(string culture)
{
// Note: the space used in digit groupings is actually a "thin space" Unicode character U+2009
Assert.Equal("1 111 m", Length.FromMeters(1111).ToUnit(LengthUnit.Meter).ToString(GetCulture(culture)));
}
// These cultures all use a decimal point in digit grouping
[Theory]
[InlineData("de-DE")]
[InlineData("da-DK")]
[InlineData("es-AR")]
[InlineData("es-ES")]
[InlineData("it-IT")]
public void DecimalPointDigitGroupingCultureFormatting(string culture)
{
Assert.Equal("1.111 m", Length.FromMeters(1111).ToUnit(LengthUnit.Meter).ToString(GetCulture(culture)));
}
// Due to rounding, the values will result in the same string representation regardless of the number of significant digits (up to a certain point)
[Theory]
[InlineData(0.819999999999, "s2", "0.82 m")]
[InlineData(0.819999999999, "s4", "0.82 m")]
[InlineData(0.00299999999, "s2", "0.003 m")]
[InlineData(0.00299999999, "s4", "0.003 m")]
[InlineData(0.0003000001, "s2", "3e-04 m")]
[InlineData(0.0003000001, "s4", "3e-04 m")]
public void RoundingErrorsWithSignificantDigitsAfterRadixFormatting(double value,
string significantDigitsAfterRadixFormatString, string expected)
{
string actual = Length.FromMeters(value).ToUnit(LengthUnit.Meter).ToString(significantDigitsAfterRadixFormatString, AmericanCulture);
Assert.Equal(expected, actual);
}
// Any value in the interval (-inf ≤ x < 1e-03] is formatted in scientific notation
[Theory]
[InlineData(double.MinValue, "-1.8e+308 m")]
[InlineData(1.23e-120, "1.23e-120 m")]
[InlineData(0.0000111, "1.11e-05 m")]
[InlineData(1.99e-4, "1.99e-04 m")]
public void ScientificNotationLowerInterval(double value, string expected)
{
string actual = Length.FromMeters(value).ToUnit(LengthUnit.Meter).ToString(AmericanCulture);
Assert.Equal(expected, actual);
}
// Any value in the interval [1e-03 ≤ x < 1e+03] is formatted in fixed point notation.
[Theory]
[InlineData(1e-3, "0.001 m")]
[InlineData(1.1, "1.1 m")]
[InlineData(999.99, "999.99 m")]
public void FixedPointNotationIntervalFormatting(double value, string expected)
{
string actual = Length.FromMeters(value).ToUnit(LengthUnit.Meter).ToString(AmericanCulture);
Assert.Equal(expected, actual);
}
// Any value in the interval [1e+03 ≤ x < 1e+06] is formatted in fixed point notation with digit grouping.
[Theory]
[InlineData(1000, "1,000 m")]
[InlineData(11000, "11,000 m")]
[InlineData(111000, "111,000 m")]
[InlineData(999999.99, "999,999.99 m")]
public void FixedPointNotationWithDigitGroupingIntervalFormatting(double value, string expected)
{
string actual = Length.FromMeters(value).ToUnit(LengthUnit.Meter).ToString(AmericanCulture);
Assert.Equal(expected, actual);
}
// Any value in the interval [1e+06 ≤ x ≤ +inf) is formatted in scientific notation.
[Theory]
[InlineData(1e6, "1e+06 m")]
[InlineData(11100000, "1.11e+07 m")]
[InlineData(double.MaxValue, "1.8e+308 m")]
public void ScientificNotationUpperIntervalFormatting(double value, string expected)
{
string actual = Length.FromMeters(value).ToUnit(LengthUnit.Meter).ToString(AmericanCulture);
Assert.Equal(expected, actual);
}
[Fact]
public void AllUnitsImplementToStringForInvariantCulture()
{
Assert.Equal("1 °", Angle.FromDegrees(1).ToString());
Assert.Equal("1 m²", Area.FromSquareMeters(1).ToString());
Assert.Equal("1 V", ElectricPotential.FromVolts(1).ToString());
Assert.Equal("1 N", Force.FromNewtons(1).ToString());
Assert.Equal("1 m", Length.FromMeters(1).ToString());
Assert.Equal("1 kg", Mass.FromKilograms(1).ToString());
Assert.Equal("1 Pa", Pressure.FromPascals(1).ToString());
Assert.Equal("1 rad/s", RotationalSpeed.FromRadiansPerSecond(1).ToString());
Assert.Equal("1 K", Temperature.FromKelvins(1).ToString());
Assert.Equal("1 N·m", Torque.FromNewtonMeters(1).ToString());
Assert.Equal("1 m³", Volume.FromCubicMeters(1).ToString());
Assert.Equal("1 m³/s", VolumeFlow.FromCubicMetersPerSecond(1).ToString());
Assert.Equal("2 ft 3 in", Length.FromFeetInches(2, 3).FeetInches.ToString());
Assert.Equal("3 st 7 lb", Mass.FromStonePounds(3, 7).StonePounds.ToString());
}
[Fact]
public void ToString_WithNorwegianCulture()
{
Assert.Equal("1 °", Angle.FromDegrees(1).ToUnit(AngleUnit.Degree).ToString(NorwegianCulture));
Assert.Equal("1 m²", Area.FromSquareMeters(1).ToUnit(AreaUnit.SquareMeter).ToString(NorwegianCulture));
Assert.Equal("1 V", ElectricPotential.FromVolts(1).ToUnit(ElectricPotentialUnit.Volt).ToString(NorwegianCulture));
Assert.Equal("1 m³/s", VolumeFlow.FromCubicMetersPerSecond(1).ToUnit(VolumeFlowUnit.CubicMeterPerSecond).ToString(NorwegianCulture));
Assert.Equal("1 N", Force.FromNewtons(1).ToUnit(ForceUnit.Newton).ToString(NorwegianCulture));
Assert.Equal("1 m", Length.FromMeters(1).ToUnit(LengthUnit.Meter).ToString(NorwegianCulture));
Assert.Equal("1 kg", Mass.FromKilograms(1).ToUnit(MassUnit.Kilogram).ToString(NorwegianCulture));
Assert.Equal("1 Pa", Pressure.FromPascals(1).ToUnit(PressureUnit.Pascal).ToString(NorwegianCulture));
Assert.Equal("1 rad/s", RotationalSpeed.FromRadiansPerSecond(1).ToUnit(RotationalSpeedUnit.RadianPerSecond).ToString(NorwegianCulture));
Assert.Equal("1 K", Temperature.FromKelvins(1).ToUnit(TemperatureUnit.Kelvin).ToString(NorwegianCulture));
Assert.Equal("1 N·m", Torque.FromNewtonMeters(1).ToUnit(TorqueUnit.NewtonMeter).ToString(NorwegianCulture));
Assert.Equal("1 m³", Volume.FromCubicMeters(1).ToUnit(VolumeUnit.CubicMeter).ToString(NorwegianCulture));
}
[Fact]
public void ToString_WithRussianCulture()
{
Assert.Equal("1 °", Angle.FromDegrees(1).ToUnit(AngleUnit.Degree).ToString(RussianCulture));
Assert.Equal("1 м²", Area.FromSquareMeters(1).ToUnit(AreaUnit.SquareMeter).ToString(RussianCulture));
Assert.Equal("1 В", ElectricPotential.FromVolts(1).ToUnit(ElectricPotentialUnit.Volt).ToString(RussianCulture));
Assert.Equal("1 м³/с", VolumeFlow.FromCubicMetersPerSecond(1).ToUnit(VolumeFlowUnit.CubicMeterPerSecond).ToString(RussianCulture));
Assert.Equal("1 Н", Force.FromNewtons(1).ToUnit(ForceUnit.Newton).ToString(RussianCulture));
Assert.Equal("1 м", Length.FromMeters(1).ToUnit(LengthUnit.Meter).ToString(RussianCulture));
Assert.Equal("1 кг", Mass.FromKilograms(1).ToUnit(MassUnit.Kilogram).ToString(RussianCulture));
Assert.Equal("1 Па", Pressure.FromPascals(1).ToUnit(PressureUnit.Pascal).ToString(RussianCulture));
Assert.Equal("1 рад/с", RotationalSpeed.FromRadiansPerSecond(1).ToUnit(RotationalSpeedUnit.RadianPerSecond).ToString(RussianCulture));
Assert.Equal("1 K", Temperature.FromKelvins(1).ToUnit(TemperatureUnit.Kelvin).ToString(RussianCulture));
Assert.Equal("1 Н·м", Torque.FromNewtonMeters(1).ToUnit(TorqueUnit.NewtonMeter).ToString(RussianCulture));
Assert.Equal("1 м³", Volume.FromCubicMeters(1).ToUnit(VolumeUnit.CubicMeter).ToString(RussianCulture));
}
[Fact]
public void GetDefaultAbbreviationThrowsNotImplementedExceptionIfNoneExist()
{
var unitAbbreviationCache = new UnitAbbreviationsCache();
Assert.Throws<NotImplementedException>(() => unitAbbreviationCache.GetDefaultAbbreviation(CustomUnit.Unit1));
}
[Fact]
public void GetDefaultAbbreviationFallsBackToUsEnglishCulture()
{
var oldCurrentCulture = CultureInfo.CurrentCulture;
var oldCurrentUICulture = CultureInfo.CurrentUICulture;
try
{
// CurrentCulture affects number formatting, such as comma or dot as decimal separator.
// CurrentUICulture affects localization, in this case the abbreviation.
// Zulu (South Africa)
var zuluCulture = new CultureInfo("zu-ZA");
CultureInfo.CurrentCulture = CultureInfo.CurrentUICulture = zuluCulture;
var abbreviationsCache = new UnitAbbreviationsCache();
abbreviationsCache.MapUnitToAbbreviation(CustomUnit.Unit1, AmericanCulture, "US english abbreviation for Unit1");
// Act
string abbreviation = abbreviationsCache.GetDefaultAbbreviation(CustomUnit.Unit1, zuluCulture);
// Assert
Assert.Equal("US english abbreviation for Unit1", abbreviation);
}
finally
{
CultureInfo.CurrentCulture = oldCurrentCulture;
CultureInfo.CurrentUICulture = oldCurrentUICulture;
}
}
[Fact]
public void MapUnitToAbbreviation_AddCustomUnit_DoesNotOverrideDefaultAbbreviationForAlreadyMappedUnits()
{
var cache = new UnitAbbreviationsCache();
cache.MapUnitToAbbreviation(AreaUnit.SquareMeter, AmericanCulture, "m^2");
Assert.Equal("m²", cache.GetDefaultAbbreviation(AreaUnit.SquareMeter));
}
[Fact]
public void MapUnitToDefaultAbbreviation_GivenUnitAndCulture_SetsDefaultAbbreviationForUnitAndCulture()
{
var cache = new UnitAbbreviationsCache();
cache.MapUnitToDefaultAbbreviation(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 = GetCulture("en-NZ");
UnitAbbreviationsCache.Default.MapUnitToDefaultAbbreviation(AreaUnit.SquareMeter, newZealandCulture, "m^2");
Assert.Equal("1 m^2", Area.FromSquareMeters(1).ToString(newZealandCulture));
}
[Fact]
public void MapUnitToDefaultAbbreviation_ParsesUsingDefaultString()
{
var cache = new UnitAbbreviationsCache();
cache.MapUnitToDefaultAbbreviation(MassFlowUnit.GramPerSecond, NorwegianCulture, "grams/second");
var parser = new QuantityParser(cache);
Assert.Equal(MassFlow.FromPoundsPerSecond(10), parser.Parse<MassFlow, MassFlowUnit>("10 lb/s", NorwegianCulture, MassFlow.From));
}
[Fact]
public void MapUnitToDefaultAbbreviation_AddsNewUnitAsAdditionalOverride()
{
var cache = new UnitAbbreviationsCache();
var abbreviationsBefore = cache.GetUnitAbbreviations(MassFlowUnit.GramPerSecond, NorwegianCulture);
cache.MapUnitToDefaultAbbreviation(MassFlowUnit.GramPerSecond, NorwegianCulture, "grams/second");
var abbreviationsAfter = cache.GetUnitAbbreviations(MassFlowUnit.GramPerSecond, NorwegianCulture);
Assert.Equal(abbreviationsBefore.Length + 1, abbreviationsAfter.Length);
}
/// <summary>
/// Convenience method to the proper culture parameter type.
/// </summary>
private static CultureInfo GetCulture(string cultureName)
{
return new CultureInfo(cultureName);
}
}
}