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
3 changes: 0 additions & 3 deletions CodeGen/Generators/UnitsNetGen/QuantityGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1151,9 +1151,6 @@ IQuantity IQuantity.ToUnit(Enum unit)
/// <inheritdoc />
IQuantity<{_unitEnumName}> IQuantity<{_unitEnumName}>.ToUnit({_unitEnumName} unit) => ToUnit(unit);

/// <inheritdoc />
IQuantity<{_unitEnumName}> IQuantity<{_unitEnumName}>.ToUnit(UnitSystem unitSystem) => this.ToUnit(unitSystem);

#endregion

#endregion
Expand Down
101 changes: 10 additions & 91 deletions CodeGen/Generators/UnitsNetGen/UnitTestBaseClassGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -412,53 +412,30 @@ public void As_UnitSystem_ThrowsArgumentNullExceptionIfNull()
[Fact]
public void ToUnit_UnitSystem_ReturnsValueInDimensionlessUnit()
{{
Assert.Multiple(() =>
{{
var quantity = new {_quantity.Name}(value: 1, unit: {_baseUnitFullName});

{_quantity.Name} convertedQuantity = quantity.ToUnit(UnitSystem.SI);

Assert.Equal({_baseUnitFullName}, convertedQuantity.Unit);
Assert.Equal(quantity.Value, convertedQuantity.Value);
}}, () =>
{{
IQuantity<{_unitEnumName}> quantity = new {_quantity.Name}(value: 1, unit: {_baseUnitFullName});

IQuantity<{_unitEnumName}> convertedQuantity = quantity.ToUnit(UnitSystem.SI);

Assert.Equal({_baseUnitFullName}, convertedQuantity.Unit);
Assert.Equal(quantity.Value, convertedQuantity.Value);
}});
}}
Comment on lines 413 to -432
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I've only kept the concrete part of this test, but on second thought there shouldn't really be any need for it to exist here either- as we already have tests for the UnitSystem constructor.
In case we decide to move forward with the removal of the interface methods from the interface in v6, then these tests should probably go to another test file- something like QuantityExtensionsTests.


[Fact]
public void ToUnitUntyped_UnitSystem_ReturnsValueInDimensionlessUnit()
{{
IQuantity quantity = new {_quantity.Name}(value: 1, unit: {_baseUnitFullName});
var quantity = new {_quantity.Name}(value: 1, unit: {_baseUnitFullName});

IQuantity convertedQuantity = quantity.ToUnitUntyped(UnitSystem.SI);
{_quantity.Name} convertedQuantity = quantity.ToUnit(UnitSystem.SI);

Assert.Equal({_baseUnitFullName}, convertedQuantity.Unit);
Assert.Equal(quantity.Value, convertedQuantity.Value);
}}


[Fact]
public void ToUnit_UnitSystem_ThrowsArgumentNullExceptionIfNull()
{{
UnitSystem nullUnitSystem = null!;
Assert.Multiple(() =>
{{
var quantity = new {_quantity.Name}(value: 1, unit: {_quantity.Name}.BaseUnit);
Assert.Throws<ArgumentNullException>(() => quantity.ToUnitUntyped(nullUnitSystem));
Assert.Throws<ArgumentNullException>(() => quantity.ToUnit(nullUnitSystem));
}}, () =>
{{
IQuantity<{_unitEnumName}> quantity = new {_quantity.Name}(value: 1, unit: {_quantity.Name}.BaseUnit);
Assert.Throws<ArgumentNullException>(() => quantity.ToUnitUntyped(nullUnitSystem));
Assert.Throws<ArgumentNullException>(() => quantity.ToUnit(nullUnitSystem));
}}, () =>
{{
IQuantity quantity = new {_quantity.Name}(value: 1, unit: {_quantity.Name}.BaseUnit);
Assert.Throws<ArgumentNullException>(() => quantity.ToUnitUntyped(nullUnitSystem));
Assert.Throws<ArgumentNullException>(() => quantity.ToUnit(nullUnitSystem));
}});
}}
");
Expand Down Expand Up @@ -508,35 +485,7 @@ public virtual void ToUnit_UnitSystem_SI_ReturnsQuantityInSIUnits()
var expectedUnit = {_quantity.Name}.Info.GetDefaultUnit(UnitSystem.SI);
var expectedValue = quantity.As(expectedUnit);

Assert.Multiple(() =>
{{
{_quantity.Name} quantityToConvert = quantity;

{_quantity.Name} convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI);

Assert.Equal(expectedUnit, convertedQuantity.Unit);
Assert.Equal(expectedValue, convertedQuantity.Value);
}}, () =>
{{
IQuantity<{_unitEnumName}> quantityToConvert = quantity;

IQuantity<{_unitEnumName}> convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI);

Assert.Equal(expectedUnit, convertedQuantity.Unit);
Assert.Equal(expectedValue, convertedQuantity.Value);
}});
}}

[Fact]
public virtual void ToUnitUntyped_UnitSystem_SI_ReturnsQuantityInSIUnits()
{{
var quantity = new {_quantity.Name}(value: 1, unit: {_quantity.Name}.BaseUnit);
var expectedUnit = {_quantity.Name}.Info.GetDefaultUnit(UnitSystem.SI);
var expectedValue = quantity.As(expectedUnit);

IQuantity quantityToConvert = quantity;

IQuantity convertedQuantity = quantityToConvert.ToUnitUntyped(UnitSystem.SI);
{_quantity.Name} convertedQuantity = quantity.ToUnit(UnitSystem.SI);

Assert.Equal(expectedUnit, convertedQuantity.Unit);
Assert.Equal(expectedValue, convertedQuantity.Value);
Expand All @@ -546,46 +495,16 @@ public virtual void ToUnitUntyped_UnitSystem_SI_ReturnsQuantityInSIUnits()
public void ToUnit_UnitSystem_ThrowsArgumentNullExceptionIfNull()
{{
UnitSystem nullUnitSystem = null!;
Assert.Multiple(() =>
{{
var quantity = new {_quantity.Name}(value: 1, unit: {_quantity.Name}.BaseUnit);
Assert.Throws<ArgumentNullException>(() => quantity.ToUnit(nullUnitSystem));
}}, () =>
{{
IQuantity<{_unitEnumName}> quantity = new {_quantity.Name}(value: 1, unit: {_quantity.Name}.BaseUnit);
Assert.Throws<ArgumentNullException>(() => quantity.ToUnit(nullUnitSystem));
}});
}}

[Fact]
public void ToUnitUntyped_UnitSystem_ThrowsArgumentNullExceptionIfNull()
{{
UnitSystem nullUnitSystem = null!;
IQuantity quantity = new {_quantity.Name}(value: 1, unit: {_quantity.Name}.BaseUnit);
Assert.Throws<ArgumentNullException>(() => quantity.ToUnitUntyped(nullUnitSystem));
var quantity = new {_quantity.Name}(value: 1, unit: {_quantity.Name}.BaseUnit);
Assert.Throws<ArgumentNullException>(() => quantity.ToUnit(nullUnitSystem));
}}

[Fact]
public void ToUnit_UnitSystem_ThrowsArgumentExceptionIfNotSupported()
{{
var unsupportedUnitSystem = new UnitSystem(UnsupportedBaseUnits);
Assert.Multiple(() =>
{{
var quantity = new {_quantity.Name}(value: 1, unit: {_quantity.Name}.BaseUnit);
Assert.Throws<ArgumentException>(() => quantity.ToUnit(unsupportedUnitSystem));
}}, () =>
{{
IQuantity<{_unitEnumName}> quantity = new {_quantity.Name}(value: 1, unit: {_quantity.Name}.BaseUnit);
Assert.Throws<ArgumentException>(() => quantity.ToUnit(unsupportedUnitSystem));
}});
}}

[Fact]
public void ToUnitUntyped_UnitSystem_ThrowsArgumentExceptionIfNotSupported()
{{
var unsupportedUnitSystem = new UnitSystem(UnsupportedBaseUnits);
IQuantity quantity = new {_quantity.Name}(value: 1, unit: {_quantity.Name}.BaseUnit);
Assert.Throws<ArgumentException>(() => quantity.ToUnitUntyped(unsupportedUnitSystem));
var quantity = new {_quantity.Name}(value: 1, unit: {_quantity.Name}.BaseUnit);
Assert.Throws<ArgumentException>(() => quantity.ToUnit(unsupportedUnitSystem));
}}
");
}
Expand Down
6 changes: 0 additions & 6 deletions UnitsNet.Tests/CustomCode/ElectricApparentEnergyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,5 @@ public override void ToUnit_UnitSystem_SI_ReturnsQuantityInSIUnits()
{
base.ToUnit_UnitSystem_SI_ReturnsQuantityInSIUnits();
}

[Fact(Skip = "See about adding an SI unit (VoltampereSecond, Joules?)")]
public override void ToUnitUntyped_UnitSystem_SI_ReturnsQuantityInSIUnits()
{
base.ToUnitUntyped_UnitSystem_SI_ReturnsQuantityInSIUnits();
}
}
}
6 changes: 0 additions & 6 deletions UnitsNet.Tests/CustomCode/ElectricReactiveEnergyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,5 @@ public override void ToUnit_UnitSystem_SI_ReturnsQuantityInSIUnits()
{
base.ToUnit_UnitSystem_SI_ReturnsQuantityInSIUnits();
}

[Fact(Skip = "See about adding an SI unit (VoltampereReactiveSecond, Joules?)")]
public override void ToUnitUntyped_UnitSystem_SI_ReturnsQuantityInSIUnits()
{
base.ToUnitUntyped_UnitSystem_SI_ReturnsQuantityInSIUnits();
}
}
}
6 changes: 0 additions & 6 deletions UnitsNet.Tests/CustomCode/FuelEfficiencyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,5 @@ public override void ToUnit_UnitSystem_SI_ReturnsQuantityInSIUnits()
{
base.ToUnit_UnitSystem_SI_ReturnsQuantityInSIUnits();
}

[Fact(Skip = "The SI unit would have to be MeterPerCubicMeter")]
public override void ToUnitUntyped_UnitSystem_SI_ReturnsQuantityInSIUnits()
{
base.ToUnitUntyped_UnitSystem_SI_ReturnsQuantityInSIUnits();
}
}
}
95 changes: 74 additions & 21 deletions UnitsNet.Tests/CustomCode/IQuantityTests.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,84 @@
// Licensed under MIT No Attribution, see LICENSE file at the root.
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.

using System;
using System.Linq;
using Xunit;
namespace UnitsNet.Tests;

namespace UnitsNet.Tests
// ReSharper disable once InconsistentNaming
public partial class IQuantityTests
{
// ReSharper disable once InconsistentNaming
public partial class IQuantityTests
[Fact]
public void As_GivenWrongUnitType_ThrowsArgumentException()
{
[Fact]
public void As_GivenWrongUnitType_ThrowsArgumentException()
Assert.All(Quantity.Infos.Select(x => x.Zero), quantity => { Assert.Throws<ArgumentException>(() => quantity.As(ComparisonType.Absolute)); });
}

[Fact]
public void ToUnit_GivenWrongUnitType_ThrowsArgumentException()
{
Assert.All(Quantity.Infos.Select(x => x.Zero), quantity => { Assert.Throws<ArgumentException>(() => quantity.ToUnit(ComparisonType.Absolute)); });
}

[Fact]
public virtual void ToUnit_UnitSystem_SI_ReturnsQuantityInSIUnits()
{
var quantity = new Mass(1, Mass.BaseUnit);
MassUnit expectedUnit = Mass.Info.GetDefaultUnit(UnitSystem.SI);
var expectedValue = quantity.As(expectedUnit);

Assert.Multiple(() =>
{
IQuantity<MassUnit> quantityToConvert = quantity;

IQuantity<MassUnit> convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI);

Assert.Equal(expectedUnit, convertedQuantity.Unit);
Assert.Equal(expectedValue, convertedQuantity.Value);
}, () =>
{
IQuantity quantityToConvert = quantity;

IQuantity convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI);

Assert.Equal(expectedUnit, convertedQuantity.Unit);
Assert.Equal(expectedValue, convertedQuantity.Value);
});
}

[Fact]
public void ToUnit_UnitSystem_ThrowsArgumentNullExceptionIfNull()
{
UnitSystem nullUnitSystem = null!;
Assert.Multiple(() =>
{
IQuantity<MassUnit> quantity = new Mass(1, Mass.BaseUnit);
Assert.Throws<ArgumentNullException>(() => quantity.ToUnit(nullUnitSystem));
}, () =>
{
IQuantity quantity = new Mass(1, Mass.BaseUnit);
Assert.Throws<ArgumentNullException>(() => quantity.ToUnit(nullUnitSystem));
});
}

[Fact]
public void ToUnit_UnitSystem_ThrowsArgumentExceptionIfNotSupported()
{
var unsupportedUnitSystem = new UnitSystem(new BaseUnits(
(LengthUnit)(-1),
(MassUnit)(-1),
(DurationUnit)(-1),
(ElectricCurrentUnit)(-1),
(TemperatureUnit)(-1),
(AmountOfSubstanceUnit)(-1),
(LuminousIntensityUnit)(-1)));

Assert.Multiple(() =>
{
Assert.All(Quantity.Infos.Select(x => x.Zero), quantity =>
{
Assert.Throws<ArgumentException>(() => quantity.As(ComparisonType.Absolute));
});
}

[Fact]
public void ToUnit_GivenWrongUnitType_ThrowsArgumentException()
IQuantity<MassUnit> quantity = new Mass(1, Mass.BaseUnit);
Assert.Throws<ArgumentException>(() => quantity.ToUnit(unsupportedUnitSystem));
}, () =>
{
Assert.All(Quantity.Infos.Select(x => x.Zero), quantity =>
{
Assert.Throws<ArgumentException>(() => quantity.ToUnit(ComparisonType.Absolute));
});
}
IQuantity quantity = new Mass(1, Mass.BaseUnit);
Assert.Throws<ArgumentException>(() => quantity.ToUnit(unsupportedUnitSystem));
});
}
}
9 changes: 0 additions & 9 deletions UnitsNet.Tests/CustomQuantities/HowMuch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ public double As(UnitKey unitKey)
return As(unitKey.ToUnit<HowMuchUnit>());
}

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

public IQuantity ToUnit(Enum unit)
{
if (unit is HowMuchUnit howMuchUnit) return new HowMuch(As(unit), howMuchUnit);
Expand All @@ -82,13 +80,6 @@ public IQuantity<HowMuchUnit> ToUnit(HowMuchUnit unit)
{
throw new NotImplementedException();
}

IQuantity<HowMuchUnit> IQuantity<HowMuchUnit>.ToUnit(UnitSystem unitSystem)
{
throw new NotImplementedException();
}

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

public override string ToString()
{
Expand Down
Loading
Loading