1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
extern crate config;
use config::*;
#[test]
fn test_set_override_scalar() {
let config = Config::builder()
.set_override("value", true)
.and_then(|b| b.build())
.unwrap();
assert_eq!(config.get("value").ok(), Some(true));
}
#[cfg(feature = "toml")]
#[test]
fn test_set_scalar_default() {
let config = Config::builder()
.add_source(File::new("tests/Settings", FileFormat::Toml))
.set_default("debug", false)
.unwrap()
.set_default("staging", false)
.unwrap()
.build()
.unwrap();
assert_eq!(config.get("debug").ok(), Some(true));
assert_eq!(config.get("staging").ok(), Some(false));
}
#[cfg(feature = "toml")]
#[test]
fn test_set_scalar_path() {
let config = Config::builder()
.set_override("first.second.third", true)
.unwrap()
.add_source(File::new("tests/Settings", FileFormat::Toml))
.set_default("place.favorite", true)
.unwrap()
.set_default("place.blocked", true)
.unwrap()
.build()
.unwrap();
assert_eq!(config.get("first.second.third").ok(), Some(true));
assert_eq!(config.get("place.favorite").ok(), Some(false));
assert_eq!(config.get("place.blocked").ok(), Some(true));
}
#[cfg(feature = "toml")]
#[test]
fn test_set_arr_path() {
let config = Config::builder()
.set_override("items[0].name", "Ivan")
.unwrap()
.set_override("data[0].things[1].name", "foo")
.unwrap()
.set_override("data[0].things[1].value", 42)
.unwrap()
.set_override("data[1]", 0)
.unwrap()
.add_source(File::new("tests/Settings", FileFormat::Toml))
.set_override("items[2]", "George")
.unwrap()
.build()
.unwrap();
assert_eq!(config.get("items[0].name").ok(), Some("Ivan".to_string()));
assert_eq!(
config.get("data[0].things[1].name").ok(),
Some("foo".to_string())
);
assert_eq!(config.get("data[0].things[1].value").ok(), Some(42));
assert_eq!(config.get("data[1]").ok(), Some(0));
assert_eq!(config.get("items[2]").ok(), Some("George".to_string()));
}
#[cfg(feature = "toml")]
#[test]
fn test_set_capital() {
let config = Config::builder()
.set_default("this", false)
.unwrap()
.set_override("ThAt", true)
.unwrap()
.add_source(File::from_str("{\"logLevel\": 5}", FileFormat::Json))
.build()
.unwrap();
assert_eq!(config.get("this").ok(), Some(false));
assert_eq!(config.get("ThAt").ok(), Some(true));
assert_eq!(config.get("logLevel").ok(), Some(5));
}
|