mirror of
https://github.com/darwincereska/envkit.git
synced 2026-06-11 10:23:23 -05:00
43 lines
1.3 KiB
Rust
43 lines
1.3 KiB
Rust
#[cfg(test)]
|
|
mod test_main {
|
|
use envkit::*;
|
|
|
|
#[test]
|
|
/// Tests that `get` successfully retrieves an existing environment variable.
|
|
fn test_get() {
|
|
let env = EnvLoader;
|
|
|
|
unsafe {
|
|
std::env::set_var("TEST_VAR", "test_value");
|
|
}
|
|
|
|
assert_eq!(env.get::<String>("TEST_VAR"), Ok("test_value".to_string()));
|
|
}
|
|
|
|
#[test]
|
|
/// Tests that `get_or` returns the default value when the environment variable is missing.
|
|
fn test_get_or() {
|
|
let env = EnvLoader;
|
|
|
|
unsafe {
|
|
std::env::remove_var("MISSING_VAR");
|
|
std::env::set_var("TEST_VAR", "test_value");
|
|
}
|
|
|
|
assert_eq!(env.get_or::<String>("MISSING_VAR", "default".to_string()), "default".to_string());
|
|
assert_eq!(env.get_or::<String>("TEST_VAR", "default".to_string()), "test_value".to_string());
|
|
}
|
|
|
|
#[test]
|
|
/// Tests that `get_opt` returns `Some` when the environment variable exists.
|
|
fn test_get_opt() {
|
|
let env = EnvLoader;
|
|
|
|
unsafe {
|
|
std::env::set_var("OPTIONAL_VAR", "optional_value");
|
|
}
|
|
|
|
assert_eq!(env.get_opt::<String>("OPTIONAL_VAR"), Some("optional_value".to_string()));
|
|
assert_eq!(env.get_opt::<bool>("DEV"), None);
|
|
}
|
|
} |