-
Notifications
You must be signed in to change notification settings - Fork 386
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
No fall-back culture when using UnitAbbreviationsCache.CreateEmpty #1509
Comments
If you meant that Assert.Equal("g", cache.GetDefaultAbbreviation(MassUnit.Gram, AmericanCulture)); should throw instead of returning empty string, then I agree 👌 |
I believe this is kind of a v6 issue, where ctor creates an empty unit abbreviations cache, while in v5 the ctor loads the default cache. |
For v5 this would be reproducible when using |
@angularsen Ok, after #1507 is merged, I'm want to make the following change to the /// <inheritdoc cref="GetDefaultAbbreviation{TUnitType}" />
/// <param name="unitKey">The key representing the unit type and value.</param>
/// <param name="culture">The localization culture. Defaults to <see cref="CultureInfo.CurrentCulture" /> if null.</param>
/// <exception cref="UnitNotFoundException">
/// Thrown when no unit information is found for the specified
/// <paramref name="unitKey" />.
/// </exception>
/// <exception cref="InvalidOperationException">
/// Thrown when no abbreviations are mapped for the specified unit.
/// </exception>
public string GetDefaultAbbreviation(UnitKey unitKey, CultureInfo? culture = null)
{
IReadOnlyList<string> abbreviations = GetUnitAbbreviations(unitKey, culture);
if (abbreviations.Count == 0)
{
throw new InvalidOperationException($"No abbreviations were found for {unitKey.UnitType.Name}.{(Enum)unitKey}. Make sure that the unit abbreviations are mapped.");
}
return abbreviations[0];
} Normally, this should only throw the
Initially I was going to just add
|
Yeah, that sounds reasonable.
|
Have a look at this test (it's targeting the v6 but that should be reproducible in v5)
It fails with
Everything works fine when initializing with the list of
QuantityInfo
:The reason why this doesn't work is not immediately obvious- it's because when calling
MapUnitToDefaultAbbreviation
, when theUnitInfo
is not found in theQuantityInfoLookup
thePerformAbbreviationMapping
creates a dummyUnitInfo
which doesn't have anything set for theQuantityName
property.In my opinion, the best solution would be to use these constructors:
and throw an exception when the
UnitInfo
isn't found in the cache (when mapping or getting).The text was updated successfully, but these errors were encountered: