Expand description
Shared test-support utilities for the tabnas parser system.
Every tabnas package ships a canonical TypeScript implementation and
ports of it, and proves they agree by running one set of TSV
fixtures from test/spec/ in every runtime. This crate is the
machinery that reads those fixtures in Rust, so there is one loader
with one set of rules instead of a copy per repository quietly
drifting from its TypeScript and Go twins.
The TypeScript half is @tabnas/support and the Go half is
github.com/tabnas/support/go. The three are written to behave
identically: same escape codec, same comment and blank-line handling,
same ERROR:<code> contract, same value comparison. Where a
difference is unavoidable it is documented at the point it appears,
and collected in doc/reference.md.
A typical suite is a few lines:
use tabnas_support::{find_spec_dir, Failure, Runner, Value};
let dir = find_spec_dir(None).expect("a test/spec directory above the crate");
Runner::new(|input| {
// Parse with your grammar, then hand back a `Value` or a `Failure`
// carrying the error code the fixture pins.
let _ = input;
Err(Failure::new("unexpected"))
})
.dir(dir.join("happy"));This crate has no required dependencies, and never will: every
tabnas Rust crate would take it as a dev-dependency, so anything it
required would land in all of them. The adder grammar that exercises
it end to end therefore lives in the separate adder/ crate, which
may depend on the engine.
Structs§
- Catalogue
Diff - What
compare_cataloguesfound. All three lists are sorted. - Census
Options - Which column holds the expectation. The default is each row’s last
column (the ordinary two-column
input/expectedfixture needs no selection at all), but a wider fixture can keep the expectation anywhere, which is why the selection exists. - Coverage
Report - What
coveragefound. Both lists are sorted. - Error
- A defect in a fixture, a suite or a runner: an unreadable file, a malformed expectation, a runner built without a parser. Carries the same sentence the TypeScript and Go halves raise for the same condition, so a failure reads the same whichever runtime reported it.
- Error
Expect - An error expectation, split into the parts a runner checks separately.
- Failure
- A parse that failed, as the runner sees it: the code the fixture pins, the position it may pin, and the message a failure report quotes.
- Register
- A fixture of recorded divergences, run by every port.
- Row
- One data row of a fixture file.
- Runner
- Drives fixture rows through one parser. Reuse it across files.
- Spec
File - A loaded fixture file.
- Spec
Options - How to interpret a fixture file. The defaults are the tabnas convention; override them only for a fixture that genuinely differs.
Enums§
- Column
- A column selector: a position, or a header name.
- Value
- A parsed value in the fixture data model. See the module notes.
Constants§
- ERROR_
PREFIX - The prefix marking an expected-failure cell.
- VERSION
- The release version, kept in step with
ts/package.json. Seetests/version_test.rs, which fails the build when they drift.
Functions§
- codes_
in_ spec_ dir - Collect every code named by a code-style expectation cell under a
fixture directory: sorted, unique. Message-style expectations and bare
ERRORcells assert a rejection without naming a code, so they are not collected; a value row is not an expectation at all. - compare_
catalogues - Diff two
{code: template}catalogues: message catalogues, hint catalogues, or one runtime’s against the other’s. Templates compare byte for byte: two templates that merely “mean the same” have still drifted, and the byte diff is what a maintainer has to reconcile. - coverage
- Compare the codes a package declares against the codes its fixtures
exercise (typically
codes_in_spec_dir’s answer). Whether inherited base codes count as declared is the caller’s choice: pass them in or leave them out. - equal_
value - Compare a parse result against an expected value with JSON semantics:
structural, key-order independent,
NaNequal to itself, and-0NOT equal to0. Those last two are the two halves of ADR-15, and they go opposite ways on purpose. SeeValue’s equality. - equal_
value_ with equal_valuewith a normalize hook, applied to every node on both sides, outermost first. This is where a runtime-specific container, an insertion-ordered map or a reference wrapper unwrapped into a plain value, is folded into the plain value the fixture’s JSON describes.- error_
code - The code from an error expectation:
ERROR:unexpectedgivesunexpected, and a bareERRORgives""(meaning “any code”). A trailing@<row>:<col>is a position expectation and is NOT part of the code; seeerror_expect. An error when handed a cell that is not an error expectation at all. - error_
expect - Read an error expectation.
- escape
- Encode a string into the fixture escape set, the inverse of
unescape. The backslash goes first so an already-escaped sequence is not double-decoded on the way back:unescape(escape(s)) == sfor everys. - find_
spec_ dir - Walk up from
from(the working directory whenNone) looking for atest/specdirectory, and return it. - format_
value - Render a value for a failure message: JSON where possible, so the text
lines up with how the fixture wrote it, with
-0spelt-0and the values JSON cannot spell written as JavaScript names them. - is_
error_ expect - Is this expected cell an error expectation?
- load_
spec - Load one fixture file by path.
- load_
spec_ dir - Load every
*.tsvin a directory, sorted by name so every runtime and every run visits them in the same order. Discovery by listing is deliberate: adding a fixture then runs it without editing a runner. - lone_
surrogate_ at - The position of the first UNPAIRED
\uXXXXsurrogate escape in an expected cell, counted in CODE POINTS, orNone. - lone_
surrogate_ message - The message the runner uses when a shared cell holds an unpaired surrogate escape. Exported so every runtime says the same thing, and so a caller building its own runner can reuse it.
- no_
divergences - Declare that a repo records no divergences at all.
- parse_
expect - Parse an expected cell as JSON. An empty cell is
Value::Undefined, the fixture convention for “no value”, as in a utility whose result is nothing at all. - parse_
spec - Parse fixture text that is already in memory.
load_specis this plus a file read; tests of the loader itself use this form. - report
- Panic with every failure at once, naming file and line, rather than stopping at the first; do nothing when there is nothing to report.
- unescape
- Decode the fixture escape set:
\n,\r,\tand\\. Any other backslash sequence, and a trailing lone backslash, are left as they are.
Type Aliases§
- Result
- The result type every fallible function here returns.