@@ -13,10 +13,10 @@ struct Settings {
13
13
}
14
14
15
15
fn config ( s : & str ) -> Config {
16
- Config :: default ( )
17
- . merge ( File :: from_str ( s, FileFormat :: Json ) )
16
+ Config :: builder ( )
17
+ . add_source ( File :: from_str ( s, FileFormat :: Json ) )
18
+ . build ( )
18
19
. unwrap ( )
19
- . clone ( )
20
20
}
21
21
22
22
#[ test]
@@ -28,25 +28,42 @@ fn test_load_level_uppercase() {
28
28
}
29
29
30
30
#[ test]
31
- fn test_load_level_lowercase ( ) {
31
+ fn test_case_sensitivity_log_level_from_str ( ) {
32
32
// to verify that this works
33
- {
34
- use std:: str:: FromStr ;
35
- let l = log:: Level :: from_str ( "error" ) . unwrap ( ) ;
36
- assert_eq ! ( l, log:: Level :: Error ) ;
37
- }
38
33
39
- let s = r#"{ "log": "error" }"# ;
34
+ use std:: str:: FromStr ;
35
+ let l = log:: Level :: from_str ( "error" ) . unwrap ( ) ;
36
+ assert_eq ! ( l, log:: Level :: Error ) ;
37
+ }
40
38
39
+ #[ test]
40
+ fn test_case_sensitivity_json_from_str ( ) {
41
41
// to confirm serde_json works as expected
42
- {
43
- let j: Settings = serde_json:: from_str ( s) . unwrap ( ) ;
44
- assert_eq ! ( j. log, log:: Level :: Error ) ;
45
- }
42
+ let s = r#"{ "log": "error" }"# ;
43
+
44
+ let j: Settings = serde_json:: from_str ( s) . unwrap ( ) ;
45
+ assert_eq ! ( j. log, log:: Level :: Error ) ;
46
+ }
46
47
48
+ #[ test]
49
+ #[ should_panic( expected = "Expected Ok(_) for Err(enum Level does not have variant constructor error)" ) ]
50
+ fn test_load_level_lowercase_failing ( ) {
51
+ // The following should fail because case sensitivity is not disabled
52
+
53
+ let s = r#"{ "log": "error" }"# ;
47
54
let c = config ( s) ;
48
- assert_eq ! ( c. get_str ( "log" ) . unwrap( ) , "error" ) ;
55
+ assert_eq ! ( c. get_string ( "log" ) . unwrap( ) , "error" ) ;
49
56
let l = c. get :: < log:: Level > ( "log" ) ;
50
57
assert ! ( l. is_ok( ) , "Expected Ok(_) for {:?}" , l) ;
51
58
assert_eq ! ( l. unwrap( ) , log:: Level :: Error ) ;
52
59
}
60
+
61
+ #[ test]
62
+ fn test_load_level_lowercase_succeeding ( ) {
63
+ let s = r#"{ "log": "error" }"# ;
64
+ let c = config ( s) ;
65
+ assert_eq ! ( c. get_string( "log" ) . unwrap( ) , "error" ) ;
66
+ let s = c. try_deserialize :: < Settings > ( ) ;
67
+ assert ! ( s. is_ok( ) , "Expected Ok(_) for {:?}" , s) ;
68
+ assert_eq ! ( s. unwrap( ) . log, log:: Level :: Error ) ;
69
+ }
0 commit comments