diff options
author | Ryan Leckey <ryan@launchbadge.com> | 2017-06-03 01:22:38 -0700 |
---|---|---|
committer | Ryan Leckey <ryan@launchbadge.com> | 2017-06-03 01:22:38 -0700 |
commit | 43c141f87a0167e50802f097cf04896258acb5e6 (patch) | |
tree | 799cc797eecf5e699322fbb55debdf75f576a7c0 | |
parent | 0a478cf075cc93ede4f631a5be4502e2da2f0cf7 (diff) |
Add many more tests
-rw-r--r-- | tests/Settings-invalid.toml (renamed from tests/Settings.invalid.toml) | 0 | ||||
-rw-r--r-- | tests/Settings-production.toml | 8 | ||||
-rw-r--r-- | tests/Settings.toml | 5 | ||||
-rw-r--r-- | tests/errors.rs | 32 | ||||
-rw-r--r-- | tests/get_array.rs | 39 | ||||
-rw-r--r-- | tests/get_scalar.rs (renamed from tests/scalar.rs) | 8 | ||||
-rw-r--r-- | tests/get_struct.rs (renamed from tests/struct.rs) | 0 | ||||
-rw-r--r-- | tests/get_table.rs | 50 | ||||
-rw-r--r-- | tests/merge.rs | 24 | ||||
-rw-r--r-- | tests/set.rs | 40 |
10 files changed, 198 insertions, 8 deletions
diff --git a/tests/Settings.invalid.toml b/tests/Settings-invalid.toml index 4d159a4..4d159a4 100644 --- a/tests/Settings.invalid.toml +++ b/tests/Settings-invalid.toml 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.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::<bool>("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::<bool>("boolean_s_parse"); + let value = c.get::<Value>("boolean_s_parse").unwrap(); + let res = value.try_into::<bool>(); - 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<String>, +} + +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<i64> = 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/scalar.rs b/tests/get_scalar.rs index 82a4148..08d8507 100644 --- a/tests/scalar.rs +++ b/tests/get_scalar.rs @@ -42,3 +42,11 @@ fn test_scalar_type_loose() { 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/struct.rs b/tests/get_struct.rs index 705c769..705c769 100644 --- a/tests/struct.rs +++ b/tests/get_struct.rs 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<String, Value>, +} + +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<String, Value> = 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<String, String> = 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/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)); +} |