use noyalib::Value;
use noyalib::cst::parse_document;
#[test]
fn single_quoted_neighbours_drive_single_quoted_emit() {
let src = "a: 'one'\nb: 'two'\nc: 0.0.1\n";
let mut doc = parse_document(src).unwrap();
doc.set_value("c", &Value::String("three".into())).unwrap();
assert!(
doc.to_string().contains("c: 'three'"),
"expected 'three' single-quoted, got: {}",
doc
);
}
#[test]
fn double_quoted_neighbours_drive_double_quoted_emit() {
let src = "a: \"one\"\nb: \"two\"\nc: foo\n";
let mut doc = parse_document(src).unwrap();
doc.set_value("c", &Value::String("three".into())).unwrap();
assert!(
doc.to_string().contains("c: \"three\""),
"expected double-quoted, got: {}",
doc
);
}
#[test]
fn plain_dominant_neighbourhood_keeps_plain() {
let src = "a: one\nb: two\nc: three\n";
let mut doc = parse_document(src).unwrap();
doc.set_value("c", &Value::String("four".into())).unwrap();
assert!(
doc.to_string().contains("c: four\n"),
"expected plain, got: {}",
doc
);
}
#[test]
fn mixed_neighbourhood_falls_back_to_plain_when_safe() {
let src = "a: 'one'\nb: \"two\"\nc: three\n";
let mut doc = parse_document(src).unwrap();
doc.set_value("c", &Value::String("four".into())).unwrap();
assert!(
doc.to_string().contains("c: four\n"),
"ambiguous neighbourhood should keep plain, got: {}",
doc
);
}
#[test]
fn explicit_quoted_site_is_preserved_regardless_of_neighbours() {
let src = "a: \"one\"\nb: \"two\"\nc: 'three'\n";
let mut doc = parse_document(src).unwrap();
doc.set_value("c", &Value::String("four".into())).unwrap();
assert!(
doc.to_string().contains("c: 'four'"),
"expected single-quoted preserved, got: {}",
doc
);
}
#[test]
fn unsafe_string_in_single_quoted_neighbourhood_uses_single_quoted() {
let src = "a: 'one'\nb: 'two'\nc: foo\n";
let mut doc = parse_document(src).unwrap();
doc.set_value("c", &Value::String("hello: world".into()))
.unwrap();
let out = doc.to_string();
assert!(
out.contains("c: 'hello: world'"),
"expected single-quoted with unsafe chars, got: {out}",
);
}
#[test]
fn neighbour_only_applies_when_target_is_plain() {
let src = "a: \"one\"\nb: \"two\"\nc: 'three'\n";
let mut doc = parse_document(src).unwrap();
doc.set_value("c", &Value::String("four".into())).unwrap();
assert!(
doc.to_string().contains("c: 'four'"),
"site style wins over neighbours, got: {}",
doc
);
}
#[test]
fn neighbour_lookup_ignores_nested_collection_values() {
let src = "\
inner:
- x
a: 'one'
b: 'two'
c: foo
";
let mut doc = parse_document(src).unwrap();
doc.set_value("c", &Value::String("three".into())).unwrap();
assert!(
doc.to_string().contains("c: 'three'"),
"expected single-quoted via plurality of scalar siblings, got: {}",
doc
);
}