From 43c141f87a0167e50802f097cf04896258acb5e6 Mon Sep 17 00:00:00 2001 From: Ryan Leckey Date: Sat, 3 Jun 2017 01:22:38 -0700 Subject: Add many more tests --- tests/Settings-invalid.toml | 2 ++ tests/Settings-production.toml | 8 +++++ tests/Settings.invalid.toml | 2 -- tests/Settings.toml | 5 ++++ tests/errors.rs | 32 +++++++++++++++----- tests/get_array.rs | 39 ++++++++++++++++++++++++ tests/get_scalar.rs | 52 ++++++++++++++++++++++++++++++++ tests/get_struct.rs | 68 ++++++++++++++++++++++++++++++++++++++++++ tests/get_table.rs | 50 +++++++++++++++++++++++++++++++ tests/merge.rs | 24 +++++++++++++++ tests/scalar.rs | 44 --------------------------- tests/set.rs | 40 +++++++++++++++++++++++++ tests/struct.rs | 68 ------------------------------------------ 13 files changed, 312 insertions(+), 122 deletions(-) create mode 100644 tests/Settings-invalid.toml create mode 100644 tests/Settings-production.toml delete mode 100644 tests/Settings.invalid.toml create mode 100644 tests/get_array.rs create mode 100644 tests/get_scalar.rs create mode 100644 tests/get_struct.rs create mode 100644 tests/get_table.rs create mode 100644 tests/merge.rs delete mode 100644 tests/scalar.rs create mode 100644 tests/set.rs delete mode 100644 tests/struct.rs diff --git a/tests/Settings-invalid.toml b/tests/Settings-invalid.toml new file mode 100644 index 0000000..4d159a4 --- /dev/null +++ b/tests/Settings-invalid.toml @@ -0,0 +1,2 @@ +ok = true +error = tru diff --git a/tests/Settings-production.toml b/tests/Settings-production.toml new file mode 100644 index 0000000..6545b4c --- /dev/null +++ b/tests/Settings-production.toml @@ -0,0 +1,8 @@ +debug = false +production = true + +[place] +rating = 4.9 + +[place.creator] +name = "Somebody New" diff --git a/tests/Settings.invalid.toml b/tests/Settings.invalid.toml deleted file mode 100644 index 4d159a4..0000000 --- a/tests/Settings.invalid.toml +++ /dev/null @@ -1,2 +0,0 @@ -ok = true -error = tru diff --git a/tests/Settings.toml b/tests/Settings.toml index 58ff5f1..b7fc7e9 100644 --- a/tests/Settings.toml +++ b/tests/Settings.toml @@ -6,6 +6,8 @@ production_s = "false" # errors boolean_s_parse = "fals" +arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] + [place] name = "Torre di Pisa" longitude = 43.7224985 @@ -13,3 +15,6 @@ latitude = 10.3970522 favorite = false reviews = 3866 rating = 4.5 + +[place.creator] +name = "John Smith" diff --git a/tests/errors.rs b/tests/errors.rs index b69774c..fa3b5ca 100644 --- a/tests/errors.rs +++ b/tests/errors.rs @@ -13,19 +13,35 @@ fn make() -> Config { #[test] fn test_error_parse() { let mut c = Config::default(); - c.merge(File::new("tests/Settings.invalid", FileFormat::Toml)) - .unwrap(); + let res = c.merge(File::new("tests/Settings-invalid", FileFormat::Toml)); + + assert!(res.is_err()); + assert_eq!(res.unwrap_err().to_string(), + "invalid number at line 2 in tests/Settings-invalid.toml" + .to_string()); +} + +#[test] +fn test_error_type() { + let c = make(); + + let res = c.get::("boolean_s_parse"); - assert!(false) + assert!(res.is_err()); + assert_eq!(res.unwrap_err().to_string(), + "invalid type: string \"fals\", expected a boolean for key `boolean_s_parse` in tests/Settings.toml" + .to_string()); } #[test] -fn test_error_type_bool() { +fn test_error_type_detached() { let c = make(); - let err = c.get::("boolean_s_parse"); + let value = c.get::("boolean_s_parse").unwrap(); + let res = value.try_into::(); - assert!(err.is_err()); - assert_eq!(err.unwrap_err().to_string(), - "invalid type: string \"fals\", expected a boolean from tests/Settings.toml".to_string()); + assert!(res.is_err()); + assert_eq!(res.unwrap_err().to_string(), + "invalid type: string \"fals\", expected a boolean" + .to_string()); } diff --git a/tests/get_array.rs b/tests/get_array.rs new file mode 100644 index 0000000..579597c --- /dev/null +++ b/tests/get_array.rs @@ -0,0 +1,39 @@ +extern crate config; +extern crate serde; + +#[macro_use] +extern crate serde_derive; + +use config::*; + +#[derive(Debug, Deserialize)] +struct Settings { + #[serde(rename = "arr")] + elements: Vec, +} + +fn make() -> Config { + let mut c = Config::default(); + c.merge(File::new("tests/Settings", FileFormat::Toml)) + .unwrap(); + + c +} + +#[test] +fn test_array_scalar() { + let c = make(); + let arr: Vec = c.get("arr").unwrap(); + + assert_eq!(arr.len(), 10); + assert_eq!(arr[3], 4); +} + +#[test] +fn test_struct_array() { + let c = make(); + let s: Settings = c.deserialize().unwrap(); + + assert_eq!(s.elements.len(), 10); + assert_eq!(s.elements[3], "4".to_string()); +} diff --git a/tests/get_scalar.rs b/tests/get_scalar.rs new file mode 100644 index 0000000..08d8507 --- /dev/null +++ b/tests/get_scalar.rs @@ -0,0 +1,52 @@ +extern crate config; + +use config::*; + +fn make() -> Config { + let mut c = Config::default(); + c.merge(File::new("tests/Settings", FileFormat::Toml)) + .unwrap(); + + c +} + +#[test] +fn test_scalar() { + let c = make(); + + assert!(c.get("debug").ok() == Some(true)); + assert!(c.get("production").ok() == Some(false)); +} + +#[test] +fn test_scalar_type_loose() { + let c = make(); + + assert!(c.get("debug").ok() == Some(true)); + assert!(c.get("debug").ok() == Some("true".to_string())); + assert!(c.get("debug").ok() == Some(1)); + assert!(c.get("debug").ok() == Some(1.0)); + + assert!(c.get("debug_s").ok() == Some(true)); + assert!(c.get("debug_s").ok() == Some("true".to_string())); + assert!(c.get("debug_s").ok() == Some(1)); + assert!(c.get("debug_s").ok() == Some(1.0)); + + assert!(c.get("production").ok() == Some(false)); + assert!(c.get("production").ok() == Some("false".to_string())); + assert!(c.get("production").ok() == Some(0)); + assert!(c.get("production").ok() == Some(0.0)); + + assert!(c.get("production_s").ok() == Some(false)); + assert!(c.get("production_s").ok() == Some("false".to_string())); + assert!(c.get("production_s").ok() == Some(0)); + assert!(c.get("production_s").ok() == Some(0.0)); +} + +#[test] +fn test_get_scalar_path() { + let c = make(); + + assert_eq!(c.get("place.favorite").ok(), Some(false)); + assert_eq!(c.get("place.creator.name").ok(), Some("John Smith".to_string())); +} diff --git a/tests/get_struct.rs b/tests/get_struct.rs new file mode 100644 index 0000000..705c769 --- /dev/null +++ b/tests/get_struct.rs @@ -0,0 +1,68 @@ +extern crate config; +extern crate serde; + +#[macro_use] +extern crate serde_derive; + +use config::*; + +#[derive(Debug, Deserialize)] +struct Place { + name: String, + longitude: f64, + latitude: f64, + favorite: bool, + telephone: Option, + reviews: u64, + rating: Option, +} + +#[derive(Debug, Deserialize)] +struct Settings { + debug: f64, + production: Option, + place: Place, +} + +fn make() -> Config { + let mut c = Config::default(); + c.merge(File::new("tests/Settings", FileFormat::Toml)) + .unwrap(); + + c +} + +#[test] +fn test_file_struct() { + let c = make(); + + // Deserialize the entire file as single struct + let s: Settings = c.deserialize().unwrap(); + + assert_eq!(s.debug, 1.0); + assert_eq!(s.production, Some("false".to_string())); + assert_eq!(s.place.name, "Torre di Pisa"); + assert_eq!(s.place.longitude, 43.7224985); + assert_eq!(s.place.latitude, 10.3970522); + 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); +} + +#[test] +fn test_scalar_struct() { + let c = make(); + + // Deserialize a scalar struct that has lots of different + // data types + let p: Place = c.get("place").unwrap(); + + assert_eq!(p.name, "Torre di Pisa"); + assert_eq!(p.longitude, 43.7224985); + assert_eq!(p.latitude, 10.3970522); + assert_eq!(p.favorite, false); + assert_eq!(p.reviews, 3866); + assert_eq!(p.rating, Some(4.5)); + assert_eq!(p.telephone, None); +} diff --git a/tests/get_table.rs b/tests/get_table.rs new file mode 100644 index 0000000..8fba334 --- /dev/null +++ b/tests/get_table.rs @@ -0,0 +1,50 @@ +extern crate config; +extern crate serde; + +#[macro_use] +extern crate serde_derive; + +use std::collections::HashMap; +use config::*; + +#[derive(Debug, Deserialize)] +struct Settings { + place: HashMap, +} + +fn make() -> Config { + let mut c = Config::default(); + c.merge(File::new("tests/Settings", FileFormat::Toml)) + .unwrap(); + + c +} + +#[test] +fn test_map() { + let c = make(); + let m: HashMap = c.get("place").unwrap(); + + assert_eq!(m.len(), 7); + assert_eq!(m["name"].clone().into_str().unwrap(), "Torre di Pisa".to_string()); + assert_eq!(m["reviews"].clone().into_int().unwrap(), 3866); +} + +#[test] +fn test_map_str() { + let c = make(); + let m: HashMap = c.get("place.creator").unwrap(); + + assert_eq!(m.len(), 1); + assert_eq!(m["name"], "John Smith".to_string()); +} + +#[test] +fn test_map_struct() { + let c = make(); + let s: Settings = c.deserialize().unwrap(); + + assert_eq!(s.place.len(), 7); + assert_eq!(s.place["name"].clone().into_str().unwrap(), "Torre di Pisa".to_string()); + assert_eq!(s.place["reviews"].clone().into_int().unwrap(), 3866); +} diff --git a/tests/merge.rs b/tests/merge.rs new file mode 100644 index 0000000..df4ace8 --- /dev/null +++ b/tests/merge.rs @@ -0,0 +1,24 @@ +extern crate config; + +use config::*; + +fn make() -> Config { + let mut c = Config::default(); + c.merge(File::new("tests/Settings", FileFormat::Toml)) + .unwrap(); + + c.merge(File::new("tests/Settings-production", FileFormat::Toml)) + .unwrap(); + + c +} + +#[test] +fn test_merge() { + let c = make(); + + assert!(c.get("debug").ok() == Some(false)); + assert!(c.get("production").ok() == Some(true)); + assert!(c.get("place.creator.name").ok() == Some("Somebody New".to_string())); + assert!(c.get("place.rating").ok() == Some(4.9)); +} diff --git a/tests/scalar.rs b/tests/scalar.rs deleted file mode 100644 index 82a4148..0000000 --- a/tests/scalar.rs +++ /dev/null @@ -1,44 +0,0 @@ -extern crate config; - -use config::*; - -fn make() -> Config { - let mut c = Config::default(); - c.merge(File::new("tests/Settings", FileFormat::Toml)) - .unwrap(); - - c -} - -#[test] -fn test_scalar() { - let c = make(); - - assert!(c.get("debug").ok() == Some(true)); - assert!(c.get("production").ok() == Some(false)); -} - -#[test] -fn test_scalar_type_loose() { - let c = make(); - - assert!(c.get("debug").ok() == Some(true)); - assert!(c.get("debug").ok() == Some("true".to_string())); - assert!(c.get("debug").ok() == Some(1)); - assert!(c.get("debug").ok() == Some(1.0)); - - assert!(c.get("debug_s").ok() == Some(true)); - assert!(c.get("debug_s").ok() == Some("true".to_string())); - assert!(c.get("debug_s").ok() == Some(1)); - assert!(c.get("debug_s").ok() == Some(1.0)); - - assert!(c.get("production").ok() == Some(false)); - assert!(c.get("production").ok() == Some("false".to_string())); - assert!(c.get("production").ok() == Some(0)); - assert!(c.get("production").ok() == Some(0.0)); - - assert!(c.get("production_s").ok() == Some(false)); - assert!(c.get("production_s").ok() == Some("false".to_string())); - assert!(c.get("production_s").ok() == Some(0)); - assert!(c.get("production_s").ok() == Some(0.0)); -} diff --git a/tests/set.rs b/tests/set.rs new file mode 100644 index 0000000..a575138 --- /dev/null +++ b/tests/set.rs @@ -0,0 +1,40 @@ +extern crate config; + +use config::*; + +#[test] +fn test_set_scalar() { + let mut c = Config::default(); + + c.set("value", true).unwrap(); + + assert_eq!(c.get("value").ok(), Some(true)); +} + +#[test] +fn test_set_scalar_default() { + let mut c = Config::default(); + + c.merge(File::new("tests/Settings", FileFormat::Toml)) + .unwrap(); + + c.set_default("debug", false).unwrap(); + c.set_default("staging", false).unwrap(); + + assert_eq!(c.get("debug").ok(), Some(true)); + assert_eq!(c.get("staging").ok(), Some(false)); +} + +#[test] +fn test_set_scalar_path() { + let mut c = Config::default(); + + c.merge(File::new("tests/Settings", FileFormat::Toml)) + .unwrap(); + + c.set_default("place.favorite", true).unwrap(); + c.set_default("place.blocked", true).unwrap(); + + assert_eq!(c.get("place.favorite").ok(), Some(false)); + assert_eq!(c.get("place.blocked").ok(), Some(true)); +} diff --git a/tests/struct.rs b/tests/struct.rs deleted file mode 100644 index 705c769..0000000 --- a/tests/struct.rs +++ /dev/null @@ -1,68 +0,0 @@ -extern crate config; -extern crate serde; - -#[macro_use] -extern crate serde_derive; - -use config::*; - -#[derive(Debug, Deserialize)] -struct Place { - name: String, - longitude: f64, - latitude: f64, - favorite: bool, - telephone: Option, - reviews: u64, - rating: Option, -} - -#[derive(Debug, Deserialize)] -struct Settings { - debug: f64, - production: Option, - place: Place, -} - -fn make() -> Config { - let mut c = Config::default(); - c.merge(File::new("tests/Settings", FileFormat::Toml)) - .unwrap(); - - c -} - -#[test] -fn test_file_struct() { - let c = make(); - - // Deserialize the entire file as single struct - let s: Settings = c.deserialize().unwrap(); - - assert_eq!(s.debug, 1.0); - assert_eq!(s.production, Some("false".to_string())); - assert_eq!(s.place.name, "Torre di Pisa"); - assert_eq!(s.place.longitude, 43.7224985); - assert_eq!(s.place.latitude, 10.3970522); - 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); -} - -#[test] -fn test_scalar_struct() { - let c = make(); - - // Deserialize a scalar struct that has lots of different - // data types - let p: Place = c.get("place").unwrap(); - - assert_eq!(p.name, "Torre di Pisa"); - assert_eq!(p.longitude, 43.7224985); - assert_eq!(p.latitude, 10.3970522); - assert_eq!(p.favorite, false); - assert_eq!(p.reviews, 3866); - assert_eq!(p.rating, Some(4.5)); - assert_eq!(p.telephone, None); -} -- cgit v1.2.3