1
1
#![ cfg( feature = "json5" ) ]
2
2
3
- use std:: path:: PathBuf ;
4
-
5
3
use chrono:: { DateTime , TimeZone , Utc } ;
6
4
use float_cmp:: ApproxEqUlps ;
7
5
use serde_derive:: Deserialize ;
@@ -32,7 +30,33 @@ fn test_file() {
32
30
}
33
31
34
32
let c = Config :: builder ( )
35
- . add_source ( File :: new ( "tests/Settings" , FileFormat :: Json5 ) )
33
+ . add_source ( File :: from_str (
34
+ r#"
35
+ {
36
+ // c
37
+ /* c */
38
+ debug: true,
39
+ production: false,
40
+ arr: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10,],
41
+ place: {
42
+ name: 'Torre di Pisa',
43
+ longitude: 43.7224985,
44
+ latitude: 10.3970522,
45
+ favorite: false,
46
+ reviews: 3866,
47
+ rating: 4.5,
48
+ creator: {
49
+ name: "John Smith",
50
+ "username": "jsmith",
51
+ "email": "jsmith@localhost",
52
+ }
53
+ },
54
+ FOO: "FOO should be overridden",
55
+ bar: "I am bar",
56
+ }
57
+ "# ,
58
+ FileFormat :: Json5 ,
59
+ ) )
36
60
. build ( )
37
61
. unwrap ( ) ;
38
62
@@ -73,18 +97,21 @@ fn test_file() {
73
97
#[ test]
74
98
fn test_error_parse ( ) {
75
99
let res = Config :: builder ( )
76
- . add_source ( File :: new ( "tests/Settings-invalid" , FileFormat :: Json5 ) )
100
+ . add_source ( File :: from_str (
101
+ r#"
102
+ {
103
+ ok: true
104
+ error
105
+ }
106
+ "# ,
107
+ FileFormat :: Json5 ,
108
+ ) )
77
109
. build ( ) ;
78
110
79
- let path_with_extension: PathBuf = [ "tests" , "Settings-invalid.json5" ] . iter ( ) . collect ( ) ;
80
-
81
111
assert ! ( res. is_err( ) ) ;
82
112
assert_eq ! (
83
113
res. unwrap_err( ) . to_string( ) ,
84
- format!(
85
- " --> 2:7\n |\n 2 | ok: true\n | ^---\n |\n = expected null in {}" ,
86
- path_with_extension. display( )
87
- )
114
+ format!( " --> 3:7\n |\n 3 | ok: true\n | ^---\n |\n = expected null" , )
88
115
) ;
89
116
}
90
117
@@ -105,7 +132,33 @@ fn test_override_uppercase_value_for_struct() {
105
132
std:: env:: set_var ( "APP_FOO" , "I HAVE BEEN OVERRIDDEN_WITH_UPPER_CASE" ) ;
106
133
107
134
let cfg = Config :: builder ( )
108
- . add_source ( File :: new ( "tests/Settings" , FileFormat :: Json5 ) )
135
+ . add_source ( File :: from_str (
136
+ r#"
137
+ {
138
+ // c
139
+ /* c */
140
+ debug: true,
141
+ production: false,
142
+ arr: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10,],
143
+ place: {
144
+ name: 'Torre di Pisa',
145
+ longitude: 43.7224985,
146
+ latitude: 10.3970522,
147
+ favorite: false,
148
+ reviews: 3866,
149
+ rating: 4.5,
150
+ creator: {
151
+ name: "John Smith",
152
+ "username": "jsmith",
153
+ "email": "jsmith@localhost",
154
+ }
155
+ },
156
+ FOO: "FOO should be overridden",
157
+ bar: "I am bar",
158
+ }
159
+ "# ,
160
+ FileFormat :: Json5 ,
161
+ ) )
109
162
. add_source ( config:: Environment :: with_prefix ( "APP" ) . separator ( "_" ) )
110
163
. build ( )
111
164
. unwrap ( ) ;
@@ -146,7 +199,33 @@ fn test_override_lowercase_value_for_struct() {
146
199
std:: env:: set_var ( "config_foo" , "I have been overridden_with_lower_case" ) ;
147
200
148
201
let cfg = Config :: builder ( )
149
- . add_source ( File :: new ( "tests/Settings" , FileFormat :: Json5 ) )
202
+ . add_source ( File :: from_str (
203
+ r#"
204
+ {
205
+ // c
206
+ /* c */
207
+ debug: true,
208
+ production: false,
209
+ arr: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10,],
210
+ place: {
211
+ name: 'Torre di Pisa',
212
+ longitude: 43.7224985,
213
+ latitude: 10.3970522,
214
+ favorite: false,
215
+ reviews: 3866,
216
+ rating: 4.5,
217
+ creator: {
218
+ name: "John Smith",
219
+ "username": "jsmith",
220
+ "email": "jsmith@localhost",
221
+ }
222
+ },
223
+ FOO: "FOO should be overridden",
224
+ bar: "I am bar",
225
+ }
226
+ "# ,
227
+ FileFormat :: Json5 ,
228
+ ) )
150
229
. add_source ( config:: Environment :: with_prefix ( "config" ) . separator ( "_" ) )
151
230
. build ( )
152
231
. unwrap ( ) ;
@@ -169,7 +248,14 @@ fn test_override_uppercase_value_for_enums() {
169
248
std:: env:: set_var ( "APPS_BAR" , "I HAVE BEEN OVERRIDDEN_WITH_UPPER_CASE" ) ;
170
249
171
250
let cfg = Config :: builder ( )
172
- . add_source ( File :: new ( "tests/Settings-enum-test" , FileFormat :: Json5 ) )
251
+ . add_source ( File :: from_str (
252
+ r#"
253
+ {
254
+ bar: "bar is a lowercase param",
255
+ }
256
+ "# ,
257
+ FileFormat :: Json5 ,
258
+ ) )
173
259
. add_source ( config:: Environment :: with_prefix ( "APPS" ) . separator ( "_" ) )
174
260
. build ( )
175
261
. unwrap ( ) ;
@@ -191,7 +277,14 @@ fn test_override_lowercase_value_for_enums() {
191
277
std:: env:: set_var ( "test_bar" , "I have been overridden_with_lower_case" ) ;
192
278
193
279
let cfg = Config :: builder ( )
194
- . add_source ( File :: new ( "tests/Settings-enum-test" , FileFormat :: Json5 ) )
280
+ . add_source ( File :: from_str (
281
+ r#"
282
+ {
283
+ bar: "bar is a lowercase param",
284
+ }
285
+ "# ,
286
+ FileFormat :: Json5 ,
287
+ ) )
195
288
. add_source ( config:: Environment :: with_prefix ( "test" ) . separator ( "_" ) )
196
289
. build ( )
197
290
. unwrap ( ) ;
0 commit comments