Skip to content

Commit

Permalink
💥Change UnitAbbreviationsCache ctor to not load defaults (#1476)
Browse files Browse the repository at this point in the history
Ref #1200

- Remove UnitAbbreviationsCache.CreateEmpty()
- Change default ctor to NOT load default unit abbreviations, use
`CreateDefault()` instead
- Fix 2 broken test cases
  • Loading branch information
angularsen authored Dec 27, 2024
1 parent a3c32b6 commit d81898a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 17 deletions.
6 changes: 3 additions & 3 deletions UnitsNet.Tests/UnitAbbreviationsCacheTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -325,10 +325,10 @@ public void MapUnitToAbbreviation_DoesNotAffectOtherCacheInstances()
var culture = AmericanCulture;
var unit = AreaUnit.SquareMeter;

var cache1 = new UnitAbbreviationsCache();
var cache1 = UnitAbbreviationsCache.CreateDefault();
cache1.MapUnitToAbbreviation(unit, culture, "m^2");

var cache2 = new UnitAbbreviationsCache();
var cache2 = UnitAbbreviationsCache.CreateDefault();
cache2.MapUnitToAbbreviation(unit, culture, "m2");

Assert.Equal(new[] { "m²", "m^2" }, cache1.GetUnitAbbreviations(unit, culture));
Expand All @@ -340,7 +340,7 @@ public void MapUnitToAbbreviation_DoesNotAffectOtherCacheInstances()
[Fact]
public void MapUnitToAbbreviation_AddCustomUnit_DoesNotOverrideDefaultAbbreviationForAlreadyMappedUnits()
{
var cache = new UnitAbbreviationsCache();
var cache = UnitAbbreviationsCache.CreateDefault();
cache.MapUnitToAbbreviation(AreaUnit.SquareMeter, AmericanCulture, "m^2");

Assert.Equal("m²", cache.GetDefaultAbbreviation(AreaUnit.SquareMeter, AmericanCulture));
Expand Down
16 changes: 2 additions & 14 deletions UnitsNet/CustomCode/UnitAbbreviationsCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,10 @@ public sealed class UnitAbbreviationsCache
private ConcurrentDictionary<AbbreviationMapKey, IReadOnlyList<string>> AbbreviationsMap { get; } = new();

/// <summary>
/// Create an instance of the cache and load all the abbreviations defined in the library.
/// Create an empty instance of the cache, with no default abbreviations loaded.
/// </summary>
// TODO Change this to create an empty cache in v6: https://github.com/angularsen/UnitsNet/issues/1200
[Obsolete("Use CreateDefault() instead to create an instance that loads the built-in units. The default ctor will change to create an empty cache in UnitsNet v6.")]
public UnitAbbreviationsCache()
: this(new QuantityInfoLookup(Quantity.ByName.Values))
: this(new QuantityInfoLookup([]))
{
}

Expand All @@ -59,16 +57,6 @@ internal UnitAbbreviationsCache(QuantityInfoLookup quantityInfoLookup)
QuantityInfoLookup = quantityInfoLookup;
}

/// <summary>
/// Create an instance with empty cache.
/// </summary>
/// <remarks>
/// Workaround until v6 changes the default ctor to create an empty cache.<br/>
/// </remarks>
/// <returns>Instance with empty cache.</returns>
// TODO Remove in v6: https://github.com/angularsen/UnitsNet/issues/1200
public static UnitAbbreviationsCache CreateEmpty() => new(new QuantityInfoLookup(new List<QuantityInfo>()));

/// <summary>
/// Create an instance of the cache and load all the built-in unit abbreviations defined in the library.
/// </summary>
Expand Down

0 comments on commit d81898a

Please sign in to comment.