tests: coverage all getenv functions

This commit is contained in:
Dmitriy Pleshevskiy 2020-01-19 22:21:34 +03:00
parent 4db97f08e6
commit 452e837d76
4 changed files with 65 additions and 66 deletions

View file

@ -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.

View file

@ -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 = []

View file

@ -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::<String>("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::<u32>("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::<u32>(env_name);
}
#[test]
fn get_env_successfully() {
env::set_var("TEST_CASE_3", "30");
let a = get_env::<u32>("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::<String>(&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::<u32>(&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::<u32, _>("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::<u32, _>("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")))
}

View file

@ -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);
}