|
| 1 | +extern crate config; |
| 2 | +extern crate log; |
| 3 | + |
| 4 | +#[macro_use] |
| 5 | +extern crate serde_derive; |
| 6 | +extern crate serde_json; |
| 7 | + |
| 8 | +use config::*; |
| 9 | + |
| 10 | +#[derive(Debug, Deserialize)] |
| 11 | +struct Settings { |
| 12 | + log: log::Level, |
| 13 | +} |
| 14 | + |
| 15 | +fn config(s: &str) -> Config { |
| 16 | + Config::builder() |
| 17 | + .add_source(File::from_str(s, FileFormat::Json)) |
| 18 | + .build() |
| 19 | + .unwrap() |
| 20 | +} |
| 21 | + |
| 22 | +#[test] |
| 23 | +fn test_load_level_uppercase() { |
| 24 | + let s = r#"{ "log": "ERROR" }"#; |
| 25 | + let c = config(s); |
| 26 | + let l = c.get::<log::Level>("log").unwrap(); |
| 27 | + assert_eq!(l, log::Level::Error); |
| 28 | +} |
| 29 | + |
| 30 | +#[test] |
| 31 | +fn test_case_sensitivity_log_level_from_str() { |
| 32 | + // to verify that this works |
| 33 | + |
| 34 | + use std::str::FromStr; |
| 35 | + let l = log::Level::from_str("error").unwrap(); |
| 36 | + assert_eq!(l, log::Level::Error); |
| 37 | +} |
| 38 | + |
| 39 | +#[test] |
| 40 | +fn test_case_sensitivity_json_from_str() { |
| 41 | + // to confirm serde_json works as expected |
| 42 | + let s = r#"{ "log": "error" }"#; |
| 43 | + |
| 44 | + let j: Settings = serde_json::from_str(s).unwrap(); |
| 45 | + assert_eq!(j.log, log::Level::Error); |
| 46 | +} |
| 47 | + |
| 48 | +#[test] |
| 49 | +fn test_load_level_lowercase_succeeding() { |
| 50 | + let s = r#"{ "log": "error" }"#; |
| 51 | + let c = config(s); |
| 52 | + assert_eq!(c.get_string("log").unwrap(), "error"); |
| 53 | + let s = c.try_deserialize::<Settings>(); |
| 54 | + assert!(s.is_ok(), "Expected Ok(_) for {:?}", s); |
| 55 | + assert_eq!(s.unwrap().log, log::Level::Error); |
| 56 | +} |
0 commit comments