From d81898a9e1eb329eef6672b18c30b39cdd313d69 Mon Sep 17 00:00:00 2001 From: Andreas Gullberg Larsen Date: Sat, 28 Dec 2024 00:08:27 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=A5Change=20UnitAbbreviationsCache=20c?= =?UTF-8?q?tor=20to=20not=20load=20defaults=20(#1476)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ref #1200 - Remove UnitAbbreviationsCache.CreateEmpty() - Change default ctor to NOT load default unit abbreviations, use `CreateDefault()` instead - Fix 2 broken test cases --- UnitsNet.Tests/UnitAbbreviationsCacheTests.cs | 6 +++--- UnitsNet/CustomCode/UnitAbbreviationsCache.cs | 16 ++-------------- 2 files changed, 5 insertions(+), 17 deletions(-) diff --git a/UnitsNet.Tests/UnitAbbreviationsCacheTests.cs b/UnitsNet.Tests/UnitAbbreviationsCacheTests.cs index f23e5a1390..00d314ff7b 100644 --- a/UnitsNet.Tests/UnitAbbreviationsCacheTests.cs +++ b/UnitsNet.Tests/UnitAbbreviationsCacheTests.cs @@ -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)); @@ -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)); diff --git a/UnitsNet/CustomCode/UnitAbbreviationsCache.cs b/UnitsNet/CustomCode/UnitAbbreviationsCache.cs index ee56cf7925..e19ad7f1d1 100644 --- a/UnitsNet/CustomCode/UnitAbbreviationsCache.cs +++ b/UnitsNet/CustomCode/UnitAbbreviationsCache.cs @@ -39,12 +39,10 @@ public sealed class UnitAbbreviationsCache private ConcurrentDictionary> AbbreviationsMap { get; } = new(); /// - /// 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. /// - // 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([])) { } @@ -59,16 +57,6 @@ internal UnitAbbreviationsCache(QuantityInfoLookup quantityInfoLookup) QuantityInfoLookup = quantityInfoLookup; } - /// - /// Create an instance with empty cache. - /// - /// - /// Workaround until v6 changes the default ctor to create an empty cache.
- ///
- /// Instance with empty cache. - // TODO Remove in v6: https://github.com/angularsen/UnitsNet/issues/1200 - public static UnitAbbreviationsCache CreateEmpty() => new(new QuantityInfoLookup(new List())); - /// /// Create an instance of the cache and load all the built-in unit abbreviations defined in the library. ///