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

Conversation

lipchev
Copy link
Collaborator

@lipchev lipchev commented Jan 4, 2025

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

- 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)
- IQuantity tests: removed the redundant UnitSystem tests and updated "wrong unit type" tests, using all quantities
Comment on lines -800 to 810
[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);
}});
}}
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

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 )

@lipchev
Copy link
Collaborator Author

lipchev commented Jan 4, 2025

I also noted the coverage:

Information
Parser: MultiReport (3x DotCover)
Assemblies: 3
Classes: 298
Files: 304
Line coverage
89%
Covered lines: 31176
Uncovered lines: 3812
Coverable lines: 34988
Total lines: 167654
Line coverage: 89.1%

and now:

Information
Parser: MultiReport (3x DotCover)
Assemblies: 3
Classes: 298
Files: 304
Line coverage
91%
Covered lines: 32063
Uncovered lines: 2925
Coverable lines: 34988
Total lines: 167654
Line coverage: 91.6%

@lipchev
Copy link
Collaborator Author

lipchev commented Jan 4, 2025

There are still some To/As tests that are not at 100% but that's mostly due to the stuff related to the DefaultUnitConverter - please ignore these for now, the same tests have us covered to 100% in for my upcoming PR.

Comment on lines -800 to 810
[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);
}});
}}
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

@angularsen angularsen merged commit 9fb882e into angularsen:release/v6 Jan 4, 2025
1 check passed
@angularsen
Copy link
Owner

Nice improvement here 👏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants