#![allow(missing_docs, clippy::unwrap_used, clippy::expect_used)]
use noyalib::{ErrorKind, Value, from_str};
#[test]
fn a_tag_suffix_holding_an_angle_bracket_is_refused() {
let cases: &[(&str, &str)] = &[
("the `!!` form", "!!)!)>! v\n"),
(
"a named handle",
"%TAG !e! tag:example.com,2026:\n--- !e!a>b v\n",
),
("the primary handle", "!a>b v\n"),
];
let mut refused = 0;
for (label, yaml) in cases {
if let Err(e) = from_str::<Value>(yaml) {
let msg = e.to_string();
if msg.contains('>') {
assert!(
msg.contains("tag suffix") || msg.contains("URI") || msg.contains("tag"),
"{label}: the refusal does not explain itself: {msg}"
);
refused += 1;
}
}
}
assert!(
refused >= 1,
"no spelling of a `>`-bearing tag suffix was refused, so the check is \
no longer reachable"
);
}
#[test]
fn a_block_collection_cannot_open_on_the_document_start_line() {
for (label, yaml) in [
("a sequence", "--- - a\n"),
("an explicit key", "--- ? a\n"),
("a mapping", "--- a: b\n"),
] {
let err = match from_str::<Value>(yaml) {
Ok(v) => panic!("{label}: {yaml:?} was accepted as {v:?}"),
Err(e) => e,
};
assert_eq!(
err.kind(),
ErrorKind::Syntax,
"{label}: wrong kind for {err}"
);
assert!(
err.to_string().contains("not allowed") || err.to_string().contains("'---'"),
"{label}: unhelpful refusal: {err}"
);
}
for yaml in ["---\n- a\n", "---\na: b\n"] {
let _: Value = from_str(yaml).unwrap_or_else(|e| panic!("{yaml:?} should be valid: {e}"));
}
}
#[test]
fn a_value_indicator_with_no_key_open_is_refused() {
for (label, yaml) in [
("a second colon on one line", "a: b: c\n"),
("a colon after a flow collection", "[a]: b\n: c\n"),
("a bare colon after a scalar", "a\n: b\n: c\n"),
] {
match from_str::<Value>(yaml) {
Err(e) => assert_eq!(e.kind(), ErrorKind::Syntax, "{label}: wrong kind for {e}"),
Ok(v) => {
assert!(
v.as_mapping().is_some_and(|m| !m.is_empty()),
"{label}: {yaml:?} produced an empty document: {v:?}"
);
}
}
}
}
#[test]
fn an_explicit_document_end_marker_is_handled() {
let docs: Vec<Value> = noyalib::load_all("a: 1\n...\n---\nb: 2\n...\n")
.expect("explicit end markers parse")
.collect::<Result<_, _>>()
.expect("both documents load");
assert_eq!(
docs.len(),
2,
"the `...` markers changed the document count"
);
assert_eq!(docs[0].get("a").and_then(Value::as_i64), Some(1));
assert_eq!(docs[1].get("b").and_then(Value::as_i64), Some(2));
}
#[test]
fn a_document_marker_at_column_zero_inside_a_block_scalar_is_refused() {
let yaml = "a: |\n line\n--- \nb: 2\n";
match from_str::<Value>(yaml) {
Err(e) => assert!(!e.to_string().is_empty(), "empty refusal"),
Ok(_) => panic!("a `---` inside a block scalar was silently accepted"),
}
}