Skip to content

Add name-based unit abbreviation lookup overloads#1695

Draft
angularsen wants to merge 1 commit into
masterfrom
agl-codex/unit-abbreviation-name-overloads
Draft

Add name-based unit abbreviation lookup overloads#1695
angularsen wants to merge 1 commit into
masterfrom
agl-codex/unit-abbreviation-name-overloads

Conversation

@angularsen

Copy link
Copy Markdown
Owner

Motivation

#1067 asks for looking up unit abbreviations by quantity and unit names instead of requiring unit enum values. The parser side and UnitInfo overloads already exist, but the direct UnitAbbreviationsCache.Default.GetDefaultAbbreviation("Length", "Millimeter") shape was still missing.

Closes #1067.

Changes

  • Added UnitAbbreviationsCache.GetDefaultAbbreviation(string quantityName, string unitName, IFormatProvider? formatProvider = null).
  • Added UnitAbbreviationsCache.GetUnitAbbreviations(string quantityName, string unitName, IFormatProvider? formatProvider = null) for the same lookup shape.
  • Added tests for valid lookups, case-insensitive names, configured quantity lookups, and invalid/null inputs.

Validation

  • dotnet test UnitsNet.Tests --filter "FullyQualifiedName~UnitAbbreviationsCacheTests"
  • dotnet test UnitsNet.Tests --no-build --filter "FullyQualifiedName~UnitAbbreviationsCacheTests"

@claude

claude Bot commented Jul 23, 2026

Copy link
Copy Markdown

Review

Small, focused addition — new name-based overloads on UnitAbbreviationsCache, no generated-code or unit-definition changes, so most of the checklist below doesn't apply.

Breaking changes: None. Purely additive public API (two new overloads with an optional trailing parameter), no changes to generated code or existing signatures.

New quantities/units: N/A — no changes to Common/UnitDefinitions/.

Generated code: N/A — only hand-written code in UnitsNet/CustomCode/UnitAbbreviationsCache.cs and tests.

Code quality / conventions:

  • The new GetDefaultAbbreviation(string, string, IFormatProvider?) / GetUnitAbbreviations(string, string, IFormatProvider?) overloads mirror the existing shape used elsewhere (e.g. QuantityInfoLookup.GetUnitByName, Quantity.From(string, string, ...)), so the API is consistent with existing name-based lookup patterns in the library.
  • GetConfiguredUnitInfo(string quantityName, string unitName) (UnitsNet/CustomCode/UnitAbbreviationsCache.cs:521-527) correctly delegates to the instance's Quantities (not UnitsNetSetup.Default.Quantities), so it respects a cache constructed with a restricted quantity set — confirmed by the QuantityAndUnitNameOverloads_UseConfiguredQuantityLookup test.
  • Null checks use the if (x is null) throw new ArgumentNullException(nameof(x)) style already used in this file (e.g. line 517), consistent with the rest of the file rather than introducing ArgumentNullException.ThrowIfNull or another convention.
  • XML doc comments are thorough and document all thrown exception types (ArgumentNullException, QuantityNotFoundException, UnitNotFoundException), matching the style of neighboring members.

Potential bugs: None spotted. The null checks happen before the lookup, and case-insensitivity is delegated to QuantityInfoLookup.GetUnitByName, which already does an OrdinalIgnoreCase comparison — the new "case insensitive" test documents existing lookup behavior rather than new logic, which is fine.

Test coverage: Good — covers success, case-insensitivity, restricted-quantity-set behavior, invalid quantity/unit names, and null argument names for both new overloads.

Performance/security: No concerns — this is a thin convenience wrapper around existing lookup logic.

Nice, minimal PR overall.

@codecov

codecov Bot commented Jul 23, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96%. Comparing base (9cd9c6d) to head (dce28df).

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #1695    +/-   ##
=======================================
  Coverage      96%     96%            
=======================================
  Files         457     455     -2     
  Lines       30163   29558   -605     
=======================================
- Hits        29098   28519   -579     
+ Misses       1065    1039    -26     
Flag Coverage Δ
net48 ?

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Map and look up unit abbreviations by strings instead of enums

1 participant