Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨Add UnitsNetSetup to hold global static state #1267

Merged
merged 2 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public AbbreviatedUnitsConverter()
/// </summary>
/// <param name="comparer">The comparer used to compare the property/quantity names (e.g. StringComparer.OrdinalIgnoreCase) </param>
public AbbreviatedUnitsConverter(IEqualityComparer<string?> comparer)
: this(new Dictionary<string, QuantityInfo>(Quantity.ByName, comparer), UnitAbbreviationsCache.Default, comparer)
: this(new Dictionary<string, QuantityInfo>(Quantity.ByName, comparer), UnitsNetSetup.Default.UnitAbbreviations, comparer)
{
}

Expand Down
29 changes: 29 additions & 0 deletions UnitsNet.Tests/AlphabeticalOrderer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Licensed under MIT No Attribution, see LICENSE file at the root.
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.

using System.Collections.Generic;
using System.Linq;
using Xunit.Abstractions;
using Xunit.Sdk;

namespace UnitsNet.Tests;

/// <summary>
/// Useful for debugging tests where a particular order of tests is required.
/// </summary>
/// <example>
/// Add the attribute to your test class:
/// <code>
/// <![CDATA[
/// [TestCaseOrderer(
/// ordererTypeName: "UnitsNet.Tests.AlphabeticalOrderer",
/// ordererAssemblyName: "UnitsNet.Tests")]
/// ]]>
/// </code>
/// </example>
public class AlphabeticalOrderer : ITestCaseOrderer
{
public IEnumerable<TTestCase> OrderTestCases<TTestCase>(
IEnumerable<TTestCase> testCases) where TTestCase : ITestCase =>
testCases.OrderBy(testCase => testCase.TestMethod.Method.Name);
}
15 changes: 8 additions & 7 deletions UnitsNet.Tests/BaseUnitsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,14 @@ public void ToStringGivesExpectedResult()
AmountOfSubstanceUnit.Mole,
LuminousIntensityUnit.Candela);

var m = UnitAbbreviationsCache.Default.GetDefaultAbbreviation(LengthUnit.Meter);
var kg = UnitAbbreviationsCache.Default.GetDefaultAbbreviation(MassUnit.Kilogram);
var s = UnitAbbreviationsCache.Default.GetDefaultAbbreviation(DurationUnit.Second);
var A = UnitAbbreviationsCache.Default.GetDefaultAbbreviation(ElectricCurrentUnit.Ampere);
var K = UnitAbbreviationsCache.Default.GetDefaultAbbreviation(TemperatureUnit.Kelvin);
var mol = UnitAbbreviationsCache.Default.GetDefaultAbbreviation(AmountOfSubstanceUnit.Mole);
var cd = UnitAbbreviationsCache.Default.GetDefaultAbbreviation(LuminousIntensityUnit.Candela);
UnitAbbreviationsCache cache = UnitsNetSetup.Default.UnitAbbreviations;
var m = cache.GetDefaultAbbreviation(LengthUnit.Meter);
var kg = cache.GetDefaultAbbreviation(MassUnit.Kilogram);
var s = cache.GetDefaultAbbreviation(DurationUnit.Second);
var A = cache.GetDefaultAbbreviation(ElectricCurrentUnit.Ampere);
var K = cache.GetDefaultAbbreviation(TemperatureUnit.Kelvin);
var mol = cache.GetDefaultAbbreviation(AmountOfSubstanceUnit.Mole);
var cd = cache.GetDefaultAbbreviation(LuminousIntensityUnit.Candela);

Assert.Equal($"[Length]: {m}, [Mass]: {kg}, [Time]: {s}, [Current]: {A}, [Temperature]: {K}, [Amount]: {mol}, [LuminousIntensity]: {cd}", siBaseUnits.ToString());
}
Expand Down
9 changes: 5 additions & 4 deletions UnitsNet.Tests/QuantityIFormattableTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ public void EmptyOrNullFormatStringEqualsGFormat()
[Fact]
public void AFormatGetsAbbreviations()
{
Assert.Equal(UnitAbbreviationsCache.Default.GetDefaultAbbreviation(MyLength.Unit, CultureInfo.InvariantCulture), MyLength.ToString("a", CultureInfo.InvariantCulture));
Assert.Equal(UnitAbbreviationsCache.Default.GetDefaultAbbreviation(MyLength.Unit, CultureInfo.InvariantCulture), MyLength.ToString("a0", CultureInfo.InvariantCulture));
UnitAbbreviationsCache cache = UnitsNetSetup.Default.UnitAbbreviations;
Assert.Equal(cache.GetDefaultAbbreviation(MyLength.Unit, CultureInfo.InvariantCulture), MyLength.ToString("a", CultureInfo.InvariantCulture));
Assert.Equal(cache.GetDefaultAbbreviation(MyLength.Unit, CultureInfo.InvariantCulture), MyLength.ToString("a0", CultureInfo.InvariantCulture));

Assert.Equal(UnitAbbreviationsCache.Default.GetUnitAbbreviations(MyLength.Unit, CultureInfo.InvariantCulture)[1], MyLength.ToString("a1", CultureInfo.InvariantCulture));
Assert.Equal(UnitAbbreviationsCache.Default.GetUnitAbbreviations(MyLength.Unit, CultureInfo.InvariantCulture)[2], MyLength.ToString("a2", CultureInfo.InvariantCulture));
Assert.Equal(cache.GetUnitAbbreviations(MyLength.Unit, CultureInfo.InvariantCulture)[1], MyLength.ToString("a1", CultureInfo.InvariantCulture));
Assert.Equal(cache.GetUnitAbbreviations(MyLength.Unit, CultureInfo.InvariantCulture)[2], MyLength.ToString("a2", CultureInfo.InvariantCulture));
}

[Fact]
Expand Down
18 changes: 18 additions & 0 deletions UnitsNet.Tests/UnitAbbreviationsCacheTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,24 @@ public void GetDefaultAbbreviationFallsBackToUsEnglishCulture()
Assert.Equal("US english abbreviation for Unit1", abbreviation);
}

[Fact]
public void MapUnitToAbbreviation_DoesNotAffectOtherCacheInstances()
{
var culture = AmericanCulture;
var unit = AreaUnit.SquareMeter;

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

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

Assert.Equal(new[] { "m²", "m^2" }, cache1.GetUnitAbbreviations(unit, culture));
Assert.Equal(new[] { "m²", "m2" }, cache2.GetUnitAbbreviations(unit, culture));
Assert.Equal("m²", cache1.GetDefaultAbbreviation(unit, culture));
Assert.Equal("m²", cache2.GetDefaultAbbreviation(unit, culture));
}

[Fact]
public void MapUnitToAbbreviation_AddCustomUnit_DoesNotOverrideDefaultAbbreviationForAlreadyMappedUnits()
{
Expand Down
10 changes: 1 addition & 9 deletions UnitsNet/CustomCode/Quantity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,7 @@ namespace UnitsNet
{
public partial class Quantity
{
static Quantity()
{
Default = new QuantityInfoLookup();
}

private static QuantityInfoLookup Default
{
get;
}
private static QuantityInfoLookup Default => UnitsNetSetup.Default.QuantityInfoLookup;

/// <summary>
/// All enum value names of <see cref="Infos"/>, such as "Length" and "Mass".
Expand Down
8 changes: 2 additions & 6 deletions UnitsNet/CustomCode/QuantityParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public class QuantityParser
/// <summary>
/// The default instance of <see cref="QuantityParser"/>, which uses <see cref="UnitAbbreviationsCache.Default"/> unit abbreviations.
/// </summary>
public static QuantityParser Default { get; }
[Obsolete("Use UnitsNetSetup.Default.QuantityParser instead.")]
public static QuantityParser Default => UnitsNetSetup.Default.QuantityParser;

/// <summary>
/// Creates an instance of <see cref="QuantityParser"/>, optionally specifying an <see cref="UnitAbbreviationsCache"/>
Expand All @@ -50,11 +51,6 @@ public QuantityParser(UnitAbbreviationsCache? unitAbbreviationsCache = null)
_unitParser = new UnitParser(_unitAbbreviationsCache);
}

static QuantityParser()
{
Default = new QuantityParser(UnitAbbreviationsCache.Default);
}

/// <summary>
/// Parses a quantity from a string, such as "1.2 kg" to <see cref="Length"/> or "100 cm" to <see cref="Mass"/>.
/// </summary>
Expand Down
Loading