diff options
author | Radosław Kot <rdkt13@gmail.com> | 2021-04-27 22:46:59 +0200 |
---|---|---|
committer | Radosław Kot <rdkt13@gmail.com> | 2021-05-08 17:43:16 +0200 |
commit | 09498fca75fe04d8a9f56047fcfbd23a4ba6cd5b (patch) | |
tree | a2f46b5a0abb888fe5628776902efd40e2e375f2 /tests | |
parent | 98662dd899d4eaab5dc2da07d5bb658960b588a6 (diff) |
Make ConfigBuilder's method consuming
Diffstat (limited to 'tests')
-rw-r--r-- | tests/datetime.rs | 10 | ||||
-rw-r--r-- | tests/errors.rs | 23 | ||||
-rw-r--r-- | tests/file.rs | 34 | ||||
-rw-r--r-- | tests/file_hjson.rs | 13 | ||||
-rw-r--r-- | tests/file_ini.rs | 13 | ||||
-rw-r--r-- | tests/file_json.rs | 27 | ||||
-rw-r--r-- | tests/file_ron.rs | 13 | ||||
-rw-r--r-- | tests/file_toml.rs | 13 | ||||
-rw-r--r-- | tests/file_yaml.rs | 13 | ||||
-rw-r--r-- | tests/get.rs | 7 | ||||
-rw-r--r-- | tests/merge.rs | 19 | ||||
-rw-r--r-- | tests/set.rs | 43 |
12 files changed, 112 insertions, 116 deletions
diff --git a/tests/datetime.rs b/tests/datetime.rs index 1bdce3c..2b0c22d 100644 --- a/tests/datetime.rs +++ b/tests/datetime.rs @@ -14,9 +14,7 @@ use chrono::{DateTime, TimeZone, Utc}; use config::*; fn make() -> Config { - let mut builder = Config::builder(); - - builder + Config::builder() .add_source(File::from_str( r#" { @@ -58,9 +56,9 @@ fn make() -> Config { ) "#, FileFormat::Ron, - )); - - builder.build().unwrap() + )) + .build() + .unwrap() } #[test] diff --git a/tests/errors.rs b/tests/errors.rs index b47d821..54bbe95 100644 --- a/tests/errors.rs +++ b/tests/errors.rs @@ -10,16 +10,17 @@ use std::path::PathBuf; use config::*; fn make() -> Config { - let mut c = Config::builder(); - c.add_source(File::new("tests/Settings", FileFormat::Toml)); - c.build().unwrap() + Config::builder() + .add_source(File::new("tests/Settings", FileFormat::Toml)) + .build() + .unwrap() } #[test] fn test_error_parse() { - let mut c = Config::builder(); - c.add_source(File::new("tests/Settings-invalid", FileFormat::Toml)); - let res = c.build(); + let res = Config::builder() + .add_source(File::new("tests/Settings-invalid", FileFormat::Toml)) + .build(); let path: PathBuf = ["tests", "Settings-invalid.toml"].iter().collect(); @@ -120,9 +121,13 @@ inner: test: ABC "#; - let mut cfg = Config::builder(); - cfg.add_source(File::from_str(CFG, FileFormat::Yaml)); - let e = cfg.build().unwrap().try_into::<Outer>().unwrap_err(); + let e = Config::builder() + .add_source(File::from_str(CFG, FileFormat::Yaml)) + .build() + .unwrap() + .try_into::<Outer>() + .unwrap_err(); + if let ConfigError::Type { key: Some(path), .. } = e diff --git a/tests/file.rs b/tests/file.rs index 8292426..c282691 100644 --- a/tests/file.rs +++ b/tests/file.rs @@ -6,18 +6,18 @@ use config::*; #[test] fn test_file_not_required() { - let mut c = Config::builder(); - c.add_source(File::new("tests/NoSettings", FileFormat::Yaml).required(false)); - let res = c.build(); + let res = Config::builder() + .add_source(File::new("tests/NoSettings", FileFormat::Yaml).required(false)) + .build(); assert!(res.is_ok()); } #[test] fn test_file_required_not_found() { - let mut c = Config::builder(); - c.add_source(File::new("tests/NoSettings", FileFormat::Yaml)); - let res = c.build(); + let res = Config::builder() + .add_source(File::new("tests/NoSettings", FileFormat::Yaml)) + .build(); assert!(res.is_err()); assert_eq!( @@ -28,10 +28,10 @@ fn test_file_required_not_found() { #[test] fn test_file_auto() { - let mut builder = Config::builder(); - builder.add_source(File::with_name("tests/Settings-production")); - - let c = builder.build().unwrap(); + let c = Config::builder() + .add_source(File::with_name("tests/Settings-production")) + .build() + .unwrap(); assert_eq!(c.get("debug").ok(), Some(false)); assert_eq!(c.get("production").ok(), Some(true)); @@ -39,9 +39,9 @@ fn test_file_auto() { #[test] fn test_file_auto_not_found() { - let mut c = Config::builder(); - c.add_source(File::with_name("tests/NoSettings")); - let res = c.build(); + let res = Config::builder() + .add_source(File::with_name("tests/NoSettings")) + .build(); assert!(res.is_err()); assert_eq!( @@ -52,10 +52,10 @@ fn test_file_auto_not_found() { #[test] fn test_file_ext() { - let mut builder = Config::builder(); - builder.add_source(File::with_name("tests/Settings.json")); - - let c = builder.build().unwrap(); + let c = Config::builder() + .add_source(File::with_name("tests/Settings.json")) + .build() + .unwrap(); assert_eq!(c.get("debug").ok(), Some(true)); assert_eq!(c.get("production").ok(), Some(false)); diff --git a/tests/file_hjson.rs b/tests/file_hjson.rs index 5d9f24d..4002a90 100644 --- a/tests/file_hjson.rs +++ b/tests/file_hjson.rs @@ -35,9 +35,10 @@ struct Settings { } fn make() -> Config { - let mut c = Config::builder(); - c.add_source(File::new("tests/Settings", FileFormat::Hjson)); - c.build().unwrap() + Config::builder() + .add_source(File::new("tests/Settings", FileFormat::Hjson)) + .build() + .unwrap() } #[test] @@ -66,9 +67,9 @@ fn test_file() { #[test] fn test_error_parse() { - let mut c = Config::builder(); - c.add_source(File::new("tests/Settings-invalid", FileFormat::Hjson)); - let res = c.build(); + let res = Config::builder() + .add_source(File::new("tests/Settings-invalid", FileFormat::Hjson)) + .build(); let path: PathBuf = ["tests", "Settings-invalid.hjson"].iter().collect(); diff --git a/tests/file_ini.rs b/tests/file_ini.rs index 1f57272..332d3ea 100644 --- a/tests/file_ini.rs +++ b/tests/file_ini.rs @@ -28,9 +28,10 @@ struct Settings { } fn make() -> Config { - let mut c = Config::builder(); - c.add_source(File::new("tests/Settings", FileFormat::Ini)); - c.build().unwrap() + Config::builder() + .add_source(File::new("tests/Settings", FileFormat::Ini)) + .build() + .unwrap() } #[test] @@ -55,9 +56,9 @@ fn test_file() { #[test] fn test_error_parse() { - let mut c = Config::builder(); - c.add_source(File::new("tests/Settings-invalid", FileFormat::Ini)); - let res = c.build(); + let res = Config::builder() + .add_source(File::new("tests/Settings-invalid", FileFormat::Ini)) + .build(); let path: PathBuf = ["tests", "Settings-invalid.ini"].iter().collect(); diff --git a/tests/file_json.rs b/tests/file_json.rs index 9d457e4..4563e42 100644 --- a/tests/file_json.rs +++ b/tests/file_json.rs @@ -35,9 +35,10 @@ struct Settings { } fn make() -> Config { - let mut c = Config::builder(); - c.add_source(File::new("tests/Settings", FileFormat::Json)); - c.build().unwrap() + Config::builder() + .add_source(File::new("tests/Settings", FileFormat::Json)) + .build() + .unwrap() } #[test] @@ -66,9 +67,9 @@ fn test_file() { #[test] fn test_error_parse() { - let mut c = Config::builder(); - c.add_source(File::new("tests/Settings-invalid", FileFormat::Json)); - let res = c.build(); + let res = Config::builder() + .add_source(File::new("tests/Settings-invalid", FileFormat::Json)) + .build(); let path_with_extension: PathBuf = ["tests", "Settings-invalid.json"].iter().collect(); @@ -84,17 +85,17 @@ fn test_error_parse() { #[test] fn test_json_vec() { - let mut builder = Config::builder(); - builder.add_source(File::from_str( - r#" + let c = Config::builder() + .add_source(File::from_str( + r#" { "WASTE": ["example_dir1", "example_dir2"] } "#, - FileFormat::Json, - )); - - let c = builder.build().unwrap(); + FileFormat::Json, + )) + .build() + .unwrap(); let v = c.get_array("WASTE").unwrap(); let mut vi = v.into_iter(); diff --git a/tests/file_ron.rs b/tests/file_ron.rs index 1e5c6d3..d60c890 100644 --- a/tests/file_ron.rs +++ b/tests/file_ron.rs @@ -36,9 +36,10 @@ struct Settings { } fn make() -> Config { - let mut c = Config::builder(); - c.add_source(File::new("tests/Settings", FileFormat::Ron)); - c.build().unwrap() + Config::builder() + .add_source(File::new("tests/Settings", FileFormat::Ron)) + .build() + .unwrap() } #[test] @@ -68,9 +69,9 @@ fn test_file() { #[test] fn test_error_parse() { - let mut c = Config::builder(); - c.add_source(File::new("tests/Settings-invalid", FileFormat::Ron)); - let res = c.build(); + let res = Config::builder() + .add_source(File::new("tests/Settings-invalid", FileFormat::Ron)) + .build(); let path_with_extension: PathBuf = ["tests", "Settings-invalid.ron"].iter().collect(); diff --git a/tests/file_toml.rs b/tests/file_toml.rs index cee80b7..9493bbd 100644 --- a/tests/file_toml.rs +++ b/tests/file_toml.rs @@ -44,9 +44,10 @@ struct Settings { #[cfg(test)] fn make() -> Config { - let mut c = Config::builder(); - c.add_source(File::new("tests/Settings", FileFormat::Toml)); - c.build().unwrap() + Config::builder() + .add_source(File::new("tests/Settings", FileFormat::Toml)) + .build() + .unwrap() } #[test] @@ -77,9 +78,9 @@ fn test_file() { #[test] fn test_error_parse() { - let mut c = Config::builder(); - c.add_source(File::new("tests/Settings-invalid", FileFormat::Toml)); - let res = c.build(); + let res = Config::builder() + .add_source(File::new("tests/Settings-invalid", FileFormat::Toml)) + .build(); let path_with_extension: PathBuf = ["tests", "Settings-invalid.toml"].iter().collect(); diff --git a/tests/file_yaml.rs b/tests/file_yaml.rs index 645829a..90feefc 100644 --- a/tests/file_yaml.rs +++ b/tests/file_yaml.rs @@ -35,9 +35,10 @@ struct Settings { } fn make() -> Config { - let mut c = Config::builder(); - c.add_source(File::new("tests/Settings", FileFormat::Yaml)); - c.build().unwrap() + Config::builder() + .add_source(File::new("tests/Settings", FileFormat::Yaml)) + .build() + .unwrap() } #[test] @@ -66,9 +67,9 @@ fn test_file() { #[test] fn test_error_parse() { - let mut c = Config::builder(); - c.add_source(File::new("tests/Settings-invalid", FileFormat::Yaml)); - let res = c.build(); + let res = Config::builder() + .add_source(File::new("tests/Settings-invalid", FileFormat::Yaml)) + .build(); let path_with_extension: PathBuf = ["tests", "Settings-invalid.yaml"].iter().collect(); diff --git a/tests/get.rs b/tests/get.rs index 10e2927..c6030ae 100644 --- a/tests/get.rs +++ b/tests/get.rs @@ -31,9 +31,10 @@ struct Settings { } fn make() -> Config { - let mut c = Config::builder(); - c.add_source(File::new("tests/Settings", FileFormat::Toml)); - c.build().unwrap() + Config::builder() + .add_source(File::new("tests/Settings", FileFormat::Toml)) + .build() + .unwrap() } #[test] diff --git a/tests/merge.rs b/tests/merge.rs index 5bb84da..9de36b2 100644 --- a/tests/merge.rs +++ b/tests/merge.rs @@ -5,11 +5,11 @@ extern crate config; use config::*; fn make() -> Config { - let mut builder = Config::builder(); - builder + Config::builder() .add_source(File::new("tests/Settings", FileFormat::Toml)) - .add_source(File::new("tests/Settings-production", FileFormat::Toml)); - builder.build().unwrap() + .add_source(File::new("tests/Settings-production", FileFormat::Toml)) + .build() + .unwrap() } #[test] @@ -27,11 +27,8 @@ fn test_merge() { #[test] fn test_merge_whole_config() { - let mut builder1 = Config::builder(); - let mut builder2 = Config::builder(); - - builder1.set_override("x", 10).unwrap(); - builder2.set_override("y", 25).unwrap(); + let builder1 = Config::builder().set_override("x", 10).unwrap(); + let builder2 = Config::builder().set_override("y", 25).unwrap(); let config1 = builder1.build_cloned().unwrap(); let config2 = builder2.build_cloned().unwrap(); @@ -42,9 +39,7 @@ fn test_merge_whole_config() { assert_eq!(config2.get("y").ok(), Some(25)); assert_eq!(config1.get::<()>("y").ok(), None); - builder1.add_source(config2); - - let config3 = builder1.build().unwrap(); + let config3 = builder1.add_source(config2).build().unwrap(); assert_eq!(config3.get("x").ok(), Some(10)); assert_eq!(config3.get("y").ok(), Some(25)); diff --git a/tests/set.rs b/tests/set.rs index 956f123..4c827d8 100644 --- a/tests/set.rs +++ b/tests/set.rs @@ -4,11 +4,10 @@ use config::*; #[test] fn test_set_override_scalar() { - let mut builder = Config::builder(); - - builder.set_override("value", true).unwrap(); - - let config = builder.build().unwrap(); + let config = Config::builder() + .set_override("value", true) + .and_then(|b| b.build()) + .unwrap(); assert_eq!(config.get("value").ok(), Some(true)); } @@ -16,17 +15,15 @@ fn test_set_override_scalar() { #[cfg(feature = "toml")] #[test] fn test_set_scalar_default() { - let mut builder = Config::builder(); - - builder + let config = Config::builder() .add_source(File::new("tests/Settings", FileFormat::Toml)) .set_default("debug", false) .unwrap() .set_default("staging", false) + .unwrap() + .build() .unwrap(); - let config = builder.build().unwrap(); - assert_eq!(config.get("debug").ok(), Some(true)); assert_eq!(config.get("staging").ok(), Some(false)); } @@ -34,19 +31,17 @@ fn test_set_scalar_default() { #[cfg(feature = "toml")] #[test] fn test_set_scalar_path() { - let mut builder = Config::builder(); - - builder + let config = Config::builder() .set_override("first.second.third", true) .unwrap() .add_source(File::new("tests/Settings", FileFormat::Toml)) .set_default("place.favorite", true) .unwrap() .set_default("place.blocked", true) + .unwrap() + .build() .unwrap(); - let config = builder.build().unwrap(); - assert_eq!(config.get("first.second.third").ok(), Some(true)); assert_eq!(config.get("place.favorite").ok(), Some(false)); assert_eq!(config.get("place.blocked").ok(), Some(true)); @@ -55,9 +50,7 @@ fn test_set_scalar_path() { #[cfg(feature = "toml")] #[test] fn test_set_arr_path() { - let mut builder = Config::builder(); - - builder + let config = Config::builder() .set_override("items[0].name", "Ivan") .unwrap() .set_override("data[0].things[1].name", "foo") @@ -68,10 +61,10 @@ fn test_set_arr_path() { .unwrap() .add_source(File::new("tests/Settings", FileFormat::Toml)) .set_override("items[2]", "George") + .unwrap() + .build() .unwrap(); - let config = builder.build().unwrap(); - assert_eq!(config.get("items[0].name").ok(), Some("Ivan".to_string())); assert_eq!( config.get("data[0].things[1].name").ok(), @@ -85,16 +78,14 @@ fn test_set_arr_path() { #[cfg(feature = "toml")] #[test] fn test_set_capital() { - let mut builder = Config::builder(); - - builder + let config = Config::builder() .set_default("this", false) .unwrap() .set_override("ThAt", true) .unwrap() - .add_source(File::from_str("{\"logLevel\": 5}", FileFormat::Json)); - - let config = builder.build().unwrap(); + .add_source(File::from_str("{\"logLevel\": 5}", FileFormat::Json)) + .build() + .unwrap(); assert_eq!(config.get("this").ok(), Some(false)); assert_eq!(config.get("ThAt").ok(), Some(true)); |