From 3fdb2a3a19bc0248763d2bf15a152b8661534a82 Mon Sep 17 00:00:00 2001 From: Ryan Leckey Date: Tue, 13 Jun 2017 17:36:41 -0700 Subject: Add YAML --- tests/Settings-invalid.yaml | 2 ++ tests/Settings.yaml | 12 ++++++++ tests/file_yaml.rs | 71 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 tests/Settings-invalid.yaml create mode 100644 tests/Settings.yaml create mode 100644 tests/file_yaml.rs (limited to 'tests') diff --git a/tests/Settings-invalid.yaml b/tests/Settings-invalid.yaml new file mode 100644 index 0000000..070ff1b --- /dev/null +++ b/tests/Settings-invalid.yaml @@ -0,0 +1,2 @@ +ok: true +error false diff --git a/tests/Settings.yaml b/tests/Settings.yaml new file mode 100644 index 0000000..c92fcb0 --- /dev/null +++ b/tests/Settings.yaml @@ -0,0 +1,12 @@ +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/file_yaml.rs b/tests/file_yaml.rs new file mode 100644 index 0000000..4ca5557 --- /dev/null +++ b/tests/file_yaml.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, + reviews: u64, + creator: HashMap, + rating: Option, +} + +#[derive(Debug, Deserialize)] +struct Settings { + debug: f64, + production: Option, + place: Place, + #[serde(rename = "arr")] + elements: Vec, +} + +fn make() -> Config { + let mut c = Config::default(); + c.merge(File::new("tests/Settings", FileFormat::Yaml)) + .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::Yaml)); + + assert!(res.is_err()); + assert_eq!(res.unwrap_err().to_string(), + "while parsing a block mapping, did not find expected key at line 2 column 1 in tests/Settings-invalid.yaml" + .to_string()); +} -- cgit v1.2.3