Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving the ToUnit test coverage #1493

Merged
merged 1 commit into from
Jan 4, 2025
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
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);
}});
}}
Comment on lines -800 to 810
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could also skip the generation for this method for the single-unit quantities, but decided not to bother (tell me if you'd rather not have it at all).

Of course we could also put back the [Skip], however I don't think that's very useful- I'm thinking of the [Skip] as something that should be addressed, and I don't think that having a single unit is something that needs fixing (as long as it's SI 😈 )

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind much either way


[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
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These can of course be moved to the generated tests, but given that we're likely going to be removing this part of the interface, decided to just leave them here for now..

In my other project there are a few other tests here, testing for the IAdditiveIdentity and such, but I didn't think that it makes sense to bring them in without the new interface definition #1461 )

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