1- using System ;
1+ using System ;
2+ using System . Collections . Generic ;
23using System . Linq ;
34using CodeGen . JsonTypes ;
45
56namespace CodeGen . Generators . UnitsNetGen
67{
78 internal class UnitTestBaseClassGenerator : GeneratorBase
89 {
10+ private static string [ ] SupportedUnitSystems = { "SI" , "CGS" , "BI" , "EE" , "USC" , "FPS" , "Astronomical" } ;
11+
912 private readonly Quantity _quantity ;
1013 private readonly Unit _baseUnit ;
1114 private readonly string _unitEnumName ;
15+ private readonly Dictionary < string , Unit > _unitSystemUnits = new Dictionary < string , Unit > ( ) ;
1216
1317 public UnitTestBaseClassGenerator ( Quantity quantity )
1418 {
@@ -17,6 +21,12 @@ public UnitTestBaseClassGenerator(Quantity quantity)
1721 throw new ArgumentException ( $ "No unit found with SingularName equal to BaseUnit [{ _quantity . BaseUnit } ]. This unit must be defined.",
1822 nameof ( quantity ) ) ;
1923 _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+ }
2030 }
2131
2232 public override string Generate ( )
@@ -73,6 +83,31 @@ public void Ctor_WithNaNValue_ThrowsArgumentException()
7383 }}
7484" ) ; Writer . WL ( $@ "
7585
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+
76111 [Fact]
77112 public void { _baseUnit . SingularName } To{ _quantity . Name } Units()
78113 {{
@@ -115,6 +150,28 @@ public void As()
115150 Writer . WL ( $@ "
116151 }}
117152
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+
118175 [Fact]
119176 public void ToUnit()
120177 {{
@@ -123,7 +180,7 @@ public void ToUnit()
123180 {
124181 var asQuantityVariableName = $ "{ unit . SingularName . ToLowerInvariant ( ) } Quantity";
125182
126- Writer . WL ( "" ) ;
183+ Writer . WL ( ) ;
127184 Writer . WL ( $@ "
128185 var { asQuantityVariableName } = { baseUnitVariableName } .ToUnit({ _unitEnumName } .{ unit . SingularName } );
129186 AssertEx.EqualTolerance({ unit . PluralName } InOne{ _baseUnit . SingularName } , (double){ asQuantityVariableName } .Value, { unit . PluralName } Tolerance);
@@ -132,6 +189,35 @@ public void ToUnit()
132189 Writer . WL ( $@ "
133190 }}
134191
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+
135221 [Fact]
136222 public void ConversionRoundTrip()
137223 {{
@@ -182,7 +268,7 @@ public void ArithmeticOperators()
182268 }
183269 else
184270 {
185- Writer . WL ( "" ) ;
271+ Writer . WL ( ) ;
186272 }
187273
188274 Writer . WL ( $@ "
0 commit comments