Struct smart_config::testing::Tester
source · pub struct Tester<'a, C> { /* private fields */ }
Expand description
Test case builder that allows configuring deserialization options etc.
Compared to test()
/ test_complete()
methods, Tester
has more control over deserialization options.
It also allows to test a ConfigSchema
with multiple configs.
§Examples
use smart_config::{testing::Tester, ConfigSchema};
// Assume the following configs and schema are defined.
#[derive(DescribeConfig, DeserializeConfig)]
struct TestConfig {
#[config(default, alias = "flag")]
boolean: bool,
}
#[derive(DescribeConfig, DeserializeConfig)]
struct OtherConfig {
str: Option<String>,
}
fn config_schema() -> ConfigSchema {
let mut schema = ConfigSchema::new(&TestConfig::DESCRIPTION, "test");
schema
.insert(&OtherConfig::DESCRIPTION, "other")
.unwrap();
schema
}
// Set the tester (can be shared across tests).
let schema: ConfigSchema = config_schema();
let mut tester = Tester::new(schema);
// Set shared deserialization options...
tester.coerce_serde_enums().coerce_variant_names();
let sample = smart_config::config!("test.flag": true, "other.str": "?");
let config: TestConfig = tester.for_config().test_complete(sample.clone())?;
assert!(config.boolean);
let config: OtherConfig = tester.for_config().test_complete(sample)?;
assert_eq!(config.str.unwrap(), "?");
Implementations§
source§impl Tester<'_, ()>
impl Tester<'_, ()>
sourcepub fn new(schema: ConfigSchema) -> Self
pub fn new(schema: ConfigSchema) -> Self
Creates a tester with the specified schema.
sourcepub fn for_config<C: DeserializeConfig + VisitConfig>(
&mut self,
) -> Tester<'_, C>
pub fn for_config<C: DeserializeConfig + VisitConfig>( &mut self, ) -> Tester<'_, C>
Specializes this tester for a config.
§Panics
Panics if the config is not contained in the schema, or is contained at multiple locations.
sourcepub fn insert<C: DeserializeConfig + VisitConfig>(
&mut self,
prefix: &'static str,
) -> Tester<'_, C>
pub fn insert<C: DeserializeConfig + VisitConfig>( &mut self, prefix: &'static str, ) -> Tester<'_, C>
Similar to Self::for_config()
, but inserts the specified config into the schema rather
than expecting it to be present.
§Panics
Panics if the config cannot be inserted into the schema.
source§impl<C> Tester<'_, C>
impl<C> Tester<'_, C>
sourcepub fn coerce_variant_names(&mut self) -> &mut Self
pub fn coerce_variant_names(&mut self) -> &mut Self
Enables coercion of enum variant names.
sourcepub fn coerce_serde_enums(&mut self) -> &mut Self
pub fn coerce_serde_enums(&mut self) -> &mut Self
Enables coercion of serde-style enums.
sourcepub fn set_env(
&mut self,
var_name: impl Into<String>,
value: impl Into<String>,
) -> &mut Self
pub fn set_env( &mut self, var_name: impl Into<String>, value: impl Into<String>, ) -> &mut Self
Sets mock environment variables that will be recognized by Environment
and Env
fallbacks.
Beware that env variable overrides are thread-local; for this reason, Tester
is not Send
(cannot be sent to another thread).
sourcepub fn new_repository(&self) -> ConfigRepository<'_>
pub fn new_repository(&self) -> ConfigRepository<'_>
Creates an empty repository based on the tester schema and the deserialization options.
source§impl<C: DeserializeConfig + VisitConfig> Tester<'_, C>
impl<C: DeserializeConfig + VisitConfig> Tester<'_, C>
sourcepub fn test(&self, sample: impl ConfigSource) -> Result<C, ParseErrors>
pub fn test(&self, sample: impl ConfigSource) -> Result<C, ParseErrors>
sourcepub fn test_complete(&self, sample: impl ConfigSource) -> Result<C, ParseErrors>
pub fn test_complete(&self, sample: impl ConfigSource) -> Result<C, ParseErrors>
Tests config deserialization ensuring that all declared config params are covered.
§Panics
Panics if the sample
doesn’t recursively cover all params in the config. The config message
will contain paths to the missing params.
§Errors
Propagates parsing errors, which allows testing negative cases.
§Examples
See test_complete()
for the examples of usage.
sourcepub fn test_minimal(&self, sample: impl ConfigSource) -> Result<C, ParseErrors>
pub fn test_minimal(&self, sample: impl ConfigSource) -> Result<C, ParseErrors>
Tests config deserialization ensuring that only the required config params are present in sample
.
§Panics
Panics if the sample
contains params with default values. The config message
will contain paths to the redundant params.
§Errors
Propagates parsing errors, which allows testing negative cases.
§Examples
See test_minimal()
for the examples of usage.