diff --git a/README.md b/README.md index 212a2d5..df5cfbc 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # itconfig [![Build Status](https://travis-ci.org/icetemple/itconfig-rs.svg?branch=master)](https://travis-ci.org/icetemple/itconfig-rs) [![Documentation](https://docs.rs/itconfig/badge.svg)](https://docs.rs/itconfig) -[![Crates.io](https://img.shields.io/badge/crates.io-v0.10.0-orange.svg?longCache=true)](https://crates.io/crates/itconfig) +[![Crates.io](https://img.shields.io/badge/crates.io-v0.10.1-orange.svg?longCache=true)](https://crates.io/crates/itconfig) [![Join the chat at https://gitter.im/icetemple/itconfig-rs](https://badges.gitter.im/icetemple/itconfig-rs.svg)](https://gitter.im/icetemple/itconfig-rs?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) Easy build a configs from environment variables and use it in globally. diff --git a/itconfig/Cargo.toml b/itconfig/Cargo.toml index 3cebc52..49130e6 100644 --- a/itconfig/Cargo.toml +++ b/itconfig/Cargo.toml @@ -25,34 +25,10 @@ failure = { version = "0.1.6", features = ["derive"]} default = ["macro", "numbers", "bool"] macro = [] -numbers = [ - "int", - "uint", - "float" -] - -int = [ - "i8", - "i16", - "i32", - "i64", - "i128", - "isize", -] - -uint = [ - "u8", - "u16", - "u32", - "u64", - "u128", - "usize", -] - -float = [ - "f32", - "f64", -] +numbers = ["int", "uint", "float"] +int = ["i8", "i16", "i32", "i64", "i128", "isize"] +uint = ["u8", "u16", "u32", "u64", "u128", "usize"] +float = ["f32", "f64"] i8 = [] i16 = [] diff --git a/itconfig_tests/tests/get_env.rs b/itconfig_tests/tests/get_env.rs index f8baaa8..f6b9459 100644 --- a/itconfig_tests/tests/get_env.rs +++ b/itconfig_tests/tests/get_env.rs @@ -1,23 +1,74 @@ use std::env; use itconfig::*; +use itconfig::EnvError::*; #[test] #[should_panic(expected = "Environment variable \"TEST_CASE_1\" is missing")] -fn missing_env_variable() { +fn get_missing_env() { get_env_or_panic::("TEST_CASE_1"); } #[test] #[should_panic(expected = "Failed to parse environment variable \"TEST_CASE_2\"")] -fn cannot_parse_env_variable() { - env::set_var("TEST_CASE_2", "30r"); - get_env_or_panic::("TEST_CASE_2"); +fn get_env_with_invalid_value() { + let env_name = "TEST_CASE_2"; + env::set_var(&env_name, "30r"); + get_env_or_panic::(env_name); } #[test] -fn get_env_successfully() { - env::set_var("TEST_CASE_3", "30"); - let a = get_env::("TEST_CASE_3").unwrap(); - - assert_eq!(a, 30); +fn get_result_of_missing_env() { + let env_name = String::from("TEST_CASE_3"); + let env_val = get_env::(&env_name); + assert_eq!(env_val, Err(MissingVariable { env_name })) +} + +#[test] +fn get_result_of_env_with_invalid_value() { + let env_name = String::from("TEST_CASE_4"); + env::set_var(&env_name, "30r"); + let env_val = get_env::(&env_name); + assert_eq!(env_val, Err(FailedToParse { env_name })) +} + +#[test] +fn get_result_of_env_successfully() { + env::set_var("TEST_CASE_5", "30"); + let env_var = get_env("TEST_CASE_5"); + assert_eq!(env_var, Ok(30)); +} + +#[test] +fn get_missing_env_with_default_value() { + let flag: bool = get_env_or_default("TEST_CASE_6", "true"); + assert_eq!(flag, true); +} + +#[test] +#[should_panic(expected = "Failed to parse environment variable \"TEST_CASE_7\"")] +fn get_invalid_env_with_default_value() { + env::set_var("TEST_CASE_7", "30r"); + get_env_or_default::("TEST_CASE_7", 30); +} + +#[test] +#[should_panic(expected = "Failed to parse environment variable \"TEST_CASE_8\"")] +fn get_env_with_invalid_default_value() { + get_env_or_default::("TEST_CASE_8", "30r"); +} + +#[test] +fn get_env_with_default_successfully() { + env::set_var("TEST_CASE_9", "10"); + let env_val: u32 = get_env_or_default("TEST_CASE_9", 30); + assert_eq!(env_val, 10) +} + +#[test] +fn get_missing_env_with_set_default_value() { + let flag: bool = get_env_or_set_default("TEST_CASE_10", "true"); + assert_eq!(flag, true); + + let env_var = env::var("TEST_CASE_10"); + assert_eq!(env_var, Ok(String::from("true"))) } diff --git a/itconfig_tests/tests/get_env_or_default.rs b/itconfig_tests/tests/get_env_or_default.rs deleted file mode 100644 index 70a86cf..0000000 --- a/itconfig_tests/tests/get_env_or_default.rs +++ /dev/null @@ -1,28 +0,0 @@ -use std::env; -use itconfig::*; - -#[test] -fn missing_env_variable() { - let flag: bool = get_env_or_default("DEFAULT_TEST_CASE_1", "true"); - assert_eq!(flag, true); -} - -#[test] -#[should_panic(expected = "Failed to parse environment variable \"DEFAULT_TEST_CASE_2\"")] -fn cannot_parse_env_variable() { - env::set_var("DEFAULT_TEST_CASE_2", "30r"); - let _: u32 = get_env_or_default("DEFAULT_TEST_CASE_2", 30); -} - -#[test] -#[should_panic(expected = "Failed to parse environment variable \"DEFAULT_TEST_CASE_2\"")] -fn cannot_parse_default_value() { - let _: u32 = get_env_or_default("DEFAULT_TEST_CASE_2", "30r"); -} - -#[test] -fn get_env_successfully() { - let a: u32 = get_env_or_default("DEFAULT_TEST_CASE_3", 30); - - assert_eq!(a, 30); -}