3
3
4
4
using System ;
5
5
using System . Collections . Generic ;
6
- using System . Globalization ;
7
6
using Newtonsoft . Json ;
8
7
using Newtonsoft . Json . Converters ;
9
8
using Newtonsoft . Json . Linq ;
@@ -38,16 +37,6 @@ public void UnitsNetBaseJsonConverter_ConvertIQuantity_throws_ArgumentNullExcept
38
37
Assert . Equal ( "Value cannot be null. (Parameter 'quantity')" , result . Message ) ;
39
38
}
40
39
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
-
51
40
[ Fact ]
52
41
public void UnitsNetBaseJsonConverter_ConvertValueUnit_throws_UnitsNetException_when_unit_does_not_exist ( )
53
42
{
@@ -58,16 +47,6 @@ public void UnitsNetBaseJsonConverter_ConvertValueUnit_throws_UnitsNetException_
58
47
Assert . Equal ( "UnitsNet.Units.SomeImaginaryUnit,UnitsNet" , result . Data [ "type" ] ) ;
59
48
}
60
49
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
-
71
50
[ Fact ]
72
51
public void UnitsNetBaseJsonConverter_CreateLocalSerializer_works_as_expected ( )
73
52
{
@@ -107,38 +86,6 @@ public void UnitsNetBaseJsonConverter_ReadValueUnit_works_with_double_quantity()
107
86
Assert . Equal ( 10.2365 , result ? . Value ) ;
108
87
}
109
88
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
-
142
89
[ Fact ]
143
90
public void UnitsNetBaseJsonConverter_ReadDoubleValueUnit_works_with_empty_token ( )
144
91
{
@@ -149,55 +96,6 @@ public void UnitsNetBaseJsonConverter_ReadDoubleValueUnit_works_with_empty_token
149
96
Assert . Null ( result ) ;
150
97
}
151
98
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
-
201
99
/// <summary>
202
100
/// Dummy converter, used to access protected methods on abstract UnitsNetBaseJsonConverter{T}
203
101
/// </summary>
@@ -214,24 +112,8 @@ private class TestConverter : UnitsNetBaseJsonConverter<string>
214
112
return ( result . Unit , result . Value ) ;
215
113
}
216
114
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
-
228
115
public IQuantity Test_ConvertDoubleValueUnit ( string unit , double value ) => Test_ConvertValueUnit ( new ValueUnit { Unit = unit , Value = value } ) ;
229
116
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
-
235
117
private IQuantity Test_ConvertValueUnit ( ValueUnit valueUnit ) => ConvertValueUnit ( valueUnit ) ;
236
118
237
119
public JsonSerializer Test_CreateLocalSerializer ( JsonSerializer serializer ) => CreateLocalSerializer ( serializer , this ) ;
@@ -246,19 +128,6 @@ public IQuantity Test_ConvertDecimalValueUnit(string unit, decimal value) => Tes
246
128
247
129
return ( result . Unit , result . Value ) ;
248
130
}
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
-
262
131
}
263
132
}
264
133
}
0 commit comments