Skip to content

Commit 2e1c12e

Browse files
authored
IQuantity As/ToUnit method exceptions (#621)
* IQuantity As/ToUnit methods would allow casting an Enum to the wrong unit type. Throw exception. * Pretty :)
1 parent f3ec420 commit 2e1c12e

File tree

92 files changed

+1482
-455
lines changed

Some content is hidden

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

92 files changed

+1482
-455
lines changed

UnitsNet.Tests/IQuantityTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Licensed under MIT No Attribution, see LICENSE file at the root.
2+
// Copyright 2013 Andreas Gullberg Larsen ([email protected]). Maintained at https://github.com/angularsen/UnitsNet.
3+
4+
using System;
5+
using UnitsNet.Units;
6+
using Xunit;
7+
8+
namespace UnitsNet.Tests
9+
{
10+
public class IQuantityTests
11+
{
12+
[Fact]
13+
public void As_GivenWrongUnitType_ThrowsArgumentException()
14+
{
15+
IQuantity length = Length.FromMeters(1.2345);
16+
Assert.Throws<ArgumentException>(() => length.As(MassUnit.Kilogram));
17+
}
18+
19+
[Fact]
20+
public void ToUnit_GivenWrongUnitType_ThrowsArgumentException()
21+
{
22+
IQuantity length = Length.FromMeters(1.2345);
23+
Assert.Throws<ArgumentException>(() => length.ToUnit(MassUnit.Kilogram));
24+
}
25+
}
26+
}

UnitsNet/GeneratedCode/Quantities/Acceleration.NetFramework.g.cs

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

UnitsNet/GeneratedCode/Quantities/AmountOfSubstance.NetFramework.g.cs

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

UnitsNet/GeneratedCode/Quantities/AmplitudeRatio.NetFramework.g.cs

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

UnitsNet/GeneratedCode/Quantities/Angle.NetFramework.g.cs

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

UnitsNet/GeneratedCode/Quantities/ApparentEnergy.NetFramework.g.cs

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

UnitsNet/GeneratedCode/Quantities/ApparentPower.NetFramework.g.cs

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

UnitsNet/GeneratedCode/Quantities/Area.NetFramework.g.cs

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

UnitsNet/GeneratedCode/Quantities/AreaDensity.NetFramework.g.cs

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

0 commit comments

Comments
 (0)