Skip to content

Commit 3ecea2a

Browse files
tmilnthorpangularsen
authored andcommitted
Add As/ToUnit conversions to UnitSystem (#622)
* Add As/ToUnit conversions to UnitSystem
1 parent 2e1c12e commit 3ecea2a

File tree

100 files changed

+2527
-28
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+2527
-28
lines changed

UnitsNet.Tests/BaseUnitsTests.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,5 +136,45 @@ public void ToStringGivesExpectedResult()
136136

137137
Assert.Equal("[Length]: m, [Mass]: kg, [Time]: s, [Current]: A, [Temperature]: K, [Amount]: mol, [LuminousIntensity]: cd", siBaseUnits.ToString());
138138
}
139+
140+
[Fact]
141+
public void IsFullyDefined_TrueIfAllBaseUnitDefined()
142+
{
143+
Assert.True(new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second,
144+
ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole,
145+
LuminousIntensityUnit.Candela).IsFullyDefined);
146+
}
147+
148+
[Fact]
149+
public void IsFullyDefined_FalseIfAnyBaseUnitIsUndefined()
150+
{
151+
Assert.False(new BaseUnits(mass: MassUnit.Kilogram, time: DurationUnit.Second,
152+
current: ElectricCurrentUnit.Ampere, temperature: TemperatureUnit.Kelvin, amount: AmountOfSubstanceUnit.Mole,
153+
luminousIntensity: LuminousIntensityUnit.Candela).IsFullyDefined);
154+
155+
Assert.False(new BaseUnits(length: LengthUnit.Meter, time: DurationUnit.Second,
156+
current: ElectricCurrentUnit.Ampere, temperature: TemperatureUnit.Kelvin, amount: AmountOfSubstanceUnit.Mole,
157+
luminousIntensity: LuminousIntensityUnit.Candela).IsFullyDefined);
158+
159+
Assert.False(new BaseUnits(length: LengthUnit.Meter, mass: MassUnit.Kilogram,
160+
current: ElectricCurrentUnit.Ampere, temperature: TemperatureUnit.Kelvin, amount: AmountOfSubstanceUnit.Mole,
161+
luminousIntensity: LuminousIntensityUnit.Candela).IsFullyDefined);
162+
163+
Assert.False(new BaseUnits(length: LengthUnit.Meter, mass: MassUnit.Kilogram, time: DurationUnit.Second,
164+
temperature: TemperatureUnit.Kelvin, amount: AmountOfSubstanceUnit.Mole,
165+
luminousIntensity: LuminousIntensityUnit.Candela).IsFullyDefined);
166+
167+
Assert.False(new BaseUnits(length: LengthUnit.Meter, mass: MassUnit.Kilogram, time: DurationUnit.Second,
168+
current: ElectricCurrentUnit.Ampere, amount: AmountOfSubstanceUnit.Mole,
169+
luminousIntensity: LuminousIntensityUnit.Candela).IsFullyDefined);
170+
171+
Assert.False(new BaseUnits(length: LengthUnit.Meter, mass: MassUnit.Kilogram, time: DurationUnit.Second,
172+
current: ElectricCurrentUnit.Ampere, temperature: TemperatureUnit.Kelvin,
173+
luminousIntensity: LuminousIntensityUnit.Candela).IsFullyDefined);
174+
175+
Assert.False(new BaseUnits(length: LengthUnit.Meter, mass: MassUnit.Kilogram, time: DurationUnit.Second,
176+
current: ElectricCurrentUnit.Ampere, temperature: TemperatureUnit.Kelvin, amount: AmountOfSubstanceUnit.Mole
177+
).IsFullyDefined);
178+
}
139179
}
140180
}

UnitsNet.Tests/CustomCode/AreaTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,19 @@ public void Constructor_UnitSystemSI_AssignsSIUnit()
9090
var area = new Area(1.0, UnitSystem.SI);
9191
Assert.Equal(AreaUnit.SquareMeter, area.Unit);
9292
}
93+
94+
[Fact]
95+
public void As_ImperialGivenSIUnitSystem_ReturnsSIValue()
96+
{
97+
var imperialArea = new Area(2.0, AreaUnit.SquareInch);
98+
Assert.Equal(0.00129032, imperialArea.As(UnitSystem.SI));
99+
}
100+
101+
[Fact]
102+
public void ToUnit_ImperialGivenSIUnitSystem_ReturnsSIValue()
103+
{
104+
var imperialArea = new Area(2.0, AreaUnit.SquareInch);
105+
Assert.Equal(Area.FromSquareMeters(0.00129032), imperialArea.ToUnit(UnitSystem.SI));
106+
}
93107
}
94108
}

UnitsNet.Tests/CustomCode/EnergyTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,19 @@ public void Constructor_UnitSystemSI_AssignsSIUnit()
6060
var energy = new Energy(1.0, UnitSystem.SI);
6161
Assert.Equal(EnergyUnit.Joule, energy.Unit);
6262
}
63+
64+
[Fact]
65+
public void As_ImperialQuantityGivenSIUnitSystem_ReturnsSIValue()
66+
{
67+
var imperialEnergy = new Energy(2.0, EnergyUnit.BritishThermalUnit);
68+
Assert.Equal(2110.11170524, imperialEnergy.As(UnitSystem.SI));
69+
}
70+
71+
[Fact]
72+
public void ToUnit_ImperialQuantityGivenSIUnitSystem_ReturnsSIValue()
73+
{
74+
var imperialEnergy = new Energy(2.0, EnergyUnit.BritishThermalUnit);
75+
Assert.Equal(Energy.FromJoules(2110.11170524), imperialEnergy.ToUnit(UnitSystem.SI));
76+
}
6377
}
6478
}

UnitsNet.Tests/CustomCode/LengthTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,5 +165,19 @@ public void Constructor_UnitSystemSI_AssignsSIUnit()
165165
var length = new Length(1.0, UnitSystem.SI);
166166
Assert.Equal(LengthUnit.Meter, length.Unit);
167167
}
168+
169+
[Fact]
170+
public void As_ImperialQuantityGivenSIUnitSystem_ReturnsSIValue()
171+
{
172+
var imperialLength = new Length(2.0, LengthUnit.Inch);
173+
Assert.Equal(0.0508, imperialLength.As(UnitSystem.SI));
174+
}
175+
176+
[Fact]
177+
public void ToUnit_ImperialQuantityGivenSIUnitSystem_ReturnsSIValue()
178+
{
179+
var imperialLength = new Length(2.0, LengthUnit.Inch);
180+
Assert.Equal(Length.FromMeters(0.0508), imperialLength.ToUnit(UnitSystem.SI));
181+
}
168182
}
169183
}

UnitsNet.Tests/IQuantityTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,39 @@ public void As_GivenWrongUnitType_ThrowsArgumentException()
1616
Assert.Throws<ArgumentException>(() => length.As(MassUnit.Kilogram));
1717
}
1818

19+
[Fact]
20+
public void As_GivenNullUnitSystem_ThrowsArgumentNullException()
21+
{
22+
IQuantity imperialLengthQuantity = new Length(2.0, LengthUnit.Inch);
23+
Assert.Throws<ArgumentNullException>(() => imperialLengthQuantity.As((UnitSystem)null));
24+
}
25+
26+
[Fact]
27+
public void As_ImperialQuantityGivenSIUnitSystem_ReturnsSIValue()
28+
{
29+
IQuantity imperialLengthQuantity = new Length(2.0, LengthUnit.Inch);
30+
Assert.Equal(0.0508, imperialLengthQuantity.As(UnitSystem.SI));
31+
}
32+
1933
[Fact]
2034
public void ToUnit_GivenWrongUnitType_ThrowsArgumentException()
2135
{
2236
IQuantity length = Length.FromMeters(1.2345);
2337
Assert.Throws<ArgumentException>(() => length.ToUnit(MassUnit.Kilogram));
2438
}
39+
40+
[Fact]
41+
public void ToUnit_GivenNullUnitSystem_ThrowsArgumentNullException()
42+
{
43+
IQuantity imperialLengthQuantity = new Length(2.0, LengthUnit.Inch);
44+
Assert.Throws<ArgumentNullException>(() => imperialLengthQuantity.ToUnit((UnitSystem)null));
45+
}
46+
47+
[Fact]
48+
public void ToUnit_ImperialQuantityGivenSIUnitSystem_ReturnsSIValue()
49+
{
50+
IQuantity imperialLengthQuantity = new Length(2.0, LengthUnit.Inch);
51+
Assert.Equal(Length.FromMeters(0.0508), imperialLengthQuantity.ToUnit(UnitSystem.SI));
52+
}
2553
}
2654
}

UnitsNet/BaseUnits.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ public BaseUnits(
4646
Temperature = temperature;
4747
Amount = amount;
4848
LuminousIntensity = luminousIntensity;
49+
50+
IsFullyDefined = Length != LengthUnit.Undefined &&
51+
Mass != MassUnit.Undefined &&
52+
Time != DurationUnit.Undefined &&
53+
Current != ElectricCurrentUnit.Undefined &&
54+
Temperature != TemperatureUnit.Undefined &&
55+
Amount != AmountOfSubstanceUnit.Undefined &&
56+
LuminousIntensity != LuminousIntensityUnit.Undefined;
4957
}
5058

5159
/// <inheritdoc />
@@ -181,5 +189,10 @@ public override string ToString()
181189
/// Gets the luminous intensity unit (J).
182190
/// </summary>
183191
public LuminousIntensityUnit LuminousIntensity{ get; }
192+
193+
/// <summary>
194+
/// Gets whether or not all of the base units are defined.
195+
/// </summary>
196+
public bool IsFullyDefined { get; }
184197
}
185198
}

UnitsNet/GeneratedCode/Quantities/Acceleration.NetFramework.g.cs

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnitsNet/GeneratedCode/Quantities/AmountOfSubstance.NetFramework.g.cs

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnitsNet/GeneratedCode/Quantities/AmplitudeRatio.NetFramework.g.cs

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnitsNet/GeneratedCode/Quantities/Angle.NetFramework.g.cs

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)