summaryrefslogtreecommitdiffstats
path: root/tests/errors.rs
diff options
context:
space:
mode:
authorEugeen Sablin <EugeenSablin@gmail.com>2018-11-10 19:23:16 +0300
committerEugeen Sablin <EugeenSablin@gmail.com>2018-11-10 19:23:16 +0300
commit2cb768bddc715c32e740a067652e7200c8d344f3 (patch)
tree1ca5ccc3a5794def758a765da835fe9b8b73b1a4 /tests/errors.rs
parent802f947fa2b5060b605aa39c0c7c201ede9a5ed8 (diff)
support reading enums from config
Diffstat (limited to 'tests/errors.rs')
-rw-r--r--tests/errors.rs40
1 files changed, 40 insertions, 0 deletions
diff --git a/tests/errors.rs b/tests/errors.rs
index 6bc674f..fa2816e 100644
--- a/tests/errors.rs
+++ b/tests/errors.rs
@@ -2,6 +2,9 @@
extern crate config;
+#[macro_use]
+extern crate serde_derive;
+
use config::*;
fn make() -> Config {
@@ -52,3 +55,40 @@ fn test_error_type_detached() {
"invalid type: string \"fals\", expected a boolean".to_string()
);
}
+
+#[test]
+fn test_error_enum_de() {
+ #[derive(Debug, Deserialize, PartialEq)]
+ enum Diode {
+ Off,
+ Brightness(i32),
+ Blinking(i32, i32),
+ Pattern { name: String, inifinite: bool },
+ }
+
+ let on_v: Value = "on".into();
+ let on_d = on_v.try_into::<Diode>();
+ assert_eq!(
+ on_d.unwrap_err().to_string(),
+ "enum Diode does not have variant constructor on".to_string()
+ );
+
+ let array_v: Value = vec![100, 100].into();
+ let array_d = array_v.try_into::<Diode>();
+ assert_eq!(
+ array_d.unwrap_err().to_string(),
+ "value of enum Diode should be represented by either string or table with exactly one key"
+ );
+
+
+ let confused_v: Value =
+ [("Brightness".to_string(), 100.into()),
+ ("Blinking".to_string(), vec![300, 700].into())]
+ .iter().cloned().collect::<std::collections::HashMap<String, Value>>().into();
+ let confused_d = confused_v.try_into::<Diode>();
+ assert_eq!(
+ confused_d.unwrap_err().to_string(),
+ "value of enum Diode should be represented by either string or table with exactly one key"
+ );
+}
+