From 322a6e3537ee24d58d9601cae9500a9be1f65dae Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 21 Jun 2022 09:26:18 +0200 Subject: Add tests for loading sources Signed-off-by: Matthias Beyer --- src/config.rs | 28 ++++++++++++++++++++++++++++ src/source.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) 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, 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, Self::Error> { + Ok(ConfigObject::new(self.0(), ConfigSourceDescription::Unknown)) + } + } +} -- cgit v1.2.3