use kataan::parser::Parser;
const PRELUDE: &str = r#"
function assert(c, m) { if (!c) { throw 'AssertionError: ' + (m || 'assertion failed'); } }
function assertEq(a, b) { if (a !== b) { throw 'AssertionError: ' + a + ' !== ' + b; } }
"#;
const FIXTURES: &[&str] = &[
"language.js",
"functions_classes.js",
"builtins.js",
"advanced.js",
"regexp.js",
"vm_features.js",
"vm_modern.js",
"vm_closures.js",
"vm_classes.js",
"vm_realworld.js",
];
#[test]
fn bytecode_runs_and_compiles_real_fixtures() {
let dir = concat!(env!("CARGO_MANIFEST_DIR"), "/testdata/conformance/");
let mut ran_ok = 0;
let mut compiled_fully = 0;
let mut results = Vec::new();
for name in FIXTURES {
let source = std::fs::read_to_string(format!("{dir}{name}")).expect("read fixture");
let combined = format!("{PRELUDE}\n{source}");
let ran = kataan::nbvm::execute(&combined).is_ok();
if ran {
ran_ok += 1;
}
let program = Parser::parse_program(&combined).expect("parse");
let fully = kataan::nbvm::compile_program(&program).is_ok();
if fully {
compiled_fully += 1;
}
results.push(format!(
" {} run={} bytecode={}",
name,
if ran { "ok" } else { "FAIL" },
if fully { "full" } else { "fallback" },
));
}
eprintln!(
"bytecode harness: {ran_ok}/{} run ok, {compiled_fully}/{} compile fully to bytecode\n{}",
FIXTURES.len(),
FIXTURES.len(),
results.join("\n"),
);
assert_eq!(ran_ok, FIXTURES.len(), "a fixture failed to run");
assert_eq!(
compiled_fully,
FIXTURES.len(),
"bytecode coverage regressed: only {compiled_fully}/{} fixtures compile fully",
FIXTURES.len(),
);
}