fixtures.rs
2.2 kB · rust · 92 lines
1#[path = "common/rows.rs"]2mod rows;34use rows::sha256;5use serde_json::Value;6use std::path::Path;78fn stored(module: &str) -> Vec<Value> {9 let file = Path::new(env!("CARGO_MANIFEST_DIR"))10 .join("fixtures")11 .join(format!("{module}.json"));12 let text = std::fs::read_to_string(&file)13 .unwrap_or_else(|_| panic!("{} is missing; run the fixtures example", file.display()));14 serde_json::from_str(&text).unwrap()15}1617fn replays(module: &str) {18 let saved = stored(module);19 let fresh = rows::rows(module);20 assert_eq!(21 saved.len(),22 fresh.len(),23 "{module}.json holds {} rows, the example writes {}",24 saved.len(),25 fresh.len()26 );27 for (index, (was, now)) in saved.iter().zip(&fresh).enumerate() {28 let name = was["fn"].as_str().unwrap_or("?");29 assert_eq!(was["fn"], now["fn"], "{module}.json row {index} is not {name}");30 assert_eq!(31 was["in"], now["in"],32 "{module}.json row {index} {name}: the arguments drifted"33 );34 assert_eq!(35 was["note"], now["note"],36 "{module}.json row {index} {name}: the note drifted"37 );38 assert_eq!(39 was["out"], now["out"],40 "{module}.json row {index} {name}: the stored value is not what the crate returns"41 );42 }43}4445#[test]46fn the_sha256_holds_its_standard_vectors() {47 assert_eq!(48 sha256::hex(b""),49 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"50 );51 assert_eq!(52 sha256::hex(b"abc"),53 "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"54 );55}5657#[test]58fn the_six_files_hold_thirty_three_rows() {59 let counted: Vec<usize> = rows::MODULES.iter().map(|m| stored(m).len()).collect();60 assert_eq!(counted, vec![4, 6, 7, 5, 6, 5]);61 assert_eq!(counted.iter().sum::<usize>(), 33);62}6364#[test]65fn core_replays() {66 replays("core");67}6869#[test]70fn num_replays() {71 replays("num");72}7374#[test]75fn math_replays() {76 replays("math");77}7879#[test]80fn gen_replays() {81 replays("gen");82}8384#[test]85fn life_replays() {86 replays("life");87}8889#[test]90fn font_replays() {91 replays("font");92}