1 #![cfg(feature = "toml")]
2
3 use serde_derive::Deserialize;
4
5 use std::path::PathBuf;
6
7 use config::{Config, ConfigError, File, FileFormat, Map, Value};
8
make() -> Config9 fn make() -> Config {
10 Config::builder()
11 .add_source(File::new("tests/Settings", FileFormat::Toml))
12 .build()
13 .unwrap()
14 }
15
16 #[test]
test_error_parse()17 fn test_error_parse() {
18 let res = Config::builder()
19 .add_source(File::new("tests/Settings-invalid", FileFormat::Toml))
20 .build();
21
22 let path: PathBuf = ["tests", "Settings-invalid.toml"].iter().collect();
23
24 assert!(res.is_err());
25 assert_eq!(
26 res.unwrap_err().to_string(),
27 format!(
28 "invalid TOML value, did you mean to use a quoted string? at line 2 column 9 in {}",
29 path.display()
30 )
31 );
32 }
33
34 #[test]
test_error_type()35 fn test_error_type() {
36 let c = make();
37
38 let res = c.get::<bool>("boolean_s_parse");
39
40 let path: PathBuf = ["tests", "Settings.toml"].iter().collect();
41
42 assert!(res.is_err());
43 assert_eq!(
44 res.unwrap_err().to_string(),
45 format!(
46 "invalid type: string \"fals\", expected a boolean for key `boolean_s_parse` in {}",
47 path.display()
48 )
49 );
50 }
51
52 #[test]
test_error_type_detached()53 fn test_error_type_detached() {
54 let c = make();
55
56 let value = c.get::<Value>("boolean_s_parse").unwrap();
57 let res = value.try_deserialize::<bool>();
58
59 assert!(res.is_err());
60 assert_eq!(
61 res.unwrap_err().to_string(),
62 "invalid type: string \"fals\", expected a boolean".to_string()
63 );
64 }
65
66 #[test]
test_error_enum_de()67 fn test_error_enum_de() {
68 #[derive(Debug, Deserialize, PartialEq)]
69 enum Diode {
70 Off,
71 Brightness(i32),
72 Blinking(i32, i32),
73 Pattern { name: String, inifinite: bool },
74 }
75
76 let on_v: Value = "on".into();
77 let on_d = on_v.try_deserialize::<Diode>();
78 assert_eq!(
79 on_d.unwrap_err().to_string(),
80 "enum Diode does not have variant constructor on".to_string()
81 );
82
83 let array_v: Value = vec![100, 100].into();
84 let array_d = array_v.try_deserialize::<Diode>();
85 assert_eq!(
86 array_d.unwrap_err().to_string(),
87 "value of enum Diode should be represented by either string or table with exactly one key"
88 );
89
90 let confused_v: Value = [
91 ("Brightness".to_string(), 100.into()),
92 ("Blinking".to_string(), vec![300, 700].into()),
93 ]
94 .iter()
95 .cloned()
96 .collect::<Map<String, Value>>()
97 .into();
98 let confused_d = confused_v.try_deserialize::<Diode>();
99 assert_eq!(
100 confused_d.unwrap_err().to_string(),
101 "value of enum Diode should be represented by either string or table with exactly one key"
102 );
103 }
104
105 #[test]
error_with_path()106 fn error_with_path() {
107 #[derive(Debug, Deserialize)]
108 struct Inner {
109 #[allow(dead_code)]
110 test: i32,
111 }
112
113 #[derive(Debug, Deserialize)]
114 struct Outer {
115 #[allow(dead_code)]
116 inner: Inner,
117 }
118 const CFG: &str = r#"
119 inner:
120 test: ABC
121 "#;
122
123 let e = Config::builder()
124 .add_source(File::from_str(CFG, FileFormat::Yaml))
125 .build()
126 .unwrap()
127 .try_deserialize::<Outer>()
128 .unwrap_err();
129
130 if let ConfigError::Type {
131 key: Some(path), ..
132 } = e
133 {
134 assert_eq!(path, "inner.test");
135 } else {
136 panic!("Wrong error {:?}", e);
137 }
138 }
139
140 #[test]
test_error_root_not_table()141 fn test_error_root_not_table() {
142 match Config::builder()
143 .add_source(File::from_str(r#"false"#, FileFormat::Json5))
144 .build()
145 {
146 Ok(_) => panic!("Should not merge if root is not a table"),
147 Err(e) => match e {
148 ConfigError::FileParse { cause, .. } => assert_eq!(
149 "invalid type: boolean `false`, expected a map",
150 format!("{}", cause)
151 ),
152 _ => panic!("Wrong error: {:?}", e),
153 },
154 }
155 }
156