Skip to content
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
4 changes: 2 additions & 2 deletions CodeGen/Generators/UnitsNetGen/QuantityGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ public static bool TryParse([NotNullWhen(true)]string? str, IFormatProvider? pro
/// <exception cref=""UnitsNetException"">Error parsing string.</exception>
public static {_unitEnumName} ParseUnit(string str, IFormatProvider? provider)
{{
return UnitsNetSetup.Default.UnitParser.Parse<{_unitEnumName}>(str, provider);
return UnitParser.Default.Parse(str, Info.UnitInfos, provider).Value;
}}

/// <inheritdoc cref=""TryParseUnit(string,IFormatProvider,out UnitsNet.Units.{_unitEnumName})""/>
Expand All @@ -586,7 +586,7 @@ public static bool TryParseUnit([NotNullWhen(true)]string? str, out {_unitEnumNa
/// <param name=""provider"">Format to use when parsing number and unit. Defaults to <see cref=""CultureInfo.CurrentCulture"" /> if null.</param>
public static bool TryParseUnit([NotNullWhen(true)]string? str, IFormatProvider? provider, out {_unitEnumName} unit)
{{
return UnitsNetSetup.Default.UnitParser.TryParse<{_unitEnumName}>(str, provider, out unit);
return UnitParser.Default.TryParse(str, Info, provider, out unit);
}}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,25 +47,4 @@ public string WithSpecificQuantityAndCustomMapping()
cache.MapUnitToDefaultAbbreviation(MassUnit.Gram, "zz");
return cache.GetDefaultAbbreviation(MassUnit.Gram);
}

[Benchmark]
public string DefaultWithoutLookup()
{
var cache = UnitAbbreviationsCache.CreateDefault();
return cache.GetAbbreviations(Mass.Info.BaseUnitInfo)[0];
}

[Benchmark]
public string EmptyWithoutLookup()
{
var cache = new UnitAbbreviationsCache();
return cache.GetAbbreviations(Mass.Info.BaseUnitInfo)[0];
}

[Benchmark]
public string WithSpecificQuantityWithoutLookup()
{
var cache = new UnitAbbreviationsCache([Mass.Info]);
return cache.GetAbbreviations(Mass.Info.BaseUnitInfo)[0];
}
}
7 changes: 6 additions & 1 deletion UnitsNet.Tests/CustomQuantities/HowMuch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public HowMuch(double value, HowMuchUnit unit)

public BaseDimensions Dimensions => BaseDimensions.Dimensionless;

public QuantityInfo QuantityInfo => new(
public static QuantityInfo Info = new(
nameof(HowMuch),
typeof(HowMuchUnit),
new UnitInfo[]
Expand All @@ -40,6 +40,11 @@ public HowMuch(double value, HowMuchUnit unit)
Zero,
BaseDimensions.Dimensionless);

QuantityInfo IQuantity.QuantityInfo
{
get { return Info; }
}

public double As(Enum unit) => Convert.ToDouble(unit);

public double As(UnitSystem unitSystem) => throw new NotImplementedException();
Expand Down
20 changes: 10 additions & 10 deletions UnitsNet.Tests/QuantityParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ public class QuantityParserTests
[Fact]
public void Parse_WithSingleCaseInsensitiveMatch_ParsesWithMatchedUnit()
{
var unitAbbreviationsCache = new UnitAbbreviationsCache();
var unitAbbreviationsCache = new UnitAbbreviationsCache([HowMuch.Info]);
unitAbbreviationsCache.MapUnitToAbbreviation(HowMuchUnit.Some, "foo");
var quantityParser = new QuantityParser(unitAbbreviationsCache);

HowMuch q = quantityParser.Parse<HowMuch, HowMuchUnit>("1 FOO",
null,
(value, unit) => new HowMuch((double) value, unit));
(value, unit) => new HowMuch(value, unit));

Assert.Equal(HowMuchUnit.Some, q.Unit);
Assert.Equal(1, q.Value);
Expand All @@ -28,14 +28,14 @@ public void Parse_WithSingleCaseInsensitiveMatch_ParsesWithMatchedUnit()
[Fact]
public void Parse_WithOneCaseInsensitiveMatchAndOneExactMatch_ParsesWithTheExactMatchUnit()
{
var unitAbbreviationsCache = new UnitAbbreviationsCache();
var unitAbbreviationsCache = new UnitAbbreviationsCache([HowMuch.Info]);
unitAbbreviationsCache.MapUnitToAbbreviation(HowMuchUnit.Some, "foo");
unitAbbreviationsCache.MapUnitToAbbreviation(HowMuchUnit.ATon, "FOO");
var quantityParser = new QuantityParser(unitAbbreviationsCache);

HowMuch q = quantityParser.Parse<HowMuch, HowMuchUnit>("1 FOO",
null,
(value, unit) => new HowMuch((double) value, unit));
(value, unit) => new HowMuch(value, unit));

Assert.Equal(HowMuchUnit.ATon, q.Unit);
Assert.Equal(1, q.Value);
Expand All @@ -44,14 +44,14 @@ public void Parse_WithOneCaseInsensitiveMatchAndOneExactMatch_ParsesWithTheExact
[Fact]
public void Parse_WithMultipleCaseInsensitiveMatchesButNoExactMatches_ThrowsAmbiguousUnitParseException()
{
var unitAbbreviationsCache = new UnitAbbreviationsCache();
var unitAbbreviationsCache = new UnitAbbreviationsCache([HowMuch.Info]);
unitAbbreviationsCache.MapUnitToAbbreviation(HowMuchUnit.Some, "foo");
unitAbbreviationsCache.MapUnitToAbbreviation(HowMuchUnit.ATon, "FOO");
var quantityParser = new QuantityParser(unitAbbreviationsCache);

void Act()
{
quantityParser.Parse<HowMuch, HowMuchUnit>("1 Foo", null, (value, unit) => new HowMuch((double) value, unit));
quantityParser.Parse<HowMuch, HowMuchUnit>("1 Foo", null, (value, unit) => new HowMuch(value, unit));
}

var ex = Assert.Throws<AmbiguousUnitParseException>(Act);
Expand All @@ -61,13 +61,13 @@ void Act()
[Fact]
public void Parse_MappedCustomUnit()
{
var unitAbbreviationsCache = new UnitAbbreviationsCache();
var unitAbbreviationsCache = new UnitAbbreviationsCache([HowMuch.Info]);
unitAbbreviationsCache.MapUnitToAbbreviation(HowMuchUnit.Some, "fooh");
var quantityParser = new QuantityParser(unitAbbreviationsCache);

HowMuch q = quantityParser.Parse<HowMuch, HowMuchUnit>("1 fooh",
null,
(value, unit) => new HowMuch((double) value, unit));
(value, unit) => new HowMuch(value, unit));

Assert.Equal(HowMuchUnit.Some, q.Unit);
Assert.Equal(1, q.Value);
Expand All @@ -76,13 +76,13 @@ public void Parse_MappedCustomUnit()
[Fact]
public void TryParse_MappedCustomUnit()
{
var unitAbbreviationsCache = new UnitAbbreviationsCache();
var unitAbbreviationsCache = new UnitAbbreviationsCache([HowMuch.Info]);
unitAbbreviationsCache.MapUnitToAbbreviation(HowMuchUnit.Some, "fooh");
var quantityParser = new QuantityParser(unitAbbreviationsCache);

bool success = quantityParser.TryParse<HowMuch, HowMuchUnit>("1 fooh",
null,
(value, unit) => new HowMuch((double) value, unit),
(value, unit) => new HowMuch(value, unit),
out HowMuch q);

Assert.True(success);
Expand Down
22 changes: 12 additions & 10 deletions UnitsNet.Tests/QuantityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,21 +174,23 @@ public void FromUnitAbbreviation_MatchingCulture_ReturnsQuantity()
}

[Fact]
public void TryFromUnitAbbreviation_MatchingCulture_ReturnsQuantity()
public void FromUnitAbbreviation_MatchingFallbackCulture_ReturnsQuantity()
{
Assert.False(Quantity.TryFromUnitAbbreviation(Russian, 5, "cm", out IQuantity? q));
// The localized abbreviation is "см"
IQuantity quantity = Quantity.FromUnitAbbreviation(Russian, 5, "cm");
Assert.Equal(5, quantity.Value);
Assert.Equal(LengthUnit.Centimeter, quantity.Unit);
}

[Fact]
public void FromUnitAbbreviation_MismatchingCulture_ThrowsUnitNotFoundException()
public void TryFromUnitAbbreviation_MatchingFallbackCulture_ReturnsQuantity()
{
Assert.Throws<UnitNotFoundException>(() => Quantity.FromUnitAbbreviation(Russian, 5, "cm")); // Expected "см"
}

[Fact]
public void TryFromUnitAbbreviation_MismatchingCulture_ThrowsUnitNotFoundException()
{
Assert.Throws<UnitNotFoundException>(() => Quantity.FromUnitAbbreviation(Russian, 5, "cm")); // Expected "см"
// The localized abbreviation is "см"
var success = Quantity.TryFromUnitAbbreviation(Russian, 5, "cm", out IQuantity? quantity);
Assert.True(success);
Assert.NotNull(quantity);
Assert.Equal(5, quantity.Value);
Assert.Equal(LengthUnit.Centimeter, quantity.Unit);
}

[Fact]
Expand Down
Loading