44c6e56a0a
Milestone-close audit found three drift items (architect) plus two carry-on items; bench all-green. Fixes (rt.tidy, boss-direct): - 5th test parse_then_print_then_parse_is_idempotent_on_every_ailx_fixture enforces Direction 2 of the Roundtrip Invariant directly. The DESIGN.md enforcement list grows from four to five tests. - docs/roadmap.md P1 'Round-trip completeness invariant' entry retired with one-line journal mirror per roadmap convention. - DESIGN.md wording sync: <16-hex> → <16-lowercase-hex> for the Float-bits-hex spelling in the new section (matches §Data model). Milestone closed clean; five workspace-wide tests anchor the .ail.json ↔ .ailx bijection plus AST-variant coverage.
278 lines
9.7 KiB
Rust
278 lines
9.7 KiB
Rust
//! Round-trip gate for the form-(A) projection.
|
|
//!
|
|
//! Three complementary checks over `examples/`, each gathering
|
|
//! fixtures dynamically via `read_dir` (no hardcoded lists). The
|
|
//! tests are pure readers — they do not write into the working
|
|
//! tree.
|
|
//!
|
|
//! 1. `print_then_parse_round_trips_every_fixture`: Direction 1 of
|
|
//! the Roundtrip Invariant (DESIGN.md §"Roundtrip Invariant").
|
|
//! For every `examples/*.ail.json` fixture, load → `print` →
|
|
//! `parse` → canonical bytes; assert byte-equal to original.
|
|
//!
|
|
//! 2. `every_ailx_fixture_matches_its_json_counterpart`: hand-
|
|
//! authored ground-truth check. For every `examples/*.ailx`
|
|
//! fixture, parse → canonical bytes; if a same-stem `.ail.json`
|
|
//! counterpart exists, assert canonical-byte equality against
|
|
//! it. Pins the `.ailx` corpus against semantic drift between
|
|
//! the two forms at the fixture level.
|
|
//!
|
|
//! 3. `parse_then_print_then_parse_is_idempotent_on_every_ailx_fixture`:
|
|
//! Direction 2 of the Roundtrip Invariant. For every well-formed
|
|
//! `.ailx` text `t`, asserts `canonical_bytes(parse(t))` equals
|
|
//! `canonical_bytes(parse(print(parse(t))))`. Robust against
|
|
//! future `.ailx` fixtures without a JSON counterpart.
|
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
fn examples_dir() -> PathBuf {
|
|
// `CARGO_MANIFEST_DIR` is the surface crate root; examples live two
|
|
// levels up.
|
|
let crate_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
|
crate_dir.parent().unwrap().parent().unwrap().join("examples")
|
|
}
|
|
|
|
fn list_json_fixtures() -> Vec<PathBuf> {
|
|
let dir = examples_dir();
|
|
let mut paths: Vec<PathBuf> = std::fs::read_dir(&dir)
|
|
.unwrap_or_else(|e| panic!("read_dir({}): {e}", dir.display()))
|
|
.filter_map(|entry| entry.ok())
|
|
.map(|e| e.path())
|
|
.filter(|p| {
|
|
p.file_name()
|
|
.and_then(|n| n.to_str())
|
|
.map(|n| {
|
|
// Round-trip every fixture. The 22b.1/22b.2/22b.3 filter
|
|
// was retired in 22b.4a once the Form-A parser+printer
|
|
// arms for ClassDef / InstanceDef landed.
|
|
n.ends_with(".ail.json")
|
|
})
|
|
.unwrap_or(false)
|
|
})
|
|
.collect();
|
|
paths.sort();
|
|
paths
|
|
}
|
|
|
|
#[test]
|
|
fn print_then_parse_round_trips_every_fixture() {
|
|
let fixtures = list_json_fixtures();
|
|
assert!(
|
|
!fixtures.is_empty(),
|
|
"no .ail.json fixtures found under {}",
|
|
examples_dir().display()
|
|
);
|
|
|
|
let mut failures = Vec::<String>::new();
|
|
let mut passed = 0usize;
|
|
for path in &fixtures {
|
|
match round_trip_one(path) {
|
|
Ok(()) => passed += 1,
|
|
Err(msg) => failures.push(format!("{}: {msg}", path.display())),
|
|
}
|
|
}
|
|
if !failures.is_empty() {
|
|
panic!(
|
|
"round-trip failed for {} of {} fixtures (passed: {}):\n{}",
|
|
failures.len(),
|
|
fixtures.len(),
|
|
passed,
|
|
failures.join("\n")
|
|
);
|
|
}
|
|
eprintln!("round-trip ok for {passed} fixtures");
|
|
}
|
|
|
|
fn round_trip_one(path: &Path) -> Result<(), String> {
|
|
let original = ailang_core::load_module(path).map_err(|e| format!("load: {e}"))?;
|
|
let text = ailang_surface::print(&original);
|
|
let parsed = ailang_surface::parse(&text)
|
|
.map_err(|e| format!("re-parse failed: {e}\n--- printed text ---\n{text}"))?;
|
|
let bytes_orig = ailang_core::canonical::to_bytes(&original);
|
|
let bytes_round = ailang_core::canonical::to_bytes(&parsed);
|
|
if bytes_orig != bytes_round {
|
|
let s_orig = String::from_utf8_lossy(&bytes_orig).into_owned();
|
|
let s_round = String::from_utf8_lossy(&bytes_round).into_owned();
|
|
return Err(format!(
|
|
"canonical bytes differ.\noriginal: {s_orig}\nround: {s_round}"
|
|
));
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn list_ailx_fixtures() -> Vec<PathBuf> {
|
|
let dir = examples_dir();
|
|
let mut paths: Vec<PathBuf> = std::fs::read_dir(&dir)
|
|
.unwrap_or_else(|e| panic!("read_dir({}): {e}", dir.display()))
|
|
.filter_map(|entry| entry.ok())
|
|
.map(|e| e.path())
|
|
.filter(|p| {
|
|
p.file_name()
|
|
.and_then(|n| n.to_str())
|
|
.map(|n| n.ends_with(".ailx"))
|
|
.unwrap_or(false)
|
|
})
|
|
.collect();
|
|
paths.sort();
|
|
paths
|
|
}
|
|
|
|
#[test]
|
|
fn every_ailx_fixture_matches_its_json_counterpart() {
|
|
let fixtures = list_ailx_fixtures();
|
|
assert!(
|
|
!fixtures.is_empty(),
|
|
"no .ailx fixtures found under {}",
|
|
examples_dir().display()
|
|
);
|
|
|
|
let mut failures = Vec::<String>::new();
|
|
let mut paired = 0usize;
|
|
let mut parse_only = 0usize;
|
|
|
|
for ailx_path in &fixtures {
|
|
let text = match std::fs::read_to_string(ailx_path) {
|
|
Ok(t) => t,
|
|
Err(e) => {
|
|
failures.push(format!("{}: read failed: {e}", ailx_path.display()));
|
|
continue;
|
|
}
|
|
};
|
|
let parsed = match ailang_surface::parse(&text) {
|
|
Ok(m) => m,
|
|
Err(e) => {
|
|
failures.push(format!("{}: parse failed: {e}", ailx_path.display()));
|
|
continue;
|
|
}
|
|
};
|
|
|
|
// Same-stem counterpart lookup. `<stem>.ailx` → `<stem>.ail.json`.
|
|
let stem = ailx_path
|
|
.file_name()
|
|
.and_then(|n| n.to_str())
|
|
.and_then(|n| n.strip_suffix(".ailx"))
|
|
.unwrap_or("");
|
|
let json_path = ailx_path.with_file_name(format!("{stem}.ail.json"));
|
|
|
|
if !json_path.exists() {
|
|
// Spec: parse success alone is sufficient for `.ailx` files
|
|
// without a JSON counterpart. (Today: none expected.)
|
|
parse_only += 1;
|
|
continue;
|
|
}
|
|
|
|
let original = match ailang_core::load_module(&json_path) {
|
|
Ok(m) => m,
|
|
Err(e) => {
|
|
failures.push(format!(
|
|
"{}: load_module({}) failed: {e}",
|
|
ailx_path.display(),
|
|
json_path.display()
|
|
));
|
|
continue;
|
|
}
|
|
};
|
|
let bytes_orig = ailang_core::canonical::to_bytes(&original);
|
|
let bytes_parsed = ailang_core::canonical::to_bytes(&parsed);
|
|
if bytes_orig != bytes_parsed {
|
|
let s_orig = String::from_utf8_lossy(&bytes_orig).into_owned();
|
|
let s_round = String::from_utf8_lossy(&bytes_parsed).into_owned();
|
|
failures.push(format!(
|
|
"{} vs {}: canonical bytes differ.\noriginal: {s_orig}\nparsed: {s_round}",
|
|
ailx_path.display(),
|
|
json_path.display()
|
|
));
|
|
continue;
|
|
}
|
|
paired += 1;
|
|
}
|
|
|
|
if !failures.is_empty() {
|
|
panic!(
|
|
"{} of {} .ailx fixture(s) failed cross-check (paired ok: {}, parse-only: {}):\n{}",
|
|
failures.len(),
|
|
fixtures.len(),
|
|
paired,
|
|
parse_only,
|
|
failures.join("\n\n")
|
|
);
|
|
}
|
|
eprintln!(
|
|
".ailx cross-check ok ({} paired, {} parse-only)",
|
|
paired, parse_only
|
|
);
|
|
}
|
|
|
|
/// Direction 2 of the Roundtrip Invariant (DESIGN.md §"Roundtrip
|
|
/// Invariant"): for every well-formed `.ailx` text `t`, the
|
|
/// composition `parse → print → parse` is idempotent on the AST.
|
|
///
|
|
/// For the 57 `.ailx` fixtures that have a JSON counterpart this
|
|
/// follows logically from `print_then_parse_round_trips_every_fixture`
|
|
/// + `every_ailx_fixture_matches_its_json_counterpart`. This test
|
|
/// asserts the property directly so it stays robust for future
|
|
/// `.ailx` fixtures without a JSON counterpart, and so the spec's
|
|
/// Direction-2 claim has a dedicated enforcement point.
|
|
#[test]
|
|
fn parse_then_print_then_parse_is_idempotent_on_every_ailx_fixture() {
|
|
let fixtures = list_ailx_fixtures();
|
|
assert!(
|
|
!fixtures.is_empty(),
|
|
"no .ailx fixtures found under {}",
|
|
examples_dir().display()
|
|
);
|
|
|
|
let mut failures = Vec::<String>::new();
|
|
let mut passed = 0usize;
|
|
for path in &fixtures {
|
|
let text = match std::fs::read_to_string(path) {
|
|
Ok(t) => t,
|
|
Err(e) => {
|
|
failures.push(format!("{}: read failed: {e}", path.display()));
|
|
continue;
|
|
}
|
|
};
|
|
let parsed_once = match ailang_surface::parse(&text) {
|
|
Ok(m) => m,
|
|
Err(e) => {
|
|
failures.push(format!("{}: parse(t) failed: {e}", path.display()));
|
|
continue;
|
|
}
|
|
};
|
|
let printed = ailang_surface::print(&parsed_once);
|
|
let parsed_twice = match ailang_surface::parse(&printed) {
|
|
Ok(m) => m,
|
|
Err(e) => {
|
|
failures.push(format!(
|
|
"{}: parse(print(parse(t))) failed: {e}\n--- printed ---\n{printed}",
|
|
path.display()
|
|
));
|
|
continue;
|
|
}
|
|
};
|
|
let bytes_a = ailang_core::canonical::to_bytes(&parsed_once);
|
|
let bytes_b = ailang_core::canonical::to_bytes(&parsed_twice);
|
|
if bytes_a != bytes_b {
|
|
let s_a = String::from_utf8_lossy(&bytes_a).into_owned();
|
|
let s_b = String::from_utf8_lossy(&bytes_b).into_owned();
|
|
failures.push(format!(
|
|
"{}: parse → print → parse is NOT idempotent.\nparse(t): {s_a}\nparse(print(parse(t))): {s_b}",
|
|
path.display()
|
|
));
|
|
continue;
|
|
}
|
|
passed += 1;
|
|
}
|
|
|
|
if !failures.is_empty() {
|
|
panic!(
|
|
"{} of {} .ailx fixture(s) failed idempotency check (passed: {}):\n{}",
|
|
failures.len(),
|
|
fixtures.len(),
|
|
passed,
|
|
failures.join("\n\n")
|
|
);
|
|
}
|
|
eprintln!("parse→print→parse idempotency ok for {passed} .ailx fixtures");
|
|
}
|