Have a look at this test (it's targeting the v6 but that should be reproducible in v5)
[Fact]publicvoidMapUnitToDefaultAbbreviation_GivenUnitAndNoCulture_SetsDefaultAbbreviationForUnitForCurrentCulture(){usingvarcultureScope=newCultureScope(NorwegianCultureName);varcache=newUnitAbbreviationsCache();cache.MapUnitToDefaultAbbreviation(MassUnit.Gram,"zz");Assert.Equal("zz",cache.GetDefaultAbbreviation(MassUnit.Gram));Assert.Equal("g",cache.GetDefaultAbbreviation(MassUnit.Gram,AmericanCulture));}It fails with
Expected: "g"
Actual: ""
Everything works fine when initializing with the list of QuantityInfo:
[Fact]publicvoidMapUnitToDefaultAbbreviation_WithDefaultLookup_GivenUnitAndNoCulture_SetsDefaultAbbreviationForUnitForCurrentCulture(){usingvarcultureScope=newCultureScope(NorwegianCultureName);varcache=UnitAbbreviationsCache.CreateDefault();cache.MapUnitToDefaultAbbreviation(MassUnit.Gram,"zz");Assert.Equal("zz",cache.GetDefaultAbbreviation(MassUnit.Gram));Assert.Equal("g",cache.GetDefaultAbbreviation(MassUnit.Gram,AmericanCulture));}The reason why this doesn't work is not immediately obvious- it's because when calling MapUnitToDefaultAbbreviation, when the UnitInfo is not found in the QuantityInfoLookup the PerformAbbreviationMapping creates a dummyUnitInfo which doesn't have anything set for the QuantityName property.
In my opinion, the best solution would be to use these constructors:
/// <summary>/// Create an instance of the cache and load all the built-in quantities defined in the library./// </summary>/// <returns>Instance for mapping any of the built-in units.</returns>publicUnitAbbreviationsCache():this(UnitsNetSetup.Default.QuantityInfoLookup){}/// <summary>/// Creates an instance of the cache using the specified set of quantities./// </summary>/// <returns>Instance for mapping the units of the provided quantities.</returns>publicUnitAbbreviationsCache(IReadOnlyCollection<QuantityInfo> quantities):this(newQuantityInfoLookup(quantities)){}/// <summary>/// Creates an instance of the cache using the specified set of quantities./// </summary>/// <remarks>/// Access type is <c>internal</c> until this class is matured and ready for external use./// </remarks>internalUnitAbbreviationsCache(QuantityInfoLookup quantities){Quantities=quantities;}and throw an exception when the UnitInfo isn't found in the cache (when mapping or getting).
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 theUnitInfois not found in theQuantityInfoLookupthePerformAbbreviationMappingcreates a dummyUnitInfowhich doesn't have anything set for theQuantityNameproperty.In my opinion, the best solution would be to use these constructors:
and throw an exception when the
UnitInfoisn't found in the cache (when mapping or getting).