76 lines
2.2 KiB
Rust
76 lines
2.2 KiB
Rust
//! Byte-equivalence test for the Python target against the JS baseline.
|
|
|
|
use std::collections::BTreeMap;
|
|
use std::path::PathBuf;
|
|
|
|
use mizan_codegen::config::{Config, SourceConfig};
|
|
use mizan_codegen::emit::{CodegenTarget, EmittedFile};
|
|
use mizan_codegen::emit::python::PythonClient;
|
|
use mizan_codegen::fetch::parse_ir_from_str;
|
|
|
|
|
|
fn load_ir() -> mizan_codegen::ir::MizanIR {
|
|
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/afi_ir.kdl");
|
|
parse_ir_from_str(&std::fs::read_to_string(&path).unwrap()).unwrap()
|
|
}
|
|
|
|
|
|
fn fixture_config() -> Config {
|
|
Config {
|
|
project_id: None,
|
|
output: PathBuf::from("/tmp"),
|
|
targets: vec!["python".to_string()],
|
|
source: SourceConfig { fastapi: None, django: None, rust: None, script: None },
|
|
rust_kernel: None,
|
|
rust_crate_name: None,
|
|
}
|
|
}
|
|
|
|
|
|
fn read_baseline(rel: &str) -> String {
|
|
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
|
.join("tests/fixtures/baselines/python")
|
|
.join(rel);
|
|
std::fs::read_to_string(&path)
|
|
.unwrap_or_else(|e| panic!("read baseline {}: {e}", path.display()))
|
|
}
|
|
|
|
|
|
fn emit_index(files: &[EmittedFile]) -> BTreeMap<PathBuf, &str> {
|
|
files.iter().map(|f| (f.rel_path.clone(), f.content.as_str())).collect()
|
|
}
|
|
|
|
|
|
fn assert_byte_equal(rel: &str, files: &BTreeMap<PathBuf, &str>) {
|
|
let actual = files
|
|
.get(&PathBuf::from(rel))
|
|
.unwrap_or_else(|| panic!("Python target did not produce {rel}"));
|
|
let expected = read_baseline(rel);
|
|
if *actual != expected {
|
|
for (lineno, (a, b)) in actual.lines().zip(expected.lines()).enumerate() {
|
|
if a != b {
|
|
panic!(
|
|
"{rel} diverges at line {}:\n expected: {b:?}\n actual: {a:?}",
|
|
lineno + 1,
|
|
);
|
|
}
|
|
}
|
|
panic!(
|
|
"{rel} diverges in length: actual={} expected={}",
|
|
actual.len(), expected.len(),
|
|
);
|
|
}
|
|
}
|
|
|
|
|
|
#[test]
|
|
fn python_target_all_files_match_baseline() {
|
|
let ir = load_ir();
|
|
let files = PythonClient.emit(&ir, &fixture_config());
|
|
let index = emit_index(&files);
|
|
|
|
for rel in ["types.py", "client.py", "__init__.py"] {
|
|
assert_byte_equal(rel, &index);
|
|
}
|
|
}
|