@@ -109,17 +109,11 @@ public void GettingStringFromArrayReturnsNull()
109
109
Assert . Null ( Parser . Parse ( hocon ) . GetString ( "array" ) ) ;
110
110
}
111
111
112
- // TODO: This behavior is incorrect, anything to array should throw instead.
113
- // see https://github.com/lightbend/config/blob/master/HOCON.md#automatic-type-conversions
114
- // see https://github.com/lightbend/config/blob/v1.3.3/config/src/main/java/com/typesafe/config/Config.java#L936
115
- // OLD BEHAVIOR:(not sure if this is the expected behavior but it is what we have established in Akka.NET)
116
112
[ Fact ]
117
113
public void GettingArrayFromLiteralsReturnsNull ( )
118
114
{
119
115
var hocon = " literal : a b c" ;
120
- var res = Parser . Parse ( hocon ) . GetStringList ( "literal" ) ;
121
-
122
- Assert . Null ( res ) ;
116
+ Parser . Parse ( hocon ) . Invoking ( c => c . GetStringList ( "literal" ) ) . Should ( ) . Throw < HoconException > ( "Anything converted to array should throw instead" ) ;
123
117
}
124
118
125
119
//Added tests to conform to the HOCON spec https://github.com/typesafehub/config/blob/master/HOCON.md
@@ -206,6 +200,27 @@ public void CanMergeObject()
206
200
Assert . Equal ( "2" , config . GetString ( "a.b.c.y" ) ) ;
207
201
Assert . Equal ( "3" , config . GetString ( "a.b.c.z" ) ) ;
208
202
}
203
+
204
+ [ Fact ]
205
+ public void Getter_failures_Should_include_bad_path ( )
206
+ {
207
+ var badConfig = Parser . Parse ( "{a.c: abc}" ) ;
208
+ var badPath = "a.c" ;
209
+
210
+ badConfig . Invoking ( c => c . GetInt ( badPath , 0 ) ) . Should ( ) . Throw < HoconValueException > ( ) . Which . FailPath . Should ( ) . Be ( badPath ) ;
211
+ badConfig . Invoking ( c => c . GetDouble ( badPath , 0 ) ) . Should ( ) . Throw < HoconValueException > ( ) . Which . FailPath . Should ( ) . Be ( badPath ) ;
212
+ badConfig . Invoking ( c => c . GetBooleanList ( badPath ) ) . Should ( ) . Throw < HoconValueException > ( ) . Which . FailPath . Should ( ) . Be ( badPath ) ;
213
+ badConfig . Invoking ( c => c . GetByteList ( badPath ) ) . Should ( ) . Throw < HoconValueException > ( ) . Which . FailPath . Should ( ) . Be ( badPath ) ;
214
+ badConfig . Invoking ( c => c . GetDecimalList ( badPath ) ) . Should ( ) . Throw < HoconValueException > ( ) . Which . FailPath . Should ( ) . Be ( badPath ) ;
215
+ badConfig . Invoking ( c => c . GetDoubleList ( badPath ) ) . Should ( ) . Throw < HoconValueException > ( ) . Which . FailPath . Should ( ) . Be ( badPath ) ;
216
+ badConfig . Invoking ( c => c . GetFloatList ( badPath ) ) . Should ( ) . Throw < HoconValueException > ( ) . Which . FailPath . Should ( ) . Be ( badPath ) ;
217
+ badConfig . Invoking ( c => c . GetIntList ( badPath ) ) . Should ( ) . Throw < HoconValueException > ( ) . Which . FailPath . Should ( ) . Be ( badPath ) ;
218
+ badConfig . Invoking ( c => c . GetLongList ( badPath ) ) . Should ( ) . Throw < HoconValueException > ( ) . Which . FailPath . Should ( ) . Be ( badPath ) ;
219
+ badConfig . Invoking ( c => c . GetObjectList ( badPath ) ) . Should ( ) . Throw < HoconValueException > ( ) . Which . FailPath . Should ( ) . Be ( badPath ) ;
220
+ badConfig . Invoking ( c => c . GetStringList ( badPath ) ) . Should ( ) . Throw < HoconValueException > ( ) . Which . FailPath . Should ( ) . Be ( badPath ) ;
221
+ badConfig . Invoking ( c => c . GetInt ( badPath , 0 ) ) . Should ( ) . Throw < HoconValueException > ( ) . Which . FailPath . Should ( ) . Be ( badPath ) ;
222
+ badConfig . Invoking ( c => c . GetInt ( badPath , 0 ) ) . Should ( ) . Throw < HoconValueException > ( ) . Which . FailPath . Should ( ) . Be ( badPath ) ;
223
+ }
209
224
210
225
[ Fact ]
211
226
public void CanOverrideObject ( )
@@ -394,6 +409,43 @@ public void CanAssignDoubleToField()
394
409
Assert . Equal ( 1.1 , Parser . Parse ( hocon ) . GetDouble ( "a" ) ) ;
395
410
}
396
411
412
+ [ Fact ]
413
+ public void CanSetDefaultValuesWhenGettingData ( )
414
+ {
415
+ var emptyConfig = Parser . Parse ( "{}" ) ;
416
+ var missingKey = "a" ;
417
+
418
+ emptyConfig . GetInt ( missingKey , 0 ) . Should ( ) . Be ( 0 ) ;
419
+ emptyConfig . GetDouble ( missingKey , 0 ) . Should ( ) . Be ( 0 ) ;
420
+
421
+ emptyConfig . GetBooleanList ( missingKey , new List < bool > ( ) ) . Should ( ) . Equal ( new List < bool > ( ) ) ;
422
+ emptyConfig . Invoking ( c => c . GetBooleanList ( missingKey ) ) . Should ( ) . Throw < HoconValueException > ( ) . Which . InnerException . Should ( ) . BeOfType < HoconParserException > ( ) ;
423
+
424
+ emptyConfig . GetByteList ( missingKey , new List < byte > ( ) ) . Should ( ) . Equal ( new List < byte > ( ) ) ;
425
+ emptyConfig . Invoking ( c => c . GetByteList ( missingKey ) ) . Should ( ) . Throw < HoconValueException > ( ) . Which . InnerException . Should ( ) . BeOfType < HoconParserException > ( ) ; ;
426
+
427
+ emptyConfig . GetDecimalList ( missingKey , new List < decimal > ( ) ) . Should ( ) . Equal ( new List < decimal > ( ) ) ;
428
+ emptyConfig . Invoking ( c => c . GetDecimalList ( missingKey ) ) . Should ( ) . Throw < HoconValueException > ( ) . Which . InnerException . Should ( ) . BeOfType < HoconParserException > ( ) ; ;
429
+
430
+ emptyConfig . GetDoubleList ( missingKey , new List < double > ( ) ) . Should ( ) . Equal ( new List < double > ( ) ) ;
431
+ emptyConfig . Invoking ( c => c . GetDoubleList ( missingKey ) ) . Should ( ) . Throw < HoconValueException > ( ) . Which . InnerException . Should ( ) . BeOfType < HoconParserException > ( ) ; ;
432
+
433
+ emptyConfig . GetFloatList ( missingKey , new List < float > ( ) ) . Should ( ) . Equal ( new List < float > ( ) ) ;
434
+ emptyConfig . Invoking ( c => c . GetFloatList ( missingKey ) ) . Should ( ) . Throw < HoconValueException > ( ) . Which . InnerException . Should ( ) . BeOfType < HoconParserException > ( ) ; ;
435
+
436
+ emptyConfig . GetIntList ( missingKey , new List < int > ( ) ) . Should ( ) . Equal ( new List < int > ( ) ) ;
437
+ emptyConfig . Invoking ( c => c . GetIntList ( missingKey ) ) . Should ( ) . Throw < HoconValueException > ( ) . Which . InnerException . Should ( ) . BeOfType < HoconParserException > ( ) ; ;
438
+
439
+ emptyConfig . GetLongList ( missingKey , new List < long > ( ) ) . Should ( ) . Equal ( new List < long > ( ) ) ;
440
+ emptyConfig . Invoking ( c => c . GetLongList ( missingKey ) ) . Should ( ) . Throw < HoconValueException > ( ) . Which . InnerException . Should ( ) . BeOfType < HoconParserException > ( ) ; ;
441
+
442
+ emptyConfig . GetObjectList ( missingKey , new List < HoconObject > ( ) ) . Should ( ) . Equal ( new List < HoconObject > ( ) ) ;
443
+ emptyConfig . Invoking ( c => c . GetObjectList ( missingKey ) ) . Should ( ) . Throw < HoconValueException > ( ) . Which . InnerException . Should ( ) . BeOfType < HoconParserException > ( ) ; ;
444
+
445
+ emptyConfig . GetStringList ( missingKey , new List < string > ( ) ) . Should ( ) . Equal ( new List < string > ( ) ) ;
446
+ emptyConfig . Invoking ( c => c . GetStringList ( missingKey ) ) . Should ( ) . Throw < HoconValueException > ( ) . Which . InnerException . Should ( ) . BeOfType < HoconParserException > ( ) ; ;
447
+ }
448
+
397
449
[ Fact ]
398
450
public void CanAssignNumbersToField ( )
399
451
{
@@ -421,9 +473,9 @@ public void CanAssignNumbersToField()
421
473
Assert . Equal ( float . NaN , config . GetFloat ( "e" ) ) ;
422
474
423
475
Assert . Equal ( 1000.05m , config . GetDecimal ( "a" ) ) ;
424
- Assert . Throws < HoconException > ( ( ) => config . GetDecimal ( "b" ) ) ;
425
- Assert . Throws < HoconException > ( ( ) => config . GetDecimal ( "c" ) ) ;
426
- Assert . Throws < HoconException > ( ( ) => config . GetDecimal ( "d" ) ) ;
476
+ Assert . Throws < HoconValueException > ( ( ) => config . GetDecimal ( "b" ) ) . GetBaseException ( ) . Should ( ) . BeOfType < HoconException > ( ) ;
477
+ Assert . Throws < HoconValueException > ( ( ) => config . GetDecimal ( "c" ) ) . GetBaseException ( ) . Should ( ) . BeOfType < HoconException > ( ) ;
478
+ Assert . Throws < HoconValueException > ( ( ) => config . GetDecimal ( "d" ) ) . GetBaseException ( ) . Should ( ) . BeOfType < HoconException > ( ) ;
427
479
428
480
Assert . Equal ( 255 , config . GetLong ( "f" ) ) ;
429
481
Assert . Equal ( 255 , config . GetLong ( "g" ) ) ;
0 commit comments