diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Settings.toml | 12 | ||||
-rw-r--r-- | tests/scalar.rs | 44 | ||||
-rw-r--r-- | tests/struct.rs | 68 |
3 files changed, 124 insertions, 0 deletions
diff --git a/tests/Settings.toml b/tests/Settings.toml new file mode 100644 index 0000000..f0d2547 --- /dev/null +++ b/tests/Settings.toml @@ -0,0 +1,12 @@ +debug = true +debug_s = "true" +production = false +production_s = "false" + +[place] +name = "Torre di Pisa" +longitude = 43.7224985 +latitude = 10.3970522 +favorite = false +reviews = 3866 +rating = 4.5 diff --git a/tests/scalar.rs b/tests/scalar.rs new file mode 100644 index 0000000..82a4148 --- /dev/null +++ b/tests/scalar.rs @@ -0,0 +1,44 @@ +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/struct.rs b/tests/struct.rs new file mode 100644 index 0000000..705c769 --- /dev/null +++ b/tests/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<String>, + reviews: u64, + rating: Option<f32>, +} + +#[derive(Debug, Deserialize)] +struct Settings { + debug: f64, + production: Option<String>, + 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); +} |