33
44using System ;
55using System . Collections . Generic ;
6- using System . Globalization ;
76using Newtonsoft . Json ;
87using Newtonsoft . Json . Converters ;
98using Newtonsoft . Json . Linq ;
@@ -38,16 +37,6 @@ public void UnitsNetBaseJsonConverter_ConvertIQuantity_throws_ArgumentNullExcept
3837 Assert . Equal ( "Value cannot be null. (Parameter 'quantity')" , result . Message ) ;
3938 }
4039
41- [ Fact ]
42- public void UnitsNetBaseJsonConverter_ConvertValueUnit_works_as_expected ( )
43- {
44- var result = _sut . Test_ConvertDecimalValueUnit ( "PowerUnit.Watt" , 10.2365m ) ;
45-
46- Assert . NotNull ( result ) ;
47- Assert . IsType < Power > ( result ) ;
48- Assert . True ( Power . FromWatts ( 10.2365 ) . Equals ( ( Power ) result , 1e-5 , ComparisonType . Absolute ) ) ;
49- }
50-
5140 [ Fact ]
5241 public void UnitsNetBaseJsonConverter_ConvertValueUnit_throws_UnitsNetException_when_unit_does_not_exist ( )
5342 {
@@ -58,16 +47,6 @@ public void UnitsNetBaseJsonConverter_ConvertValueUnit_throws_UnitsNetException_
5847 Assert . Equal ( "UnitsNet.Units.SomeImaginaryUnit,UnitsNet" , result . Data [ "type" ] ) ;
5948 }
6049
61- [ Fact ]
62- public void UnitsNetBaseJsonConverter_ConvertValueUnit_throws_UnitsNetException_when_unit_is_in_unexpected_format ( )
63- {
64- var result = Assert . Throws < UnitsNetException > ( ( ) => _sut . Test_ConvertDecimalValueUnit ( "PowerUnit Watt" , 10.2365m ) ) ;
65-
66- Assert . Equal ( "\" PowerUnit Watt\" is not a valid unit." , result . Message ) ;
67- Assert . True ( result . Data . Contains ( "type" ) ) ;
68- Assert . Equal ( "PowerUnit Watt" , result . Data [ "type" ] ) ;
69- }
70-
7150 [ Fact ]
7251 public void UnitsNetBaseJsonConverter_CreateLocalSerializer_works_as_expected ( )
7352 {
@@ -107,38 +86,6 @@ public void UnitsNetBaseJsonConverter_ReadValueUnit_works_with_double_quantity()
10786 Assert . Equal ( 10.2365 , result ? . Value ) ;
10887 }
10988
110- [ Fact ]
111- public void UnitsNetBaseJsonConverter_ReadValueUnit_works_with_decimal_quantity ( )
112- {
113- var token = new JObject { { "Unit" , "PowerUnit.Watt" } , { "Value" , 10.2365m } , { "ValueString" , "10.2365" } , { "ValueType" , "decimal" } } ;
114-
115- var result = _sut . Test_ReadDecimalValueUnit ( token ) ;
116-
117- Assert . NotNull ( result ) ;
118- Assert . Equal ( "PowerUnit.Watt" , result ? . Unit ) ;
119- Assert . Equal ( 10.2365m , result ? . Value ) ;
120- }
121-
122- [ Fact ]
123- public void UnitsNetBaseJsonConverter_ReadValueUnit_returns_null_when_value_is_a_string ( )
124- {
125- var token = new JObject { { "Unit" , "PowerUnit.Watt" } , { "Value" , "10.2365" } } ;
126-
127- var result = _sut . Test_ReadDecimalValueUnit ( token ) ;
128-
129- Assert . Null ( result ) ;
130- }
131-
132- [ Fact ]
133- public void UnitsNetBaseJsonConverter_ReadValueUnit_returns_null_when_value_type_is_not_a_string ( )
134- {
135- var token = new JObject { { "Unit" , "PowerUnit.Watt" } , { "Value" , 10.2365 } , { "ValueType" , 123 } } ;
136-
137- var result = _sut . Test_ReadDecimalValueUnit ( token ) ;
138-
139- Assert . Null ( result ) ;
140- }
141-
14289 [ Fact ]
14390 public void UnitsNetBaseJsonConverter_ReadDoubleValueUnit_works_with_empty_token ( )
14491 {
@@ -149,55 +96,6 @@ public void UnitsNetBaseJsonConverter_ReadDoubleValueUnit_works_with_empty_token
14996 Assert . Null ( result ) ;
15097 }
15198
152- [ Theory ]
153- [ InlineData ( false , true ) ]
154- [ InlineData ( true , false ) ]
155- public void UnitsNetBaseJsonConverter_ReadValueUnit_returns_null_when_unit_or_value_is_missing ( bool withUnit , bool withValue )
156- {
157- var token = new JObject ( ) ;
158-
159- if ( withUnit )
160- {
161- token . Add ( "Unit" , "PowerUnit.Watt" ) ;
162- }
163-
164- if ( withValue )
165- {
166- token . Add ( "Value" , 10.2365m ) ;
167- }
168-
169- var result = _sut . Test_ReadDecimalValueUnit ( token ) ;
170-
171- Assert . Null ( result ) ;
172- }
173-
174- [ Theory ]
175- [ InlineData ( "Unit" , "Value" , "ValueString" , "ValueType" ) ]
176- [ InlineData ( "unit" , "Value" , "ValueString" , "ValueType" ) ]
177- [ InlineData ( "Unit" , "value" , "valueString" , "valueType" ) ]
178- [ InlineData ( "unit" , "value" , "valueString" , "valueType" ) ]
179- [ InlineData ( "unIT" , "vAlUe" , "vAlUeString" , "vAlUeType" ) ]
180- public void UnitsNetBaseJsonConverter_ReadValueUnit_works_case_insensitive (
181- string unitPropertyName ,
182- string valuePropertyName ,
183- string valueStringPropertyName ,
184- string valueTypePropertyName )
185- {
186- var token = new JObject
187- {
188- { unitPropertyName , "PowerUnit.Watt" } ,
189- { valuePropertyName , 10.2365m } ,
190- { valueStringPropertyName , 10.2365m . ToString ( CultureInfo . InvariantCulture ) } ,
191- { valueTypePropertyName , "decimal" }
192- } ;
193-
194- var result = _sut . Test_ReadDecimalValueUnit ( token ) ;
195-
196- Assert . NotNull ( result ) ;
197- Assert . Equal ( "PowerUnit.Watt" , result ? . Unit ) ;
198- Assert . Equal ( 10.2365m , result ? . Value ) ;
199- }
200-
20199 /// <summary>
202100 /// Dummy converter, used to access protected methods on abstract UnitsNetBaseJsonConverter{T}
203101 /// </summary>
@@ -214,24 +112,8 @@ private class TestConverter : UnitsNetBaseJsonConverter<string>
214112 return ( result . Unit , result . Value ) ;
215113 }
216114
217- public ( string Unit , decimal Value ) Test_ConvertDecimalIQuantity ( IQuantity value )
218- {
219- var result = ConvertIQuantity ( value ) ;
220- if ( result is ExtendedValueUnit { ValueType : "decimal" } decimalResult )
221- {
222- return ( result . Unit , decimal . Parse ( decimalResult . ValueString , CultureInfo . InvariantCulture ) ) ;
223- }
224-
225- throw new ArgumentException ( "The quantity does not have a decimal value" , nameof ( value ) ) ;
226- }
227-
228115 public IQuantity Test_ConvertDoubleValueUnit ( string unit , double value ) => Test_ConvertValueUnit ( new ValueUnit { Unit = unit , Value = value } ) ;
229116
230- public IQuantity Test_ConvertDecimalValueUnit ( string unit , decimal value ) => Test_ConvertValueUnit ( new ExtendedValueUnit
231- {
232- Unit = unit , Value = ( double ) value , ValueString = value . ToString ( CultureInfo . InvariantCulture ) , ValueType = "decimal"
233- } ) ;
234-
235117 private IQuantity Test_ConvertValueUnit ( ValueUnit valueUnit ) => ConvertValueUnit ( valueUnit ) ;
236118
237119 public JsonSerializer Test_CreateLocalSerializer ( JsonSerializer serializer ) => CreateLocalSerializer ( serializer , this ) ;
@@ -246,19 +128,6 @@ public IQuantity Test_ConvertDecimalValueUnit(string unit, decimal value) => Tes
246128
247129 return ( result . Unit , result . Value ) ;
248130 }
249-
250- public ( string Unit , decimal Value ) ? Test_ReadDecimalValueUnit ( JToken jsonToken )
251- {
252- var result = ReadValueUnit ( jsonToken ) ;
253-
254- if ( result is ExtendedValueUnit { ValueType : "decimal" } decimalResult )
255- {
256- return ( result . Unit , decimal . Parse ( decimalResult . ValueString , CultureInfo . InvariantCulture ) ) ;
257- }
258-
259- return null ;
260- }
261-
262131 }
263132 }
264133}
0 commit comments