tests: fix tests

This commit is contained in:
Dmitriy Pleshevskiy 2019-12-25 23:05:09 +03:00
parent dead8e444b
commit f363bc69cc

View file

@ -5,41 +5,69 @@ use std::env;
#[macro_use] #[macro_use]
extern crate itconfig; extern crate itconfig;
#[test]
#[should_panic]
fn should_panic_if_miss_env_variable() {
config! {
MISS_VARIABLE: bool,
}
cfg::init();
}
#[test] #[test]
fn one_variable() { fn one_variable() {
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() {
config! { config! {
DEBUG: bool => true, DEBUG: bool => true,
} }
cfg::init();
assert_eq!(cfg::DEBUG(), true); assert_eq!(cfg::DEBUG(), true);
} }
#[test] #[test]
fn few_variables() { fn few_variables_with_default_value() {
config! { config! {
FOO: bool => true, FOO: bool => true,
BAR: bool => false, BAR: bool => false,
} }
cfg::init();
assert_eq!(cfg::FOO(), true); assert_eq!(cfg::FOO(), true);
assert_eq!(cfg::BAR(), false); assert_eq!(cfg::BAR(), false);
} }
#[test] #[test]
fn different_types() { fn different_types_with_default_value() {
config! { config! {
NUMBER: i32 => 30, NUMBER: i32 => 30,
BOOL: bool => true, BOOL: bool => true,
STRING: String => "string".to_string(), STRING: String => "string".to_string(),
} }
cfg::init();
assert_eq!(cfg::NUMBER(), 30); assert_eq!(cfg::NUMBER(), 30);
assert_eq!(cfg::BOOL(), true); assert_eq!(cfg::BOOL(), true);
assert_eq!(cfg::STRING(), "string"); assert_eq!(cfg::STRING(), "string");
} }
#[test] #[test]
fn convert_bool_type_from_env() { fn convert_bool_type_value_from_env() {
env::set_var("T_BOOL", "t"); env::set_var("T_BOOL", "t");
env::set_var("TRUE_BOOL", "true"); env::set_var("TRUE_BOOL", "true");
env::set_var("NUM_BOOL", "1"); env::set_var("NUM_BOOL", "1");
@ -57,6 +85,7 @@ fn convert_bool_type_from_env() {
} }
cfg::init();
assert_eq!(cfg::T_BOOL(), true); assert_eq!(cfg::T_BOOL(), true);
assert_eq!(cfg::TRUE_BOOL(), true); assert_eq!(cfg::TRUE_BOOL(), true);
assert_eq!(cfg::NUM_BOOL(), true); assert_eq!(cfg::NUM_BOOL(), true);
@ -66,7 +95,7 @@ fn convert_bool_type_from_env() {
} }
#[test] #[test]
fn convert_number_value_from_env() { fn convert_number_type_value_from_env() {
env::set_var("I8", "10"); env::set_var("I8", "10");
env::set_var("I16", "10"); env::set_var("I16", "10");
env::set_var("I32", "10"); env::set_var("I32", "10");
@ -99,6 +128,7 @@ fn convert_number_value_from_env() {
F64: f64, F64: f64,
} }
cfg::init();
assert_eq!(cfg::I8(), 10); assert_eq!(cfg::I8(), 10);
assert_eq!(cfg::I16(), 10); assert_eq!(cfg::I16(), 10);
assert_eq!(cfg::I32(), 10); assert_eq!(cfg::I32(), 10);
@ -115,19 +145,20 @@ fn convert_number_value_from_env() {
#[test] #[test]
fn change_config_module_name() { fn change_configuration_module_name() {
config! { config! {
#![mod_name = custom_config_name] #![mod_name = custom_config_name]
DEBUG: bool => true, DEBUG: bool => true,
} }
custom_config_name::init();
assert_eq!(custom_config_name::DEBUG(), true); assert_eq!(custom_config_name::DEBUG(), true);
} }
#[test] #[test]
fn config_namespace() { fn configuration_with_namespace() {
env::set_var("POSTGRES_HOST", "t"); env::set_var("POSTGRES_HOST", "t");
config! { config! {
@ -142,24 +173,29 @@ fn config_namespace() {
APP {} APP {}
} }
cfg::init();
assert_eq!(cfg::DEBUG(), true); assert_eq!(cfg::DEBUG(), true);
assert_eq!(cfg::POSTGRES::HOST(), true); assert_eq!(cfg::POSTGRES::HOST(), true);
env::remove_var("POSTGRES_HOST");
} }
#[test] #[test]
fn config_in_lowercase() { fn configuration_variables_and_namespace_in_lowercase() {
env::set_var("DEBUG", "t"); env::set_var("TESTING", "t");
env::set_var("NAMESPACE_FOO", "t"); env::set_var("NAMESPACE_FOO", "t");
config! { config! {
debug: bool, testing: bool,
namespace { namespace {
foo: bool, foo: bool,
} }
} }
assert_eq!(cfg::debug(), true); cfg::init();
assert_eq!(cfg::testing(), true);
assert_eq!(cfg::namespace::foo(), true); assert_eq!(cfg::namespace::foo(), true);
env::remove_var("TESTING");
env::remove_var("NAMESPACE_FOO");
} }