-
Notifications
You must be signed in to change notification settings - Fork 403
Replace ToUnit(UnitSystem) from the IQuantity<TUnit> interface with an extension method #1606
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
Merged
angularsen
merged 12 commits into
angularsen:master
from
lipchev:unit-system-extensions
Sep 21, 2025
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
50994bb
- refeactored the As(UnitSystem) and ToUnit(UnitSystem) methods as ex…
lipchev 79570f5
Refactor untyped IQuantity.ToUnit(UnitSystem) to extension method
angularsen 1bbaafc
Rename to ToUnitUntyped()
angularsen 8ab6e68
Update tests after rename to ToUnitUntyped
angularsen a9ca1d8
Merge branch 'master' into unit-system-extensions
angularsen a6c3337
Merge branch 'angularsen:master' into unit-system-extensions
lipchev 9b41886
- replaced the ToUnit(UnitSystem) overload from the IQuantity<TUnit> …
lipchev e36f77a
Merge branch 'master' into unit-system-extensions
lipchev 163ccc1
Merge branch 'master' into unit-system-extensions
lipchev 6615f98
Merge branch 'master' into unit-system-extensions
lipchev 6bb44a4
Merge branch 'master' into unit-system-extensions
lipchev 0e3d49d
Merge branch 'master' into unit-system-extensions
angularsen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,31 +1,84 @@ | ||
| // Licensed under MIT No Attribution, see LICENSE file at the root. | ||
| // Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet. | ||
|
|
||
| using System; | ||
| using System.Linq; | ||
| using Xunit; | ||
| namespace UnitsNet.Tests; | ||
|
|
||
| namespace UnitsNet.Tests | ||
| // ReSharper disable once InconsistentNaming | ||
| public partial class IQuantityTests | ||
| { | ||
| // ReSharper disable once InconsistentNaming | ||
| public partial class IQuantityTests | ||
| [Fact] | ||
| public void As_GivenWrongUnitType_ThrowsArgumentException() | ||
| { | ||
| [Fact] | ||
| public void As_GivenWrongUnitType_ThrowsArgumentException() | ||
| Assert.All(Quantity.Infos.Select(x => x.Zero), quantity => { Assert.Throws<ArgumentException>(() => quantity.As(ComparisonType.Absolute)); }); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ToUnit_GivenWrongUnitType_ThrowsArgumentException() | ||
| { | ||
| Assert.All(Quantity.Infos.Select(x => x.Zero), quantity => { Assert.Throws<ArgumentException>(() => quantity.ToUnit(ComparisonType.Absolute)); }); | ||
| } | ||
|
|
||
| [Fact] | ||
| public virtual void ToUnit_UnitSystem_SI_ReturnsQuantityInSIUnits() | ||
| { | ||
| var quantity = new Mass(1, Mass.BaseUnit); | ||
| MassUnit expectedUnit = Mass.Info.GetDefaultUnit(UnitSystem.SI); | ||
| var expectedValue = quantity.As(expectedUnit); | ||
|
|
||
| Assert.Multiple(() => | ||
| { | ||
| IQuantity<MassUnit> quantityToConvert = quantity; | ||
|
|
||
| IQuantity<MassUnit> convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI); | ||
|
|
||
| Assert.Equal(expectedUnit, convertedQuantity.Unit); | ||
| Assert.Equal(expectedValue, convertedQuantity.Value); | ||
| }, () => | ||
| { | ||
| IQuantity quantityToConvert = quantity; | ||
|
|
||
| IQuantity convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI); | ||
|
|
||
| Assert.Equal(expectedUnit, convertedQuantity.Unit); | ||
| Assert.Equal(expectedValue, convertedQuantity.Value); | ||
| }); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ToUnit_UnitSystem_ThrowsArgumentNullExceptionIfNull() | ||
| { | ||
| UnitSystem nullUnitSystem = null!; | ||
| Assert.Multiple(() => | ||
| { | ||
| IQuantity<MassUnit> quantity = new Mass(1, Mass.BaseUnit); | ||
| Assert.Throws<ArgumentNullException>(() => quantity.ToUnit(nullUnitSystem)); | ||
| }, () => | ||
| { | ||
| IQuantity quantity = new Mass(1, Mass.BaseUnit); | ||
| Assert.Throws<ArgumentNullException>(() => quantity.ToUnit(nullUnitSystem)); | ||
| }); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ToUnit_UnitSystem_ThrowsArgumentExceptionIfNotSupported() | ||
| { | ||
| var unsupportedUnitSystem = new UnitSystem(new BaseUnits( | ||
| (LengthUnit)(-1), | ||
| (MassUnit)(-1), | ||
| (DurationUnit)(-1), | ||
| (ElectricCurrentUnit)(-1), | ||
| (TemperatureUnit)(-1), | ||
| (AmountOfSubstanceUnit)(-1), | ||
| (LuminousIntensityUnit)(-1))); | ||
|
|
||
| Assert.Multiple(() => | ||
| { | ||
| Assert.All(Quantity.Infos.Select(x => x.Zero), quantity => | ||
| { | ||
| Assert.Throws<ArgumentException>(() => quantity.As(ComparisonType.Absolute)); | ||
| }); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ToUnit_GivenWrongUnitType_ThrowsArgumentException() | ||
| IQuantity<MassUnit> quantity = new Mass(1, Mass.BaseUnit); | ||
| Assert.Throws<ArgumentException>(() => quantity.ToUnit(unsupportedUnitSystem)); | ||
| }, () => | ||
| { | ||
| Assert.All(Quantity.Infos.Select(x => x.Zero), quantity => | ||
| { | ||
| Assert.Throws<ArgumentException>(() => quantity.ToUnit(ComparisonType.Absolute)); | ||
| }); | ||
| } | ||
| IQuantity quantity = new Mass(1, Mass.BaseUnit); | ||
| Assert.Throws<ArgumentException>(() => quantity.ToUnit(unsupportedUnitSystem)); | ||
| }); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've only kept the concrete part of this test, but on second thought there shouldn't really be any need for it to exist here either- as we already have tests for the
UnitSystemconstructor.In case we decide to move forward with the removal of the interface methods from the interface in
v6, then these tests should probably go to another test file- something likeQuantityExtensionsTests.