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<'_, ()>

source

pub fn new(schema: ConfigSchema) -> Self

Creates a tester with the specified schema.

source

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.

source

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>

source

pub fn coerce_variant_names(&mut self) -> &mut Self

Enables coercion of enum variant names.

source

pub fn coerce_serde_enums(&mut self) -> &mut Self

Enables coercion of serde-style enums.

source

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).

source

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>

source

pub fn test(&self, sample: impl ConfigSource) -> Result<C, ParseErrors>

Tests config deserialization from the provided sample. Takes into account param aliases, performs sample preprocessing etc.

§Errors

Propagates parsing errors, which allows testing negative cases.

§Examples

See test() for the examples of usage.

source

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.

source

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.

Trait Implementations§

source§

impl<'a, C: Debug> Debug for Tester<'a, C>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Tester<'static, ()>

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<C: DeserializeConfig + VisitConfig> Default for Tester<'static, C>

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<'a, C> Freeze for Tester<'a, C>

§

impl<'a, C> !RefUnwindSafe for Tester<'a, C>

§

impl<'a, C> !Send for Tester<'a, C>

§

impl<'a, C> !Sync for Tester<'a, C>

§

impl<'a, C> Unpin for Tester<'a, C>
where C: Unpin,

§

impl<'a, C> !UnwindSafe for Tester<'a, C>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more