Skip to content

Commit d89306b

Browse files
authored
Use HowMuchUnit instead of Enum in HowMuch quantity (#850)
2 parents f0ba50c + 01d0108 commit d89306b

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

UnitsNet.Tests/CustomQuantities/HowMuch.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,18 @@ namespace UnitsNet.Tests.CustomQuantities
88
/// </summary>
99
public struct HowMuch : IQuantity
1010
{
11-
public HowMuch(double value, Enum unit) : this()
11+
public HowMuch(double value, HowMuchUnit unit)
1212
{
1313
Unit = unit;
1414
Value = value;
1515
}
1616

17-
public Enum Unit { get; }
17+
Enum IQuantity.Unit => Unit;
18+
public HowMuchUnit Unit { get; }
19+
1820
public double Value { get; }
1921

20-
#region Crud to satisfy IQuantity, but not really used for anything
22+
#region IQuantity
2123

2224
private static readonly HowMuch Zero = new HowMuch(0, HowMuchUnit.Some);
2325

@@ -39,10 +41,15 @@ public HowMuch(double value, Enum unit) : this()
3941

4042
public double As(UnitSystem unitSystem) => throw new NotImplementedException();
4143

42-
public IQuantity ToUnit(Enum unit) => new HowMuch(As(unit), unit);
44+
public IQuantity ToUnit(Enum unit)
45+
{
46+
if (unit is HowMuchUnit howMuchUnit) return new HowMuch(As(unit), howMuchUnit);
47+
throw new ArgumentException("Must be of type HowMuchUnit.", nameof(unit));
48+
}
4349

4450
public IQuantity ToUnit(UnitSystem unitSystem) => throw new NotImplementedException();
4551

52+
public override string ToString() => $"{Value} {Unit}";
4653
public string ToString(string format, IFormatProvider formatProvider) => $"HowMuch ({format}, {formatProvider})";
4754
public string ToString(IFormatProvider provider) => $"HowMuch ({provider})";
4855
public string ToString(IFormatProvider provider, int significantDigitsAfterRadix) => $"HowMuch ({provider}, {significantDigitsAfterRadix})";

UnitsNet.Tests/UnitConverterTest.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Licensed under MIT No Attribution, see LICENSE file at the root.
22
// Copyright 2013 Andreas Gullberg Larsen ([email protected]). Maintained at https://github.com/angularsen/UnitsNet.
33

4-
using System;
54
using UnitsNet.Tests.CustomQuantities;
65
using UnitsNet.Units;
76
using Xunit;
@@ -96,7 +95,7 @@ public void ConversionToSameUnit_ReturnsSameQuantity()
9695
[InlineData(1, HowMuchUnit.Some, HowMuchUnit.Some, 1)]
9796
[InlineData(1, HowMuchUnit.Some, HowMuchUnit.ATon, 2)]
9897
[InlineData(1, HowMuchUnit.Some, HowMuchUnit.AShitTon, 10)]
99-
public void ConversionForUnitsOfCustomQuantity(double fromValue, Enum fromUnit, Enum toUnit, double expectedValue)
98+
public void ConversionForUnitsOfCustomQuantity(double fromValue, HowMuchUnit fromUnit, HowMuchUnit toUnit, double expectedValue)
10099
{
101100
// Intentionally don't map conversion Some->Some, it is not necessary
102101
var unitConverter = new UnitConverter();

0 commit comments

Comments
 (0)