Skip to content

Commit 38e868a

Browse files
committed
replaced the public usages of the UnitKey constructor and improved the exception comments
1 parent 3d7a7c5 commit 38e868a

File tree

3 files changed

+49
-7
lines changed

3 files changed

+49
-7
lines changed

UnitsNet.Tests/UnitAbbreviationsCacheTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@ public void GetDefaultAbbreviationFallsBackToInvariantCulture()
126126
// Assert
127127
Assert.Equal("Invariant abbreviation for Unit1", abbreviation);
128128
}
129+
130+
[Fact]
131+
public void GetDefaultAbbreviation_WithNullUnitType_ThrowsArgumentNullException()
132+
{
133+
Assert.Throws<ArgumentNullException>(() => UnitAbbreviationsCache.Default.GetDefaultAbbreviation(null!, 1));
134+
}
129135

130136
[Fact]
131137
public void GetDefaultAbbreviationThrowsUnitNotFoundExceptionIfNoneExist()
@@ -149,6 +155,12 @@ public void GetAllUnitAbbreviationsForQuantity_WithQuantityWithoutAbbreviations_
149155
Assert.Empty(unitAbbreviationsCache.GetAllUnitAbbreviationsForQuantity(typeof(HowMuchUnit)));
150156
}
151157

158+
[Fact]
159+
public void GetAllUnitAbbreviationsForQuantity_WithNullUnitType_ThrowsArgumentNullException()
160+
{
161+
Assert.Throws<ArgumentNullException>(() => UnitAbbreviationsCache.Default.GetAllUnitAbbreviationsForQuantity(null!));
162+
}
163+
152164
[Fact]
153165
public void GetAllUnitAbbreviationsForQuantity_WithInvalidUnitType_ThrowsArgumentException()
154166
{

UnitsNet/CustomCode/UnitAbbreviationsCache.cs

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public void MapUnitToAbbreviation<TUnitType>(TUnitType unit, params IEnumerable<
116116
/// <param name="abbreviations">Unit abbreviations to add.</param>
117117
public void MapUnitToAbbreviation(Type unitType, int unitValue, IFormatProvider? formatProvider, params IEnumerable<string> abbreviations)
118118
{
119-
MapUnitToAbbreviation(new UnitKey(unitType, unitValue), formatProvider, abbreviations);
119+
MapUnitToAbbreviation(UnitKey.Create(unitType, unitValue), formatProvider, abbreviations);
120120
}
121121

122122
/// <inheritdoc cref="MapUnitToAbbreviation{TUnitType}(TUnitType,IEnumerable{string})"/>>
@@ -159,6 +159,10 @@ public void MapUnitToAbbreviation(UnitKey unitKey, IFormatProvider? formatProvid
159159
/// <param name="unit">The unit enum value.</param>
160160
/// <param name="abbreviation">Unit abbreviations to add as default.</param>
161161
/// <typeparam name="TUnitType">The type of unit enum.</typeparam>
162+
/// <exception cref="UnitNotFoundException">
163+
/// Thrown when no unit information is found for the specified
164+
/// <paramref name="unit" />.
165+
/// </exception>
162166
public void MapUnitToDefaultAbbreviation<TUnitType>(TUnitType unit, string abbreviation)
163167
where TUnitType : struct, Enum
164168
{
@@ -189,9 +193,15 @@ public void MapUnitToDefaultAbbreviation<TUnitType>(TUnitType unit, IFormatProvi
189193
/// <param name="unitValue">The unit enum value.</param>
190194
/// <param name="formatProvider">The format provider to use for lookup. Defaults to <see cref="CultureInfo.CurrentCulture" /> if null.</param>
191195
/// <param name="abbreviation">Unit abbreviation to add as default.</param>
196+
/// <exception cref="ArgumentNullException">
197+
/// Thrown when the provided type is null.
198+
/// </exception>
199+
/// <exception cref="ArgumentException">
200+
/// Thrown when the provided type is not an enumeration type.
201+
/// </exception>
192202
public void MapUnitToDefaultAbbreviation(Type unitType, int unitValue, IFormatProvider? formatProvider, string abbreviation)
193203
{
194-
MapUnitToDefaultAbbreviation(new UnitKey(unitType, unitValue), formatProvider, abbreviation);
204+
MapUnitToDefaultAbbreviation(UnitKey.Create(unitType, unitValue), formatProvider, abbreviation);
195205
}
196206

197207
/// <inheritdoc cref="MapUnitToDefaultAbbreviation{TUnitType}(TUnitType,string)"/>>
@@ -240,6 +250,12 @@ public string GetDefaultAbbreviation<TUnitType>(TUnitType unit, IFormatProvider?
240250
/// <param name="unitType">The unit enum type.</param>
241251
/// <param name="unitValue">The unit enum value.</param>
242252
/// <param name="formatProvider">The format provider to use for lookup. Defaults to <see cref="CultureInfo.CurrentCulture" /> if null.</param>
253+
/// <exception cref="ArgumentNullException">
254+
/// Thrown when the provided type is null.
255+
/// </exception>
256+
/// <exception cref="ArgumentException">
257+
/// Thrown when the provided type is not an enumeration type.
258+
/// </exception>
243259
/// <exception cref="UnitNotFoundException">
244260
/// Thrown when no unit information is found for the specified
245261
/// <paramref name="unitType" /> and <paramref name="unitValue" />.
@@ -249,7 +265,7 @@ public string GetDefaultAbbreviation<TUnitType>(TUnitType unit, IFormatProvider?
249265
/// </exception>
250266
public string GetDefaultAbbreviation(Type unitType, int unitValue, IFormatProvider? formatProvider = null)
251267
{
252-
return GetDefaultAbbreviation(new UnitKey(unitType, unitValue), formatProvider);
268+
return GetDefaultAbbreviation(UnitKey.Create(unitType, unitValue), formatProvider);
253269
}
254270

255271
/// <inheritdoc cref="GetDefaultAbbreviation{TUnitType}" />
@@ -297,13 +313,19 @@ public IReadOnlyList<string> GetUnitAbbreviations<TUnitType>(TUnitType unit, IFo
297313
/// <param name="unitValue">Enum value for unit.</param>
298314
/// <param name="formatProvider">The format provider to use for lookup. Defaults to <see cref="CultureInfo.CurrentCulture" /> if null.</param>
299315
/// <returns>Unit abbreviations associated with unit.</returns>
316+
/// <exception cref="ArgumentNullException">
317+
/// Thrown when the provided type is null.
318+
/// </exception>
319+
/// <exception cref="ArgumentException">
320+
/// Thrown when the provided type is not an enumeration type.
321+
/// </exception>
300322
/// <exception cref="UnitNotFoundException">
301323
/// Thrown when no unit information is found for the specified
302324
/// <paramref name="unitType" /> and <paramref name="unitValue" />.
303325
/// </exception>
304326
public IReadOnlyList<string> GetUnitAbbreviations(Type unitType, int unitValue, IFormatProvider? formatProvider = null)
305327
{
306-
return GetUnitAbbreviations(new UnitKey(unitType, unitValue), formatProvider);
328+
return GetUnitAbbreviations(UnitKey.Create(unitType, unitValue), formatProvider);
307329
}
308330

309331
/// <summary>
@@ -336,19 +358,27 @@ public IReadOnlyList<string> GetUnitAbbreviations(UnitKey unitKey, IFormatProvid
336358
/// <returns>
337359
/// A read-only list of unit abbreviations associated with the specified unit type.
338360
/// </returns>
361+
/// <exception cref="ArgumentNullException">
362+
/// Thrown when the provided type is null.
363+
/// </exception>
339364
/// <exception cref="ArgumentException">
340-
/// Thrown when the provided <paramref name="unitEnumType" /> is not an enum type.
365+
/// Thrown when the provided type is not an enumeration type.
341366
/// </exception>
342367
/// <exception cref="UnitNotFoundException">
343368
/// Thrown when no quantity is found for the specified unit type.
344369
/// </exception>
345370
public IReadOnlyList<string> GetAllUnitAbbreviationsForQuantity(Type unitEnumType, IFormatProvider? formatProvider = null)
346371
{
372+
if (unitEnumType == null)
373+
{
374+
throw new ArgumentNullException(nameof(unitEnumType));
375+
}
376+
347377
if (!Quantities.TryGetQuantityByUnitType(unitEnumType, out QuantityInfo? quantityInfo))
348378
{
349379
if (!unitEnumType.IsEnum)
350380
{
351-
throw new ArgumentException($"Type {unitEnumType.FullName} is not a supported unit type.");
381+
throw new ArgumentException($"Unit type must be an enumeration, but was {unitEnumType.FullName}.", nameof(unitEnumType));
352382
}
353383

354384
throw new UnitNotFoundException($"No quantity was found with the specified unit type: '{unitEnumType}'.") { Data = { ["unitType"] = unitEnumType.Name } };

UnitsNet/CustomCode/UnitKey.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public static UnitKey Create(Type unitType, int unitValue)
105105

106106
if (!unitType.IsEnum)
107107
{
108-
throw new ArgumentException($"Unit type must be an enumeration, but was {unitType.FullName}.");
108+
throw new ArgumentException($"Unit type must be an enumeration, but was {unitType.FullName}.", nameof(unitType));
109109
}
110110

111111
return new UnitKey(unitType, unitValue);

0 commit comments

Comments
 (0)