Function smart_config::testing::test_minimal

source ·
pub fn test_minimal<C: DeserializeConfig>(
    sample: impl ConfigSource,
) -> Result<C, ParseErrors>
Expand description

Tests config deserialization ensuring that only required config params are covered. This is useful to detect new required params (i.e., backward-incompatible changes).

§Panics

Panics if the sample contains non-required params in the config. The config message will contain paths to the redundant params.

§Errors

Propagates parsing errors, which allows testing negative cases.

§Examples

§Basic usage

use smart_config::{metadata::SizeUnit, testing, ByteSize};

#[derive(DescribeConfig, DeserializeConfig)]
struct TestConfig {
    #[config(default_t = true, alias = "flag")]
    boolean: bool,
    #[config(with = SizeUnit::MiB)]
    size_mb: ByteSize,
}

let sample = smart_config::config!("size_mb": 64);
let config: TestConfig = testing::test_minimal(sample)?;
assert!(config.boolean);
assert_eq!(config.size_mb, ByteSize(64 << 20));

§Panics on redundant sample

#[derive(DescribeConfig, DeserializeConfig)]
struct TestConfig {
    #[config(default_t = true, alias = "flag")]
    boolean: bool,
    #[config(with = SizeUnit::MiB)]
    size_mb: ByteSize,
}

let redundant_sample = smart_config::config!("flag": "false", "size_mb": 64);
// Will panic with a message detailing redundant params (`boolean` in this case)
testing::test_minimal::<TestConfig>(redundant_sample)?;