Debian Codegraph @

tests/test.rs

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