silcrow 0.2.1

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

const SCHEMA: &str = "// it's the schema\ndatasource db {\n  provider = \"postgresql\"\n  url      = env(\"DATABASE_URL\")\n}\n\nmodel User {\n  // pin\n  id    Int    @id @default(autoincrement())\n  email String @unique // it's unique\n  /// Shown in the client.\n  role  Role   @default(USER)\n  @@index([email])\n}\n\nenum Role {\n  // § one abcdefgh1\n  USER\n  ADMIN\n}\n";

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

#[test]
fn a_marker_spans_its_block_and_a_one_marker_the_member_below_it() {
    assert!(!parse(&PRISMA, SCHEMA).unwrap().root_node().has_error());
    let model = &SCHEMA[SCHEMA.find("model").unwrap()..SCHEMA.find("\n\nenum").unwrap()];
    assert_eq!(spanned(SCHEMA, 8), Some(model));
    assert_eq!(spanned(SCHEMA, 17), Some("USER"));
    let one = SCHEMA.replace("  // pin\n  id", "  // § one abcdefgh1\n  id");
    assert_eq!(
        spanned(&one, 8),
        Some("id    Int    @id @default(autoincrement())")
    );
}

#[test]
fn a_field_change_moves_and_a_realignment_or_comment_stays() {
    for (from, to) in [
        ("@unique", "@id"),
        ("autoincrement()", "uuid()"),
        ("Role   @default(USER)", "Role   @default(ADMIN)"),
        ("[email]", "[email, role]"),
        ("String @unique", "String? @unique"),
    ] {
        moves(&PRISMA, SCHEMA, &SCHEMA.replace(from, to), 8);
    }
    let realigned = SCHEMA
        .replace("id    Int    @id", "id Int @id")
        .replace("it's unique", "unique")
        .replace("Shown in the client.", "Shown.");
    stays(&PRISMA, SCHEMA, &realigned, 8);
    moves(
        &PRISMA,
        SCHEMA,
        &SCHEMA.replace("  USER\n  ADMIN", "  MEMBER\n  ADMIN"),
        17,
    );
    every_byte_reaches_the_walk(&PRISMA, SCHEMA, 8);
}

#[test]
fn an_apostrophe_in_a_comment_opens_no_string() {
    let marked = SCHEMA.replace("// pin", "// § abcdefgh1");
    let tree = parse(&PRISMA, &marked).expect("parse");
    assert!(literal_lines(&PRISMA, &marked, &tree).iter().all(|l| !l));
}