diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2022-06-21 09:26:18 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2022-06-21 10:17:02 +0200 |
commit | 322a6e3537ee24d58d9601cae9500a9be1f65dae (patch) | |
tree | d2cb2e6be71feea90c3690154e76e1beaac965b8 | |
parent | f649fd20dc4b1f664bcc42293b135ac188c9eec5 (diff) |
Add tests for loading sources
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r-- | src/config.rs | 28 | ||||
-rw-r--r-- | src/source.rs | 30 |
2 files changed, 58 insertions, 0 deletions
diff --git a/src/config.rs b/src/config.rs index d9b5a64..6d581d1 100644 --- a/src/config.rs +++ b/src/config.rs @@ -128,3 +128,31 @@ impl<'a> ConfigBuilder<'a> { } } +#[cfg(test)] +mod tests { + use super::*; + use crate::element::AsConfigElement; + + #[test] + fn test_compile_loading() { + let _c = Config::builder() + .load(&crate::source::test_source::TestSource(|| ConfigElement::Null)) + .unwrap() + .build(); + } + + #[test] + #[cfg(feature = "json")] + fn test_load_json() { + let json: serde_json::Value = serde_json::from_str(r#" + { "key": "value" } + "#).unwrap(); + let json = std::sync::Arc::new(json); + + let _c = Config::builder() + .load(&crate::source::test_source::TestSource(|| json.as_config_element().unwrap())) + .unwrap() + .build(); + } + +} diff --git a/src/source.rs b/src/source.rs index d3ff323..485da8a 100644 --- a/src/source.rs +++ b/src/source.rs @@ -5,3 +5,33 @@ pub trait ConfigSource: std::fmt::Debug { fn load<'a>(&'a self) -> Result<ConfigObject<'a>, Self::Error>; } + + +#[cfg(test)] +pub(crate) mod test_source { + use crate::source::ConfigSource; + use crate::object::ConfigObject; + use crate::element::ConfigElement; + use crate::description::ConfigSourceDescription; + + pub(crate) struct TestSource<'a, G>(pub(crate) G) + where G: Fn() -> ConfigElement<'a>; + + impl<'g, G> std::fmt::Debug for TestSource<'g, G> + where G: Fn() -> ConfigElement<'g> + { + fn fmt(&self, _: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + Ok(()) + } + } + + impl<'g, G> ConfigSource for TestSource<'g, G> + where G: Fn() -> ConfigElement<'g> + { + type Error = std::convert::Infallible; // can never happen + + fn load<'a>(&'a self) -> Result<ConfigObject<'a>, Self::Error> { + Ok(ConfigObject::new(self.0(), ConfigSourceDescription::Unknown)) + } + } +} |