summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorup9cloud <8325632+up9cloud@users.noreply.github.com>2020-07-06 13:07:05 -0700
committerMatthias Beyer <mail@beyermatthias.de>2021-05-15 14:35:58 +0200
commit70c503af8b3cb3d73bbbea673bb49460df318e5e (patch)
tree3550f1015af6186e5f6c1cade83345ee48609e22 /tests
parent266f504d9f23e192c03ef486f58a678847249b60 (diff)
Support format json5
Diffstat (limited to 'tests')
-rw-r--r--tests/Settings-invalid.json54
-rw-r--r--tests/Settings.json518
-rw-r--r--tests/errors.rs17
-rw-r--r--tests/file_json5.rs83
4 files changed, 122 insertions, 0 deletions
diff --git a/tests/Settings-invalid.json5 b/tests/Settings-invalid.json5
new file mode 100644
index 0000000..7e97bc1
--- /dev/null
+++ b/tests/Settings-invalid.json5
@@ -0,0 +1,4 @@
+{
+ ok: true
+ error
+}
diff --git a/tests/Settings.json5 b/tests/Settings.json5
new file mode 100644
index 0000000..cfab1f5
--- /dev/null
+++ b/tests/Settings.json5
@@ -0,0 +1,18 @@
+{
+ // c
+ /* c */
+ 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/errors.rs b/tests/errors.rs
index 54bbe95..e11ad69 100644
--- a/tests/errors.rs
+++ b/tests/errors.rs
@@ -137,3 +137,20 @@ inner:
panic!("Wrong error {:?}", e);
}
}
+
+#[test]
+fn test_error_root_not_table() {
+ match Config::builder()
+ .add_source(File::from_str(r#"false"#, FileFormat::Json5))
+ .build()
+ {
+ Ok(_) => panic!("Should not merge if root is not a table"),
+ Err(e) => match e {
+ ConfigError::FileParse { cause, .. } => assert_eq!(
+ "invalid type: boolean `false`, expected a map",
+ format!("{}", cause)
+ ),
+ _ => panic!("Wrong error: {:?}", e),
+ },
+ }
+}
diff --git a/tests/file_json5.rs b/tests/file_json5.rs
new file mode 100644
index 0000000..d22b726
--- /dev/null
+++ b/tests/file_json5.rs
@@ -0,0 +1,83 @@
+#![cfg(feature = "json5")]
+
+extern crate config;
+extern crate float_cmp;
+extern crate serde;
+
+#[macro_use]
+extern crate serde_derive;
+
+use config::*;
+use float_cmp::ApproxEqUlps;
+use std::collections::HashMap;
+use std::path::PathBuf;
+
+#[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 {
+ Config::builder()
+ .add_source(File::new("tests/Settings", FileFormat::Json5))
+ .build()
+ .unwrap()
+}
+
+#[test]
+fn test_file() {
+ let c = make();
+
+ // Deserialize the entire file as single struct
+ let s: Settings = c.try_into().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_string().unwrap(),
+ "John Smith".to_string()
+ );
+}
+
+#[test]
+fn test_error_parse() {
+ let res = Config::builder()
+ .add_source(File::new("tests/Settings-invalid", FileFormat::Json5))
+ .build();
+
+ let path_with_extension: PathBuf = ["tests", "Settings-invalid.json5"].iter().collect();
+
+ assert!(res.is_err());
+ assert_eq!(
+ res.unwrap_err().to_string(),
+ format!(
+ " --> 2:7\n |\n2 | ok: true␊\n | ^---\n |\n = expected null in {}",
+ path_with_extension.display()
+ )
+ );
+}