Skip to main content

Crate test_lang

Crate test_lang 

Source
Expand description

§test_lang

A snapshot test harness for language front-ends: give it source, run that source through a stage, and assert the rendered result — the token stream, the syntax tree, or the diagnostics — against an expected block of text.

§Why a snapshot harness

The output of a lexer, parser, or diagnostics renderer is a shape: a list of tokens, a tree of nodes, a set of caret-annotated errors. Asserting that shape field by field is verbose and brittle — one added node and a dozen index-based assertions shift. A snapshot test instead captures the whole rendered shape as text and compares it against a known-good block. When the output changes, you get a line-level diff pointing at exactly what moved, and accepting the new behavior is a copy-paste.

This crate owns no grammar and depends on no other front-end crate. It works on anything that can render itself to text — a Display value, a Debug tree, or an iterator of displayable items — so the same harness serves a hand-written lexer, a generated parser, or a diagnostics layer without coupling to any of them.

§The two types

§Example

Snapshot a token stream and assert it:

use test_lang::Snapshot;

// Whatever your lexer produces — here, a stand-in that yields display strings.
fn lex(source: &str) -> Vec<String> {
    source.split_whitespace().map(str::to_string).collect()
}

let tokens = lex("let x = 1");
let snapshot = Snapshot::per_line(&tokens);

snapshot.check("let\nx\n=\n1").expect("token stream matches");

When the output drifts, the returned Mismatch shows precisely what changed:

use test_lang::Snapshot;

let snapshot = Snapshot::per_line(["let", "y", "=", "1"]);
let err = snapshot.check("let\nx\n=\n1").unwrap_err();

// `-x` was expected; `+y` was produced in its place.
assert!(err.to_string().contains("-x"));
assert!(err.to_string().contains("+y"));

§no_std

The crate is no_std + alloc when the default std feature is disabled. Every type is available in both modes; the only difference is which crate the Error impl is anchored to.

Structs§

Diff
A minimal line-level edit script between two blocks of text.
Mismatch
The error returned by Snapshot::check when the snapshot does not match the expected text.
Snapshot
A normalized, comparable rendering of some compiler output.

Enums§

Change
The role a line plays in a Diff.