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