Files
faunosteo/crates/core/tests/curation_format.rs
T

176 lines
6.4 KiB
Rust

//! The pipeline's formatting and JavaScript semantics (R5.2) against the TypeScript tools
//! themselves: Prettier's JSON layout and table alignment, `slugify`, `deterministicUuid`,
//! `Number()`, `Math.round`, `Object.keys` order, `trim`. The expectations were written by
//! Node (`packages/anatomy/tests/r5.format-oracles.test.ts`, `r5.pipeline-oracles.test.ts`);
//! nothing here computes its own expected text.
#![allow(clippy::disallowed_methods, clippy::unwrap_used, clippy::expect_used)]
mod common;
use faunosteo_core::anatomy::svg::number::{is_js_space, js_number_of, js_split_list};
use faunosteo_core::curation::PipelineError;
use faunosteo_core::curation::ids::deterministic_uuid;
use faunosteo_core::curation::markdown::format_markdown;
use faunosteo_core::curation::prettier_json::format_json;
use faunosteo_core::curation::text::{js_keys, js_round, slugify};
use faunosteo_core::domain::js::parse_json;
use faunosteo_core::json::js_number;
use serde_json::Value;
fn oracle(name: &str) -> Value {
serde_json::from_str(&common::repo_file(&format!(
"crates/core/tests/oracles/r5/{name}"
)))
.unwrap()
}
fn rows<'a>(value: &'a Value, key: &str) -> &'a Vec<Value> {
value[key].as_array().unwrap()
}
/// Every case prints as Prettier printed it, except the declared refusal: a text holding an
/// emoji, whose Prettier width table is not reproduced (`PipelineError::UnsupportedWidth`).
#[test]
fn json_layout_matches_prettier_on_every_probe() {
let oracle = oracle("prettier-json.json");
let mut refused = Vec::new();
for (index, case) in rows(&oracle, "cases").iter().enumerate() {
let input = parse_json(case["input"].as_str().unwrap()).unwrap();
match format_json(&input) {
Ok(text) => assert_eq!(text, case["output"].as_str().unwrap(), "case {index}"),
Err(PipelineError::UnsupportedWidth(_)) => refused.push(index),
Err(other) => panic!("case {index}: {other}"),
}
}
assert_eq!(refused, vec![19], "only the emoji case is refused");
}
#[test]
fn every_generated_json_file_of_the_seed_prints_byte_for_byte() {
let seed = "supabase/seed/anatomical-sheets/equine";
for file in [
"legacy-import/document.json",
"legacy-import/legacy-id-map.json",
"legacy-import/assets.json",
"draft/document.json",
"draft/assets.json",
"draft/sheet-ref.json",
"draft/report-migration-map.json",
] {
let text = common::repo_file(&format!("{seed}/{file}"));
let value = parse_json(&text).unwrap();
assert_eq!(format_json(&value).unwrap(), text, "{file}");
}
}
#[test]
fn tables_align_as_prettier_aligns_them() {
let oracle = oracle("markdown-tables.json");
let mut refused = Vec::new();
for (index, case) in rows(&oracle, "cases").iter().enumerate() {
match format_markdown(case["raw"].as_str().unwrap()) {
Ok(text) => assert_eq!(text, case["output"].as_str().unwrap(), "case {index}"),
Err(PipelineError::UnsupportedWidth(_)) => refused.push(index),
Err(other) => panic!("case {index}: {other}"),
}
}
assert_eq!(refused, vec![5], "only the emoji table is refused");
}
#[test]
fn slugs_and_uuids_match_the_typescript() {
let semantics = oracle("js-semantics.json");
let ids = oracle("ids.json");
for row in rows(&semantics, "slugify").iter().chain(rows(&ids, "slug")) {
let label = row["label"].as_str().unwrap();
assert_eq!(slugify(label), row["slug"].as_str().unwrap(), "{label:?}");
}
let uuid_rows = rows(&semantics, "deterministicUuid")
.iter()
.chain(rows(&ids, "uuid"));
for row in uuid_rows {
let name = row["name"].as_str().unwrap();
assert_eq!(
deterministic_uuid(name),
row["uuid"].as_str().unwrap(),
"{name:?}"
);
}
}
#[test]
fn number_points_round_and_trim_follow_javascript() {
let semantics = oracle("js-semantics.json");
for row in rows(&semantics, "number") {
let value = js_number_of(row["text"].as_str().unwrap());
assert_eq!(js_string(value), row["string"].as_str().unwrap(), "{row}");
let negative_zero = value == 0.0 && value.is_sign_negative();
assert_eq!(
negative_zero,
row["negativeZero"].as_bool().unwrap(),
"{row}"
);
}
for row in rows(&semantics, "points") {
let numbers: Vec<String> = js_split_list(row["text"].as_str().unwrap())
.into_iter()
.map(|part| js_string(js_number_of(part)))
.collect();
let expected: Vec<&str> = rows(row, "numbers")
.iter()
.map(|n| n.as_str().unwrap())
.collect();
assert_eq!(numbers, expected, "{row}");
}
for row in rows(&semantics, "mathRound") {
let bits = u64::from_str_radix(row["bits"].as_str().unwrap(), 16).unwrap();
let value = f64::from_bits(bits);
let rounded = js_round(value);
assert_eq!(js_string(rounded), row["round"].as_str().unwrap(), "{row}");
let negative_zero = rounded == 0.0 && rounded.is_sign_negative();
assert_eq!(
negative_zero,
row["negativeZero"].as_bool().unwrap(),
"{row}"
);
}
for row in rows(&semantics, "trim") {
let text = row["text"].as_str().unwrap();
assert_eq!(
text.trim_matches(is_js_space),
row["trimmed"].as_str().unwrap()
);
let space = text.chars().next().unwrap();
assert_eq!(
is_js_space(space),
row["isSpace"].as_bool().unwrap(),
"{space:?}"
);
}
}
#[test]
fn object_keys_follow_javascript_order() {
let semantics = oracle("js-semantics.json");
for row in rows(&semantics, "objectEntriesOrder") {
let value = parse_json(row["text"].as_str().unwrap()).unwrap();
let keys: Vec<&String> = js_keys(value.as_object().unwrap());
let expected: Vec<&str> = rows(row, "keys")
.iter()
.map(|k| k.as_str().unwrap())
.collect();
assert_eq!(keys, expected, "{row}");
}
}
/// `String(number)`, NaN and the infinities included.
fn js_string(value: f64) -> String {
if value.is_nan() {
"NaN".to_owned()
} else if value.is_infinite() {
if value > 0.0 { "Infinity" } else { "-Infinity" }.to_owned()
} else {
js_number(value)
}
}