Skip to content

Commit df6eafb

Browse files
authored
Improve test coverage of quantity methods with unit system parameter (#844)
1 parent d4f05cb commit df6eafb

File tree

211 files changed

+4545
-469
lines changed

Some content is hidden

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

211 files changed

+4545
-469
lines changed

CodeGen/Generators/UnitsNetGen/UnitTestBaseClassGenerator.cs

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public override string Generate()
7474
using System.Globalization;
7575
using System.Linq;
7676
using System.Threading;
77+
using UnitsNet.Tests.TestsBase;
7778
using UnitsNet.Units;
7879
using Xunit;
7980
@@ -87,7 +88,7 @@ namespace UnitsNet.Tests
8788
/// Test of {_quantity.Name}.
8889
/// </summary>
8990
// ReSharper disable once PartialTypeWithSinglePart
90-
public abstract partial class {_quantity.Name}TestsBase
91+
public abstract partial class {_quantity.Name}TestsBase : QuantityTestsBase
9192
{{");
9293
foreach (var unit in _quantity.Units) Writer.WL($@"
9394
protected abstract double {unit.PluralName}InOne{_baseUnit.SingularName} {{ get; }}");
@@ -127,13 +128,28 @@ public void Ctor_WithNaNValue_ThrowsArgumentException()
127128
{{
128129
Assert.Throws<ArgumentException>(() => new {_quantity.Name}(double.NaN, {_baseUnitFullName}));
129130
}}
131+
"); Writer.WL($@"
130132
131133
[Fact]
132134
public void Ctor_NullAsUnitSystem_ThrowsArgumentNullException()
133135
{{
134-
Assert.Throws<ArgumentNullException>(() => new {_quantity.Name}(value: 1.0, unitSystem: null));
136+
Assert.Throws<ArgumentNullException>(() => new {_quantity.Name}(value: 1, unitSystem: null));
137+
}}
138+
139+
[Fact]
140+
public void Ctor_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
141+
{{
142+
Func<object> TestCode = () => new {_quantity.Name}(value: 1, unitSystem: UnitSystem.SI);
143+
if (SupportsSIUnitSystem)
144+
{{
145+
var quantity = ({_quantity.Name}) TestCode();
146+
Assert.Equal(1, quantity.Value);
147+
}}
148+
else
149+
{{
150+
Assert.Throws<ArgumentException>(TestCode);
151+
}}
135152
}}
136-
"); Writer.WL($@"
137153
138154
[Fact]
139155
public void {_quantity.Name}_QuantityInfo_ReturnsQuantityInfoDescribingQuantity()
@@ -207,6 +223,23 @@ public void As()
207223
Writer.WL($@"
208224
}}
209225
226+
[Fact]
227+
public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
228+
{{
229+
var quantity = new {_quantity.Name}(value: 1, unit: {_quantity.Name}.BaseUnit);
230+
Func<object> AsWithSIUnitSystem = () => quantity.As(UnitSystem.SI);
231+
232+
if (SupportsSIUnitSystem)
233+
{{
234+
var value = (double) AsWithSIUnitSystem();
235+
Assert.Equal(1, value);
236+
}}
237+
else
238+
{{
239+
Assert.Throws<ArgumentException>(AsWithSIUnitSystem);
240+
}}
241+
}}
242+
210243
[Fact]
211244
public void ToUnit()
212245
{{
@@ -224,6 +257,14 @@ public void ToUnit()
224257
Writer.WL($@"
225258
}}
226259
260+
[Fact]
261+
public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
262+
{{
263+
var quantityInBaseUnit = {_quantity.Name}.From{_baseUnit.PluralName}(1).ToBaseUnit();
264+
Assert.Equal({_quantity.Name}.BaseUnit, quantityInBaseUnit.Unit);");
265+
Writer.WL($@"
266+
}}
267+
227268
[Fact]
228269
public void ConversionRoundTrip()
229270
{{
@@ -657,11 +698,10 @@ public void NegationOperator_ReturnsQuantity_WithNegatedValue(double value)
657698
{{
658699
var quantity = {_quantity.Name}.From{_baseUnit.PluralName}(value);
659700
Assert.Equal({_quantity.Name}.From{_baseUnit.PluralName}(-value), -quantity);
660-
}}
661-
");
701+
}}");
662702
}
663703

664-
Writer.WL( $@"
704+
Writer.WL($@"
665705
}}
666706
}}" );
667707
return Writer.ToString();

Common/UnitDefinitions/Volume.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
"FromUnitToBaseFunc": "x",
3030
"FromBaseToUnitFunc": "x",
3131
"Prefixes": [ "Hecto", "Kilo" ],
32+
"BaseUnits": {
33+
"L": "Meter"
34+
},
3235
"Localization": [
3336
{
3437
"Culture": "en-US",

UnitsNet.Tests/CustomCode/AccelerationTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ namespace UnitsNet.Tests.CustomCode
77
{
88
public class AccelerationTests : AccelerationTestsBase
99
{
10+
protected override bool SupportsSIUnitSystem => true;
11+
1012
protected override double KilometersPerSecondSquaredInOneMeterPerSecondSquared => 1E-3;
1113

1214
protected override double MetersPerSecondSquaredInOneMeterPerSecondSquared => 1;

UnitsNet.Tests/CustomCode/AmountOfSubstanceTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ namespace UnitsNet.Tests.CustomCode
2828
{
2929
public class AmountOfSubstanceTests : AmountOfSubstanceTestsBase
3030
{
31+
protected override bool SupportsSIUnitSystem => true;
32+
3133
protected override double CentimolesInOneMole => 1e2;
3234
protected override double CentipoundMolesInOneMole => 0.002204622621848776 * 1e2;
3335
protected override double DecimolesInOneMole => 1e1;
@@ -43,7 +45,7 @@ public class AmountOfSubstanceTests : AmountOfSubstanceTestsBase
4345
protected override double NanopoundMolesInOneMole => 0.002204622621848776 * 1e9;
4446
protected override double PoundMolesInOneMole => 0.002204622621848776;
4547
protected override double MegamolesInOneMole => 1e-6;
46-
48+
4749
[Fact]
4850
public void NumberOfParticlesInOneMoleEqualsAvogadroConstant()
4951
{
@@ -71,7 +73,7 @@ public void MassFromAmountOfSubstanceAndMolarMass(
7173
{
7274
AmountOfSubstance amountOfSubstance = new AmountOfSubstance(amountOfSubstanceValue, amountOfSubstanceUnit);
7375
MolarMass molarMass = new MolarMass(molarMassValue, molarMassUnit);
74-
76+
7577
Mass mass = amountOfSubstance * molarMass;
7678

7779
AssertEx.EqualTolerance(expectedMass, mass.As(expectedMassUnit), tolerence);

UnitsNet.Tests/CustomCode/AmplitudeRatioTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace UnitsNet.Tests.CustomCode
88
{
99
public class AmplitudeRatioTests : AmplitudeRatioTestsBase
1010
{
11+
protected override bool SupportsSIUnitSystem => false;
1112
protected override double DecibelMicrovoltsInOneDecibelVolt => 121;
1213

1314
protected override double DecibelMillivoltsInOneDecibelVolt => 61;

UnitsNet.Tests/CustomCode/AngleTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ namespace UnitsNet.Tests.CustomCode
88
{
99
public class AngleTests : AngleTestsBase
1010
{
11+
protected override bool SupportsSIUnitSystem => false;
12+
1113
protected override double DegreesInOneDegree => 1;
1214

1315
protected override double GradiansInOneDegree => 400 / 360.0;

UnitsNet.Tests/CustomCode/ApparentEnergyTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ namespace UnitsNet.Tests.CustomCode
2727
{
2828
public class ApparentEnergyTests : ApparentEnergyTestsBase
2929
{
30+
protected override bool SupportsSIUnitSystem => false;
31+
3032
protected override double VoltampereHoursInOneVoltampereHour => 1;
3133

3234
protected override double KilovoltampereHoursInOneVoltampereHour => 1E-3;

UnitsNet.Tests/CustomCode/ApparentPowerTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ namespace UnitsNet.Tests.CustomCode
2727
{
2828
public class ApparentPowerTests : ApparentPowerTestsBase
2929
{
30+
protected override bool SupportsSIUnitSystem => false;
31+
3032
protected override double VoltamperesInOneVoltampere => 1;
3133

3234
protected override double KilovoltamperesInOneVoltampere => 1E-3;

UnitsNet.Tests/CustomCode/AreaDensityTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ namespace UnitsNet.Tests.CustomCode
2727
{
2828
public class AreaDensityTests : AreaDensityTestsBase
2929
{
30+
protected override bool SupportsSIUnitSystem => true;
31+
3032
protected override double KilogramsPerSquareMeterInOneKilogramPerSquareMeter => 1;
3133
}
3234
}

UnitsNet.Tests/CustomCode/AreaMomentOfInertiaTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ namespace UnitsNet.Tests.CustomCode
2828
{
2929
public class AreaMomentOfInertiaTests : AreaMomentOfInertiaTestsBase
3030
{
31+
protected override bool SupportsSIUnitSystem => true;
32+
3133
protected override double CentimetersToTheFourthInOneMeterToTheFourth => 1e8;
3234

3335
protected override double DecimetersToTheFourthInOneMeterToTheFourth => 1e4;

0 commit comments

Comments
 (0)