use apollo_configuration::{configuration, parse_yaml};
use std::num::NonZeroUsize;
#[test]
fn fancy_default_expr() {
#[configuration]
struct InMemoryCacheConfig {
#[config(default = NonZeroUsize::new(500).unwrap())]
pub limit: NonZeroUsize,
}
let default = InMemoryCacheConfig::default();
assert_eq!(default.limit.get(), 500);
}
#[test]
fn default_defaults() {
#[configuration]
struct InMemoryCacheConfig {
pub limit: usize,
}
let default = InMemoryCacheConfig::default();
assert_eq!(default.limit, 0);
}
#[test]
fn custom_default() {
#[configuration]
struct InMemoryCacheConfig {
#[config(default = 10)]
pub limit: usize,
}
let default = InMemoryCacheConfig::default();
assert_eq!(default.limit, 10);
}
#[test]
fn empty_yaml_string_parses_to_default() {
#[derive(PartialEq)]
#[configuration]
struct Config {
a: u8,
c: bool,
}
let parsed = parse_yaml::<Config>("", &Default::default());
assert_eq!(parsed.unwrap(), Config { a: 0, c: false });
}
#[test]
fn empty_yaml_string_with_required_fails() {
#[configuration]
struct Config {
a: u8,
#[config(required)]
c: bool,
}
let parsed = parse_yaml::<Config>("", &Default::default())
.expect_err("should fail with validation error");
assert_eq!(parsed.to_string(), "schema validation error");
}
#[test]
fn comment_yaml_string_parses_to_default() {
#[derive(PartialEq)]
#[configuration]
struct Config {
a: u8,
c: bool,
}
let parsed = parse_yaml::<Config>(
r#"
# this is a comment
# there's also some whitespace...if you even care...
"#,
&Default::default(),
);
assert_eq!(parsed.unwrap(), Config { a: 0, c: false });
}