Skip to content

Commit

Permalink
add more esp-config tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MabezDev committed Sep 18, 2024
1 parent 07de843 commit 03a5cf1
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 33 deletions.
3 changes: 3 additions & 0 deletions esp-config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ rust-version = "1.79.0"
[dependencies]
document-features = "0.2.10"

[dev-dependencies]
temp-env = "0.3.6"

[features]
## Enable the generation and parsing of a config
build = []
118 changes: 85 additions & 33 deletions esp-config/src/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,42 +243,94 @@ mod test {

#[test]
fn env_override() {
unsafe {
env::set_var("ESP_TEST_NUMBER", "0xaa");
env::set_var("ESP_TEST_STRING", "Hello world!");
env::set_var("ESP_TEST_BOOL", "true");
}

let configs = generate_config(
"esp-test",
&[
("number", Value::Number(999), "NA"),
("string", Value::String("Demo".to_owned()), "NA"),
("bool", Value::Bool(false), "NA"),
temp_env::with_vars(
[
("ESP_TEST_NUMBER", Some("0xaa")),
("ESP_TEST_STRING", Some("Hello world!")),
("ESP_TEST_BOOL", Some("true")),
],
false,
);

assert_eq!(
match configs.get("ESP_TEST_NUMBER").unwrap() {
Value::Number(num) => *num,
_ => unreachable!(),
|| {
let configs = generate_config(
"esp-test",
&[
("number", Value::Number(999), "NA"),
("string", Value::String("Demo".to_owned()), "NA"),
("bool", Value::Bool(false), "NA"),
("number_default", Value::Number(999), "NA"),
("string_default", Value::String("Demo".to_owned()), "NA"),
("bool_default", Value::Bool(false), "NA"),
],
false,
);

// some values have changed
assert_eq!(
match configs.get("ESP_TEST_NUMBER").unwrap() {
Value::Number(num) => *num,
_ => unreachable!(),
},
0xaa
);
assert_eq!(
match configs.get("ESP_TEST_STRING").unwrap() {
Value::String(val) => val,
_ => unreachable!(),
},
"Hello world!"
);
assert_eq!(
match configs.get("ESP_TEST_BOOL").unwrap() {
Value::Bool(val) => *val,
_ => unreachable!(),
},
true
);

// the rest are the defaults
assert_eq!(
match configs.get("ESP_TEST_NUMBER_DEFAULT").unwrap() {
Value::Number(num) => *num,
_ => unreachable!(),
},
999
);
assert_eq!(
match configs.get("ESP_TEST_STRING_DEFAULT").unwrap() {
Value::String(val) => val,
_ => unreachable!(),
},
"Demo"
);
assert_eq!(
match configs.get("ESP_TEST_BOOL_DEFAULT").unwrap() {
Value::Bool(val) => *val,
_ => unreachable!(),
},
false
);
},
0xaa
);
assert_eq!(
match configs.get("ESP_TEST_STRING").unwrap() {
Value::String(val) => val,
_ => unreachable!(),
},
"Hello world!"
);
assert_eq!(
match configs.get("ESP_TEST_BOOL").unwrap() {
Value::Bool(val) => *val,
_ => unreachable!(),
)
}

#[test]
#[should_panic]
fn env_unknown_bails() {
temp_env::with_vars(
[
("ESP_TEST_NUMBER", Some("0xaa")),
("ESP_TEST_RANDOM_VARIABLE", Some("")),
],
|| {
generate_config("esp-test", &[("number", Value::Number(999), "NA")], false);
},
true
);
}

#[test]
#[should_panic]
fn env_invalid_values_bails() {
temp_env::with_vars([("ESP_TEST_NUMBER", Some("Hello world"))], || {
generate_config("esp-test", &[("number", Value::Number(999), "NA")], false);
});
}
}

0 comments on commit 03a5cf1

Please sign in to comment.