summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorRaphael Cohn <raphael.cohn@stormmq.com>2017-09-01 09:06:27 +0100
committerRaphael Cohn <raphael.cohn@stormmq.com>2017-09-01 09:06:27 +0100
commit9826b2cda730116e148120dafe0fa89bd389626e (patch)
tree164597d8e40017ca690c259c75eacee9134f6f5d /tests
parent13151213a7b368296c616e0a770fb2c238fff1a0 (diff)
Added HJSON (Human-Readable JSON) as a config file format
Diffstat (limited to 'tests')
-rw-r--r--tests/Settings-invalid.hjson4
-rw-r--r--tests/Settings.hjson16
-rw-r--r--tests/datetime.rs19
-rw-r--r--tests/file_hjson.rs75
4 files changed, 114 insertions, 0 deletions
diff --git a/tests/Settings-invalid.hjson b/tests/Settings-invalid.hjson
new file mode 100644
index 0000000..7e31ec3
--- /dev/null
+++ b/tests/Settings-invalid.hjson
@@ -0,0 +1,4 @@
+{
+ ok: true,
+ error
+}
diff --git a/tests/Settings.hjson b/tests/Settings.hjson
new file mode 100644
index 0000000..3e04ccf
--- /dev/null
+++ b/tests/Settings.hjson
@@ -0,0 +1,16 @@
+{
+ debug: true
+ production: false
+ arr: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
+ place: {
+ name: Torre di Pisa
+ longitude: 43.7224985
+ latitude: 10.3970522
+ favorite: false
+ reviews: 3866
+ rating: 4.5
+ creator: {
+ name: John Smith
+ }
+ }
+}
diff --git a/tests/datetime.rs b/tests/datetime.rs
index 8a50c01..89a34a1 100644
--- a/tests/datetime.rs
+++ b/tests/datetime.rs
@@ -29,6 +29,15 @@ fn make() -> Config {
FileFormat::Toml,
))
.unwrap()
+ .merge(File::from_str(
+ r#"
+ {
+ "hjson_datetime": "2017-05-10T02:14:53Z"
+ }
+ "#,
+ FileFormat::Hjson,
+ ))
+ .unwrap()
.clone()
}
@@ -50,6 +59,11 @@ fn test_datetime_string() {
let date: String = s.get("yaml_datetime").unwrap();
assert_eq!(&date, "2017-06-12T10:58:30Z");
+
+ // HJSON
+ let date: String = s.get("hjson_datetime").unwrap();
+
+ assert_eq!(&date, "2017-05-10T02:14:53Z");
}
#[test]
@@ -70,4 +84,9 @@ fn test_datetime() {
let date: DateTime<Utc> = s.get("yaml_datetime").unwrap();
assert_eq!(date, Utc.ymd(2017, 6, 12).and_hms(10, 58, 30));
+
+ // HJSON
+ let date: DateTime<Utc> = s.get("hjson_datetime").unwrap();
+
+ assert_eq!(date, Utc.ymd(2017, 5, 10).and_hms(2, 14, 53));
}
diff --git a/tests/file_hjson.rs b/tests/file_hjson.rs
new file mode 100644
index 0000000..f36843a
--- /dev/null
+++ b/tests/file_hjson.rs
@@ -0,0 +1,75 @@
+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::Hjson))
+ .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::Hjson));
+
+ assert!(res.is_err());
+ assert_eq!(
+ res.unwrap_err().to_string(),
+ "Found a punctuator where a key name was expected (check your syntax or use quotes if the key name includes {}[],: or whitespace) at line 1 column 1 in tests/Settings-invalid.hjson".to_string()
+ );
+}