Files

65 lines
2.6 KiB
Rust

//! Shared helpers of the core integration tests: repository files and the Zod oracles.
#![allow(
dead_code,
clippy::disallowed_methods,
clippy::unwrap_used,
clippy::expect_used
)]
pub mod r4;
pub mod svg_tree;
use faunosteo_core::domain::js::{find_lone_surrogate, parse_json};
use serde_json::Value;
/// The absolute path of a repository file.
#[allow(dead_code)]
pub fn repo_path(relative: &str) -> String {
format!("{}/../../{relative}", env!("CARGO_MANIFEST_DIR"))
}
/// A repository file, by path from the repository root.
pub fn repo_file(relative: &str) -> String {
let path = format!("{}/../../{relative}", env!("CARGO_MANIFEST_DIR"));
std::fs::read_to_string(&path).unwrap_or_else(|error| panic!("{path}: {error}"))
}
/// A frozen oracle file (`crates/core/tests/oracles/<name>`): its text and its value. The
/// TypeScript outputs it freezes keep the lone surrogate escapes `JSON.parse` kept (the L1
/// cases), which the core refuses to read; each one is read here as literal text (`\\uD800`
/// spelt out), never replaced, so the raw text stays the evidence of what TypeScript did.
pub fn oracle_file(name: &str) -> (String, Value) {
let text = repo_file(&format!("crates/core/tests/oracles/{name}"));
let mut spelt = String::with_capacity(text.len());
let mut rest = text.as_str();
while let Some(at) = find_lone_surrogate(rest) {
spelt.push_str(&rest[..at]);
spelt.push('\\');
spelt.push_str(&rest[at..at + 6]);
rest = &rest[at + 6..];
}
spelt.push_str(rest);
let value = parse_json(&spelt).unwrap();
(text, value)
}
/// A Zod oracle (`crates/core/tests/oracles/<name>`): its source file and the Zod output in
/// the JavaScript pretty form.
///
/// The expected text is printed here by `to_js_pretty`, the function under test; so the
/// oracle's own bytes, written by `JSON.stringify` in Node, are first checked against the
/// same value printed compactly: a number this side prints differently from V8 fails here
/// (R5.1b, F1), not silently on both sides of the comparison.
pub fn zod_oracle(name: &str) -> (String, String) {
let raw = repo_file(&format!("crates/core/tests/oracles/{name}"));
let oracle: Value = serde_json::from_str(&raw).unwrap();
let reprinted = faunosteo_core::json::to_js_compact(&oracle).unwrap();
assert!(
raw == format!("{reprinted}\n"),
"{name}: the core does not print the oracle's numbers as V8 wrote them"
);
let source = oracle["source"].as_str().unwrap().to_owned();
let output = faunosteo_core::json::to_js_pretty(&oracle["output"]).unwrap();
(repo_file(&source), output)
}