use noyalib_wasm::WasmDocument;
use noyalib_wasm::core::{
document_comments_at, document_get_source, document_get_value, document_span_at, merge_yaml,
parse_yaml_to_value, validate_yaml_json, value_to_yaml, yaml_get_path, yaml_round_trip,
};
#[test]
fn wasm_document_round_trips_unedited_input() {
let yaml = "name: noyalib\nversion: 1\n";
let doc = WasmDocument::new(yaml).unwrap();
assert_eq!(doc.to_string(), yaml);
}
#[test]
fn wasm_document_replace_span_edits_source_in_place() {
let mut doc = WasmDocument::new("name: noyalib\nversion: 1\n").unwrap();
doc.replace_span(6, 13, "fast-yaml").unwrap();
assert_eq!(doc.to_string(), "name: fast-yaml\nversion: 1\n");
}
#[test]
fn wasm_document_set_replaces_value() {
let mut doc = WasmDocument::new("name: noyalib\nversion: 1\n").unwrap();
doc.set("version", "2").unwrap();
assert_eq!(doc.to_string(), "name: noyalib\nversion: 2\n");
}
#[test]
fn wasm_document_as_document_exposes_inner() {
let doc = WasmDocument::new("a: 1\n").unwrap();
let inner = doc.as_document();
assert!(inner.as_value().get_path("a").is_some());
}
#[test]
fn core_parse_yaml_to_value_smoke() {
let v = parse_yaml_to_value("k: 42\n").unwrap();
assert_eq!(v["k"].as_i64(), Some(42));
}
#[test]
fn core_value_to_yaml_smoke() {
let v = parse_yaml_to_value("k: 42\n").unwrap();
let s = value_to_yaml(&v).unwrap();
assert!(s.contains("k:"));
}
#[test]
fn core_yaml_round_trip_smoke() {
assert!(yaml_round_trip("k: 42\n").unwrap().contains("42"));
}
#[test]
fn core_validate_yaml_json_smoke() {
assert!(validate_yaml_json("k: 1\n").unwrap());
}
#[test]
fn core_yaml_get_path_smoke() {
let v = yaml_get_path("a:\n b: 1\n", "a.b").unwrap();
assert_eq!(v.unwrap().as_i64(), Some(1));
}
#[test]
fn core_merge_yaml_smoke() {
let merged = merge_yaml("a: 1\n", "b: 2\n").unwrap();
let v = parse_yaml_to_value(&merged).unwrap();
assert_eq!(v["a"].as_i64(), Some(1));
assert_eq!(v["b"].as_i64(), Some(2));
}
#[test]
fn core_document_helpers_against_real_doc() {
let yaml = "# top\nname: noyalib # inline\nversion: 1\n";
let doc = noyalib::cst::parse_document(yaml).unwrap();
let (start, end) = document_span_at(&doc, "name").unwrap();
assert_eq!(&yaml[start..end], "noyalib");
let v = document_get_value(&doc, "version").unwrap();
assert_eq!(v.as_i64(), Some(1));
assert_eq!(document_get_source(&doc, "name").unwrap(), "noyalib");
let (before, inline) = document_comments_at(&doc, "name");
assert!(!before.is_empty());
assert!(inline.is_some());
}