use super::NodeSection;
#[derive(Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
struct DecodeOptions {
threads: usize,
}
#[test]
fn a_section_states_its_options_and_runs() {
let section = NodeSection::new(DecodeOptions { threads: 8 });
assert_eq!(section.options(), Some(&DecodeOptions { threads: 8 }));
assert_eq!(section.into_options(), Some(DecodeOptions { threads: 8 }));
}
#[test]
fn a_bare_section_runs_with_no_options_of_its_own() {
#[derive(Debug, PartialEq, Eq)]
struct Mandatory {
path: &'static str,
}
let bare = NodeSection::<Mandatory>::default();
assert_eq!(bare.options(), None);
}
#[cfg(feature = "serde")]
mod deserialization {
use super::{DecodeOptions, NodeSection};
fn section(document: &str) -> Result<NodeSection<DecodeOptions>, serde_json::Error> {
serde_json::from_str(document)
}
#[test]
fn an_empty_section_runs_at_the_options_own_defaults() {
let parsed = section("{}").expect("an empty section parses");
assert_eq!(parsed.options(), None);
}
#[test]
fn the_dead_switch_is_refused_as_an_unknown_key() {
let refusal =
section(r#"{ "enabled": false }"#).expect_err("`enabled` is no longer on the shelf");
let sentence = refusal.to_string();
assert!(sentence.contains("enabled"), "{sentence}");
assert!(sentence.contains("unknown field"), "{sentence}");
}
#[test]
fn the_knobs_sit_under_their_own_key() {
let parsed =
section(r#"{ "options": { "threads": 8 } }"#).expect("a section with knobs parses");
assert_eq!(parsed.options(), Some(&DecodeOptions { threads: 8 }));
}
#[test]
fn an_unknown_key_is_refused_by_name() {
let refusal = section(r#"{ "enabeld": false }"#).expect_err("`enabeld` is not on the shelf");
let sentence = refusal.to_string();
assert!(sentence.contains("enabeld"), "{sentence}");
assert!(sentence.contains("unknown field"), "{sentence}");
}
#[test]
fn a_missing_knob_inside_a_present_options_block_is_refused() {
let refusal = section(r#"{ "options": {} }"#).expect_err("`threads` has no default of its own");
assert!(refusal.to_string().contains("threads"), "{refusal}");
}
}