use noyalib::{Value, from_str, load_all_as};
use serde::Deserialize;
const MULTI: &str = "a: 1\n---\nb: 2\n";
const EXPECTED_MESSAGE: &str =
"deserializing from YAML containing more than one document is not supported";
#[derive(Debug, Deserialize)]
struct A {
#[allow(dead_code)]
a: i32,
}
#[test]
fn typed_target_rejects_multi_document_stream() {
let err = from_str::<A>(MULTI).expect_err("a second document must be rejected");
assert_eq!(err.to_string(), EXPECTED_MESSAGE);
}
#[test]
fn value_target_rejects_multi_document_stream() {
let err = from_str::<Value>(MULTI).expect_err("a second document must be rejected");
assert_eq!(err.to_string(), EXPECTED_MESSAGE);
}
#[test]
fn single_document_with_leading_marker_still_parses() {
let v: Value = from_str("---\na: 1\n").expect("a single leading `---` is not multi-document");
assert_eq!(v.get("a").and_then(Value::as_i64), Some(1));
}
#[test]
fn single_document_with_trailing_end_marker_still_parses() {
let v: Value = from_str("a: 1\n...\n").expect("a trailing `...` is not multi-document");
assert_eq!(v.get("a").and_then(Value::as_i64), Some(1));
}
#[test]
fn single_document_with_both_markers_still_parses() {
let v: Value =
from_str("---\na: 1\n...\n").expect("both markers around one document still parse");
assert_eq!(v.get("a").and_then(Value::as_i64), Some(1));
}
#[test]
fn from_str_multi_still_returns_every_document() {
let docs: Vec<i32> =
load_all_as("1\n---\n2\n---\n3\n").expect("multi-doc entry point still works");
assert_eq!(docs, vec![1, 2, 3]);
}