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
40 changes: 40 additions & 0 deletions UnitsNet.Tests/BaseUnitsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,5 +136,45 @@ public void ToStringGivesExpectedResult()

Assert.Equal("[Length]: m, [Mass]: kg, [Time]: s, [Current]: A, [Temperature]: K, [Amount]: mol, [LuminousIntensity]: cd", siBaseUnits.ToString());
}

[Fact]
public void IsFullyDefined_TrueIfAllBaseUnitDefined()
{
Assert.True(new BaseUnits(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second,
ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole,
LuminousIntensityUnit.Candela).IsFullyDefined);
}

[Fact]
public void IsFullyDefined_FalseIfAnyBaseUnitIsUndefined()
{
Assert.False(new BaseUnits(mass: MassUnit.Kilogram, time: DurationUnit.Second,
current: ElectricCurrentUnit.Ampere, temperature: TemperatureUnit.Kelvin, amount: AmountOfSubstanceUnit.Mole,
luminousIntensity: LuminousIntensityUnit.Candela).IsFullyDefined);

Assert.False(new BaseUnits(length: LengthUnit.Meter, time: DurationUnit.Second,
current: ElectricCurrentUnit.Ampere, temperature: TemperatureUnit.Kelvin, amount: AmountOfSubstanceUnit.Mole,
luminousIntensity: LuminousIntensityUnit.Candela).IsFullyDefined);

Assert.False(new BaseUnits(length: LengthUnit.Meter, mass: MassUnit.Kilogram,
current: ElectricCurrentUnit.Ampere, temperature: TemperatureUnit.Kelvin, amount: AmountOfSubstanceUnit.Mole,
luminousIntensity: LuminousIntensityUnit.Candela).IsFullyDefined);

Assert.False(new BaseUnits(length: LengthUnit.Meter, mass: MassUnit.Kilogram, time: DurationUnit.Second,
temperature: TemperatureUnit.Kelvin, amount: AmountOfSubstanceUnit.Mole,
luminousIntensity: LuminousIntensityUnit.Candela).IsFullyDefined);

Assert.False(new BaseUnits(length: LengthUnit.Meter, mass: MassUnit.Kilogram, time: DurationUnit.Second,
current: ElectricCurrentUnit.Ampere, amount: AmountOfSubstanceUnit.Mole,
luminousIntensity: LuminousIntensityUnit.Candela).IsFullyDefined);

Assert.False(new BaseUnits(length: LengthUnit.Meter, mass: MassUnit.Kilogram, time: DurationUnit.Second,
current: ElectricCurrentUnit.Ampere, temperature: TemperatureUnit.Kelvin,
luminousIntensity: LuminousIntensityUnit.Candela).IsFullyDefined);

Assert.False(new BaseUnits(length: LengthUnit.Meter, mass: MassUnit.Kilogram, time: DurationUnit.Second,
current: ElectricCurrentUnit.Ampere, temperature: TemperatureUnit.Kelvin, amount: AmountOfSubstanceUnit.Mole
).IsFullyDefined);
}
}
}
14 changes: 14 additions & 0 deletions UnitsNet.Tests/CustomCode/AreaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,19 @@ public void Constructor_UnitSystemSI_AssignsSIUnit()
var area = new Area(1.0, UnitSystem.SI);
Assert.Equal(AreaUnit.SquareMeter, area.Unit);
}

[Fact]
public void As_ImperialGivenSIUnitSystem_ReturnsSIValue()
{
var imperialArea = new Area(2.0, AreaUnit.SquareInch);
Assert.Equal(0.00129032, imperialArea.As(UnitSystem.SI));
}

[Fact]
public void ToUnit_ImperialGivenSIUnitSystem_ReturnsSIValue()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand it right, this test would pass regardless if we pass it SI unit system or some other system. The quantity doesn't change when changing the unit.
Maybe the test should instead assert this:

Assert.Equal(LengthUnit.SquareMeter, imperialArea.ToUnit(UnitSystem.SI).Unit)

If so, maybe also rename the test to something like this ToUnit_ImperialQuantityGivenSIUnitSystem_ConvertsToSIUnit.

Same for other files below.

Copy link
Collaborator Author

@tmilnthorp tmilnthorp Mar 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will pass only for a unit system that has a base unit in meters. You could define one in feet and it would have a different quantity value and unit (whatever value in square feet). That conversion is handled by other tests, but I added it here regardless. I can change it for sure.

Copy link
Owner

@angularsen angularsen Mar 4, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, I forgot this one before merging.

Are you saying that this test will only pass because ToUnit(UnitSystem.SI) converts to the Meter unit?
I think that is false, because the Area equality check converts to the default/base unit before comparing values.

As an example, I just tested this: Length.FromCentimeters(100).Equals(Length.FromMeters(1)) is true.

This is why I think this test does not actually test the ToUnit behavior at all - because it could have incorrectly converted to nautical mile unit and it would still equal the left hand side.

{
var imperialArea = new Area(2.0, AreaUnit.SquareInch);
Assert.Equal(Area.FromSquareMeters(0.00129032), imperialArea.ToUnit(UnitSystem.SI));
}
}
}
14 changes: 14 additions & 0 deletions UnitsNet.Tests/CustomCode/EnergyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,19 @@ public void Constructor_UnitSystemSI_AssignsSIUnit()
var energy = new Energy(1.0, UnitSystem.SI);
Assert.Equal(EnergyUnit.Joule, energy.Unit);
}

[Fact]
public void As_ImperialQuantityGivenSIUnitSystem_ReturnsSIValue()
{
var imperialEnergy = new Energy(2.0, EnergyUnit.BritishThermalUnit);
Assert.Equal(2110.11170524, imperialEnergy.As(UnitSystem.SI));
}

[Fact]
public void ToUnit_ImperialQuantityGivenSIUnitSystem_ReturnsSIValue()
{
var imperialEnergy = new Energy(2.0, EnergyUnit.BritishThermalUnit);
Assert.Equal(Energy.FromJoules(2110.11170524), imperialEnergy.ToUnit(UnitSystem.SI));
}
}
}
14 changes: 14 additions & 0 deletions UnitsNet.Tests/CustomCode/LengthTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,5 +165,19 @@ public void Constructor_UnitSystemSI_AssignsSIUnit()
var length = new Length(1.0, UnitSystem.SI);
Assert.Equal(LengthUnit.Meter, length.Unit);
}

[Fact]
public void As_ImperialQuantityGivenSIUnitSystem_ReturnsSIValue()
{
var imperialLength = new Length(2.0, LengthUnit.Inch);
Assert.Equal(0.0508, imperialLength.As(UnitSystem.SI));
}

[Fact]
public void ToUnit_ImperialQuantityGivenSIUnitSystem_ReturnsSIValue()
{
var imperialLength = new Length(2.0, LengthUnit.Inch);
Assert.Equal(Length.FromMeters(0.0508), imperialLength.ToUnit(UnitSystem.SI));
}
}
}
28 changes: 28 additions & 0 deletions UnitsNet.Tests/IQuantityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,39 @@ public void As_GivenWrongUnitType_ThrowsArgumentException()
Assert.Throws<ArgumentException>(() => length.As(MassUnit.Kilogram));
}

[Fact]
public void As_GivenNullUnitSystem_ThrowsArgumentNullException()
{
IQuantity imperialLengthQuantity = new Length(2.0, LengthUnit.Inch);
Assert.Throws<ArgumentNullException>(() => imperialLengthQuantity.As((UnitSystem)null));
}

[Fact]
public void As_ImperialQuantityGivenSIUnitSystem_ReturnsSIValue()
{
IQuantity imperialLengthQuantity = new Length(2.0, LengthUnit.Inch);
Assert.Equal(0.0508, imperialLengthQuantity.As(UnitSystem.SI));
}

[Fact]
public void ToUnit_GivenWrongUnitType_ThrowsArgumentException()
{
IQuantity length = Length.FromMeters(1.2345);
Assert.Throws<ArgumentException>(() => length.ToUnit(MassUnit.Kilogram));
}

[Fact]
public void ToUnit_GivenNullUnitSystem_ThrowsArgumentNullException()
{
IQuantity imperialLengthQuantity = new Length(2.0, LengthUnit.Inch);
Assert.Throws<ArgumentNullException>(() => imperialLengthQuantity.ToUnit((UnitSystem)null));
}

[Fact]
public void ToUnit_ImperialQuantityGivenSIUnitSystem_ReturnsSIValue()
{
IQuantity imperialLengthQuantity = new Length(2.0, LengthUnit.Inch);
Assert.Equal(Length.FromMeters(0.0508), imperialLengthQuantity.ToUnit(UnitSystem.SI));
}
}
}
13 changes: 13 additions & 0 deletions UnitsNet/BaseUnits.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ public BaseUnits(
Temperature = temperature;
Amount = amount;
LuminousIntensity = luminousIntensity;

IsFullyDefined = Length != LengthUnit.Undefined &&
Mass != MassUnit.Undefined &&
Time != DurationUnit.Undefined &&
Current != ElectricCurrentUnit.Undefined &&
Temperature != TemperatureUnit.Undefined &&
Amount != AmountOfSubstanceUnit.Undefined &&
LuminousIntensity != LuminousIntensityUnit.Undefined;
}

/// <inheritdoc />
Expand Down Expand Up @@ -181,5 +189,10 @@ public override string ToString()
/// Gets the luminous intensity unit (J).
/// </summary>
public LuminousIntensityUnit LuminousIntensity{ get; }

/// <summary>
/// Gets whether or not all of the base units are defined.
/// </summary>
public bool IsFullyDefined { get; }
}
}
26 changes: 26 additions & 0 deletions UnitsNet/GeneratedCode/Quantities/Acceleration.NetFramework.g.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions UnitsNet/GeneratedCode/Quantities/AmplitudeRatio.NetFramework.g.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions UnitsNet/GeneratedCode/Quantities/Angle.NetFramework.g.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions UnitsNet/GeneratedCode/Quantities/ApparentEnergy.NetFramework.g.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading