From 032de3328c52630b8aae5e620152baa7efba3eb3 Mon Sep 17 00:00:00 2001 From: Andreas Gullberg Larsen Date: Thu, 23 Jul 2026 23:13:49 +0200 Subject: [PATCH] Add name-based unit abbreviation lookup overloads --- UnitsNet.Tests/UnitAbbreviationsCacheTests.cs | 73 +++++++++++++++++++ UnitsNet/CustomCode/UnitAbbreviationsCache.cs | 54 ++++++++++++++ 2 files changed, 127 insertions(+) diff --git a/UnitsNet.Tests/UnitAbbreviationsCacheTests.cs b/UnitsNet.Tests/UnitAbbreviationsCacheTests.cs index 1e68f6c185..4188cd77c1 100644 --- a/UnitsNet.Tests/UnitAbbreviationsCacheTests.cs +++ b/UnitsNet.Tests/UnitAbbreviationsCacheTests.cs @@ -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(() => unitAbbreviationCache.GetDefaultAbbreviation("Length", "Meter", AmericanCulture)), + () => Assert.Throws(() => unitAbbreviationCache.GetUnitAbbreviations("Length", "Meter", AmericanCulture)) + ]); + } + + [Fact] + public void QuantityAndUnitNameOverloads_WithInvalidNames_ThrowExpectedExceptions() + { + var unitAbbreviationCache = new UnitAbbreviationsCache([Length.Info]); + + Assert.Multiple(checks: + [ + () => Assert.Throws(() => unitAbbreviationCache.GetDefaultAbbreviation("InvalidQuantity", "Meter", AmericanCulture)), + () => Assert.Throws(() => unitAbbreviationCache.GetDefaultAbbreviation("Length", "InvalidUnit", AmericanCulture)), + () => Assert.Throws(() => unitAbbreviationCache.GetUnitAbbreviations("InvalidQuantity", "Meter", AmericanCulture)), + () => Assert.Throws(() => unitAbbreviationCache.GetUnitAbbreviations("Length", "InvalidUnit", AmericanCulture)) + ]); + } + + [Fact] + public void QuantityAndUnitNameOverloads_WithNullNames_ThrowArgumentNullException() + { + Assert.Multiple(checks: + [ + () => Assert.Equal("quantityName", + Assert.Throws(() => UnitAbbreviationsCache.Default.GetDefaultAbbreviation(null!, "Meter")).ParamName), + () => Assert.Equal("unitName", + Assert.Throws(() => UnitAbbreviationsCache.Default.GetDefaultAbbreviation("Length", null!)).ParamName), + () => Assert.Equal("quantityName", + Assert.Throws(() => UnitAbbreviationsCache.Default.GetUnitAbbreviations(null!, "Meter")).ParamName), + () => Assert.Equal("unitName", + Assert.Throws(() => UnitAbbreviationsCache.Default.GetUnitAbbreviations("Length", null!)).ParamName) + ]); + } + [Fact] public void GetDefaultAbbreviationReturnsTheExpectedAbbreviationWhenConstructedWithTheSpecificQuantityInfo() { diff --git a/UnitsNet/CustomCode/UnitAbbreviationsCache.cs b/UnitsNet/CustomCode/UnitAbbreviationsCache.cs index 0a023f5d06..ae7eb81487 100644 --- a/UnitsNet/CustomCode/UnitAbbreviationsCache.cs +++ b/UnitsNet/CustomCode/UnitAbbreviationsCache.cs @@ -319,6 +319,29 @@ public string GetDefaultAbbreviation(UnitKey unitKey, IFormatProvider? formatPro return GetDefaultAbbreviation(Quantities.GetUnitInfo(unitKey), formatProvider); } + /// + /// The invariant quantity name, such as "Length". This parameter does not support localization. + /// The invariant unit name, such as "Meter". This parameter does not support localization. + /// The format provider to use for lookup. Defaults to if null. + /// + /// Thrown when or is null. + /// + /// + /// Thrown when no quantity information is found for the specified + /// . + /// + /// + /// Thrown when no unit information is found for the specified + /// and . + /// + /// + /// Thrown when no abbreviations are mapped for the specified unit. + /// + public string GetDefaultAbbreviation(string quantityName, string unitName, IFormatProvider? formatProvider = null) + { + return GetDefaultAbbreviation(GetConfiguredUnitInfo(quantityName, unitName), formatProvider); + } + /// /// The info representing the unit. /// The format provider to use for lookup. Defaults to if null. @@ -395,6 +418,29 @@ public IReadOnlyList GetUnitAbbreviations(UnitKey unitKey, IFormatProvid return GetUnitAbbreviations(Quantities.GetUnitInfo(unitKey), formatProvider); } + /// + /// Retrieves the unit abbreviations for a specified quantity and unit name with an optional format provider. + /// + /// The invariant quantity name, such as "Length". This parameter does not support localization. + /// The invariant unit name, such as "Meter". This parameter does not support localization. + /// The format provider to use for lookup. Defaults to if null. + /// A read-only collection of unit abbreviation strings. + /// + /// Thrown when or is null. + /// + /// + /// Thrown when no quantity information is found for the specified + /// . + /// + /// + /// Thrown when no unit information is found for the specified + /// and . + /// + public IReadOnlyList GetUnitAbbreviations(string quantityName, string unitName, IFormatProvider? formatProvider = null) + { + return GetUnitAbbreviations(GetConfiguredUnitInfo(quantityName, unitName), formatProvider); + } + /// /// Retrieves the unit abbreviations for a specified unit info and optional format provider. /// @@ -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); + } + /// /// Get all abbreviations for the given unit and culture. ///