Skip to content

Commit 9fb882e

Browse files
authored
Improving the ToUnit test coverage (#1493)
Improving the `ToUnit` test coverage: - `ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit` now also tests the non-base to non-base conversions - `ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit` is no longer skipped for quantities with a single unit (the test is still generated) - added the `ToUnit_FromIQuantity_ReturnsTheExpectedIQuantity` test: covering the `IQuantity` interfaces - added the `Convert_GetTypeCode_Returns_Object` test (completing the `IConvertible` interface coverage) - `IQuantityTests`: removed the redundant `UnitSystem` tests and updated "wrong unit type" tests, using all quantities
1 parent 572f151 commit 9fb882e

File tree

128 files changed

+4087
-815
lines changed

Some content is hidden

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

128 files changed

+4087
-815
lines changed

CodeGen/Generators/UnitsNetGen/UnitTestBaseClassGenerator.cs

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -797,16 +797,16 @@ public void ToUnit_WithSameUnits_AreEqual({_unitEnumName} unit)
797797
Assert.Equal(quantity, toUnitWithSameUnit);
798798
}}
799799
800-
[Theory{(_quantity.Units.Length == 1 ? "(Skip = \"Multiple units required\")" : string.Empty)}]
800+
[Theory]
801801
[MemberData(nameof(UnitTypes))]
802802
public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit({_unitEnumName} unit)
803803
{{
804-
// See if there is a unit available that is not the base unit, fallback to base unit if it has only a single unit.
805-
var fromUnit = {_quantity.Name}.Units.First(u => u != {_quantity.Name}.BaseUnit);
806-
807-
var quantity = {_quantity.Name}.From(3.0, fromUnit);
808-
var converted = quantity.ToUnit(unit);
809-
Assert.Equal(converted.Unit, unit);
804+
Assert.All({_quantity.Name}.Units.Where(u => u != {_quantity.Name}.BaseUnit), fromUnit =>
805+
{{
806+
var quantity = {_quantity.Name}.From(3.0, fromUnit);
807+
var converted = quantity.ToUnit(unit);
808+
Assert.Equal(converted.Unit, unit);
809+
}});
810810
}}
811811
812812
[Theory]
@@ -818,6 +818,25 @@ public virtual void ToUnit_FromDefaultQuantity_ReturnsQuantityWithGivenUnit({_un
818818
Assert.Equal(converted.Unit, unit);
819819
}}
820820
821+
[Theory]
822+
[MemberData(nameof(UnitTypes))]
823+
public void ToUnit_FromIQuantity_ReturnsTheExpectedIQuantity({_unitEnumName} unit)
824+
{{
825+
var quantity = {_quantity.Name}.From(3, {_quantity.Name}.BaseUnit);
826+
{_quantity.Name} expectedQuantity = quantity.ToUnit(unit);
827+
Assert.Multiple(() =>
828+
{{
829+
IQuantity<{_unitEnumName}> quantityToConvert = quantity;
830+
IQuantity<{_unitEnumName}> convertedQuantity = quantityToConvert.ToUnit(unit);
831+
Assert.Equal(unit, convertedQuantity.Unit);
832+
}}, () =>
833+
{{
834+
IQuantity quantityToConvert = quantity;
835+
IQuantity convertedQuantity = quantityToConvert.ToUnit(unit);
836+
Assert.Equal(unit, convertedQuantity.Unit);
837+
}});
838+
}}
839+
821840
[Fact]
822841
public void ConversionRoundTrip()
823842
{{
@@ -1213,6 +1232,13 @@ public void Convert_ChangeType_InvalidType_ThrowsInvalidCastException()
12131232
Assert.Throws<InvalidCastException>(() => Convert.ChangeType(quantity, typeof(QuantityFormatter)));
12141233
}}
12151234
1235+
[Fact]
1236+
public void Convert_GetTypeCode_Returns_Object()
1237+
{{
1238+
var quantity = {_quantity.Name}.From{_baseUnit.PluralName}(1.0);
1239+
Assert.Equal(TypeCode.Object, Convert.GetTypeCode(quantity));
1240+
}}
1241+
12161242
[Fact]
12171243
public void GetHashCode_Equals()
12181244
{{

UnitsNet.Tests/CustomCode/IQuantityTests.cs

Lines changed: 9 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
// Copyright 2013 Andreas Gullberg Larsen ([email protected]). Maintained at https://github.com/angularsen/UnitsNet.
33

44
using System;
5-
using System.Diagnostics.CodeAnalysis;
6-
using UnitsNet.Units;
5+
using System.Linq;
76
using Xunit;
87

98
namespace UnitsNet.Tests
@@ -14,48 +13,19 @@ public partial class IQuantityTests
1413
[Fact]
1514
public void As_GivenWrongUnitType_ThrowsArgumentException()
1615
{
17-
IQuantity length = Length.FromMeters(1.2345);
18-
Assert.Throws<ArgumentException>(() => length.As(MassUnit.Kilogram));
19-
}
20-
21-
[Fact]
22-
[SuppressMessage("ReSharper", "AssignNullToNotNullAttribute")]
23-
public void As_GivenNullUnitSystem_ThrowsArgumentNullException()
24-
{
25-
IQuantity imperialLengthQuantity = new Length(2.0, LengthUnit.Inch);
26-
Assert.Throws<ArgumentNullException>(() => imperialLengthQuantity.As((UnitSystem)null!));
27-
}
28-
29-
[Fact]
30-
public void As_GivenSIUnitSystem_ReturnsSIValue()
31-
{
32-
IQuantity inches = new Length(2.0, LengthUnit.Inch);
33-
Assert.Equal(0.0508, inches.As(UnitSystem.SI));
16+
Assert.All(Quantity.Infos.Select(x => x.Zero), quantity =>
17+
{
18+
Assert.Throws<ArgumentException>(() => quantity.As(ComparisonType.Absolute));
19+
});
3420
}
3521

3622
[Fact]
3723
public void ToUnit_GivenWrongUnitType_ThrowsArgumentException()
3824
{
39-
IQuantity length = Length.FromMeters(1.2345);
40-
Assert.Throws<ArgumentException>(() => length.ToUnit(MassUnit.Kilogram));
41-
}
42-
43-
[Fact]
44-
public void ToUnit_GivenNullUnitSystem_ThrowsArgumentNullException()
45-
{
46-
IQuantity imperialLengthQuantity = new Length(2.0, LengthUnit.Inch);
47-
Assert.Throws<ArgumentNullException>(() => imperialLengthQuantity.ToUnit((UnitSystem)null!));
48-
}
49-
50-
[Fact]
51-
public void ToUnit_GivenSIUnitSystem_ReturnsSIQuantity()
52-
{
53-
IQuantity inches = new Length(2.0, LengthUnit.Inch);
54-
55-
IQuantity inSI = inches.ToUnit(UnitSystem.SI);
56-
57-
Assert.Equal(0.0508, inSI.Value);
58-
Assert.Equal(LengthUnit.Meter, inSI.Unit);
25+
Assert.All(Quantity.Infos.Select(x => x.Zero), quantity =>
26+
{
27+
Assert.Throws<ArgumentException>(() => quantity.ToUnit(ComparisonType.Absolute));
28+
});
5929
}
6030
}
6131
}

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

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

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

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

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

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

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

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

0 commit comments

Comments
 (0)