use apollo_configuration::configuration;
use apollo_configuration::parse_yaml;
#[test]
fn simple_struct() {
#[configuration]
#[derive(PartialEq, Eq)]
struct Config {
name: String,
#[config(default = 10)]
value: i32,
}
let default = Config::default();
assert_eq!(default.name, "");
assert_eq!(default.value, 10);
let config: Config =
parse_yaml(r#"{ name: "test", value: 42 }"#, &Default::default()).expect("should parse");
assert_eq!(config.name, "test");
assert_eq!(config.value, 42);
}
#[test]
fn struct_with_cfg_gated_fields() {
#[configuration]
#[derive(PartialEq, Eq)]
struct Config {
name: String,
#[cfg(test)]
#[config(default = 42)]
enabled_field: i32,
#[cfg(not(test))]
#[config(default = 99)]
disabled_field: i32,
}
let default = Config::default();
assert_eq!(default.name, "");
assert_eq!(default.enabled_field, 42);
let config: Config = parse_yaml(
r#"{ name: "test", enabled_field: 100 }"#,
&Default::default(),
)
.expect("should parse");
assert_eq!(config.name, "test");
assert_eq!(config.enabled_field, 100);
let result: Result<Config, _> = parse_yaml(
r#"{ name: "test", disabled_field: 100 }"#,
&Default::default(),
);
assert!(result.is_err(), "cfg-disabled field should not be accepted");
}