Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions UnitsNet.Tests/UnitAbbreviationsCacheTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,79 @@ public void UnitInfoOverloads_WithUnitOutsideConfiguredQuantities_ThrowUnitNotFo
]);
}

[Fact]
public void GetDefaultAbbreviation_WithQuantityAndUnitNames_ReturnsTheExpectedAbbreviation()
{
var unitAbbreviationCache = new UnitAbbreviationsCache([Length.Info]);

string abbreviation = unitAbbreviationCache.GetDefaultAbbreviation("Length", "Millimeter", AmericanCulture);

Assert.Equal("mm", abbreviation);
}

[Fact]
public void GetUnitAbbreviations_WithQuantityAndUnitNames_ReturnsTheExpectedAbbreviations()
{
var unitAbbreviationCache = new UnitAbbreviationsCache([Area.Info]);

var abbreviations = unitAbbreviationCache.GetUnitAbbreviations("Area", "SquareMeter", AmericanCulture);

Assert.Contains("m²", abbreviations);
}

[Fact]
public void QuantityAndUnitNameOverloads_AreCaseInsensitive()
{
var unitAbbreviationCache = new UnitAbbreviationsCache([Length.Info]);

string abbreviation = unitAbbreviationCache.GetDefaultAbbreviation("length", "millimeter", AmericanCulture);

Assert.Equal("mm", abbreviation);
}

[Fact]
public void QuantityAndUnitNameOverloads_UseConfiguredQuantityLookup()
{
var unitAbbreviationCache = new UnitAbbreviationsCache([Mass.Info]);

Assert.Multiple(checks:
[
() => Assert.Equal("g", unitAbbreviationCache.GetDefaultAbbreviation("Mass", "Gram", AmericanCulture)),
() => Assert.Throws<QuantityNotFoundException>(() => unitAbbreviationCache.GetDefaultAbbreviation("Length", "Meter", AmericanCulture)),
() => Assert.Throws<QuantityNotFoundException>(() => unitAbbreviationCache.GetUnitAbbreviations("Length", "Meter", AmericanCulture))
]);
}

[Fact]
public void QuantityAndUnitNameOverloads_WithInvalidNames_ThrowExpectedExceptions()
{
var unitAbbreviationCache = new UnitAbbreviationsCache([Length.Info]);

Assert.Multiple(checks:
[
() => Assert.Throws<QuantityNotFoundException>(() => unitAbbreviationCache.GetDefaultAbbreviation("InvalidQuantity", "Meter", AmericanCulture)),
() => Assert.Throws<UnitNotFoundException>(() => unitAbbreviationCache.GetDefaultAbbreviation("Length", "InvalidUnit", AmericanCulture)),
() => Assert.Throws<QuantityNotFoundException>(() => unitAbbreviationCache.GetUnitAbbreviations("InvalidQuantity", "Meter", AmericanCulture)),
() => Assert.Throws<UnitNotFoundException>(() => unitAbbreviationCache.GetUnitAbbreviations("Length", "InvalidUnit", AmericanCulture))
]);
}

[Fact]
public void QuantityAndUnitNameOverloads_WithNullNames_ThrowArgumentNullException()
{
Assert.Multiple(checks:
[
() => Assert.Equal("quantityName",
Assert.Throws<ArgumentNullException>(() => UnitAbbreviationsCache.Default.GetDefaultAbbreviation(null!, "Meter")).ParamName),
() => Assert.Equal("unitName",
Assert.Throws<ArgumentNullException>(() => UnitAbbreviationsCache.Default.GetDefaultAbbreviation("Length", null!)).ParamName),
() => Assert.Equal("quantityName",
Assert.Throws<ArgumentNullException>(() => UnitAbbreviationsCache.Default.GetUnitAbbreviations(null!, "Meter")).ParamName),
() => Assert.Equal("unitName",
Assert.Throws<ArgumentNullException>(() => UnitAbbreviationsCache.Default.GetUnitAbbreviations("Length", null!)).ParamName)
]);
}

[Fact]
public void GetDefaultAbbreviationReturnsTheExpectedAbbreviationWhenConstructedWithTheSpecificQuantityInfo()
{
Expand Down
54 changes: 54 additions & 0 deletions UnitsNet/CustomCode/UnitAbbreviationsCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,29 @@ public string GetDefaultAbbreviation(UnitKey unitKey, IFormatProvider? formatPro
return GetDefaultAbbreviation(Quantities.GetUnitInfo(unitKey), formatProvider);
}

/// <inheritdoc cref="GetDefaultAbbreviation{TUnitType}" />
/// <param name="quantityName">The invariant quantity name, such as "Length". This parameter does not support localization.</param>
/// <param name="unitName">The invariant unit name, such as "Meter". This parameter does not support localization.</param>
/// <param name="formatProvider">The format provider to use for lookup. Defaults to <see cref="CultureInfo.CurrentCulture" /> if null.</param>
/// <exception cref="ArgumentNullException">
/// Thrown when <paramref name="quantityName" /> or <paramref name="unitName" /> is null.
/// </exception>
/// <exception cref="QuantityNotFoundException">
/// Thrown when no quantity information is found for the specified
/// <paramref name="quantityName" />.
/// </exception>
/// <exception cref="UnitNotFoundException">
/// Thrown when no unit information is found for the specified
/// <paramref name="quantityName" /> and <paramref name="unitName" />.
/// </exception>
/// <exception cref="InvalidOperationException">
/// Thrown when no abbreviations are mapped for the specified unit.
/// </exception>
public string GetDefaultAbbreviation(string quantityName, string unitName, IFormatProvider? formatProvider = null)
{
return GetDefaultAbbreviation(GetConfiguredUnitInfo(quantityName, unitName), formatProvider);
}

/// <inheritdoc cref="GetDefaultAbbreviation{TUnitType}" />
/// <param name="unitInfo">The info representing the unit.</param>
/// <param name="formatProvider">The format provider to use for lookup. Defaults to <see cref="CultureInfo.CurrentCulture" /> if null.</param>
Expand Down Expand Up @@ -395,6 +418,29 @@ public IReadOnlyList<string> GetUnitAbbreviations(UnitKey unitKey, IFormatProvid
return GetUnitAbbreviations(Quantities.GetUnitInfo(unitKey), formatProvider);
}

/// <summary>
/// Retrieves the unit abbreviations for a specified quantity and unit name with an optional format provider.
/// </summary>
/// <param name="quantityName">The invariant quantity name, such as "Length". This parameter does not support localization.</param>
/// <param name="unitName">The invariant unit name, such as "Meter". This parameter does not support localization.</param>
/// <param name="formatProvider">The format provider to use for lookup. Defaults to <see cref="CultureInfo.CurrentCulture" /> if null.</param>
/// <returns>A read-only collection of unit abbreviation strings.</returns>
/// <exception cref="ArgumentNullException">
/// Thrown when <paramref name="quantityName" /> or <paramref name="unitName" /> is null.
/// </exception>
/// <exception cref="QuantityNotFoundException">
/// Thrown when no quantity information is found for the specified
/// <paramref name="quantityName" />.
/// </exception>
/// <exception cref="UnitNotFoundException">
/// Thrown when no unit information is found for the specified
/// <paramref name="quantityName" /> and <paramref name="unitName" />.
/// </exception>
public IReadOnlyList<string> GetUnitAbbreviations(string quantityName, string unitName, IFormatProvider? formatProvider = null)
{
return GetUnitAbbreviations(GetConfiguredUnitInfo(quantityName, unitName), formatProvider);
}

/// <summary>
/// Retrieves the unit abbreviations for a specified unit info and optional format provider.
/// </summary>
Expand Down Expand Up @@ -472,6 +518,14 @@ private UnitInfo GetConfiguredUnitInfo(UnitInfo unitInfo)
return Quantities.GetUnitInfo(unitInfo.UnitKey);
}

private UnitInfo GetConfiguredUnitInfo(string quantityName, string unitName)
{
if (quantityName is null) throw new ArgumentNullException(nameof(quantityName));
if (unitName is null) throw new ArgumentNullException(nameof(unitName));

return Quantities.GetUnitByName(quantityName, unitName);
}

/// <summary>
/// Get all abbreviations for the given unit and culture.
/// </summary>
Expand Down
Loading