summaryrefslogtreecommitdiffstats
path: root/tests/file_json.rs
diff options
context:
space:
mode:
authorRyan Leckey <ryan@launchbadge.com>2017-06-13 15:57:21 -0700
committerRyan Leckey <ryan@launchbadge.com>2017-06-13 15:57:21 -0700
commit3c3d1e860eded0027f1efe3bb25e0fa6fa8b8940 (patch)
treedc27dc47ebf2537b3f21fe10ae23de2efe824b53 /tests/file_json.rs
parentab0d8cb9aa107c8d561f3c188e6cbf472a7df23b (diff)
Add JSON
Diffstat (limited to 'tests/file_json.rs')
-rw-r--r--tests/file_json.rs71
1 files changed, 71 insertions, 0 deletions
diff --git a/tests/file_json.rs b/tests/file_json.rs
new file mode 100644
index 0000000..983be86
--- /dev/null
+++ b/tests/file_json.rs
@@ -0,0 +1,71 @@
+extern crate config;
+extern crate serde;
+extern crate float_cmp;
+
+#[macro_use]
+extern crate serde_derive;
+
+use std::collections::HashMap;
+use float_cmp::ApproxEqUlps;
+use config::*;
+
+#[derive(Debug, Deserialize)]
+struct Place {
+ name: String,
+ longitude: f64,
+ latitude: f64,
+ favorite: bool,
+ telephone: Option<String>,
+ reviews: u64,
+ creator: HashMap<String, Value>,
+ rating: Option<f32>,
+}
+
+#[derive(Debug, Deserialize)]
+struct Settings {
+ debug: f64,
+ production: Option<String>,
+ place: Place,
+ #[serde(rename = "arr")]
+ elements: Vec<String>,
+}
+
+fn make() -> Config {
+ let mut c = Config::default();
+ c.merge(File::new("tests/Settings", FileFormat::Json))
+ .unwrap();
+
+ c
+}
+
+#[test]
+fn test_file() {
+ let c = make();
+
+ // Deserialize the entire file as single struct
+ let s: Settings = c.deserialize().unwrap();
+
+ assert!(s.debug.approx_eq_ulps(&1.0, 2));
+ assert_eq!(s.production, Some("false".to_string()));
+ assert_eq!(s.place.name, "Torre di Pisa");
+ assert!(s.place.longitude.approx_eq_ulps(&43.7224985, 2));
+ assert!(s.place.latitude.approx_eq_ulps(&10.3970522, 2));
+ assert_eq!(s.place.favorite, false);
+ assert_eq!(s.place.reviews, 3866);
+ assert_eq!(s.place.rating, Some(4.5));
+ assert_eq!(s.place.telephone, None);
+ assert_eq!(s.elements.len(), 10);
+ assert_eq!(s.elements[3], "4".to_string());
+ assert_eq!(s.place.creator["name"].clone().into_str().unwrap(), "John Smith".to_string());
+}
+
+#[test]
+fn test_error_parse() {
+ let mut c = Config::default();
+ let res = c.merge(File::new("tests/Settings-invalid", FileFormat::Json));
+
+ assert!(res.is_err());
+ assert_eq!(res.unwrap_err().to_string(),
+ "expected `:` at line 4 column 1 in tests/Settings-invalid.json"
+ .to_string());
+}