Skip to content

Commit 751bf32

Browse files
authored
Implementing IConvertible (#596)
* Implementing IConvertible * Allow conversions to QuantityType and BaseDimensions * Skip IConvertible code for WRC
1 parent 03711d0 commit 751bf32

File tree

182 files changed

+9190
-92
lines changed

Some content is hidden

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

182 files changed

+9190
-92
lines changed
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
using System;
2+
using UnitsNet.Units;
3+
using Xunit;
4+
5+
namespace UnitsNet.Tests
6+
{
7+
public class QuantityIConvertibleTests
8+
{
9+
private static Length length = Length.FromMeters(3.0);
10+
private static IConvertible lengthAsIConvertible = Length.FromMeters(3.0);
11+
12+
[Fact]
13+
public void GetTypeCodeTest()
14+
{
15+
Assert.Equal(TypeCode.Object, lengthAsIConvertible.GetTypeCode());
16+
Assert.Equal(TypeCode.Object, Convert.GetTypeCode(length));
17+
}
18+
19+
[Fact]
20+
public void ToBooleanTest()
21+
{
22+
Assert.Throws<InvalidCastException>(() => lengthAsIConvertible.ToBoolean(null));
23+
Assert.Throws<InvalidCastException>(() => Convert.ToBoolean(length));
24+
Assert.Throws<InvalidCastException>(() => Convert.ChangeType(length, typeof(bool)));
25+
}
26+
27+
[Fact]
28+
public void ToByteTest()
29+
{
30+
byte expected = 3;
31+
Assert.Equal(expected, lengthAsIConvertible.ToByte(null));
32+
Assert.Equal(expected, Convert.ToByte(length));
33+
Assert.Equal(expected, Convert.ChangeType(length, typeof(byte)));
34+
}
35+
36+
[Fact]
37+
public void ToCharTest()
38+
{
39+
Assert.Throws<InvalidCastException>(() => lengthAsIConvertible.ToChar(null));
40+
Assert.Throws<InvalidCastException>(() => Convert.ToChar(length));
41+
Assert.Throws<InvalidCastException>(() => Convert.ChangeType(length, typeof(char)));
42+
}
43+
44+
[Fact]
45+
public void ToDateTimeTest()
46+
{
47+
Assert.Throws<InvalidCastException>(() => lengthAsIConvertible.ToDateTime(null));
48+
Assert.Throws<InvalidCastException>(() => Convert.ToDateTime(length));
49+
Assert.Throws<InvalidCastException>(() => Convert.ChangeType(length, typeof(DateTime)));
50+
}
51+
52+
[Fact]
53+
public void ToDecimalTest()
54+
{
55+
decimal expected = 3;
56+
Assert.Equal(expected, lengthAsIConvertible.ToDecimal(null));
57+
Assert.Equal(expected, Convert.ToDecimal(length));
58+
Assert.Equal(expected, Convert.ChangeType(length, typeof(decimal)));
59+
}
60+
61+
[Fact]
62+
public void ToDoubleTest()
63+
{
64+
double expected = 3.0;
65+
Assert.Equal(expected, lengthAsIConvertible.ToDouble(null));
66+
Assert.Equal(expected, Convert.ToDouble(length));
67+
Assert.Equal(expected, Convert.ChangeType(length, typeof(double)));
68+
}
69+
70+
[Fact]
71+
public void ToInt16Test()
72+
{
73+
short expected = 3;
74+
Assert.Equal(expected, lengthAsIConvertible.ToInt16(null));
75+
Assert.Equal(expected, Convert.ToInt16(length));
76+
Assert.Equal(expected, Convert.ChangeType(length, typeof(short)));
77+
}
78+
79+
[Fact]
80+
public void ToInt32Test()
81+
{
82+
int expected = 3;
83+
Assert.Equal(expected, lengthAsIConvertible.ToInt32(null));
84+
Assert.Equal(expected, Convert.ToInt32(length));
85+
Assert.Equal(expected, Convert.ChangeType(length, typeof(int)));
86+
}
87+
88+
[Fact]
89+
public void ToInt64Test()
90+
{
91+
long expected = 3;
92+
Assert.Equal(expected, lengthAsIConvertible.ToInt64(null));
93+
Assert.Equal(expected, Convert.ToInt64(length));
94+
Assert.Equal(expected, Convert.ChangeType(length, typeof(long)));
95+
}
96+
97+
[Fact]
98+
public void ToSByteTest()
99+
{
100+
sbyte expected = 3;
101+
Assert.Equal(expected, lengthAsIConvertible.ToSByte(null));
102+
Assert.Equal(expected, Convert.ToSByte(length));
103+
Assert.Equal(expected, Convert.ChangeType(length, typeof(sbyte)));
104+
}
105+
106+
[Fact]
107+
public void ToSingleTest()
108+
{
109+
float expected = 3;
110+
Assert.Equal(expected, lengthAsIConvertible.ToSingle(null));
111+
Assert.Equal(expected, Convert.ToSingle(length));
112+
Assert.Equal(expected, Convert.ChangeType(length, typeof(float)));
113+
}
114+
115+
[Fact]
116+
public void ToStringTest()
117+
{
118+
string expected = length.ToString(GlobalConfiguration.DefaultCulture);
119+
Assert.Equal(expected, lengthAsIConvertible.ToString(GlobalConfiguration.DefaultCulture));
120+
Assert.Equal(expected, Convert.ToString(length, GlobalConfiguration.DefaultCulture));
121+
Assert.Equal(expected, Convert.ChangeType(length, typeof(string), GlobalConfiguration.DefaultCulture));
122+
}
123+
124+
[Fact]
125+
public void ToTypeTest()
126+
{
127+
// Same quantity type
128+
Assert.Equal(length, lengthAsIConvertible.ToType(typeof(Length), null));
129+
Assert.Equal(length, Convert.ChangeType(length, typeof(Length)));
130+
131+
// Same unit type
132+
Assert.Equal(length.Unit, lengthAsIConvertible.ToType(typeof(LengthUnit), null));
133+
Assert.Equal(length.Unit, Convert.ChangeType(length, typeof(LengthUnit)));
134+
135+
// Different type
136+
Assert.Throws<InvalidCastException>(() => lengthAsIConvertible.ToType(typeof(Duration), null));
137+
Assert.Throws<InvalidCastException>(() => Convert.ChangeType(length, typeof(Duration)));
138+
139+
// Different unit type
140+
Assert.Throws<InvalidCastException>(() => lengthAsIConvertible.ToType(typeof(DurationUnit), null));
141+
Assert.Throws<InvalidCastException>(() => Convert.ChangeType(length, typeof(DurationUnit)));
142+
143+
// QuantityType
144+
Assert.Equal(Length.QuantityType, lengthAsIConvertible.ToType(typeof(QuantityType), null));
145+
Assert.Equal(Length.QuantityType, Convert.ChangeType(length, typeof(QuantityType)));
146+
147+
// BaseDimensions
148+
Assert.Equal(Length.BaseDimensions, lengthAsIConvertible.ToType(typeof(BaseDimensions), null));
149+
Assert.Equal(Length.BaseDimensions, Convert.ChangeType(length, typeof(BaseDimensions)));
150+
}
151+
152+
[Fact]
153+
public void ToUInt16Test()
154+
{
155+
ushort expected = 3;
156+
Assert.Equal(expected, lengthAsIConvertible.ToUInt16(null));
157+
Assert.Equal(expected, Convert.ToUInt16(length));
158+
Assert.Equal(expected, Convert.ChangeType(length, typeof(ushort)));
159+
}
160+
161+
[Fact]
162+
public void ToUInt32Test()
163+
{
164+
uint expected = 3;
165+
Assert.Equal(expected, lengthAsIConvertible.ToUInt32(null));
166+
Assert.Equal(expected, Convert.ToUInt32(length));
167+
Assert.Equal(expected, Convert.ChangeType(length, typeof(uint)));
168+
}
169+
170+
[Fact]
171+
public void ToUInt64Test()
172+
{
173+
ulong expected = 3;
174+
Assert.Equal(expected, lengthAsIConvertible.ToUInt64(null));
175+
Assert.Equal(expected, Convert.ToUInt64(length));
176+
Assert.Equal(expected, Convert.ChangeType(length, typeof(ulong)));
177+
}
178+
}
179+
}

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

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/AreaMomentOfInertia.WindowsRuntimeComponent.g.cs

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)