diff options
author | simon-an <26556185+simon-an@users.noreply.github.com> | 2022-02-28 18:14:36 +0100 |
---|---|---|
committer | simon-an <26556185+simon-an@users.noreply.github.com> | 2022-02-28 18:18:37 +0100 |
commit | 53322d4ca277a2e55a7efeb654e400e5c05eb672 (patch) | |
tree | 08ee0506ba46a4ad6e25330b20b07558374b0603 /tests/env.rs | |
parent | 92e29a3c1351f9a8f1c7e1fd752d9c2b4d6ee2b4 (diff) |
feat: env contains list of strings
Signed-off-by: simon-an <26556185+simon-an@users.noreply.github.com>
Diffstat (limited to 'tests/env.rs')
-rw-r--r-- | tests/env.rs | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/tests/env.rs b/tests/env.rs index 90852e0..fcadf81 100644 --- a/tests/env.rs +++ b/tests/env.rs @@ -393,6 +393,56 @@ fn test_parse_bool_fail() { } #[test] +fn test_parse_string_and_list() { + // using a struct in an enum here to make serde use `deserialize_any` + #[derive(Deserialize, Debug)] + #[serde(tag = "tag")] + enum TestStringEnum { + String(TestString), + } + + #[derive(Deserialize, Debug)] + struct TestString { + string_val: String, + string_list: Vec<String>, + } + + env::set_var("LIST_STRING_LIST", "test,string"); + env::set_var("LIST_STRING_VAL", "test,string"); + + let environment = Environment::default() + .prefix("LIST") + .list_separator(",") + .with_list_parse_key("string_list") + .try_parsing(true); + + let config = Config::builder() + .set_default("tag", "String") + .unwrap() + .add_source(environment) + .build() + .unwrap(); + + let config: TestStringEnum = config.try_deserialize().unwrap(); + + match config { + TestStringEnum::String(TestString { + string_val, + string_list, + }) => { + assert_eq!(String::from("test,string"), string_val); + assert_eq!( + vec![String::from("test"), String::from("string")], + string_list + ); + } + } + + env::remove_var("LIST_STRING_VAL"); + env::remove_var("LIST_STRING_LIST"); +} + +#[test] fn test_parse_string() { // using a struct in an enum here to make serde use `deserialize_any` #[derive(Deserialize, Debug)] @@ -429,6 +479,42 @@ fn test_parse_string() { } #[test] +fn test_parse_string_list() { + // using a struct in an enum here to make serde use `deserialize_any` + #[derive(Deserialize, Debug)] + #[serde(tag = "tag")] + enum TestListEnum { + StringList(TestList), + } + + #[derive(Deserialize, Debug)] + struct TestList { + string_list: Vec<String>, + } + + env::set_var("STRING_LIST", "test string"); + + let environment = Environment::default().try_parsing(true).list_separator(" "); + + let config = Config::builder() + .set_default("tag", "StringList") + .unwrap() + .add_source(environment) + .build() + .unwrap(); + + let config: TestListEnum = config.try_deserialize().unwrap(); + + let test_string = vec![String::from("test"), String::from("string")]; + + match config { + TestListEnum::StringList(TestList { string_list }) => assert_eq!(test_string, string_list), + } + + env::remove_var("STRING_LIST"); +} + +#[test] fn test_parse_off_string() { // using a struct in an enum here to make serde use `deserialize_any` #[derive(Deserialize, Debug)] |