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
22 changes: 1 addition & 21 deletions CodeGen/Generators/UnitsNetGen/QuantityGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1034,15 +1034,6 @@ public double As(UnitKey unitKey)
}}
");

Writer.WL( $@"

/// <inheritdoc cref=""IQuantity.As(UnitSystem)""/>
public double As(UnitSystem unitSystem)
{{
return As(Info.GetDefaultUnit(unitSystem));
}}
");

Writer.WL($@"
/// <summary>
/// Converts this {_quantity.Name} to another {_quantity.Name} with the unit representation <paramref name=""unit"" />.
Expand Down Expand Up @@ -1140,14 +1131,6 @@ private bool TryToUnit({_unitEnumName} unit, [NotNullWhen(true)] out {_quantity.
return true;
}}
");
Writer.WL($@"
/// <inheritdoc cref=""IQuantity.ToUnit(UnitSystem)""/>
public {_quantity.Name} ToUnit(UnitSystem unitSystem)
{{
return ToUnit(Info.GetDefaultUnit(unitSystem));
}}
");

Writer.WL($@"
#region Explicit implementations

Expand All @@ -1168,14 +1151,11 @@ IQuantity IQuantity.ToUnit(Enum unit)
return ToUnit(typedUnit, DefaultConversionFunctions);
}}

/// <inheritdoc />
IQuantity IQuantity.ToUnit(UnitSystem unitSystem) => ToUnit(unitSystem);

/// <inheritdoc />
IQuantity<{_unitEnumName}> IQuantity<{_unitEnumName}>.ToUnit({_unitEnumName} unit) => ToUnit(unit);

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

#endregion

Expand Down
71 changes: 45 additions & 26 deletions CodeGen/Generators/UnitsNetGen/UnitTestBaseClassGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ public void As_UnitSystem_ThrowsArgumentNullExceptionIfNull()
}}

[Fact]
public void ToUnitSystem_ReturnsValueInDimensionlessUnit()
public void ToUnit_UnitSystem_ReturnsValueInDimensionlessUnit()
{{
Assert.Multiple(() =>
{{
Expand All @@ -428,33 +428,37 @@ public void ToUnitSystem_ReturnsValueInDimensionlessUnit()

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

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

IQuantity convertedQuantity = quantity.ToUnit(UnitSystem.SI);
IQuantity convertedQuantity = quantity.ToUnitUntyped(UnitSystem.SI);

Assert.Equal({_baseUnitFullName}, convertedQuantity.Unit);
Assert.Equal(quantity.Value, convertedQuantity.Value);
}});
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.ToUnit(nullUnitSystem));
Assert.Throws<ArgumentNullException>(() => quantity.ToUnitUntyped(nullUnitSystem));
}}, () =>
{{
IQuantity<{_unitEnumName}> quantity = new {_quantity.Name}(value: 1, unit: {_quantity.Name}.BaseUnit);
Assert.Throws<ArgumentNullException>(() => quantity.ToUnit(nullUnitSystem));
Assert.Throws<ArgumentNullException>(() => quantity.ToUnitUntyped(nullUnitSystem));
}}, () =>
{{
IQuantity quantity = new {_quantity.Name}(value: 1, unit: {_quantity.Name}.BaseUnit);
Assert.Throws<ArgumentNullException>(() => quantity.ToUnit(nullUnitSystem));
Assert.Throws<ArgumentNullException>(() => quantity.ToUnitUntyped(nullUnitSystem));
}});
}}
");
Expand Down Expand Up @@ -520,15 +524,22 @@ public virtual void ToUnit_UnitSystem_SI_ReturnsQuantityInSIUnits()

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

IQuantity convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI);
[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);

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

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

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

[Fact]
Expand All @@ -543,13 +554,17 @@ public void ToUnit_UnitSystem_ThrowsArgumentNullExceptionIfNull()
{{
IQuantity<{_unitEnumName}> quantity = new {_quantity.Name}(value: 1, unit: {_quantity.Name}.BaseUnit);
Assert.Throws<ArgumentNullException>(() => quantity.ToUnit(nullUnitSystem));
}}, () =>
{{
IQuantity 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));
}}

[Fact]
public void ToUnit_UnitSystem_ThrowsArgumentExceptionIfNotSupported()
{{
Expand All @@ -562,12 +577,16 @@ public void ToUnit_UnitSystem_ThrowsArgumentExceptionIfNotSupported()
{{
IQuantity<{_unitEnumName}> quantity = new {_quantity.Name}(value: 1, unit: {_quantity.Name}.BaseUnit);
Assert.Throws<ArgumentException>(() => quantity.ToUnit(unsupportedUnitSystem));
}}, () =>
{{
IQuantity 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));
}}
");
}

Expand Down
12 changes: 9 additions & 3 deletions UnitsNet.Tests/CustomCode/ElectricApparentEnergyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,29 @@ public override void Ctor_SIUnitSystem_ReturnsQuantityWithSIUnits()
{
base.Ctor_SIUnitSystem_ReturnsQuantityWithSIUnits();
}

[Fact(Skip = "See about changing the BaseUnit to VoltampereSecond or Joule (adding the unit)")]
public override void BaseUnit_HasSIBase()
{
base.BaseUnit_HasSIBase();
}

[Fact(Skip = "See about adding an SI unit (VoltampereSecond, Joules?)")]
public override void As_UnitSystem_SI_ReturnsQuantityInSIUnits()
{
base.As_UnitSystem_SI_ReturnsQuantityInSIUnits();
}

[Fact(Skip = "See about adding an SI unit (VoltampereSecond, Joules?)")]
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();
}
}
}
12 changes: 9 additions & 3 deletions UnitsNet.Tests/CustomCode/ElectricReactiveEnergyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,29 @@ public override void Ctor_SIUnitSystem_ReturnsQuantityWithSIUnits()
{
base.Ctor_SIUnitSystem_ReturnsQuantityWithSIUnits();
}

[Fact(Skip = "See about changing the BaseUnit to VoltampereReactiveSecond or Joule (adding the unit)")]
public override void BaseUnit_HasSIBase()
{
base.BaseUnit_HasSIBase();
}

[Fact(Skip = "See about adding an SI unit (VoltampereReactiveSecond, Joules?)")]
public override void As_UnitSystem_SI_ReturnsQuantityInSIUnits()
{
base.As_UnitSystem_SI_ReturnsQuantityInSIUnits();
}

[Fact(Skip = "See about adding an SI unit (VoltampereReactiveSecond, Joules?)")]
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();
}
}
}
12 changes: 9 additions & 3 deletions UnitsNet.Tests/CustomCode/FuelEfficiencyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ namespace UnitsNet.Tests.CustomCode
public class FuelEfficiencyTests : FuelEfficiencyTestsBase
{
protected override bool SupportsSIUnitSystem => false;

protected override double KilometersPerLiterInOneKilometerPerLiter => 1;
protected override double LitersPer100KilometersInOneKilometerPerLiter => 100;
protected override double MilesPerUkGallonInOneKilometerPerLiter => 2.824809363318222;
protected override double MilesPerUsGallonInOneKilometerPerLiter => 2.352145833333333;


[Fact(Skip = "The SI unit would have to be MeterPerCubicMeter")]
public override void BaseUnit_HasSIBase()
{
base.BaseUnit_HasSIBase();
}

[Fact(Skip = "The SI unit would have to be MeterPerCubicMeter")]
public override void Ctor_SIUnitSystem_ReturnsQuantityWithSIUnits()
{
Expand All @@ -54,5 +54,11 @@ 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();
}
}
}

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

Loading