Skip to content

Commit

Permalink
Merge pull request #33 from TheNeikos/feature/add_hashmap_test
Browse files Browse the repository at this point in the history
Feature/add hashmap test
  • Loading branch information
TheNeikos authored Mar 28, 2024
2 parents c55be9e + 4858091 commit c5f537a
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 184 deletions.
145 changes: 0 additions & 145 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@ serde = "1.0.152"
thiserror = "1.0.38"

[dev-dependencies]
temp-env = "0.3"
serde = { version = "1.0.152", features = ["derive"] }
9 changes: 4 additions & 5 deletions tests/complex_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@ struct Config {
#[test]
fn parse_from_env() {
let vars = [
("upstairs__doors__0__material__Wood__kind", Some("Mahagony")),
("upstairs__doors__1__material__Plastic", Some("25")),
("upstairs__doors__foo__material", Some("Unknown")),
("upstairs__doors__0__material__Wood__kind", "Mahagony"),
("upstairs__doors__1__material__Plastic", "25"),
("upstairs__doors__foo__material", "Unknown"),
];

let config: Config =
temp_env::with_vars(vars, || envious::Config::new().build_from_env().unwrap());
let config: Config = envious::Config::new().build_from_iter(vars).unwrap();
println!("{:#?}", config);
}
10 changes: 4 additions & 6 deletions tests/failure.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![allow(dead_code)]
use envious::Config;
use serde::Deserialize;

#[derive(Deserialize, Debug)]
Expand All @@ -9,19 +8,18 @@ struct Simple {

#[test]
fn wrongly_nested_fields() {
let vars = [("test", Some("true")), ("test__bar", Some("true"))];
let vars = [("test", ("true")), ("test__bar", ("true"))];

let config: Result<Simple, _> = temp_env::with_vars(vars, || Config::new().build_from_env());
let config: Result<Simple, _> = envious::Config::new().build_from_iter(vars);

println!("{:?}", config.unwrap_err());
}

#[test]
fn wrongly_nested_prefixed_fields() {
let vars = [("PRE_test", Some("true")), ("PRE_test__bar", Some("true"))];
let vars = [("PRE_test", ("true")), ("PRE_test__bar", ("true"))];

let config: Result<Simple, _> =
temp_env::with_vars(vars, || Config::new().with_prefix("PRE_").build_from_env());
let config: Result<Simple, _> = envious::Config::new().build_from_iter(vars);

println!("{:?}", config.unwrap_err());
}
30 changes: 30 additions & 0 deletions tests/hashmap.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#![allow(dead_code)]
use std::collections::HashMap;

use serde::Deserialize;

#[derive(Debug, Deserialize)]
struct Outer {
values: HashMap<String, String>,
inner: Inner,
}

#[derive(Debug, Deserialize)]
struct Inner {
more_values: HashMap<String, String>,
}

#[test]
fn parse_hashmap() {
let vars = [
("values__0key", ("first key")),
("values__0val", ("first value")),
("values__1key", ("second key")),
("values__1val", ("second value")),
("inner__more_values__0key", ("first inner key")),
("inner__more_values__0val", ("first inner value")),
];

let config: Outer = envious::Config::new().build_from_iter(vars).unwrap();
println!("{:#?}", config);
}
38 changes: 16 additions & 22 deletions tests/prefix_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,15 @@ struct Config {
#[test]
fn parse_from_env() {
let vars = [
("ENVIOUS_upstairs__doors__0__material", Some("Wood")),
("ENVIOUS_upstairs__doors__2__material", Some("Glass")),
("ENVIOUS_upstairs__doors__1__material", Some("Plastic")),
("ENVIOUS_upstairs__doors__0__material", ("Wood")),
("ENVIOUS_upstairs__doors__2__material", ("Glass")),
("ENVIOUS_upstairs__doors__1__material", ("Plastic")),
];

let config: Config = temp_env::with_vars(vars, || {
envious::Config::new()
.with_prefix("ENVIOUS_")
.build_from_env()
.unwrap()
});
let config: Config = envious::Config::new()
.with_prefix("ENVIOUS_")
.build_from_iter(vars)
.unwrap();

assert_eq!(
config,
Expand All @@ -58,23 +56,19 @@ fn parse_from_env() {
);

// When case insensitive, the same test should succeed with a lowercase prefix.
let config: Config = temp_env::with_vars(vars, || {
envious::Config::new()
.case_sensitive(false)
.with_prefix("envious_")
.build_from_env()
.unwrap()
});
let config: Config = envious::Config::new()
.case_sensitive(false)
.with_prefix("envious_")
.build_from_iter(vars)
.unwrap();

println!("{:#?}", config);

// However when case sensitive, it will fail.
let result: Result<Config, _> = temp_env::with_vars(vars, || {
envious::Config::new()
.case_sensitive(true)
.with_prefix("envious_")
.build_from_env()
});
let result: Result<Config, _> = envious::Config::new()
.case_sensitive(true)
.with_prefix("envious_")
.build_from_iter(vars);
let err = result.unwrap_err();

println!("{:#?}", err);
Expand Down
9 changes: 4 additions & 5 deletions tests/simple_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ struct Config {
#[test]
fn parse_from_env() {
let vars = [
("target_temp", Some("25.0")),
("automate_doors", Some("true")),
("staircase_orientation", Some("Left")),
("target_temp", ("25.0")),
("automate_doors", ("true")),
("staircase_orientation", ("Left")),
];

let config: Config =
temp_env::with_vars(vars, || envious::Config::new().build_from_env().unwrap());
let config: Config = envious::Config::new().build_from_iter(vars).unwrap();
println!("{:#?}", config);
}

0 comments on commit c5f537a

Please sign in to comment.