-
Notifications
You must be signed in to change notification settings - Fork 412
Expand file tree
/
Copy pathIQuantityTests.cs
More file actions
193 lines (156 loc) · 6.44 KB
/
Copy pathIQuantityTests.cs
File metadata and controls
193 lines (156 loc) · 6.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// 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.
namespace UnitsNet.Tests;
// ReSharper disable once InconsistentNaming
public partial class IQuantityTests
{
[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 GetUnitInfo_ReturnsUnitInfoForQuantityUnit()
{
var length = new Length(3.0, LengthUnit.Centimeter);
IQuantity quantity = length;
UnitInfo unitInfo = quantity.GetUnitInfo();
Assert.Equal(nameof(LengthUnit.Centimeter), unitInfo.Name);
Assert.Equal(quantity.UnitKey, unitInfo.UnitKey);
}
[Fact]
public void GetUnitInfo_Zero_ReturnsBaseUnitInfo()
{
IQuantity quantity = Length.Info.Zero;
UnitInfo unitInfo = quantity.GetUnitInfo();
Assert.Equal(Length.Info.BaseUnitInfo.UnitKey, unitInfo.UnitKey);
}
[Fact]
public void GetUnitInfo_ConcreteQuantity_ReturnsFullyTypedUnitInfo()
{
var quantity = new Length(3.0, LengthUnit.Centimeter);
// Overload resolution picks GetUnitInfo<TQuantity, TUnit> for the concrete struct receiver,
// returning the most specific UnitInfo<Length, LengthUnit>.
UnitInfo<Length, LengthUnit> unitInfo = quantity.GetUnitInfo();
Assert.Equal(LengthUnit.Centimeter, unitInfo.Value);
Assert.Equal(nameof(LengthUnit.Centimeter), unitInfo.Name);
}
[Fact]
public void GetUnitInfo_TypedQuantityReference_FallsBackToNonGeneric()
{
IQuantity<LengthUnit> quantity = new Length(3.0, LengthUnit.Centimeter);
// The IQuantity<TUnit> reference does not satisfy the IQuantity<TSelf, TUnit> constraint
// (TSelf would be IQuantity<MassUnit>), so resolution falls back to GetUnitInfo(IQuantity).
UnitInfo unitInfo = quantity.GetUnitInfo();
Assert.Equal(LengthUnit.Centimeter, ((UnitInfo<LengthUnit>)unitInfo).Value);
Assert.Equal(nameof(LengthUnit.Centimeter), unitInfo.Name);
}
[Fact]
public void GetUnitInfo_MatchesUnit()
{
Assert.All(Quantity.Infos.Select(x => x.Zero), quantity =>
{
Assert.Equal(quantity.Unit, quantity.GetUnitInfo().Value);
});
}
[Fact]
public void GetQuantityInfo_NonGeneric_ReturnsRegisteredQuantityInfo()
{
IQuantity quantity = new Mass(1.0, MassUnit.Kilogram);
QuantityInfo info = quantity.GetQuantityInfo();
Assert.Same(Mass.Info, info);
}
[Fact]
public void GetQuantityInfo_Typed_ReturnsRegisteredQuantityInfo()
{
IQuantity<MassUnit> quantity = new Mass(1.0, MassUnit.Kilogram);
QuantityInfo<MassUnit> info = quantity.GetQuantityInfo();
Assert.Same(Mass.Info, info);
}
[Fact]
public void StaticAbstract_Info_ReturnsSameAsTypedInfo()
{
// Calls IQuantity<TSelf, TUnitType>.Info via the static abstract member.
QuantityInfo<Mass, MassUnit> typedInfo = Mass.Info;
QuantityInfo<Mass, MassUnit> viaStaticAbstract = StaticAbstractAccess<Mass, MassUnit>();
Assert.Same(typedInfo, viaStaticAbstract);
static QuantityInfo<TSelf, TUnit> StaticAbstractAccess<TSelf, TUnit>()
where TSelf : IQuantity<TSelf, TUnit>
where TUnit : struct, Enum
{
return TSelf.Info;
}
}
[Fact]
public void StaticAbstract_Info_NonGeneric_ReturnsSameAsTypedInfo()
{
// Calls IQuantityOfType<TQuantity>.Info via the static abstract member.
QuantityInfo info = StaticAbstractAccess<Mass>();
Assert.Same((QuantityInfo)Mass.Info, info);
static QuantityInfo StaticAbstractAccess<TQuantity>()
where TQuantity : IQuantityOfType<TQuantity>
{
return TQuantity.Info;
}
}
[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(() =>
{
IQuantity<MassUnit> quantity = new Mass(1, Mass.BaseUnit);
Assert.Throws<ArgumentException>(() => quantity.ToUnit(unsupportedUnitSystem));
}, () =>
{
IQuantity quantity = new Mass(1, Mass.BaseUnit);
Assert.Throws<ArgumentException>(() => quantity.ToUnit(unsupportedUnitSystem));
});
}
}