silcrow 0.2.1

Deterministic CLI for a citable, checkable graph of laws in a git tree.
use super::*;

const TSCONFIG: &str = "{\n  // pin\n  \"compilerOptions\": {\n    /* it's strict */\n    // ยง one abcdefgh1\n    \"strict\": true,\n    \"paths\": { \"@/*\": [\"./src/*\"] },\n  },\n  \"include\": [\"src\",],\n}\n";

fn spanned(src: &str, line: usize) -> Option<&str> {
    let tree = parse(&JSONC, src).expect("parse");
    span_range(&JSONC, src, &tree, line).map(|(s, e)| &src[s..e])
}

#[test]
fn a_config_whose_reader_accepts_comments_reads_as_jsonc() {
    for name in [
        "tsconfig.json",
        "pkg/tsconfig.build.json",
        "jsconfig.json",
        "deno.json",
        "a.jsonc",
    ] {
        assert_eq!(
            lang_for_path(Path::new(name)).map(|l| l.id),
            Some("jsonc"),
            "{name}"
        );
    }
    for name in ["package.json", "tsconfig.json5", "a.json5"] {
        assert!(lang_for_path(Path::new(name)).is_none(), "{name}");
    }
    assert!(!parse(&JSONC, TSCONFIG).unwrap().root_node().has_error());
}

#[test]
fn a_marker_spans_its_object_and_a_one_marker_the_member_below_it() {
    assert_eq!(spanned(TSCONFIG, 2), Some(&TSCONFIG[..TSCONFIG.len() - 1]));
    assert_eq!(spanned(TSCONFIG, 5), Some("\"strict\": true"));
}

#[test]
fn a_value_change_moves_and_a_trailing_comma_or_comment_stays() {
    for (from, to) in [
        ("true", "false"),
        ("./src/*", "./lib/*"),
        ("\"src\",]", "\"src\", \"test\",]"),
    ] {
        moves(&JSONC, TSCONFIG, &TSCONFIG.replace(from, to), 2);
    }
    moves(&JSONC, TSCONFIG, &TSCONFIG.replace("true", "false"), 5);
    let reformatted = TSCONFIG
        .replace("\"src\",]", "\"src\"]")
        .replace("},\n  },", "}\n  },")
        .replace("it's strict", "strict")
        .replace("{ \"@/*\"", "{\"@/*\"");
    stays(&JSONC, TSCONFIG, &reformatted, 2);
    every_byte_reaches_the_walk(&JSONC, TSCONFIG, 2);
}