Skip to content

Commit

Permalink
Improving the ToUnit test coverage (#1493)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
lipchev authored Jan 4, 2025
1 parent 572f151 commit 9fb882e
Show file tree
Hide file tree
Showing 128 changed files with 4,087 additions and 815 deletions.
40 changes: 33 additions & 7 deletions CodeGen/Generators/UnitsNetGen/UnitTestBaseClassGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -797,16 +797,16 @@ public void ToUnit_WithSameUnits_AreEqual({_unitEnumName} unit)
Assert.Equal(quantity, toUnitWithSameUnit);
}}
[Theory{(_quantity.Units.Length == 1 ? "(Skip = \"Multiple units required\")" : string.Empty)}]
[Theory]
[MemberData(nameof(UnitTypes))]
public void ToUnit_FromNonBaseUnit_ReturnsQuantityWithGivenUnit({_unitEnumName} unit)
{{
// See if there is a unit available that is not the base unit, fallback to base unit if it has only a single unit.
var fromUnit = {_quantity.Name}.Units.First(u => u != {_quantity.Name}.BaseUnit);
var quantity = {_quantity.Name}.From(3.0, fromUnit);
var converted = quantity.ToUnit(unit);
Assert.Equal(converted.Unit, unit);
Assert.All({_quantity.Name}.Units.Where(u => u != {_quantity.Name}.BaseUnit), fromUnit =>
{{
var quantity = {_quantity.Name}.From(3.0, fromUnit);
var converted = quantity.ToUnit(unit);
Assert.Equal(converted.Unit, unit);
}});
}}
[Theory]
Expand All @@ -818,6 +818,25 @@ public virtual void ToUnit_FromDefaultQuantity_ReturnsQuantityWithGivenUnit({_un
Assert.Equal(converted.Unit, unit);
}}
[Theory]
[MemberData(nameof(UnitTypes))]
public void ToUnit_FromIQuantity_ReturnsTheExpectedIQuantity({_unitEnumName} unit)
{{
var quantity = {_quantity.Name}.From(3, {_quantity.Name}.BaseUnit);
{_quantity.Name} expectedQuantity = quantity.ToUnit(unit);
Assert.Multiple(() =>
{{
IQuantity<{_unitEnumName}> quantityToConvert = quantity;
IQuantity<{_unitEnumName}> convertedQuantity = quantityToConvert.ToUnit(unit);
Assert.Equal(unit, convertedQuantity.Unit);
}}, () =>
{{
IQuantity quantityToConvert = quantity;
IQuantity convertedQuantity = quantityToConvert.ToUnit(unit);
Assert.Equal(unit, convertedQuantity.Unit);
}});
}}
[Fact]
public void ConversionRoundTrip()
{{
Expand Down Expand Up @@ -1213,6 +1232,13 @@ public void Convert_ChangeType_InvalidType_ThrowsInvalidCastException()
Assert.Throws<InvalidCastException>(() => Convert.ChangeType(quantity, typeof(QuantityFormatter)));
}}
[Fact]
public void Convert_GetTypeCode_Returns_Object()
{{
var quantity = {_quantity.Name}.From{_baseUnit.PluralName}(1.0);
Assert.Equal(TypeCode.Object, Convert.GetTypeCode(quantity));
}}
[Fact]
public void GetHashCode_Equals()
{{
Expand Down
48 changes: 9 additions & 39 deletions UnitsNet.Tests/CustomCode/IQuantityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
// Copyright 2013 Andreas Gullberg Larsen ([email protected]). Maintained at https://github.com/angularsen/UnitsNet.

using System;
using System.Diagnostics.CodeAnalysis;
using UnitsNet.Units;
using System.Linq;
using Xunit;

namespace UnitsNet.Tests
Expand All @@ -14,48 +13,19 @@ public partial class IQuantityTests
[Fact]
public void As_GivenWrongUnitType_ThrowsArgumentException()
{
IQuantity length = Length.FromMeters(1.2345);
Assert.Throws<ArgumentException>(() => length.As(MassUnit.Kilogram));
}

[Fact]
[SuppressMessage("ReSharper", "AssignNullToNotNullAttribute")]
public void As_GivenNullUnitSystem_ThrowsArgumentNullException()
{
IQuantity imperialLengthQuantity = new Length(2.0, LengthUnit.Inch);
Assert.Throws<ArgumentNullException>(() => imperialLengthQuantity.As((UnitSystem)null!));
}

[Fact]
public void As_GivenSIUnitSystem_ReturnsSIValue()
{
IQuantity inches = new Length(2.0, LengthUnit.Inch);
Assert.Equal(0.0508, inches.As(UnitSystem.SI));
Assert.All(Quantity.Infos.Select(x => x.Zero), quantity =>
{
Assert.Throws<ArgumentException>(() => quantity.As(ComparisonType.Absolute));
});
}

[Fact]
public void ToUnit_GivenWrongUnitType_ThrowsArgumentException()
{
IQuantity length = Length.FromMeters(1.2345);
Assert.Throws<ArgumentException>(() => length.ToUnit(MassUnit.Kilogram));
}

[Fact]
public void ToUnit_GivenNullUnitSystem_ThrowsArgumentNullException()
{
IQuantity imperialLengthQuantity = new Length(2.0, LengthUnit.Inch);
Assert.Throws<ArgumentNullException>(() => imperialLengthQuantity.ToUnit((UnitSystem)null!));
}

[Fact]
public void ToUnit_GivenSIUnitSystem_ReturnsSIQuantity()
{
IQuantity inches = new Length(2.0, LengthUnit.Inch);

IQuantity inSI = inches.ToUnit(UnitSystem.SI);

Assert.Equal(0.0508, inSI.Value);
Assert.Equal(LengthUnit.Meter, inSI.Unit);
Assert.All(Quantity.Infos.Select(x => x.Zero), quantity =>
{
Assert.Throws<ArgumentException>(() => quantity.ToUnit(ComparisonType.Absolute));
});
}
}
}

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

38 changes: 32 additions & 6 deletions UnitsNet.Tests/GeneratedCode/TestsBase/AccelerationTestsBase.g.cs

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

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

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

Loading

0 comments on commit 9fb882e

Please sign in to comment.