This repository has been archived on 2022-07-24. You can view files and clone it, but cannot push or open issues or pull requests.
itconfig/itconfig_tests/tests/config_macro.rs

199 lines
3.9 KiB
Rust
Raw Normal View History

2019-12-22 13:13:29 +03:00
use std::env;
2019-12-25 10:04:20 +03:00
2019-12-22 13:13:29 +03:00
#[macro_use]
2019-12-22 23:16:32 +03:00
extern crate itconfig;
2019-12-22 13:13:29 +03:00
2019-12-25 23:05:09 +03:00
#[test]
#[should_panic]
fn should_panic_if_miss_env_variable() {
config! {
MISS_VARIABLE: bool,
}
cfg::init();
}
2019-12-22 13:13:29 +03:00
#[test]
fn one_variable() {
2019-12-25 23:05:09 +03:00
env::set_var("DEBUG", "t");
config! {
DEBUG: bool,
}
cfg::init();
assert_eq!(cfg::DEBUG(), true);
env::remove_var("DEBUG");
}
#[test]
fn one_variable_with_default_value() {
2019-12-22 13:13:29 +03:00
config! {
DEBUG: bool => true,
}
2019-12-25 23:05:09 +03:00
cfg::init();
2019-12-22 13:13:29 +03:00
assert_eq!(cfg::DEBUG(), true);
}
#[test]
2019-12-25 23:05:09 +03:00
fn few_variables_with_default_value() {
2019-12-22 13:13:29 +03:00
config! {
FOO: bool => true,
BAR: bool => false,
}
2019-12-25 23:05:09 +03:00
cfg::init();
2019-12-22 13:13:29 +03:00
assert_eq!(cfg::FOO(), true);
assert_eq!(cfg::BAR(), false);
}
#[test]
2019-12-25 23:05:09 +03:00
fn different_types_with_default_value() {
2019-12-22 13:13:29 +03:00
config! {
NUMBER: i32 => 30,
BOOL: bool => true,
STRING: String => "string".to_string(),
}
2019-12-25 23:05:09 +03:00
cfg::init();
2019-12-22 13:13:29 +03:00
assert_eq!(cfg::NUMBER(), 30);
assert_eq!(cfg::BOOL(), true);
assert_eq!(cfg::STRING(), "string");
}
#[test]
2019-12-25 23:05:09 +03:00
fn convert_bool_type_value_from_env() {
2019-12-22 13:13:29 +03:00
env::set_var("T_BOOL", "t");
env::set_var("TRUE_BOOL", "true");
env::set_var("NUM_BOOL", "1");
env::set_var("ON_BOOL", "on");
env::set_var("CAMEL_CASE", "True");
env::set_var("FALSE_BOOL", "false");
config! {
T_BOOL: bool,
TRUE_BOOL: bool,
NUM_BOOL: bool,
ON_BOOL: bool,
CAMEL_CASE: bool,
FALSE_BOOL: bool,
}
2019-12-25 23:05:09 +03:00
cfg::init();
2019-12-22 13:13:29 +03:00
assert_eq!(cfg::T_BOOL(), true);
assert_eq!(cfg::TRUE_BOOL(), true);
assert_eq!(cfg::NUM_BOOL(), true);
assert_eq!(cfg::ON_BOOL(), true);
assert_eq!(cfg::CAMEL_CASE(), true);
assert_eq!(cfg::FALSE_BOOL(), false);
}
#[test]
2019-12-25 23:05:09 +03:00
fn convert_number_type_value_from_env() {
2019-12-22 13:13:29 +03:00
env::set_var("I8", "10");
env::set_var("I16", "10");
env::set_var("I32", "10");
env::set_var("I64", "10");
env::set_var("I128", "10");
env::set_var("ISIZE","10");
env::set_var("U8", "10");
env::set_var("U16", "10");
env::set_var("U32", "10");
env::set_var("U64", "10");
env::set_var("U128", "10");
env::set_var("USIZE","10");
env::set_var("F32", "10");
env::set_var("F64","10");
config! {
I8: i8,
I16: i16,
I32: i32,
I64: i64,
I128: i128,
ISIZE: isize,
U8: u8,
U16: u16,
U32: u32,
U64: u64,
U128: u128,
USIZE: usize,
F32: f32,
F64: f64,
}
2019-12-25 23:05:09 +03:00
cfg::init();
2019-12-22 13:13:29 +03:00
assert_eq!(cfg::I8(), 10);
assert_eq!(cfg::I16(), 10);
assert_eq!(cfg::I32(), 10);
assert_eq!(cfg::I64(), 10);
assert_eq!(cfg::ISIZE(), 10);
assert_eq!(cfg::U8(), 10);
assert_eq!(cfg::U16(), 10);
assert_eq!(cfg::U32(), 10);
assert_eq!(cfg::U64(), 10);
assert_eq!(cfg::USIZE(), 10);
assert_eq!(cfg::F32(), 10.0);
assert_eq!(cfg::F64(), 10.0);
}
#[test]
2019-12-25 23:05:09 +03:00
fn change_configuration_module_name() {
config! {
2019-12-23 14:56:19 +03:00
#![mod_name = custom_config_name]
DEBUG: bool => true,
}
2019-12-25 23:05:09 +03:00
custom_config_name::init();
assert_eq!(custom_config_name::DEBUG(), true);
2019-12-25 10:04:20 +03:00
}
#[test]
2019-12-25 23:05:09 +03:00
fn configuration_with_namespace() {
2019-12-25 15:31:10 +03:00
env::set_var("POSTGRES_HOST", "t");
2019-12-25 10:04:20 +03:00
config! {
DEBUG: bool => true,
POSTGRES {
2019-12-25 15:31:10 +03:00
HOST: bool,
2019-12-25 10:04:20 +03:00
PORT: bool => true,
USERNAME: bool => true,
}
2019-12-25 15:31:10 +03:00
APP {}
2019-12-25 10:04:20 +03:00
}
2019-12-25 23:05:09 +03:00
cfg::init();
2019-12-25 10:04:20 +03:00
assert_eq!(cfg::DEBUG(), true);
assert_eq!(cfg::POSTGRES::HOST(), true);
2019-12-25 23:05:09 +03:00
env::remove_var("POSTGRES_HOST");
2019-12-25 15:31:10 +03:00
}
#[test]
2019-12-25 23:05:09 +03:00
fn configuration_variables_and_namespace_in_lowercase() {
env::set_var("TESTING", "t");
2019-12-25 15:31:10 +03:00
env::set_var("NAMESPACE_FOO", "t");
config! {
2019-12-25 23:05:09 +03:00
testing: bool,
2019-12-25 15:31:10 +03:00
namespace {
foo: bool,
}
}
2019-12-25 23:05:09 +03:00
cfg::init();
assert_eq!(cfg::testing(), true);
2019-12-25 15:31:10 +03:00
assert_eq!(cfg::namespace::foo(), true);
2019-12-25 23:05:09 +03:00
env::remove_var("TESTING");
env::remove_var("NAMESPACE_FOO");
}