Skip to content

Commit 834a163

Browse files
Merge pull request #188 from matthiasbeyer/log-deser
Add test for log::Level deserialization
2 parents cea4deb + 912f1ed commit 834a163

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,4 @@ glob = "0.3"
5353
lazy_static = "1"
5454
notify = "^5.0.0"
5555
temp-env = "0.3.0"
56+
log = { version = "0.4", features = ["serde"] }

tests/log.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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

Comments
 (0)