|
1 |
| -#![cfg(feature = "json5")] |
2 |
| - |
3 |
| -extern crate config; |
4 |
| -extern crate float_cmp; |
5 |
| -extern crate serde; |
6 |
| - |
7 |
| -#[macro_use] |
8 |
| -extern crate serde_derive; |
9 |
| - |
10 |
| -use config::*; |
11 |
| -use float_cmp::ApproxEqUlps; |
12 |
| -use std::collections::HashMap; |
13 |
| -use std::path::PathBuf; |
14 |
| - |
15 |
| -#[derive(Debug, Deserialize)] |
16 |
| -struct Place { |
17 |
| - name: String, |
18 |
| - longitude: f64, |
19 |
| - latitude: f64, |
20 |
| - favorite: bool, |
21 |
| - telephone: Option<String>, |
22 |
| - reviews: u64, |
23 |
| - creator: HashMap<String, Value>, |
24 |
| - rating: Option<f32>, |
25 |
| -} |
26 |
| - |
27 |
| -#[derive(Debug, Deserialize)] |
28 |
| -struct Settings { |
29 |
| - debug: f64, |
30 |
| - production: Option<String>, |
31 |
| - place: Place, |
32 |
| - #[serde(rename = "arr")] |
33 |
| - elements: Vec<String>, |
34 |
| -} |
35 |
| - |
36 |
| -fn make() -> Config { |
37 |
| - Config::builder() |
38 |
| - .add_source(File::new("tests/Settings", FileFormat::Json5)) |
39 |
| - .build() |
40 |
| - .unwrap() |
41 |
| -} |
42 |
| - |
43 |
| -#[test] |
44 |
| -fn test_file() { |
45 |
| - let c = make(); |
46 |
| - |
47 |
| - // Deserialize the entire file as single struct |
48 |
| - let s: Settings = c.try_into().unwrap(); |
49 |
| - |
50 |
| - assert!(s.debug.approx_eq_ulps(&1.0, 2)); |
51 |
| - assert_eq!(s.production, Some("false".to_string())); |
52 |
| - assert_eq!(s.place.name, "Torre di Pisa"); |
53 |
| - assert!(s.place.longitude.approx_eq_ulps(&43.7224985, 2)); |
54 |
| - assert!(s.place.latitude.approx_eq_ulps(&10.3970522, 2)); |
55 |
| - assert_eq!(s.place.favorite, false); |
56 |
| - assert_eq!(s.place.reviews, 3866); |
57 |
| - assert_eq!(s.place.rating, Some(4.5)); |
58 |
| - assert_eq!(s.place.telephone, None); |
59 |
| - assert_eq!(s.elements.len(), 10); |
60 |
| - assert_eq!(s.elements[3], "4".to_string()); |
61 |
| - assert_eq!( |
62 |
| - s.place.creator["name"].clone().into_string().unwrap(), |
63 |
| - "John Smith".to_string() |
64 |
| - ); |
65 |
| -} |
66 |
| - |
67 |
| -#[test] |
68 |
| -fn test_error_parse() { |
69 |
| - let res = Config::builder() |
70 |
| - .add_source(File::new("tests/Settings-invalid", FileFormat::Json5)) |
71 |
| - .build(); |
72 |
| - |
73 |
| - let path_with_extension: PathBuf = ["tests", "Settings-invalid.json5"].iter().collect(); |
74 |
| - |
75 |
| - assert!(res.is_err()); |
76 |
| - assert_eq!( |
77 |
| - res.unwrap_err().to_string(), |
78 |
| - format!( |
79 |
| - " --> 2:7\n |\n2 | ok: true␊\n | ^---\n |\n = expected null in {}", |
80 |
| - path_with_extension.display() |
81 |
| - ) |
82 |
| - ); |
83 |
| -} |
| 1 | +#![cfg(feature = "json5")] |
| 2 | + |
| 3 | +extern crate config; |
| 4 | +extern crate float_cmp; |
| 5 | +extern crate serde; |
| 6 | + |
| 7 | +#[macro_use] |
| 8 | +extern crate serde_derive; |
| 9 | + |
| 10 | +use config::*; |
| 11 | +use float_cmp::ApproxEqUlps; |
| 12 | +use std::collections::HashMap; |
| 13 | +use std::path::PathBuf; |
| 14 | + |
| 15 | +#[derive(Debug, Deserialize)] |
| 16 | +struct Place { |
| 17 | + name: String, |
| 18 | + longitude: f64, |
| 19 | + latitude: f64, |
| 20 | + favorite: bool, |
| 21 | + telephone: Option<String>, |
| 22 | + reviews: u64, |
| 23 | + creator: HashMap<String, Value>, |
| 24 | + rating: Option<f32>, |
| 25 | +} |
| 26 | + |
| 27 | +#[derive(Debug, Deserialize)] |
| 28 | +struct Settings { |
| 29 | + debug: f64, |
| 30 | + production: Option<String>, |
| 31 | + place: Place, |
| 32 | + #[serde(rename = "arr")] |
| 33 | + elements: Vec<String>, |
| 34 | +} |
| 35 | + |
| 36 | +fn make() -> Config { |
| 37 | + Config::builder() |
| 38 | + .add_source(File::new("tests/Settings", FileFormat::Json5)) |
| 39 | + .build() |
| 40 | + .unwrap() |
| 41 | +} |
| 42 | + |
| 43 | +#[test] |
| 44 | +fn test_file() { |
| 45 | + let c = make(); |
| 46 | + |
| 47 | + // Deserialize the entire file as single struct |
| 48 | + let s: Settings = c.try_into().unwrap(); |
| 49 | + |
| 50 | + assert!(s.debug.approx_eq_ulps(&1.0, 2)); |
| 51 | + assert_eq!(s.production, Some("false".to_string())); |
| 52 | + assert_eq!(s.place.name, "Torre di Pisa"); |
| 53 | + assert!(s.place.longitude.approx_eq_ulps(&43.7224985, 2)); |
| 54 | + assert!(s.place.latitude.approx_eq_ulps(&10.3970522, 2)); |
| 55 | + assert_eq!(s.place.favorite, false); |
| 56 | + assert_eq!(s.place.reviews, 3866); |
| 57 | + assert_eq!(s.place.rating, Some(4.5)); |
| 58 | + assert_eq!(s.place.telephone, None); |
| 59 | + assert_eq!(s.elements.len(), 10); |
| 60 | + assert_eq!(s.elements[3], "4".to_string()); |
| 61 | + assert_eq!( |
| 62 | + s.place.creator["name"].clone().into_string().unwrap(), |
| 63 | + "John Smith".to_string() |
| 64 | + ); |
| 65 | +} |
| 66 | + |
| 67 | +#[test] |
| 68 | +fn test_error_parse() { |
| 69 | + let res = Config::builder() |
| 70 | + .add_source(File::new("tests/Settings-invalid", FileFormat::Json5)) |
| 71 | + .build(); |
| 72 | + |
| 73 | + let path_with_extension: PathBuf = ["tests", "Settings-invalid.json5"].iter().collect(); |
| 74 | + |
| 75 | + assert!(res.is_err()); |
| 76 | + assert_eq!( |
| 77 | + res.unwrap_err().to_string(), |
| 78 | + format!( |
| 79 | + " --> 2:7\n |\n2 | ok: true␊\n | ^---\n |\n = expected null in {}", |
| 80 | + path_with_extension.display() |
| 81 | + ) |
| 82 | + ); |
| 83 | +} |
0 commit comments