diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2021-09-09 08:29:23 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2021-09-09 08:53:59 +0200 |
commit | 5b88edcb3e0a63079d9f2e0f0ca95f0de31805e5 (patch) | |
tree | b190ef360d563b0747d93e8e2eaa6e2ffb8485e8 /tests | |
parent | 3f179990e1cca46f9b3d82e676a4a01d5314052e (diff) |
Add tests for weird keys
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/weird_keys.rs | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/tests/weird_keys.rs b/tests/weird_keys.rs new file mode 100644 index 0000000..9c5f1f8 --- /dev/null +++ b/tests/weird_keys.rs @@ -0,0 +1,115 @@ +// Please note: This file is named "weird" keys because these things are normally not keys, not +// because your software is weird if it expects these keys in the config file. +// +// Please don't be offended! +// + +extern crate config; + +#[macro_use] +extern crate serde_derive; +extern crate serde; + +use config::*; + +/// Helper fn to test the different deserializations +fn test_config_as<'a, T>(config: &str, format: FileFormat) -> T +where + T: serde::Deserialize<'a> + std::fmt::Debug, +{ + let cfg = config::Config::builder() + .add_source(File::from_str(config, format)) + .build(); + + assert!(cfg.is_ok(), "Config could not be built: {:?}", cfg); + let cfg = cfg.unwrap().try_into(); + + assert!(cfg.is_ok(), "Config could not be transformed: {:?}", cfg); + let cfg: T = cfg.unwrap(); + cfg +} + +#[derive(Debug, Serialize, Deserialize)] +struct SettingsColon { + #[serde(rename = "foo:foo")] + foo: u8, + + bar: u8, +} + +#[test] +fn test_colon_key_toml() { + let config = r#" + "foo:foo" = 8 + bar = 12 + "#; + + let cfg = test_config_as::<SettingsColon>(config, FileFormat::Toml); + assert_eq!(cfg.foo, 8); + assert_eq!(cfg.bar, 12); +} + +#[test] +fn test_colon_key_json() { + let config = r#" {"foo:foo": 8, "bar": 12 } "#; + + let cfg = test_config_as::<SettingsColon>(config, FileFormat::Json); + assert_eq!(cfg.foo, 8); + assert_eq!(cfg.bar, 12); +} + +#[derive(Debug, Serialize, Deserialize)] +struct SettingsSlash { + #[serde(rename = "foo/foo")] + foo: u8, + bar: u8, +} + +#[test] +fn test_slash_key_toml() { + let config = r#" + "foo/foo" = 8 + bar = 12 + "#; + + let cfg = test_config_as::<SettingsSlash>(config, FileFormat::Toml); + assert_eq!(cfg.foo, 8); + assert_eq!(cfg.bar, 12); +} + +#[test] +fn test_slash_key_json() { + let config = r#" {"foo/foo": 8, "bar": 12 } "#; + + let cfg = test_config_as::<SettingsSlash>(config, FileFormat::Json); + assert_eq!(cfg.foo, 8); + assert_eq!(cfg.bar, 12); +} + +#[derive(Debug, Serialize, Deserialize)] +struct SettingsDoubleBackslash { + #[serde(rename = "foo\\foo")] + foo: u8, + bar: u8, +} + +#[test] +fn test_doublebackslash_key_toml() { + let config = r#" + "foo\\foo" = 8 + bar = 12 + "#; + + let cfg = test_config_as::<SettingsDoubleBackslash>(config, FileFormat::Toml); + assert_eq!(cfg.foo, 8); + assert_eq!(cfg.bar, 12); +} + +#[test] +fn test_doublebackslash_key_json() { + let config = r#" {"foo\\foo": 8, "bar": 12 } "#; + + let cfg = test_config_as::<SettingsDoubleBackslash>(config, FileFormat::Json); + assert_eq!(cfg.foo, 8); + assert_eq!(cfg.bar, 12); +} |