Skip to content

Commit bde4d1a

Browse files
lipchevangularsen
andauthored
Replace ToUnit(UnitSystem) from the IQuantity<TUnit> interface with an extension method (#1606)
- replaced the `ToUnit(UnitSystem)` overload from the `IQuantity<TUnit>` interface with an extension method (`[Obsolete]`) - renamed the `ToUnitUntyped` extension back to `ToUnit` and made it `[Obsolete]` - replaced the generated `UnitSystem` tests for the `IQuantity` / `IQuantity<TUnit>` with a single set of tests in the `IQauntityTests` --------- Co-authored-by: Andreas Gullberg Larsen <[email protected]>
1 parent 0cf7949 commit bde4d1a

File tree

265 files changed

+752
-8151
lines changed

Some content is hidden

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

265 files changed

+752
-8151
lines changed

CodeGen/Generators/UnitsNetGen/QuantityGenerator.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,9 +1151,6 @@ IQuantity IQuantity.ToUnit(Enum unit)
11511151
/// <inheritdoc />
11521152
IQuantity<{_unitEnumName}> IQuantity<{_unitEnumName}>.ToUnit({_unitEnumName} unit) => ToUnit(unit);
11531153
1154-
/// <inheritdoc />
1155-
IQuantity<{_unitEnumName}> IQuantity<{_unitEnumName}>.ToUnit(UnitSystem unitSystem) => this.ToUnit(unitSystem);
1156-
11571154
#endregion
11581155
11591156
#endregion

CodeGen/Generators/UnitsNetGen/UnitTestBaseClassGenerator.cs

Lines changed: 10 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -412,53 +412,30 @@ public void As_UnitSystem_ThrowsArgumentNullExceptionIfNull()
412412
[Fact]
413413
public void ToUnit_UnitSystem_ReturnsValueInDimensionlessUnit()
414414
{{
415-
Assert.Multiple(() =>
416-
{{
417-
var quantity = new {_quantity.Name}(value: 1, unit: {_baseUnitFullName});
418-
419-
{_quantity.Name} convertedQuantity = quantity.ToUnit(UnitSystem.SI);
420-
421-
Assert.Equal({_baseUnitFullName}, convertedQuantity.Unit);
422-
Assert.Equal(quantity.Value, convertedQuantity.Value);
423-
}}, () =>
424-
{{
425-
IQuantity<{_unitEnumName}> quantity = new {_quantity.Name}(value: 1, unit: {_baseUnitFullName});
426-
427-
IQuantity<{_unitEnumName}> convertedQuantity = quantity.ToUnit(UnitSystem.SI);
428-
429-
Assert.Equal({_baseUnitFullName}, convertedQuantity.Unit);
430-
Assert.Equal(quantity.Value, convertedQuantity.Value);
431-
}});
432-
}}
433-
434-
[Fact]
435-
public void ToUnitUntyped_UnitSystem_ReturnsValueInDimensionlessUnit()
436-
{{
437-
IQuantity quantity = new {_quantity.Name}(value: 1, unit: {_baseUnitFullName});
415+
var quantity = new {_quantity.Name}(value: 1, unit: {_baseUnitFullName});
438416
439-
IQuantity convertedQuantity = quantity.ToUnitUntyped(UnitSystem.SI);
417+
{_quantity.Name} convertedQuantity = quantity.ToUnit(UnitSystem.SI);
440418
441419
Assert.Equal({_baseUnitFullName}, convertedQuantity.Unit);
442420
Assert.Equal(quantity.Value, convertedQuantity.Value);
443421
}}
444422
445-
446423
[Fact]
447424
public void ToUnit_UnitSystem_ThrowsArgumentNullExceptionIfNull()
448425
{{
449426
UnitSystem nullUnitSystem = null!;
450427
Assert.Multiple(() =>
451428
{{
452429
var quantity = new {_quantity.Name}(value: 1, unit: {_quantity.Name}.BaseUnit);
453-
Assert.Throws<ArgumentNullException>(() => quantity.ToUnitUntyped(nullUnitSystem));
430+
Assert.Throws<ArgumentNullException>(() => quantity.ToUnit(nullUnitSystem));
454431
}}, () =>
455432
{{
456433
IQuantity<{_unitEnumName}> quantity = new {_quantity.Name}(value: 1, unit: {_quantity.Name}.BaseUnit);
457-
Assert.Throws<ArgumentNullException>(() => quantity.ToUnitUntyped(nullUnitSystem));
434+
Assert.Throws<ArgumentNullException>(() => quantity.ToUnit(nullUnitSystem));
458435
}}, () =>
459436
{{
460437
IQuantity quantity = new {_quantity.Name}(value: 1, unit: {_quantity.Name}.BaseUnit);
461-
Assert.Throws<ArgumentNullException>(() => quantity.ToUnitUntyped(nullUnitSystem));
438+
Assert.Throws<ArgumentNullException>(() => quantity.ToUnit(nullUnitSystem));
462439
}});
463440
}}
464441
");
@@ -508,35 +485,7 @@ public virtual void ToUnit_UnitSystem_SI_ReturnsQuantityInSIUnits()
508485
var expectedUnit = {_quantity.Name}.Info.GetDefaultUnit(UnitSystem.SI);
509486
var expectedValue = quantity.As(expectedUnit);
510487
511-
Assert.Multiple(() =>
512-
{{
513-
{_quantity.Name} quantityToConvert = quantity;
514-
515-
{_quantity.Name} convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI);
516-
517-
Assert.Equal(expectedUnit, convertedQuantity.Unit);
518-
Assert.Equal(expectedValue, convertedQuantity.Value);
519-
}}, () =>
520-
{{
521-
IQuantity<{_unitEnumName}> quantityToConvert = quantity;
522-
523-
IQuantity<{_unitEnumName}> convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI);
524-
525-
Assert.Equal(expectedUnit, convertedQuantity.Unit);
526-
Assert.Equal(expectedValue, convertedQuantity.Value);
527-
}});
528-
}}
529-
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);
536-
537-
IQuantity quantityToConvert = quantity;
538-
539-
IQuantity convertedQuantity = quantityToConvert.ToUnitUntyped(UnitSystem.SI);
488+
{_quantity.Name} convertedQuantity = quantity.ToUnit(UnitSystem.SI);
540489
541490
Assert.Equal(expectedUnit, convertedQuantity.Unit);
542491
Assert.Equal(expectedValue, convertedQuantity.Value);
@@ -546,46 +495,16 @@ public virtual void ToUnitUntyped_UnitSystem_SI_ReturnsQuantityInSIUnits()
546495
public void ToUnit_UnitSystem_ThrowsArgumentNullExceptionIfNull()
547496
{{
548497
UnitSystem nullUnitSystem = null!;
549-
Assert.Multiple(() =>
550-
{{
551-
var quantity = new {_quantity.Name}(value: 1, unit: {_quantity.Name}.BaseUnit);
552-
Assert.Throws<ArgumentNullException>(() => quantity.ToUnit(nullUnitSystem));
553-
}}, () =>
554-
{{
555-
IQuantity<{_unitEnumName}> quantity = new {_quantity.Name}(value: 1, unit: {_quantity.Name}.BaseUnit);
556-
Assert.Throws<ArgumentNullException>(() => quantity.ToUnit(nullUnitSystem));
557-
}});
558-
}}
559-
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));
498+
var quantity = new {_quantity.Name}(value: 1, unit: {_quantity.Name}.BaseUnit);
499+
Assert.Throws<ArgumentNullException>(() => quantity.ToUnit(nullUnitSystem));
566500
}}
567501
568502
[Fact]
569503
public void ToUnit_UnitSystem_ThrowsArgumentExceptionIfNotSupported()
570504
{{
571505
var unsupportedUnitSystem = new UnitSystem(UnsupportedBaseUnits);
572-
Assert.Multiple(() =>
573-
{{
574-
var quantity = new {_quantity.Name}(value: 1, unit: {_quantity.Name}.BaseUnit);
575-
Assert.Throws<ArgumentException>(() => quantity.ToUnit(unsupportedUnitSystem));
576-
}}, () =>
577-
{{
578-
IQuantity<{_unitEnumName}> quantity = new {_quantity.Name}(value: 1, unit: {_quantity.Name}.BaseUnit);
579-
Assert.Throws<ArgumentException>(() => quantity.ToUnit(unsupportedUnitSystem));
580-
}});
581-
}}
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));
506+
var quantity = new {_quantity.Name}(value: 1, unit: {_quantity.Name}.BaseUnit);
507+
Assert.Throws<ArgumentException>(() => quantity.ToUnit(unsupportedUnitSystem));
589508
}}
590509
");
591510
}

UnitsNet.Tests/CustomCode/ElectricApparentEnergyTests.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,5 @@ 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-
}
6458
}
6559
}

UnitsNet.Tests/CustomCode/ElectricReactiveEnergyTests.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,5 @@ 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-
}
6357
}
6458
}

UnitsNet.Tests/CustomCode/FuelEfficiencyTests.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,5 @@ 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-
}
6357
}
6458
}
Lines changed: 74 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,84 @@
11
// Licensed under MIT No Attribution, see LICENSE file at the root.
22
// Copyright 2013 Andreas Gullberg Larsen ([email protected]). Maintained at https://github.com/angularsen/UnitsNet.
33

4-
using System;
5-
using System.Linq;
6-
using Xunit;
4+
namespace UnitsNet.Tests;
75

8-
namespace UnitsNet.Tests
6+
// ReSharper disable once InconsistentNaming
7+
public partial class IQuantityTests
98
{
10-
// ReSharper disable once InconsistentNaming
11-
public partial class IQuantityTests
9+
[Fact]
10+
public void As_GivenWrongUnitType_ThrowsArgumentException()
1211
{
13-
[Fact]
14-
public void As_GivenWrongUnitType_ThrowsArgumentException()
12+
Assert.All(Quantity.Infos.Select(x => x.Zero), quantity => { Assert.Throws<ArgumentException>(() => quantity.As(ComparisonType.Absolute)); });
13+
}
14+
15+
[Fact]
16+
public void ToUnit_GivenWrongUnitType_ThrowsArgumentException()
17+
{
18+
Assert.All(Quantity.Infos.Select(x => x.Zero), quantity => { Assert.Throws<ArgumentException>(() => quantity.ToUnit(ComparisonType.Absolute)); });
19+
}
20+
21+
[Fact]
22+
public virtual void ToUnit_UnitSystem_SI_ReturnsQuantityInSIUnits()
23+
{
24+
var quantity = new Mass(1, Mass.BaseUnit);
25+
MassUnit expectedUnit = Mass.Info.GetDefaultUnit(UnitSystem.SI);
26+
var expectedValue = quantity.As(expectedUnit);
27+
28+
Assert.Multiple(() =>
29+
{
30+
IQuantity<MassUnit> quantityToConvert = quantity;
31+
32+
IQuantity<MassUnit> convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI);
33+
34+
Assert.Equal(expectedUnit, convertedQuantity.Unit);
35+
Assert.Equal(expectedValue, convertedQuantity.Value);
36+
}, () =>
37+
{
38+
IQuantity quantityToConvert = quantity;
39+
40+
IQuantity convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI);
41+
42+
Assert.Equal(expectedUnit, convertedQuantity.Unit);
43+
Assert.Equal(expectedValue, convertedQuantity.Value);
44+
});
45+
}
46+
47+
[Fact]
48+
public void ToUnit_UnitSystem_ThrowsArgumentNullExceptionIfNull()
49+
{
50+
UnitSystem nullUnitSystem = null!;
51+
Assert.Multiple(() =>
52+
{
53+
IQuantity<MassUnit> quantity = new Mass(1, Mass.BaseUnit);
54+
Assert.Throws<ArgumentNullException>(() => quantity.ToUnit(nullUnitSystem));
55+
}, () =>
56+
{
57+
IQuantity quantity = new Mass(1, Mass.BaseUnit);
58+
Assert.Throws<ArgumentNullException>(() => quantity.ToUnit(nullUnitSystem));
59+
});
60+
}
61+
62+
[Fact]
63+
public void ToUnit_UnitSystem_ThrowsArgumentExceptionIfNotSupported()
64+
{
65+
var unsupportedUnitSystem = new UnitSystem(new BaseUnits(
66+
(LengthUnit)(-1),
67+
(MassUnit)(-1),
68+
(DurationUnit)(-1),
69+
(ElectricCurrentUnit)(-1),
70+
(TemperatureUnit)(-1),
71+
(AmountOfSubstanceUnit)(-1),
72+
(LuminousIntensityUnit)(-1)));
73+
74+
Assert.Multiple(() =>
1575
{
16-
Assert.All(Quantity.Infos.Select(x => x.Zero), quantity =>
17-
{
18-
Assert.Throws<ArgumentException>(() => quantity.As(ComparisonType.Absolute));
19-
});
20-
}
21-
22-
[Fact]
23-
public void ToUnit_GivenWrongUnitType_ThrowsArgumentException()
76+
IQuantity<MassUnit> quantity = new Mass(1, Mass.BaseUnit);
77+
Assert.Throws<ArgumentException>(() => quantity.ToUnit(unsupportedUnitSystem));
78+
}, () =>
2479
{
25-
Assert.All(Quantity.Infos.Select(x => x.Zero), quantity =>
26-
{
27-
Assert.Throws<ArgumentException>(() => quantity.ToUnit(ComparisonType.Absolute));
28-
});
29-
}
80+
IQuantity quantity = new Mass(1, Mass.BaseUnit);
81+
Assert.Throws<ArgumentException>(() => quantity.ToUnit(unsupportedUnitSystem));
82+
});
3083
}
3184
}

UnitsNet.Tests/CustomQuantities/HowMuch.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,6 @@ public double As(UnitKey unitKey)
7070
return As(unitKey.ToUnit<HowMuchUnit>());
7171
}
7272

73-
public double As(UnitSystem unitSystem) => throw new NotImplementedException();
74-
7573
public IQuantity ToUnit(Enum unit)
7674
{
7775
if (unit is HowMuchUnit howMuchUnit) return new HowMuch(As(unit), howMuchUnit);
@@ -82,13 +80,6 @@ public IQuantity<HowMuchUnit> ToUnit(HowMuchUnit unit)
8280
{
8381
throw new NotImplementedException();
8482
}
85-
86-
IQuantity<HowMuchUnit> IQuantity<HowMuchUnit>.ToUnit(UnitSystem unitSystem)
87-
{
88-
throw new NotImplementedException();
89-
}
90-
91-
public IQuantity ToUnit(UnitSystem unitSystem) => throw new NotImplementedException();
9283

9384
public override string ToString()
9485
{

0 commit comments

Comments
 (0)