feat(validate): 2.1 errors for the glossary registries

Validation now checks what 2.1 adds: registry ids carry their prefix and
are unique across all registries (E2101); a target's terms, equations,
values, and misconceptions name declared entries (E2102); equation
symbols resolve (E2103); values give a value or an ordered range and a
declared term (E2104); citation keys in the new registries resolve
(E2105); brace references in student-facing prose resolve (E2106);
`conflicts` and `target_text_forbid` patterns compile (E2107); and a bank
option's misconception_id is declared (E2108).

An unresolved {lo-...} or {t-...} in a field 2.0 already had (objective
and target text, reading notes) is left to the linter, because 2.0
accepted it and rendered it literally; the new reference kinds and the
new fields are checked everywhere.

Adds the `regex` crate, with default features off, for the course's own
patterns. Each error has a test built from the prototype's self-test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
alexmandClaude committed 2026-11-16 11:58:53 -05:00
1 parent 51a2fd235f
commit 960cf6cbd3
7 files changed
+915 -36

No files matched your search

+269
View File
@@ -0,0 +1,269 @@
// SPDX-License-Identifier: Prosperity-3.0.0
// Copyright Scienting
// Source: https://git.scient.ing/education/coursebank
//! The errors 2.1 adds, one test each.
//!
//! Each test breaks the example course in `docs/design/2.1/examples/v21/` the way
//! the prototype's self-test does (`prototype/check21.py --selftest`) and checks
//! that validation reports it. `docs/design/2.1/CODES.md` maps the
//! specification's codes to the messages asserted here.
mod common;
use common::{edit, v21_course};
use coursebank::catalog::Catalog;
use coursebank::course::CourseFile;
use coursebank::course::glossary::Symbol;
/// The example course, loaded.
fn course(tag: &str) -> CourseFile {
CourseFile::load_dir(&v21_course(tag)).expect("the example loads")
}
/// Asserts exactly one issue contains `needle`.
fn reports(issues: &[String], needle: &str) {
let hits = issues.iter().filter(|i| i.contains(needle)).count();
assert_eq!(
hits, 1,
"expected one issue containing {needle:?}, got {issues:#?}"
);
}
#[test]
fn the_example_validates_with_no_errors() {
let catalog = Catalog::load(&v21_course("valid")).unwrap();
let issues = catalog.validate().unwrap();
assert!(issues.is_empty(), "{issues:#?}");
}
#[test]
fn e2101_an_id_in_two_registries() {
let mut c = course("e2101");
let target = c.learning_targets["t-align-fill-matrix"].clone();
c.learning_targets.insert("term-gap".into(), target);
reports(
&c.validate(),
"`term-gap` is declared as both a learning target and a term",
);
}
#[test]
fn e2101_a_registry_id_without_its_prefix() {
let mut c = course("e2101-prefix");
let term = c.terms.remove("term-gap").unwrap();
c.terms.insert("gap".into(), term);
let issues = c.validate();
reports(&issues, "term `gap`: a term id is `term-` followed by");
}
#[test]
fn e2102_a_target_lists_an_undeclared_entry() {
let mut c = course("e2102");
c.learning_targets
.get_mut("t-align-fill-matrix")
.unwrap()
.terms
.push("term-nope".into());
reports(
&c.validate(),
"target `t-align-fill-matrix`: `terms` names `term-nope`, which is not a declared term",
);
}
#[test]
fn e2102_an_id_listed_under_the_wrong_registry() {
let mut c = course("e2102-wrong");
c.learning_targets
.get_mut("t-align-fill-matrix")
.unwrap()
.equations
.push("term-gap".into());
reports(
&c.validate(),
"`equations` names `term-gap`, which is not a declared equation",
);
}
#[test]
fn e2103_a_symbol_with_neither_term_nor_meaning() {
let mut c = course("e2103");
c.equations
.get_mut("eq-global-recurrence")
.unwrap()
.symbols
.push(Symbol {
symbol: "q".into(),
term: None,
meaning: None,
units: None,
});
reports(
&c.validate(),
"equation `eq-global-recurrence`: symbol `q` needs a `term` or a `meaning`",
);
}
#[test]
fn e2103_a_symbol_naming_an_undeclared_term() {
let mut c = course("e2103-term");
c.equations.get_mut("eq-global-recurrence").unwrap().symbols[2].term = Some("term-nope".into());
reports(
&c.validate(),
"symbol `d` names the term `term-nope`, which is not declared",
);
}
#[test]
fn e2104_a_value_with_neither_value_nor_range() {
let mut c = course("e2104");
c.values.get_mut("val-alignment-count-100").unwrap().value = None;
reports(
&c.validate(),
"value `val-alignment-count-100`: give a `value` or a `range`",
);
}
#[test]
fn e2104_a_range_that_runs_high_to_low_and_an_undeclared_term() {
let mut c = course("e2104-range");
let v = c.values.get_mut("val-per-decade").unwrap();
v.range = Some([7.0, 5.0]);
v.term = Some("term-nope".into());
let issues = c.validate();
reports(
&issues,
"value `val-per-decade`: range runs from 7 down to 5",
);
reports(
&issues,
"value `val-per-decade`: term `term-nope` is not declared",
);
}
#[test]
fn e2105_a_citation_key_not_in_references() {
let mut c = course("e2105");
c.values
.get_mut("val-alignment-count-100")
.unwrap()
.source
.as_mut()
.unwrap()
.reference = "nobody2020".into();
c.terms.get_mut("term-alignment-matrix").unwrap().textbook[0].reference =
Some("nobody2021".into());
c.misconceptions
.get_mut("mis-only-matches-count")
.unwrap()
.evidence[0]
.reference = Some("nobody2022".into());
let issues = c.validate();
reports(
&issues,
"source cites `nobody2020`, which is not in references",
);
reports(&issues, "textbook cites `nobody2021`");
reports(&issues, "evidence cites `nobody2022`");
}
#[test]
fn e2106_a_brace_reference_that_does_not_resolve() {
let mut c = course("e2106");
c.terms.get_mut("term-gap").unwrap().definition = "See {term-missing}.".into();
reports(
&c.validate(),
"term `term-gap`: definition refers to `{term-missing}`, which is not a declared term",
);
}
#[test]
fn e2106_an_old_reading_keeps_its_2_0_behaviour_for_targets() {
// A 2.0 reading with a mistyped `{t-...}` validated and rendered the braces
// literally. It still validates (the linter reports it); a new kind of
// reference in the same field is checked.
let mut c = course("e2106-legacy");
let reading = &mut c.lectures.get_mut("l09").unwrap().readings[0];
reading.focus = Some("See {t-typo} and {val-typo}.".into());
let issues = c.validate();
assert!(!issues.iter().any(|i| i.contains("t-typo")), "{issues:#?}");
reports(&issues, "reading 1 focus refers to `{val-typo}`");
}
#[test]
fn e2106_a_new_field_checks_every_kind() {
let mut c = course("e2106-new");
c.learning_objectives
.get_mut("lo-alignment-scoring")
.unwrap()
.description = Some("Builds on {t-typo}.".into());
reports(
&c.validate(),
"objective `lo-alignment-scoring`: description refers to `{t-typo}`",
);
}
#[test]
fn e2107_a_pattern_that_does_not_compile() {
let mut c = course("e2107");
c.values
.get_mut("val-per-decade")
.unwrap()
.conflicts
.push("5.7(".into());
c.policy.target_text_forbid.push("[unclosed".into());
let issues = c.validate();
reports(
&issues,
"value `val-per-decade`: conflicts pattern `5.7(` does not compile",
);
reports(
&issues,
"policy.target_text_forbid: pattern `[unclosed` does not compile",
);
}
#[test]
fn e2108_an_option_names_an_undeclared_misconception() {
let root = v21_course("e2108");
edit(
&root,
"banks/alignment.yaml",
"misconception_id: mis-only-matches-count",
"misconception_id: mis-nope",
);
let issues = Catalog::load(&root).unwrap().validate().unwrap();
reports(
&issues,
"option o-matches-only: misconception_id `mis-nope` is not a declared misconception",
);
}
#[test]
fn messages_name_the_file_that_defined_the_entry() {
let mut c = course("attribution");
c.terms.get_mut("term-gap").unwrap().definition = "See {term-missing}.".into();
let issues = c.validate();
assert!(
issues
.iter()
.any(|i| i.starts_with("objectives/lo-alignment-scoring.yaml: term `term-gap`")),
"{issues:#?}"
);
}
#[test]
fn policy_levels_and_blueprints_are_keyed_by_level() {
let mut c = course("policy");
let level = c.policy.levels["1"].clone();
c.policy.levels.insert("6".into(), level);
c.policy
.blueprints
.get_mut("module")
.unwrap()
.level_counts
.insert("7".into(), 1);
let issues = c.validate();
reports(&issues, "policy.levels: `6` is not a level");
reports(&issues, "policy.blueprints: `module` counts items at `7`");
}