Debian Codegraph @

tests/test.rs

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 use configparser::ini::Ini;
2 use std::error::Error;
3
4 #[test]
5 #[allow(clippy::approx_constant)]
6 fn non_cs() -> Result<(), Box<dyn Error>> {
•7 use std::path::PathBuf;
8 let mut config = Ini::new();
9 let map = config.load("tests/test.ini")?;
10 config.set_comment_symbols(&[';', '#', '!']);
11 let inpstring = config.read(
12 "defaultvalues=defaultvalues
13 [topsecret]
14 KFC = the secret herb is orega-
15 colon:value after colon
16 Empty string =
17 None string
18 Password=[in-brackets]
19 [ spacing ]
20 indented=indented
21 not indented = not indented ;testcomment
22 !modified comment
23 [values]#another comment
24 Bool = True
25 Boolcoerce = 0
26 Int = -31415
27 Uint = 31415
28 Float = 3.1415"
29 .to_owned(),
30 )?;
31 assert_eq!(map, inpstring);
32 config.set("DEFAULT", "defaultvalues", Some("notdefault".to_owned()));
33 assert_eq!(
34 config.get("DEFAULT", "defaultvalues").unwrap(),
35 "notdefault"
36 );
37 config.setstr("DEFAULT", "defaultvalues", Some("defaultvalues"));
38 assert_eq!(
39 config.get("DEFAULT", "defaultvalues").unwrap(),
40 "defaultvalues"
41 );
42 config.setstr("DEFAULT", "defaultvalues", None);
•43 let path = PathBuf::from(env!("CARGO_TARGET_TMPDIR"));
44 config.write(path.join("output.ini"))?;
45 let map2 = config.clone().load(path.join("output.ini"))?;
46 assert_eq!(map2, *config.get_map_ref());
47 let map3 = config.clone().read(config.writes())?;
48 assert_eq!(map2, map3);
49 assert_eq!(config.sections().len(), 4);
50 assert_eq!(config.get("DEFAULT", "defaultvalues"), None);
51 assert_eq!(
52 config.get("topsecret", "KFC").unwrap(),
53 "the secret herb is orega-"
54 );
55 assert_eq!(config.get("topsecret", "Empty string").unwrap(), "");
56 assert_eq!(config.get("topsecret", "None string"), None);
57 assert_eq!(config.get("spacing", "indented").unwrap(), "indented");
58 assert_eq!(
59 config.get("spacing", "not indented").unwrap(),
60 "not indented"
61 );
62 assert_eq!(
63 config.get("topsecret", "colon").unwrap(),
64 "value after colon"
65 );
66 assert!(config.getbool("values", "Bool")?.unwrap());
67 assert!(!config.getboolcoerce("values", "Boolcoerce")?.unwrap());
68 assert_eq!(config.getint("values", "Int")?.unwrap(), -31415);
69 assert_eq!(config.getuint("values", "Uint")?.unwrap(), 31415);
70 assert_eq!(config.getfloat("values", "Float")?.unwrap(), 3.1415);
71 assert_eq!(config.getfloat("topsecret", "None string"), Ok(None));
72 assert_eq!(
73 map["default"]["defaultvalues"].clone().unwrap(),
74 "defaultvalues"
75 );
76 assert_eq!(
77 map["topsecret"]["kfc"].clone().unwrap(),
78 "the secret herb is orega-"
79 );
80 assert_eq!(map["topsecret"]["empty string"].clone().unwrap(), "");
81 assert_eq!(map["topsecret"]["none string"], None);
82 assert_eq!(map["spacing"]["indented"].clone().unwrap(), "indented");
83 assert_eq!(
84 map["spacing"]["not indented"].clone().unwrap(),
85 "not indented"
86 );
87 let mut config2 = config.clone();
88 let val = config2.remove_key("default", "defaultvalues");
89 assert_eq!(val, Some(None));
90 assert_eq!(config2.get("default", "defaultvalues"), None);
91 config2.remove_section("default");
92 assert_eq!(config2.get("default", "nope"), None);
93 let mut_map = config.get_mut_map();
94 mut_map.get_mut("topsecret").unwrap().insert(
95 String::from("none string"),
96 Some(String::from("None string")),
97 );
98 assert_eq!(
99 mut_map["topsecret"]["none string"].clone().unwrap(),
100 "None string"
101 );
102 mut_map.clear();
103 config2.clear();
104 assert_eq!(config.get_map_ref(), config2.get_map_ref());
105
106 config.load("tests/test.ini")?;
107 config.read_and_append("defaultvalues=somenewvalue".to_owned())?;
108 assert_eq!(
109 config.get("default", "defaultvalues").unwrap(),
110 "somenewvalue"
111 );
112 assert_eq!(
113 config.get("topsecret", "KFC").unwrap(),
114 "the secret herb is orega-"
115 );
116
117 let mut config3 = config.clone();
118 let mut_map = config3.get_mut_map();
119 mut_map.clear();
120 config3.load("tests/test.ini")?;
121 config3.load_and_append("tests/test_more.ini")?;
122 assert_eq!(
123 config3.get("default", "defaultvalues").unwrap(),
124 "overwritten"
125 );
126 assert_eq!(config3.get("topsecret", "KFC").unwrap(), "redacted");
127 // spacing -> indented exists in tests/test.ini, but not tests/test_more.ini
128 assert_eq!(config3.get("spacing", "indented").unwrap(), "indented");
129 assert_eq!(config3.getbool("values", "Bool")?.unwrap(), false);
130
131 Ok(())
132 }
133
134 #[test]
135 #[allow(clippy::approx_constant)]
136 fn cs() -> Result<(), Box<dyn Error>> {
•137 use std::path::PathBuf;
138 let mut config = Ini::new_cs();
139 let map = config.load("tests/test.ini")?;
140 config.set_comment_symbols(&[';', '#', '!']);
141 let inpstring = config.read(
142 "defaultvalues=defaultvalues
143 [topsecret]
144 KFC = the secret herb is orega-
145 colon:value after colon
146 Empty string =
147 None string
148 Password=[in-brackets]
149 [ spacing ]
150 indented=indented
151 not indented = not indented ;testcomment
152 !modified comment
153 [values]#another comment
154 Bool = True
155 Boolcoerce = 0
156 Int = -31415
157 Uint = 31415
158 Float = 3.1415"
159 .to_owned(),
160 )?;
161 assert_eq!(map, inpstring);
162 config.set("default", "defaultvalues", Some("notdefault".to_owned()));
163 assert_eq!(
164 config.get("default", "defaultvalues").unwrap(),
165 "notdefault"
166 );
167 config.setstr("default", "defaultvalues", Some("defaultvalues"));
168 assert_eq!(
169 config.get("default", "defaultvalues").unwrap(),
170 "defaultvalues"
171 );
172 config.setstr("default", "defaultvalues", None);
•173 let path = PathBuf::from(env!("CARGO_TARGET_TMPDIR"));
174 config.write(path.join("output2.ini"))?;
175 let map2 = config.clone().load(path.join("output2.ini"))?;
176 assert_eq!(map2, *config.get_map_ref());
177 let map3 = config.clone().read(config.writes())?;
178 assert_eq!(map2, map3);
179 assert_eq!(config.sections().len(), 4);
180 assert_eq!(config.get("default", "defaultvalues"), None);
181 assert_eq!(
182 config.get("topsecret", "KFC").unwrap(),
183 "the secret herb is orega-"
184 );
185 assert_eq!(config.get("topsecret", "Empty string").unwrap(), "");
186 assert_eq!(config.get("topsecret", "None string"), None);
187 assert_eq!(config.get("spacing", "indented").unwrap(), "indented");
188 assert_eq!(
189 config.get("spacing", "not indented").unwrap(),
190 "not indented"
191 );
192 assert_eq!(
193 config.get("topsecret", "colon").unwrap(),
194 "value after colon"
195 );
196 assert!(config.getbool("values", "Bool")?.unwrap());
197 assert!(!config.getboolcoerce("values", "Boolcoerce")?.unwrap());
198 assert_eq!(config.getint("values", "Int")?.unwrap(), -31415);
199 assert_eq!(config.getuint("values", "Uint")?.unwrap(), 31415);
200 assert_eq!(config.getfloat("values", "Float")?.unwrap(), 3.1415);
201 assert_eq!(config.getfloat("topsecret", "None string"), Ok(None));
202 assert_eq!(
203 map["default"]["defaultvalues"].clone().unwrap(),
204 "defaultvalues"
205 );
206 assert_eq!(
207 map["topsecret"]["KFC"].clone().unwrap(),
208 "the secret herb is orega-"
209 );
210 assert_eq!(map["topsecret"]["Empty string"].clone().unwrap(), "");
211 assert_eq!(map["topsecret"]["None string"], None);
212 assert_eq!(map["spacing"]["indented"].clone().unwrap(), "indented");
213 assert_eq!(
214 map["spacing"]["not indented"].clone().unwrap(),
215 "not indented"
216 );
217 let mut config2 = config.clone();
218 let val = config2.remove_key("default", "defaultvalues");
219 assert_eq!(val, Some(None));
220 assert_eq!(config2.get("default", "defaultvalues"), None);
221 config2.remove_section("default");
222 assert_eq!(config2.get("default", "nope"), None);
223 let mut_map = config.get_mut_map();
224 mut_map.get_mut("topsecret").unwrap().insert(
225 String::from("none string"),
226 Some(String::from("None string")),
227 );
228 assert_eq!(
229 mut_map["topsecret"]["none string"].clone().unwrap(),
230 "None string"
231 );
232 mut_map.clear();
233 config2.clear();
234 assert_eq!(config.get_map_ref(), config2.get_map_ref());
235 Ok(())
236 }
237
238 #[test]
239 #[cfg(feature = "indexmap")]
240 fn sort_on_write() -> Result<(), Box<dyn Error>> {
241 let mut config = Ini::new_cs();
242 config.load("tests/test.ini")?;
243
244 assert_eq!(
245 config.writes(),
246 "defaultvalues=defaultvalues
247 [topsecret]
248 KFC=the secret herb is orega-
249 colon=value after colon
250 Empty string=
251 None string
252 Password=[in-brackets]
253 [spacing]
254 indented=indented
255 not indented=not indented
256 [values]
257 Bool=True
258 Boolcoerce=0
259 Int=-31415
260 Uint=31415
261 Float=3.1415
262 "
263 );
264
265 Ok(())
266 }
267
268 #[test]
269 #[cfg(feature = "async-std")]
270 fn async_load_write() -> Result<(), Box<dyn Error>> {
•271 use std::path::PathBuf;
272 const OUT_FILE_CONTENTS: &str = "defaultvalues=defaultvalues
273 [topsecret]
274 KFC = the secret herb is orega-
275 colon:value after colon
276 Empty string =
277 None string
278 Password=[in-brackets]
279 [ spacing ]
280 indented=indented
281 not indented = not indented ;testcomment
282 !modified comment
283 [values]#another comment
284 Bool = True
285 Boolcoerce = 0
286 Int = -31415
287 Uint = 31415
288 Float = 3.1415";
289
290 let mut config = Ini::new();
291 config.read(OUT_FILE_CONTENTS.to_owned())?;
•292 let path = PathBuf::from(env!("CARGO_TARGET_TMPDIR"));
293 config.write(path.join("output_sync.ini"))?;
294
295 async_std::task::block_on::<_, Result<_, String>>(async {
296 let mut config_async = Ini::new();
297 config_async.read(OUT_FILE_CONTENTS.to_owned())?;
298 config_async
•299 .write_async(path.join("output_async.ini"))
300 .await
301 .map_err(|e| e.to_string())?;
302 Ok(())
303 })?;
304
305 let mut sync_content = Ini::new();
•306 sync_content.load(path.join("output_sync.ini"))?;
307
308 let async_content = async_std::task::block_on::<_, Result<_, String>>(async {
309 let mut async_content = Ini::new();
•310 async_content.load_async(path.join("output_async.ini")).await?;
311 Ok(async_content)
312 })?;
313
314 assert_eq!(sync_content, async_content);
315
316 Ok(())
317 }
318
319 #[test]
320 #[cfg(feature = "async-std")]
321 fn async_load_and_append() -> Result<(), Box<dyn Error>> {
322 let mut sync_content = Ini::new();
323 sync_content.load("tests/test.ini")?;
324 sync_content.load_and_append("tests/test_more.ini")?;
325
326 let async_content = async_std::task::block_on::<_, Result<_, String>>(async {
327 let mut async_content = Ini::new();
328 async_content.load_async("tests/test.ini").await?;
329 async_content
330 .load_and_append_async("tests/test_more.ini")
331 .await?;
332 Ok(async_content)
333 })?;
334
335 assert_eq!(sync_content, async_content);
336
337 Ok(())
338 }
339
340 #[test]
341 #[cfg(feature = "indexmap")]
342 fn multiline_off() -> Result<(), Box<dyn Error>> {
343 let mut config = Ini::new_cs();
344 config.load("tests/test_multiline.ini")?;
345
346 let map = config.get_map_ref();
347
348 let section = map.get("Section").unwrap();
349
350 assert_eq!(config.get("Section", "Key1").unwrap(), "Value1");
351 assert_eq!(config.get("Section", "Key2").unwrap(), "Value Two");
352 assert_eq!(config.get("Section", "Key3").unwrap(), "this is a haiku");
353 assert!(section.contains_key("spread across separate lines"));
354 assert!(section.contains_key("a single value"));
355
356 assert_eq!(config.get("Section", "Key4").unwrap(), "Four");
357
358 assert_eq!(
359 config.writes(),
360 "[Section]
361 Key1=Value1
362 Key2=Value Two
363 Key3=this is a haiku
364 spread across separate lines
365 a single value
366 Key4=Four
367 "
368 );
369
370 Ok(())
371 }
372
373 #[test]
374 #[cfg(feature = "indexmap")]
375 fn multiline_on() -> Result<(), Box<dyn Error>> {
376 let mut config = Ini::new_cs();
377 config.set_multiline(true);
378 config.load("tests/test_multiline.ini")?;
379
380 assert_eq!(config.get("Section", "Key1").unwrap(), "Value1");
381 assert_eq!(config.get("Section", "Key2").unwrap(), "Value Two");
382 assert_eq!(
383 config.get("Section", "Key3").unwrap(),
384 "this is a haiku\nspread across separate lines\na single value"
385 );
386 assert_eq!(config.get("Section", "Key4").unwrap(), "Four");
387
388 assert_eq!(
389 config.writes(),
390 "[Section]
391 Key1=Value1
392 Key2=Value Two
393 Key3=this is a haiku
394 spread across separate lines
395 a single value
396 Key4=Four
397 "
398 );
399
400 Ok(())
401 }