summaryrefslogtreecommitdiffstats
path: root/tests
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
parent802f947fa2b5060b605aa39c0c7c201ede9a5ed8 (diff)
support reading enums from config
Diffstat (limited to 'tests')
-rw-r--r--tests/Settings.toml13
-rw-r--r--tests/errors.rs40
-rw-r--r--tests/get.rs23
3 files changed, 76 insertions, 0 deletions
diff --git a/tests/Settings.toml b/tests/Settings.toml
index 14f881f..26e6d26 100644
--- a/tests/Settings.toml
+++ b/tests/Settings.toml
@@ -10,6 +10,19 @@ boolean_s_parse = "fals"
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
+[diodes]
+green = "off"
+
+[diodes.red]
+brightness = 100
+
+[diodes.blue]
+blinking = [300, 700]
+
+[diodes.white.pattern]
+name = "christmas"
+inifinite = true
+
[[items]]
name = "1"
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"
+ );
+}
+
diff --git a/tests/get.rs b/tests/get.rs
index 73eeeaf..7e3be8a 100644
--- a/tests/get.rs
+++ b/tests/get.rs
@@ -202,3 +202,26 @@ fn test_struct_array() {
assert_eq!(s.elements.len(), 10);
assert_eq!(s.elements[3], "4".to_string());
}
+
+#[test]
+fn test_enum() {
+ #[derive(Debug, Deserialize, PartialEq)]
+ enum Diode {
+ Off,
+ Brightness(i32),
+ Blinking(i32, i32),
+ Pattern { name: String, inifinite: bool },
+ }
+ #[derive(Debug, Deserialize)]
+ struct Settings {
+ diodes: HashMap<String, Diode>,
+ }
+
+ let c = make();
+ let s: Settings = c.try_into().unwrap();
+
+ assert_eq!(s.diodes["green"], Diode::Off);
+ assert_eq!(s.diodes["red"], Diode::Brightness(100));
+ assert_eq!(s.diodes["blue"], Diode::Blinking(300, 700));
+ assert_eq!(s.diodes["white"], Diode::Pattern{name: "christmas".into(), inifinite: true,});
+}