Skip to content

Commit be9c772

Browse files
lipchevangularsen
andauthored
Replace As(UnitSystem) and ToUnit(UnitSystem) with extension methods (#1600)
- refactored the `As(UnitSystem)` and `ToUnit(UnitSystem)` methods as extensions - removed the `As(UnitSystem)` method from the `IQuantity` interface - Moved untyped `ToUnit(UnitSystem)` from `IQuantity` to extension method named `ToUnitUntyped(UnitSystem)` --------- Co-authored-by: Andreas Gullberg Larsen <andreas.larsen84@gmail.com>
1 parent 1d2645b commit be9c772

File tree

264 files changed

+3971
-3994
lines changed

Some content is hidden

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

264 files changed

+3971
-3994
lines changed

CodeGen/Generators/UnitsNetGen/QuantityGenerator.cs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,15 +1034,6 @@ public double As(UnitKey unitKey)
10341034
}}
10351035
");
10361036

1037-
Writer.WL( $@"
1038-
1039-
/// <inheritdoc cref=""IQuantity.As(UnitSystem)""/>
1040-
public double As(UnitSystem unitSystem)
1041-
{{
1042-
return As(Info.GetDefaultUnit(unitSystem));
1043-
}}
1044-
");
1045-
10461037
Writer.WL($@"
10471038
/// <summary>
10481039
/// Converts this {_quantity.Name} to another {_quantity.Name} with the unit representation <paramref name=""unit"" />.
@@ -1140,14 +1131,6 @@ private bool TryToUnit({_unitEnumName} unit, [NotNullWhen(true)] out {_quantity.
11401131
return true;
11411132
}}
11421133
");
1143-
Writer.WL($@"
1144-
/// <inheritdoc cref=""IQuantity.ToUnit(UnitSystem)""/>
1145-
public {_quantity.Name} ToUnit(UnitSystem unitSystem)
1146-
{{
1147-
return ToUnit(Info.GetDefaultUnit(unitSystem));
1148-
}}
1149-
");
1150-
11511134
Writer.WL($@"
11521135
#region Explicit implementations
11531136
@@ -1168,14 +1151,11 @@ IQuantity IQuantity.ToUnit(Enum unit)
11681151
return ToUnit(typedUnit, DefaultConversionFunctions);
11691152
}}
11701153
1171-
/// <inheritdoc />
1172-
IQuantity IQuantity.ToUnit(UnitSystem unitSystem) => ToUnit(unitSystem);
1173-
11741154
/// <inheritdoc />
11751155
IQuantity<{_unitEnumName}> IQuantity<{_unitEnumName}>.ToUnit({_unitEnumName} unit) => ToUnit(unit);
11761156
11771157
/// <inheritdoc />
1178-
IQuantity<{_unitEnumName}> IQuantity<{_unitEnumName}>.ToUnit(UnitSystem unitSystem) => ToUnit(unitSystem);
1158+
IQuantity<{_unitEnumName}> IQuantity<{_unitEnumName}>.ToUnit(UnitSystem unitSystem) => this.ToUnit(unitSystem);
11791159
11801160
#endregion
11811161

CodeGen/Generators/UnitsNetGen/UnitTestBaseClassGenerator.cs

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ public void As_UnitSystem_ThrowsArgumentNullExceptionIfNull()
410410
}}
411411
412412
[Fact]
413-
public void ToUnitSystem_ReturnsValueInDimensionlessUnit()
413+
public void ToUnit_UnitSystem_ReturnsValueInDimensionlessUnit()
414414
{{
415415
Assert.Multiple(() =>
416416
{{
@@ -428,33 +428,37 @@ public void ToUnitSystem_ReturnsValueInDimensionlessUnit()
428428
429429
Assert.Equal({_baseUnitFullName}, convertedQuantity.Unit);
430430
Assert.Equal(quantity.Value, convertedQuantity.Value);
431-
}}, () =>
432-
{{
433-
IQuantity quantity = new {_quantity.Name}(value: 1, unit: {_baseUnitFullName});
431+
}});
432+
}}
433+
434+
[Fact]
435+
public void ToUnitUntyped_UnitSystem_ReturnsValueInDimensionlessUnit()
436+
{{
437+
IQuantity quantity = new {_quantity.Name}(value: 1, unit: {_baseUnitFullName});
434438
435-
IQuantity convertedQuantity = quantity.ToUnit(UnitSystem.SI);
439+
IQuantity convertedQuantity = quantity.ToUnitUntyped(UnitSystem.SI);
436440
437-
Assert.Equal({_baseUnitFullName}, convertedQuantity.Unit);
438-
Assert.Equal(quantity.Value, convertedQuantity.Value);
439-
}});
441+
Assert.Equal({_baseUnitFullName}, convertedQuantity.Unit);
442+
Assert.Equal(quantity.Value, convertedQuantity.Value);
440443
}}
441444
445+
442446
[Fact]
443447
public void ToUnit_UnitSystem_ThrowsArgumentNullExceptionIfNull()
444448
{{
445449
UnitSystem nullUnitSystem = null!;
446450
Assert.Multiple(() =>
447451
{{
448452
var quantity = new {_quantity.Name}(value: 1, unit: {_quantity.Name}.BaseUnit);
449-
Assert.Throws<ArgumentNullException>(() => quantity.ToUnit(nullUnitSystem));
453+
Assert.Throws<ArgumentNullException>(() => quantity.ToUnitUntyped(nullUnitSystem));
450454
}}, () =>
451455
{{
452456
IQuantity<{_unitEnumName}> quantity = new {_quantity.Name}(value: 1, unit: {_quantity.Name}.BaseUnit);
453-
Assert.Throws<ArgumentNullException>(() => quantity.ToUnit(nullUnitSystem));
457+
Assert.Throws<ArgumentNullException>(() => quantity.ToUnitUntyped(nullUnitSystem));
454458
}}, () =>
455459
{{
456460
IQuantity quantity = new {_quantity.Name}(value: 1, unit: {_quantity.Name}.BaseUnit);
457-
Assert.Throws<ArgumentNullException>(() => quantity.ToUnit(nullUnitSystem));
461+
Assert.Throws<ArgumentNullException>(() => quantity.ToUnitUntyped(nullUnitSystem));
458462
}});
459463
}}
460464
");
@@ -520,15 +524,22 @@ public virtual void ToUnit_UnitSystem_SI_ReturnsQuantityInSIUnits()
520524
521525
Assert.Equal(expectedUnit, convertedQuantity.Unit);
522526
Assert.Equal(expectedValue, convertedQuantity.Value);
523-
}}, () =>
524-
{{
525-
IQuantity quantityToConvert = quantity;
527+
}});
528+
}}
526529
527-
IQuantity convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI);
530+
[Fact]
531+
public virtual void ToUnitUntyped_UnitSystem_SI_ReturnsQuantityInSIUnits()
532+
{{
533+
var quantity = new {_quantity.Name}(value: 1, unit: {_quantity.Name}.BaseUnit);
534+
var expectedUnit = {_quantity.Name}.Info.GetDefaultUnit(UnitSystem.SI);
535+
var expectedValue = quantity.As(expectedUnit);
528536
529-
Assert.Equal(expectedUnit, convertedQuantity.Unit);
530-
Assert.Equal(expectedValue, convertedQuantity.Value);
531-
}});
537+
IQuantity quantityToConvert = quantity;
538+
539+
IQuantity convertedQuantity = quantityToConvert.ToUnitUntyped(UnitSystem.SI);
540+
541+
Assert.Equal(expectedUnit, convertedQuantity.Unit);
542+
Assert.Equal(expectedValue, convertedQuantity.Value);
532543
}}
533544
534545
[Fact]
@@ -543,13 +554,17 @@ public void ToUnit_UnitSystem_ThrowsArgumentNullExceptionIfNull()
543554
{{
544555
IQuantity<{_unitEnumName}> quantity = new {_quantity.Name}(value: 1, unit: {_quantity.Name}.BaseUnit);
545556
Assert.Throws<ArgumentNullException>(() => quantity.ToUnit(nullUnitSystem));
546-
}}, () =>
547-
{{
548-
IQuantity quantity = new {_quantity.Name}(value: 1, unit: {_quantity.Name}.BaseUnit);
549-
Assert.Throws<ArgumentNullException>(() => quantity.ToUnit(nullUnitSystem));
550557
}});
551558
}}
552559
560+
[Fact]
561+
public void ToUnitUntyped_UnitSystem_ThrowsArgumentNullExceptionIfNull()
562+
{{
563+
UnitSystem nullUnitSystem = null!;
564+
IQuantity quantity = new {_quantity.Name}(value: 1, unit: {_quantity.Name}.BaseUnit);
565+
Assert.Throws<ArgumentNullException>(() => quantity.ToUnitUntyped(nullUnitSystem));
566+
}}
567+
553568
[Fact]
554569
public void ToUnit_UnitSystem_ThrowsArgumentExceptionIfNotSupported()
555570
{{
@@ -562,12 +577,16 @@ public void ToUnit_UnitSystem_ThrowsArgumentExceptionIfNotSupported()
562577
{{
563578
IQuantity<{_unitEnumName}> quantity = new {_quantity.Name}(value: 1, unit: {_quantity.Name}.BaseUnit);
564579
Assert.Throws<ArgumentException>(() => quantity.ToUnit(unsupportedUnitSystem));
565-
}}, () =>
566-
{{
567-
IQuantity quantity = new {_quantity.Name}(value: 1, unit: {_quantity.Name}.BaseUnit);
568-
Assert.Throws<ArgumentException>(() => quantity.ToUnit(unsupportedUnitSystem));
569580
}});
570581
}}
582+
583+
[Fact]
584+
public void ToUnitUntyped_UnitSystem_ThrowsArgumentExceptionIfNotSupported()
585+
{{
586+
var unsupportedUnitSystem = new UnitSystem(UnsupportedBaseUnits);
587+
IQuantity quantity = new {_quantity.Name}(value: 1, unit: {_quantity.Name}.BaseUnit);
588+
Assert.Throws<ArgumentException>(() => quantity.ToUnitUntyped(unsupportedUnitSystem));
589+
}}
571590
");
572591
}
573592

UnitsNet.Tests/CustomCode/ElectricApparentEnergyTests.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,29 @@ public override void Ctor_SIUnitSystem_ReturnsQuantityWithSIUnits()
3737
{
3838
base.Ctor_SIUnitSystem_ReturnsQuantityWithSIUnits();
3939
}
40-
40+
4141
[Fact(Skip = "See about changing the BaseUnit to VoltampereSecond or Joule (adding the unit)")]
4242
public override void BaseUnit_HasSIBase()
4343
{
4444
base.BaseUnit_HasSIBase();
4545
}
46-
46+
4747
[Fact(Skip = "See about adding an SI unit (VoltampereSecond, Joules?)")]
4848
public override void As_UnitSystem_SI_ReturnsQuantityInSIUnits()
4949
{
5050
base.As_UnitSystem_SI_ReturnsQuantityInSIUnits();
5151
}
52-
52+
5353
[Fact(Skip = "See about adding an SI unit (VoltampereSecond, Joules?)")]
5454
public override void ToUnit_UnitSystem_SI_ReturnsQuantityInSIUnits()
5555
{
5656
base.ToUnit_UnitSystem_SI_ReturnsQuantityInSIUnits();
5757
}
58+
59+
[Fact(Skip = "See about adding an SI unit (VoltampereSecond, Joules?)")]
60+
public override void ToUnitUntyped_UnitSystem_SI_ReturnsQuantityInSIUnits()
61+
{
62+
base.ToUnitUntyped_UnitSystem_SI_ReturnsQuantityInSIUnits();
63+
}
5864
}
5965
}

UnitsNet.Tests/CustomCode/ElectricReactiveEnergyTests.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,29 @@ public override void Ctor_SIUnitSystem_ReturnsQuantityWithSIUnits()
3636
{
3737
base.Ctor_SIUnitSystem_ReturnsQuantityWithSIUnits();
3838
}
39-
39+
4040
[Fact(Skip = "See about changing the BaseUnit to VoltampereReactiveSecond or Joule (adding the unit)")]
4141
public override void BaseUnit_HasSIBase()
4242
{
4343
base.BaseUnit_HasSIBase();
4444
}
45-
45+
4646
[Fact(Skip = "See about adding an SI unit (VoltampereReactiveSecond, Joules?)")]
4747
public override void As_UnitSystem_SI_ReturnsQuantityInSIUnits()
4848
{
4949
base.As_UnitSystem_SI_ReturnsQuantityInSIUnits();
5050
}
51-
51+
5252
[Fact(Skip = "See about adding an SI unit (VoltampereReactiveSecond, Joules?)")]
5353
public override void ToUnit_UnitSystem_SI_ReturnsQuantityInSIUnits()
5454
{
5555
base.ToUnit_UnitSystem_SI_ReturnsQuantityInSIUnits();
5656
}
57+
58+
[Fact(Skip = "See about adding an SI unit (VoltampereReactiveSecond, Joules?)")]
59+
public override void ToUnitUntyped_UnitSystem_SI_ReturnsQuantityInSIUnits()
60+
{
61+
base.ToUnitUntyped_UnitSystem_SI_ReturnsQuantityInSIUnits();
62+
}
5763
}
5864
}

UnitsNet.Tests/CustomCode/FuelEfficiencyTests.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,19 @@ namespace UnitsNet.Tests.CustomCode
2424
public class FuelEfficiencyTests : FuelEfficiencyTestsBase
2525
{
2626
protected override bool SupportsSIUnitSystem => false;
27-
27+
2828
protected override double KilometersPerLiterInOneKilometerPerLiter => 1;
2929
protected override double LitersPer100KilometersInOneKilometerPerLiter => 100;
3030
protected override double MilesPerUkGallonInOneKilometerPerLiter => 2.824809363318222;
3131
protected override double MilesPerUsGallonInOneKilometerPerLiter => 2.352145833333333;
32-
32+
3333

3434
[Fact(Skip = "The SI unit would have to be MeterPerCubicMeter")]
3535
public override void BaseUnit_HasSIBase()
3636
{
3737
base.BaseUnit_HasSIBase();
3838
}
39-
39+
4040
[Fact(Skip = "The SI unit would have to be MeterPerCubicMeter")]
4141
public override void Ctor_SIUnitSystem_ReturnsQuantityWithSIUnits()
4242
{
@@ -54,5 +54,11 @@ public override void ToUnit_UnitSystem_SI_ReturnsQuantityInSIUnits()
5454
{
5555
base.ToUnit_UnitSystem_SI_ReturnsQuantityInSIUnits();
5656
}
57+
58+
[Fact(Skip = "The SI unit would have to be MeterPerCubicMeter")]
59+
public override void ToUnitUntyped_UnitSystem_SI_ReturnsQuantityInSIUnits()
60+
{
61+
base.ToUnitUntyped_UnitSystem_SI_ReturnsQuantityInSIUnits();
62+
}
5763
}
5864
}

UnitsNet.Tests/GeneratedCode/TestsBase/AbsorbedDoseOfIonizingRadiationTestsBase.g.cs

Lines changed: 30 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)