//! `wai.delivery.rung_price` (extensions/rung-price.md): the committed corpus //! `live-conformance/prices/` regenerates byte for byte from its fixed seeds, holds no other file, //! every case in it gets the outcome it states, §8 lists exactly the reasons the reference reports //! and §6.2 exactly its limits, in order. #![cfg(feature = "rung_price")] use std::collections::BTreeMap; use std::path::PathBuf; use wai::rung_price::{corpus, CODES, LIMITS}; fn wai_dir() -> PathBuf { PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("..") } fn committed() -> BTreeMap { let dir = wai_dir().join("live-conformance/prices"); std::fs::read_dir(&dir) .expect("the committed corpus") .flatten() .map(|e| (e.file_name().to_string_lossy().into_owned(), e.path())) .filter(|(n, _)| n != "README.md") .map(|(n, p)| (n, std::fs::read_to_string(p).expect("a text file"))) .collect() } /// The text of the section of `text` that starts at the line `heading`, to the next heading of /// the same level. fn section<'a>(text: &'a str, heading: &str) -> &'a str { let at = text.find(&format!("\n{heading}")).unwrap_or_else(|| panic!("no {heading}")); let level = heading.split(' ').next().expect("a heading's hashes"); let end = text[at + 1..].find(&format!("\n{level} ")).map_or(text.len(), |e| at + 1 + e); &text[at..end] } /// The first code of each table row of a section: `| \`code\` | …`. fn listed(section: &str) -> Vec { section.lines().filter(|l| l.starts_with("| `")).filter_map(|l| l.split('`').nth(1).map(String::from)).collect() } #[test] fn the_committed_corpus_regenerates_from_its_seeds() { let files = committed(); let generated: BTreeMap = corpus::generate().into_iter().collect(); let extra: Vec<&String> = files.keys().filter(|k| !generated.contains_key(*k)).collect(); let missing: Vec<&String> = generated.keys().filter(|k| !files.contains_key(*k)).collect(); let differ: Vec<&String> = generated.iter().filter(|(k, v)| files.get(*k).is_some_and(|f| f != *v)).map(|(k, _)| k).collect(); assert!(extra.is_empty() && missing.is_empty() && differ.is_empty(), "extra {extra:?}, missing {missing:?}, differ {differ:?}: run `wai_rung_price_vectors gen`"); } #[test] fn every_case_gets_its_stated_outcome() { let fails = corpus::verify(&committed()); assert!(fails.is_empty(), "{}", fails.join("\n")); } /// §8 of the extension lists exactly the codes `CODES` has, each once, in order. #[test] fn the_extension_lists_the_reasons() { let text = std::fs::read_to_string(wai_dir().join("extensions/rung-price.md")).expect("the extension"); let want: Vec = CODES.iter().map(|c| c.to_string()).collect(); assert_eq!(listed(section(&text, "## 8. Reasons")), want, "§8's table and CODES differ"); } /// §6.2's table lists exactly the limits `LIMITS` has, in order. #[test] fn the_extension_lists_the_limits() { let text = std::fs::read_to_string(wai_dir().join("extensions/rung-price.md")).expect("the extension"); let want: Vec = LIMITS.iter().map(|c| c.to_string()).collect(); assert_eq!(listed(section(&text, "### 6.2 Candidates and removals")), want, "§6.2's table and LIMITS differ"); }