aboutsummaryrefslogtreecommitdiffstats
path: root/src/federation/harness.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/federation/harness.rs')
-rw-r--r--src/federation/harness.rs72
1 files changed, 72 insertions, 0 deletions
diff --git a/src/federation/harness.rs b/src/federation/harness.rs
index 42ee870..4baf049 100644
--- a/src/federation/harness.rs
+++ b/src/federation/harness.rs
@@ -259,3 +259,75 @@ impl Spec {
.bytes()
}
}
+
+/// Checks the attack table of `doc` (its section Attacks): every row has its test, named
+/// `{prefix}{row}`, among the sources of its kind, `unit` rows among `unit` and `sim`
+/// rows among `sim`, but for the rows `later`, whose tests come with later stages; a row
+/// marked `—` has none; and no test names a row that isn't there, nor two tests one row.
+pub(crate) fn every_row_has_its_test(
+ doc: &str,
+ prefix: &str,
+ unit: &[&str],
+ sim: &[&str],
+ later: &[u32],
+) {
+ let attacks = doc.split("\n## Attacks").nth(1).expect("an attack table");
+ let attacks = attacks.split("\n## ").next().unwrap_or(attacks);
+ let rows: std::collections::BTreeMap<u32, String> = attacks
+ .lines()
+ .filter_map(|line| {
+ let cells: Vec<&str> = line.split('|').map(str::trim).collect();
+ Some((cells.get(1)?.parse().ok()?, cells.get(4)?.to_string()))
+ })
+ .collect();
+ let tests = |sources: &[&str]| -> Vec<u32> {
+ sources
+ .iter()
+ .flat_map(|source| source.lines())
+ .filter_map(|line| {
+ let line = line.trim();
+ line.strip_prefix("async ")
+ .unwrap_or(line)
+ .strip_prefix("fn ")?
+ .strip_prefix(prefix)?
+ .split('(')
+ .next()?
+ .parse()
+ .ok()
+ })
+ .collect()
+ };
+ let (unit, sim) = (tests(unit), tests(sim));
+ let all: Vec<u32> = unit.iter().chain(&sim).copied().collect();
+ for n in &all {
+ assert!(rows.contains_key(n), "a test of row {n}, which isn't there");
+ assert_eq!(
+ all.iter().filter(|m| *m == n).count(),
+ 1,
+ "row {n} tested twice"
+ );
+ }
+ let mut missing = Vec::new();
+ for (n, kind) in &rows {
+ let none = kind == "—";
+ let tested = if kind.starts_with("unit") {
+ unit.contains(n)
+ } else if kind == "sim" {
+ sim.contains(n)
+ } else {
+ false
+ };
+ if later.contains(n) {
+ assert!(!all.contains(n), "row {n} has its test now");
+ assert!(!none, "row {n} asks for no test");
+ } else if none {
+ assert!(!all.contains(n), "row {n} asks for no test, and has one");
+ } else if !tested {
+ missing.push(*n);
+ }
+ }
+ assert!(
+ missing.is_empty(),
+ "rows without their test of their kind: {missing:?}"
+ );
+}