Skip to content

Improve test coverage of quantity methods with unit system parameter #844

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

Merged
merged 6 commits into from
Nov 13, 2020
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
52 changes: 46 additions & 6 deletions CodeGen/Generators/UnitsNetGen/UnitTestBaseClassGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public override string Generate()
using System.Globalization;
using System.Linq;
using System.Threading;
using UnitsNet.Tests.TestsBase;
using UnitsNet.Units;
using Xunit;

Expand All @@ -87,7 +88,7 @@ namespace UnitsNet.Tests
/// Test of {_quantity.Name}.
/// </summary>
// ReSharper disable once PartialTypeWithSinglePart
public abstract partial class {_quantity.Name}TestsBase
public abstract partial class {_quantity.Name}TestsBase : QuantityTestsBase
{{");
foreach (var unit in _quantity.Units) Writer.WL($@"
protected abstract double {unit.PluralName}InOne{_baseUnit.SingularName} {{ get; }}");
Expand Down Expand Up @@ -127,13 +128,28 @@ public void Ctor_WithNaNValue_ThrowsArgumentException()
{{
Assert.Throws<ArgumentException>(() => new {_quantity.Name}(double.NaN, {_baseUnitFullName}));
}}
"); Writer.WL($@"

[Fact]
public void Ctor_NullAsUnitSystem_ThrowsArgumentNullException()
{{
Assert.Throws<ArgumentNullException>(() => new {_quantity.Name}(value: 1.0, unitSystem: null));
Assert.Throws<ArgumentNullException>(() => new {_quantity.Name}(value: 1, unitSystem: null));
}}

[Fact]
public void Ctor_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
{{
Func<object> TestCode = () => new {_quantity.Name}(value: 1, unitSystem: UnitSystem.SI);
if (SupportsSIUnitSystem)
{{
var quantity = ({_quantity.Name}) TestCode();
Assert.Equal(1, quantity.Value);
}}
else
{{
Assert.Throws<ArgumentException>(TestCode);
}}
}}
"); Writer.WL($@"

[Fact]
public void {_quantity.Name}_QuantityInfo_ReturnsQuantityInfoDescribingQuantity()
Expand Down Expand Up @@ -207,6 +223,23 @@ public void As()
Writer.WL($@"
}}

[Fact]
public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
{{
var quantity = new {_quantity.Name}(value: 1, unit: {_quantity.Name}.BaseUnit);
Func<object> AsWithSIUnitSystem = () => quantity.As(UnitSystem.SI);

if (SupportsSIUnitSystem)
{{
var value = (double) AsWithSIUnitSystem();
Assert.Equal(1, value);
}}
else
{{
Assert.Throws<ArgumentException>(AsWithSIUnitSystem);
}}
}}

[Fact]
public void ToUnit()
{{
Expand All @@ -224,6 +257,14 @@ public void ToUnit()
Writer.WL($@"
}}

[Fact]
public void ToBaseUnit_ReturnsQuantityWithBaseUnit()
{{
var quantityInBaseUnit = {_quantity.Name}.From{_baseUnit.PluralName}(1).ToBaseUnit();
Assert.Equal({_quantity.Name}.BaseUnit, quantityInBaseUnit.Unit);");
Writer.WL($@"
}}

[Fact]
public void ConversionRoundTrip()
{{
Expand Down Expand Up @@ -657,11 +698,10 @@ public void NegationOperator_ReturnsQuantity_WithNegatedValue(double value)
{{
var quantity = {_quantity.Name}.From{_baseUnit.PluralName}(value);
Assert.Equal({_quantity.Name}.From{_baseUnit.PluralName}(-value), -quantity);
}}
");
}}");
}

Writer.WL( $@"
Writer.WL($@"
}}
}}" );
return Writer.ToString();
Expand Down
3 changes: 3 additions & 0 deletions Common/UnitDefinitions/Volume.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
"FromUnitToBaseFunc": "x",
"FromBaseToUnitFunc": "x",
"Prefixes": [ "Hecto", "Kilo" ],
"BaseUnits": {
"L": "Meter"
},
"Localization": [
{
"Culture": "en-US",
Expand Down
2 changes: 2 additions & 0 deletions UnitsNet.Tests/CustomCode/AccelerationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ namespace UnitsNet.Tests.CustomCode
{
public class AccelerationTests : AccelerationTestsBase
{
protected override bool SupportsSIUnitSystem => true;

protected override double KilometersPerSecondSquaredInOneMeterPerSecondSquared => 1E-3;

protected override double MetersPerSecondSquaredInOneMeterPerSecondSquared => 1;
Expand Down
6 changes: 4 additions & 2 deletions UnitsNet.Tests/CustomCode/AmountOfSubstanceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ namespace UnitsNet.Tests.CustomCode
{
public class AmountOfSubstanceTests : AmountOfSubstanceTestsBase
{
protected override bool SupportsSIUnitSystem => true;

protected override double CentimolesInOneMole => 1e2;
protected override double CentipoundMolesInOneMole => 0.002204622621848776 * 1e2;
protected override double DecimolesInOneMole => 1e1;
Expand All @@ -43,7 +45,7 @@ public class AmountOfSubstanceTests : AmountOfSubstanceTestsBase
protected override double NanopoundMolesInOneMole => 0.002204622621848776 * 1e9;
protected override double PoundMolesInOneMole => 0.002204622621848776;
protected override double MegamolesInOneMole => 1e-6;

[Fact]
public void NumberOfParticlesInOneMoleEqualsAvogadroConstant()
{
Expand Down Expand Up @@ -71,7 +73,7 @@ public void MassFromAmountOfSubstanceAndMolarMass(
{
AmountOfSubstance amountOfSubstance = new AmountOfSubstance(amountOfSubstanceValue, amountOfSubstanceUnit);
MolarMass molarMass = new MolarMass(molarMassValue, molarMassUnit);

Mass mass = amountOfSubstance * molarMass;

AssertEx.EqualTolerance(expectedMass, mass.As(expectedMassUnit), tolerence);
Expand Down
1 change: 1 addition & 0 deletions UnitsNet.Tests/CustomCode/AmplitudeRatioTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace UnitsNet.Tests.CustomCode
{
public class AmplitudeRatioTests : AmplitudeRatioTestsBase
{
protected override bool SupportsSIUnitSystem => false;
protected override double DecibelMicrovoltsInOneDecibelVolt => 121;

protected override double DecibelMillivoltsInOneDecibelVolt => 61;
Expand Down
2 changes: 2 additions & 0 deletions UnitsNet.Tests/CustomCode/AngleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace UnitsNet.Tests.CustomCode
{
public class AngleTests : AngleTestsBase
{
protected override bool SupportsSIUnitSystem => false;

protected override double DegreesInOneDegree => 1;

protected override double GradiansInOneDegree => 400 / 360.0;
Expand Down
2 changes: 2 additions & 0 deletions UnitsNet.Tests/CustomCode/ApparentEnergyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ namespace UnitsNet.Tests.CustomCode
{
public class ApparentEnergyTests : ApparentEnergyTestsBase
{
protected override bool SupportsSIUnitSystem => false;

protected override double VoltampereHoursInOneVoltampereHour => 1;

protected override double KilovoltampereHoursInOneVoltampereHour => 1E-3;
Expand Down
2 changes: 2 additions & 0 deletions UnitsNet.Tests/CustomCode/ApparentPowerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ namespace UnitsNet.Tests.CustomCode
{
public class ApparentPowerTests : ApparentPowerTestsBase
{
protected override bool SupportsSIUnitSystem => false;

protected override double VoltamperesInOneVoltampere => 1;

protected override double KilovoltamperesInOneVoltampere => 1E-3;
Expand Down
2 changes: 2 additions & 0 deletions UnitsNet.Tests/CustomCode/AreaDensityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ namespace UnitsNet.Tests.CustomCode
{
public class AreaDensityTests : AreaDensityTestsBase
{
protected override bool SupportsSIUnitSystem => true;

protected override double KilogramsPerSquareMeterInOneKilogramPerSquareMeter => 1;
}
}
2 changes: 2 additions & 0 deletions UnitsNet.Tests/CustomCode/AreaMomentOfInertiaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ namespace UnitsNet.Tests.CustomCode
{
public class AreaMomentOfInertiaTests : AreaMomentOfInertiaTestsBase
{
protected override bool SupportsSIUnitSystem => true;

protected override double CentimetersToTheFourthInOneMeterToTheFourth => 1e8;

protected override double DecimetersToTheFourthInOneMeterToTheFourth => 1e4;
Expand Down
1 change: 1 addition & 0 deletions UnitsNet.Tests/CustomCode/AreaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace UnitsNet.Tests.CustomCode
{
public class AreaTests : AreaTestsBase
{
protected override bool SupportsSIUnitSystem => true;

protected override double SquareKilometersInOneSquareMeter => 1E-6;

Expand Down
2 changes: 2 additions & 0 deletions UnitsNet.Tests/CustomCode/BitRateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ namespace UnitsNet.Tests.CustomCode
{
public class BitRateTests : BitRateTestsBase
{
protected override bool SupportsSIUnitSystem => false;

protected override double BitsPerSecondInOneBitPerSecond => 1d;
protected override double BytesPerSecondInOneBitPerSecond => 1.25E-1d;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace UnitsNet.Tests.CustomCode
{
public class BrakeSpecificFuelConsumptionTests : BrakeSpecificFuelConsumptionTestsBase
{
protected override bool SupportsSIUnitSystem => false;

protected override double GramsPerKiloWattHourInOneKilogramPerJoule => 3600000000;

protected override double KilogramsPerJouleInOneKilogramPerJoule => 1.0;
Expand Down
2 changes: 2 additions & 0 deletions UnitsNet.Tests/CustomCode/CapacitanceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ namespace UnitsNet.Tests.CustomCode
{
public class CapacitanceTests : CapacitanceTestsBase
{
protected override bool SupportsSIUnitSystem => true;

protected override double FaradsInOneFarad => 1;

protected override double MillifaradsInOneFarad => 1e3;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ namespace UnitsNet.Tests.CustomCode
{
public class CoefficientOfThermalExpansionTests : CoefficientOfThermalExpansionTestsBase
{
protected override bool SupportsSIUnitSystem => true;

protected override double InverseDegreeCelsiusInOneInverseKelvin => 1.0;

protected override double InverseDegreeFahrenheitInOneInverseKelvin => 0.5555555555555556;
Expand Down
4 changes: 3 additions & 1 deletion UnitsNet.Tests/CustomCode/DensityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ namespace UnitsNet.Tests.CustomCode
{
public class DensityTests : DensityTestsBase
{
protected override bool SupportsSIUnitSystem => false;

protected override double MilligramsPerCubicMeterInOneKilogramPerCubicMeter => 1e6;

protected override double GramsPerCubicCentimeterInOneKilogramPerCubicMeter => 1e-3;
Expand Down Expand Up @@ -107,7 +109,7 @@ public static void DensityTimesKinematicViscosityEqualsDynamicViscosity()
DynamicViscosity dynamicViscosity = Density.FromKilogramsPerCubicMeter(2) * KinematicViscosity.FromSquareMetersPerSecond(10);
Assert.Equal(dynamicViscosity, DynamicViscosity.FromNewtonSecondsPerMeterSquared(20));
}

[Fact]
public void DensityTimesSpeedEqualsMassFlux()
{
Expand Down
2 changes: 2 additions & 0 deletions UnitsNet.Tests/CustomCode/DurationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace UnitsNet.Tests.CustomCode
{
public class DurationTests : DurationTestsBase
{
protected override bool SupportsSIUnitSystem => true;

protected override double DaysInOneSecond => 1.15741e-5;

protected override double HoursInOneSecond => 0.0002777784;
Expand Down
2 changes: 2 additions & 0 deletions UnitsNet.Tests/CustomCode/DynamicViscosityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace UnitsNet.Tests.CustomCode
{
public class DynamicViscosityTests : DynamicViscosityTestsBase
{
protected override bool SupportsSIUnitSystem => false;

protected override double CentipoiseInOneNewtonSecondPerMeterSquared => 1e3;
protected override double MicropascalSecondsInOneNewtonSecondPerMeterSquared => 1e6;
protected override double MillipascalSecondsInOneNewtonSecondPerMeterSquared => 1e3;
Expand Down
2 changes: 2 additions & 0 deletions UnitsNet.Tests/CustomCode/ElectricAdmittanceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ namespace UnitsNet.Tests.CustomCode
{
public class ElectricAdmittanceTests : ElectricAdmittanceTestsBase
{
protected override bool SupportsSIUnitSystem => false;

protected override double MicrosiemensInOneSiemens => 1e+6;

protected override double MillisiemensInOneSiemens => 1000;
Expand Down
1 change: 1 addition & 0 deletions UnitsNet.Tests/CustomCode/ElectricChargeDensityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace UnitsNet.Tests.CustomCode
{
public class ElectricChargeDensityTests : ElectricChargeDensityTestsBase
{
protected override bool SupportsSIUnitSystem => true;
protected override double CoulombsPerCubicMeterInOneCoulombPerCubicMeter => 1;
}
}
1 change: 1 addition & 0 deletions UnitsNet.Tests/CustomCode/ElectricChargeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace UnitsNet.Tests.CustomCode
{
public class ElectricChargeTests : ElectricChargeTestsBase
{
protected override bool SupportsSIUnitSystem => false;
protected override double CoulombsInOneCoulomb => 1;
protected override double MilliampereHoursInOneCoulomb => 2.77777777777e-1;
protected override double AmpereHoursInOneCoulomb => 2.77777777777e-4;
Expand Down
1 change: 1 addition & 0 deletions UnitsNet.Tests/CustomCode/ElectricConductanceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace UnitsNet.Tests.CustomCode
{
public class ElectricConductanceTests : ElectricConductanceTestsBase
{
protected override bool SupportsSIUnitSystem => false;
protected override double SiemensInOneSiemens => 1;

protected override double MillisiemensInOneSiemens => 1E3;
Expand Down
1 change: 1 addition & 0 deletions UnitsNet.Tests/CustomCode/ElectricConductivityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace UnitsNet.Tests.CustomCode
{
public class ElectricConductivityTests : ElectricConductivityTestsBase
{
protected override bool SupportsSIUnitSystem => true;
protected override double SiemensPerMeterInOneSiemensPerMeter => 1;
protected override double SiemensPerInchInOneSiemensPerMeter => 2.54e-2;
protected override double SiemensPerFootInOneSiemensPerMeter => 3.048e-1;
Expand Down
1 change: 1 addition & 0 deletions UnitsNet.Tests/CustomCode/ElectricCurrentDensityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace UnitsNet.Tests.CustomCode
{
public class ElectricCurrentDensityTests : ElectricCurrentDensityTestsBase
{
protected override bool SupportsSIUnitSystem => true;
protected override double AmperesPerSquareMeterInOneAmperePerSquareMeter => 1;
protected override double AmperesPerSquareInchInOneAmperePerSquareMeter => 6.4516e-4;
protected override double AmperesPerSquareFootInOneAmperePerSquareMeter => 9.290304e-2;
Expand Down
1 change: 1 addition & 0 deletions UnitsNet.Tests/CustomCode/ElectricCurrentGradientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace UnitsNet.Tests.CustomCode
{
public class ElectricCurrentGradientTests : ElectricCurrentGradientTestsBase
{
protected override bool SupportsSIUnitSystem => false;
protected override double AmperesPerSecondInOneAmperePerSecond => 1;
}
}
1 change: 1 addition & 0 deletions UnitsNet.Tests/CustomCode/ElectricCurrentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace UnitsNet.Tests.CustomCode
{
public class ElectricCurrentTests : ElectricCurrentTestsBase
{
protected override bool SupportsSIUnitSystem => true;
protected override double PicoamperesInOneAmpere => 1e12;

protected override double NanoamperesInOneAmpere => 1e9;
Expand Down
1 change: 1 addition & 0 deletions UnitsNet.Tests/CustomCode/ElectricFieldTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace UnitsNet.Tests.CustomCode
{
public class ElectricFieldTests : ElectricFieldTestsBase
{
protected override bool SupportsSIUnitSystem => true;
protected override double VoltsPerMeterInOneVoltPerMeter => 1;
}
}
1 change: 1 addition & 0 deletions UnitsNet.Tests/CustomCode/ElectricInductanceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace UnitsNet.Tests.CustomCode
{
public class ElectricInductanceTests : ElectricInductanceTestsBase
{
protected override bool SupportsSIUnitSystem => false;
protected override double HenriesInOneHenry => 1;
protected override double NanohenriesInOneHenry => 1e9;
protected override double MicrohenriesInOneHenry => 1e6;
Expand Down
1 change: 1 addition & 0 deletions UnitsNet.Tests/CustomCode/ElectricPotentialAcTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace UnitsNet.Tests.CustomCode
{
public class ElectricPotentialAcTests : ElectricPotentialAcTestsBase
{
protected override bool SupportsSIUnitSystem => false;
protected override double KilovoltsAcInOneVoltAc => 1e-3;

protected override double MegavoltsAcInOneVoltAc => 1e-6;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace UnitsNet.Tests.CustomCode
{
public class ElectricPotentialChangeRateTests : ElectricPotentialChangeRateTestsBase
{
protected override bool SupportsSIUnitSystem => true;
protected override double KilovoltsPerHoursInOneVoltPerSecond => 3.6;
protected override double KilovoltsPerMicrosecondsInOneVoltPerSecond => 1e-09;
protected override double KilovoltsPerMinutesInOneVoltPerSecond => 6e-2;
Expand Down
1 change: 1 addition & 0 deletions UnitsNet.Tests/CustomCode/ElectricPotentialDcTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace UnitsNet.Tests.CustomCode
{
public class ElectricPotentialDcTests : ElectricPotentialDcTestsBase
{
protected override bool SupportsSIUnitSystem => false;
protected override double KilovoltsDcInOneVoltDc => 1e-3;

protected override double MegavoltsDcInOneVoltDc => 1e-6;
Expand Down
1 change: 1 addition & 0 deletions UnitsNet.Tests/CustomCode/ElectricPotentialTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace UnitsNet.Tests.CustomCode
{
public class ElectricPotentialTests : ElectricPotentialTestsBase
{
protected override bool SupportsSIUnitSystem => true;
protected override double MicrovoltsInOneVolt => 1e6;

protected override double MillivoltsInOneVolt => 1e3;
Expand Down
Loading