1
- using System ;
1
+ using System ;
2
+ using System . Collections . Generic ;
2
3
using System . Linq ;
3
4
using CodeGen . JsonTypes ;
4
5
5
6
namespace CodeGen . Generators . UnitsNetGen
6
7
{
7
8
internal class UnitTestBaseClassGenerator : GeneratorBase
8
9
{
10
+ private static string [ ] SupportedUnitSystems = { "SI" , "CGS" , "BI" , "EE" , "USC" , "FPS" , "Astronomical" } ;
11
+
9
12
private readonly Quantity _quantity ;
10
13
private readonly Unit _baseUnit ;
11
14
private readonly string _unitEnumName ;
15
+ private readonly Dictionary < string , Unit > _unitSystemUnits = new Dictionary < string , Unit > ( ) ;
12
16
13
17
public UnitTestBaseClassGenerator ( Quantity quantity )
14
18
{
@@ -17,6 +21,12 @@ public UnitTestBaseClassGenerator(Quantity quantity)
17
21
throw new ArgumentException ( $ "No unit found with SingularName equal to BaseUnit [{ _quantity . BaseUnit } ]. This unit must be defined.",
18
22
nameof ( quantity ) ) ;
19
23
_unitEnumName = $ "{ quantity . Name } Unit";
24
+ foreach ( var unitSystemMapping in quantity . UnitSystems )
25
+ {
26
+ _unitSystemUnits . Add ( unitSystemMapping . UnitSystem , quantity . Units . FirstOrDefault ( u => u . SingularName == unitSystemMapping . BaseUnit ) ??
27
+ throw new ArgumentException ( $ "No unit found with SingularName equal to the one defined for '{ unitSystemMapping . UnitSystem } ' [{ unitSystemMapping . BaseUnit } ]. This unit must be defined.",
28
+ nameof ( quantity ) ) ) ;
29
+ }
20
30
}
21
31
22
32
public override string Generate ( )
@@ -73,6 +83,31 @@ public void Ctor_WithNaNValue_ThrowsArgumentException()
73
83
}}
74
84
" ) ; Writer . WL ( $@ "
75
85
86
+ [Fact]
87
+ public void Ctor_UnitSystem_ThrowsArgumentExceptionIfNotSupported()
88
+ {{" ) ;
89
+ foreach ( var unit in _unitSystemUnits )
90
+ {
91
+ var asQuantityVariableName = $ "{ unit . Key . ToLowerInvariant ( ) } Quantity";
92
+
93
+ Writer . WL ( $@ "
94
+ var { asQuantityVariableName } = new { _quantity . Name } (1, UnitSystem.{ unit . Key } );
95
+ Assert.Equal(1, (double){ asQuantityVariableName } .Value);
96
+ Assert.Equal({ _unitEnumName } .{ unit . Value . SingularName } , { asQuantityVariableName } .Unit);" ) ;
97
+ Writer . WL ( ) ;
98
+ }
99
+ foreach ( var unitSystem in SupportedUnitSystems . Where ( x => ! _unitSystemUnits . ContainsKey ( x ) ) ) Writer . WL ( $@ "
100
+ Assert.Throws<ArgumentException>(() => new { _quantity . Name } (1, UnitSystem.{ unitSystem } ));" ) ;
101
+ Writer . WL ( $@ "
102
+ }}
103
+
104
+ [Fact]
105
+ public void Ctor_WithNullUnitSystem_ThrowsArgumentNullException()
106
+ {{
107
+ Assert.Throws<ArgumentNullException>(() => new { _quantity . Name } (1, null));" ) ;
108
+ Writer . WL ( $@ "
109
+ }}
110
+
76
111
[Fact]
77
112
public void { _baseUnit . SingularName } To{ _quantity . Name } Units()
78
113
{{
@@ -115,6 +150,28 @@ public void As()
115
150
Writer . WL ( $@ "
116
151
}}
117
152
153
+ [Fact]
154
+ public void As_UnitSystem_ThrowsArgumentExceptionIfNotSupported()
155
+ {{
156
+ var { baseUnitVariableName } = { _quantity . Name } .From{ _baseUnit . PluralName } (1);" ) ;
157
+ if ( _unitSystemUnits . Any ( ) ) Writer . WL ( ) ;
158
+ foreach ( var unitSystem in _unitSystemUnits ) Writer . WL ( $@ "
159
+ AssertEx.EqualTolerance({ unitSystem . Value . PluralName } InOne{ _baseUnit . SingularName } , { baseUnitVariableName } .As(UnitSystem.{ unitSystem . Key } ), { unitSystem . Value . PluralName } Tolerance);" ) ;
160
+ if ( _unitSystemUnits . Count < SupportedUnitSystems . Length ) Writer . WL ( ) ;
161
+ foreach ( var unitSystem in SupportedUnitSystems . Where ( x => ! _unitSystemUnits . ContainsKey ( x ) ) ) Writer . WL ( $@ "
162
+ Assert.Throws<ArgumentException>(() => { baseUnitVariableName } .As(UnitSystem.{ unitSystem } ));" ) ;
163
+ Writer . WL ( $@ "
164
+ }}
165
+
166
+ [Fact]
167
+ public void As_WithNullUnitSystem_ThrowsArgumentNullException()
168
+ {{
169
+ var { baseUnitVariableName } = { _quantity . Name } .From{ _baseUnit . PluralName } (1);" ) ;
170
+ Writer . WL ( $@ "
171
+ Assert.Throws<ArgumentNullException>(() => { baseUnitVariableName } .As(null));" ) ;
172
+ Writer . WL ( $@ "
173
+ }}
174
+
118
175
[Fact]
119
176
public void ToUnit()
120
177
{{
@@ -123,7 +180,7 @@ public void ToUnit()
123
180
{
124
181
var asQuantityVariableName = $ "{ unit . SingularName . ToLowerInvariant ( ) } Quantity";
125
182
126
- Writer . WL ( "" ) ;
183
+ Writer . WL ( ) ;
127
184
Writer . WL ( $@ "
128
185
var { asQuantityVariableName } = { baseUnitVariableName } .ToUnit({ _unitEnumName } .{ unit . SingularName } );
129
186
AssertEx.EqualTolerance({ unit . PluralName } InOne{ _baseUnit . SingularName } , (double){ asQuantityVariableName } .Value, { unit . PluralName } Tolerance);
@@ -132,6 +189,35 @@ public void ToUnit()
132
189
Writer . WL ( $@ "
133
190
}}
134
191
192
+ [Fact]
193
+ public void To_UnitSystem_ThrowsArgumentExceptionIfNotSupported()
194
+ {{
195
+ var { baseUnitVariableName } = { _quantity . Name } .From{ _baseUnit . PluralName } (1);" ) ;
196
+ foreach ( var unit in _unitSystemUnits )
197
+ {
198
+ var asQuantityVariableName = $ "{ unit . Key . ToLowerInvariant ( ) } Quantity";
199
+
200
+ Writer . WL ( ) ;
201
+ Writer . WL ( $@ "
202
+ var { asQuantityVariableName } = { baseUnitVariableName } .ToUnit(UnitSystem.{ unit . Key } );
203
+ AssertEx.EqualTolerance({ unit . Value . PluralName } InOne{ _baseUnit . SingularName } , (double){ asQuantityVariableName } .Value, { unit . Value . PluralName } Tolerance);
204
+ Assert.Equal({ _unitEnumName } .{ unit . Value . SingularName } , { asQuantityVariableName } .Unit);" ) ;
205
+ }
206
+ if ( _unitSystemUnits . Count < SupportedUnitSystems . Length ) Writer . WL ( ) ;
207
+ foreach ( var unitSystem in SupportedUnitSystems . Where ( x => ! _unitSystemUnits . ContainsKey ( x ) ) ) Writer . WL ( $@ "
208
+ Assert.Throws<ArgumentException>(() => { baseUnitVariableName } .ToUnit(UnitSystem.{ unitSystem } ));" ) ;
209
+ Writer . WL ( $@ "
210
+ }}
211
+
212
+ [Fact]
213
+ public void ToUnit_WithNullUnitSystem_ThrowsNullException()
214
+ {{
215
+ var { baseUnitVariableName } = { _quantity . Name } .From{ _baseUnit . PluralName } (1);" ) ;
216
+ Writer . WL ( $@ "
217
+ Assert.Throws<ArgumentNullException>(() => { baseUnitVariableName } .ToUnit(null));" ) ;
218
+ Writer . WL ( $@ "
219
+ }}
220
+
135
221
[Fact]
136
222
public void ConversionRoundTrip()
137
223
{{
@@ -182,7 +268,7 @@ public void ArithmeticOperators()
182
268
}
183
269
else
184
270
{
185
- Writer . WL ( "" ) ;
271
+ Writer . WL ( ) ;
186
272
}
187
273
188
274
Writer . WL ( $@ "
0 commit comments