From 960cf6cbd3d209533cda3d915a26a4405b5fd999 Mon Sep 17 00:00:00 2001 From: Alex Maldonado Date: Mon, 16 Nov 2026 11:58:53 -0500 Subject: [PATCH] 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 --- Cargo.toml | 1 + src/model/bank.rs | 17 ++ src/model/course.rs | 1 + src/model/course/glossary.rs | 569 +++++++++++++++++++++++++++++++++++ tests/common/mod.rs | 54 ++++ tests/schema_2_1.rs | 40 +-- tests/validate_2_1.rs | 269 +++++++++++++++++ 7 files changed, 915 insertions(+), 36 deletions(-) create mode 100644 tests/common/mod.rs create mode 100644 tests/validate_2_1.rs diff --git a/Cargo.toml b/Cargo.toml index e3ba4ff..028e69b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,6 +33,7 @@ pbkdf2 = { version = "0.12", default-features = false, features = ["hmac"]} sha2 = { version = "0.10"} base64 = { version = "0.22"} getrandom = { version = "0.2"} +regex = { version = "1.11", default-features = false, features = ["std", "unicode-perl", "unicode-case"] } [profile.release] opt-level = 3 diff --git a/src/model/bank.rs b/src/model/bank.rs index b2345a4..bd56feb 100644 --- a/src/model/bank.rs +++ b/src/model/bank.rs @@ -440,6 +440,23 @@ fn validate_item( o.id )); } + // A registry misconception named by id has to exist, or the report + // that groups misses by it would group them under nothing. + if let Some(id) = &o.misconception_id { + let declared = match course { + Some(c) => c.misconceptions.contains_key(id), + None => crate::course::glossary::is_prefixed_id( + id, + crate::course::glossary::MISCONCEPTION_PREFIX, + ), + }; + if !declared { + issues.push(format!( + "option {}: misconception_id `{id}` is not a declared misconception", + o.id + )); + } + } } if let Some(n) = expected_options { if it.options.len() != n && !it.options.is_empty() { diff --git a/src/model/course.rs b/src/model/course.rs index 802a2ea..1dd1cd2 100644 --- a/src/model/course.rs +++ b/src/model/course.rs @@ -1628,6 +1628,7 @@ impl CourseFile { } issues.extend(self.prerequisite_cycles()); + issues.extend(self.glossary_problems()); issues } diff --git a/src/model/course/glossary.rs b/src/model/course/glossary.rs index 6a8619e..3f8bb2a 100644 --- a/src/model/course/glossary.rs +++ b/src/model/course/glossary.rs @@ -606,6 +606,552 @@ pub fn brace_refs(prose: &str) -> Vec<(std::ops::Range, &str)> { out } +/// Compiles one of the course's regular expressions. +/// +/// The syntax is the `regex` crate's, which matches Python's `re` for +/// everything the patterns in a course need (classes, `\b`, `\s`, escapes, +/// quantifiers) but has no look-around and no backreferences. +/// +/// # Arguments +/// +/// * `pattern` - the pattern as written in YAML. +/// * `ignore_case` - whether to match case-insensitively. +/// +/// # Returns +/// +/// The compiled pattern. +/// +/// # Errors +/// +/// The parser's own one-line reason when the pattern does not compile. +pub fn compile(pattern: &str, ignore_case: bool) -> std::result::Result { + regex::RegexBuilder::new(pattern) + .case_insensitive(ignore_case) + .build() + .map_err(|e| { + let text = e.to_string(); + // The parser's message spans several lines with a caret under the + // offending character; its last line says what is wrong. + text.lines() + .rev() + .find_map(|l| l.trim().strip_prefix("error: ")) + .unwrap_or(text.trim()) + .to_string() + }) +} + +/// Whether a word or phrase appears in text as a whole word, ignoring case. +/// +/// # Arguments +/// +/// * `haystack` - the text to search. +/// * `needle` - the word or phrase. +/// +/// # Returns +/// +/// `true` when found with a non-alphanumeric character, or the end of the text, +/// on both sides. +pub fn contains_word(haystack: &str, needle: &str) -> bool { + let needle = needle.trim(); + if needle.is_empty() { + return false; + } + let pattern = format!(r"\b{}\b", regex::escape(needle)); + compile(&pattern, true).is_ok_and(|re| re.is_match(haystack)) +} + +/// One student-facing prose field, for the checks that read every one. +#[derive(Debug, Clone)] +pub struct Prose<'a> { + /// What owns it, as a message names it: ``target `t-x` ``. + pub owner: String, + /// The owning id. + pub id: &'a str, + /// The field, with its position for a list: `implications[2]`. + pub field: String, + /// The text. + pub text: &'a str, + /// Whether the field existed before 2.1. An unresolved `{lo-...}` or + /// `{t-...}` in one of these rendered literally under 2.0, so it is a lint + /// warning rather than an error; see `docs/design/2.1/CODES.md`. + pub legacy: bool, +} + +impl super::CourseFile { + /// Whether a brace reference names a declared entry of its registry. + /// + /// An objective or target reference resolves against either tier, as the + /// 2.0 placeholder rule did. + /// + /// # Arguments + /// + /// * `id` - the referenced id. + /// + /// # Returns + /// + /// `true` when declared. + pub fn resolves(&self, id: &str) -> bool { + match RefKind::of(id) { + Some(RefKind::Term) => self.terms.contains_key(id), + Some(RefKind::Equation) => self.equations.contains_key(id), + Some(RefKind::Value) => self.values.contains_key(id), + Some(RefKind::Misconception) => self.misconceptions.contains_key(id), + Some(RefKind::Objective | RefKind::Target) => { + self.learning_objectives.contains_key(id) || self.learning_targets.contains_key(id) + } + None => false, + } + } + + /// Every student-facing prose field in the course, in registry order. + /// + /// This is the list brace references are resolved in and `conflicts` + /// patterns are matched against: objective and target text, descriptions + /// and explanations, definitions, what an equation says and implies, + /// beliefs and corrections, and a lecture's description and reading notes. + /// + /// # Returns + /// + /// One entry per non-empty field. + pub fn student_prose(&self) -> Vec> { + /// Adds one field when it holds text. + fn push<'a>( + out: &mut Vec>, + (noun, id): (&str, &'a str), + field: String, + text: Option<&'a str>, + legacy: bool, + ) { + if let Some(text) = text.filter(|t| !t.trim().is_empty()) { + out.push(Prose { + owner: format!("{noun} `{id}`"), + id, + field, + text, + legacy, + }); + } + } + + let mut out = Vec::new(); + for (id, o) in &self.learning_objectives { + let at = ("objective", id.as_str()); + push(&mut out, at, "text".into(), Some(&o.text), true); + push( + &mut out, + at, + "description".into(), + o.description.as_deref(), + false, + ); + } + for (id, t) in &self.learning_targets { + let at = ("target", id.as_str()); + push(&mut out, at, "text".into(), Some(&t.text), true); + push( + &mut out, + at, + "explanation".into(), + t.explanation.as_deref(), + false, + ); + } + for (id, t) in &self.terms { + push( + &mut out, + ("term", id), + "definition".into(), + Some(&t.definition), + false, + ); + } + for (id, e) in &self.equations { + let at = ("equation", id.as_str()); + push(&mut out, at, "says".into(), Some(&e.says), false); + for (k, imp) in e.implications.iter().enumerate() { + push( + &mut out, + at, + format!("implications[{k}]"), + Some(&imp.statement), + false, + ); + } + for (k, a) in e.assumes.iter().enumerate() { + push(&mut out, at, format!("assumes[{k}]"), Some(a), false); + } + for (k, a) in e.fails_when.iter().enumerate() { + push(&mut out, at, format!("fails_when[{k}]"), Some(a), false); + } + } + for (id, m) in &self.misconceptions { + let at = ("misconception", id.as_str()); + push(&mut out, at, "belief".into(), Some(&m.belief), false); + push( + &mut out, + at, + "correction".into(), + Some(&m.correction), + false, + ); + } + for (id, lecture) in &self.lectures { + let at = ("lecture", id.as_str()); + push( + &mut out, + at, + "description".into(), + lecture.description.as_deref(), + false, + ); + for (k, r) in lecture.readings.iter().enumerate() { + for (name, text) in [ + ("summary", &r.summary), + ("focus", &r.focus), + ("skip", &r.skip), + ] { + let field = format!("reading {} {name}", k + 1); + push(&mut out, at, field, text.as_deref(), true); + } + } + } + out + } + + /// The validation messages for everything 2.1 adds. + /// + /// Each message names the id it is about first, in backticks, so + /// [`CourseFile::validate`](super::CourseFile::validate) can prefix it + /// with the file that defined the id. `docs/design/2.1/CODES.md` maps each + /// message to the specification's error code. + /// + /// # Returns + /// + /// One message per problem. + pub(super) fn glossary_problems(&self) -> Vec { + let mut issues = Vec::new(); + issues.extend(self.registry_id_problems()); + issues.extend(self.entry_problems()); + issues.extend(self.target_link_problems()); + issues.extend(self.brace_problems()); + issues.extend(self.pattern_problems()); + issues.extend(self.policy_problems()); + issues + } + + /// Ids that lack their registry's prefix, and ids declared in two + /// registries (E2101, and the `propertyNames` patterns of the schemas). + fn registry_id_problems(&self) -> Vec { + let mut issues = Vec::new(); + let prefixed: [(&str, &str, Vec<&String>); 4] = [ + ("term", TERM_PREFIX, self.terms.keys().collect()), + ("equation", EQUATION_PREFIX, self.equations.keys().collect()), + ("value", VALUE_PREFIX, self.values.keys().collect()), + ( + "misconception", + MISCONCEPTION_PREFIX, + self.misconceptions.keys().collect(), + ), + ]; + for (noun, prefix, ids) in &prefixed { + for id in ids { + if !is_prefixed_id(id, prefix) { + issues.push(format!( + "{noun} `{id}`: a {noun} id is `{prefix}` followed by lowercase words \ + joined by hyphens, such as `{prefix}{}`", + super::slugify(id.trim_start_matches(prefix)) + )); + } + } + } + + // The objective/target pair is checked in `problems`, as it was in 2.0. + // Every other pair is checked here, so an id says which registry it + // belongs to and a brace reference cannot be ambiguous. + let registries: [(&str, Vec<&String>); 6] = [ + ( + "learning objective", + self.learning_objectives.keys().collect(), + ), + ("learning target", self.learning_targets.keys().collect()), + ("term", self.terms.keys().collect()), + ("equation", self.equations.keys().collect()), + ("value", self.values.keys().collect()), + ("misconception", self.misconceptions.keys().collect()), + ]; + for (i, (first, ids)) in registries.iter().enumerate() { + for (second, others) in registries.iter().skip(i + 1) { + if i == 0 && *second == "learning target" { + continue; + } + for id in ids.iter().filter(|id| others.contains(id)) { + issues.push(format!( + "`{id}` is declared as both a {first} and a {second}; an id is unique \ + across all registries" + )); + } + } + } + issues + } + + /// Fields within one entry: required text, equation symbols (E2103), + /// values (E2104), and citation keys (E2105). + fn entry_problems(&self) -> Vec { + let mut issues = Vec::new(); + let empty = |s: &str| s.trim().is_empty(); + let cite = |issues: &mut Vec, owner: String, field: &str, key: &str| { + if !self.references.contains_key(key) { + issues.push(format!( + "{owner}: {field} cites `{key}`, which is not in references" + )); + } + }; + + for (id, t) in &self.terms { + if empty(&t.term) { + issues.push(format!("term `{id}`: empty term")); + } + if empty(&t.definition) { + issues.push(format!("term `{id}`: empty definition")); + } + for (k, name) in t.textbook.iter().enumerate() { + if empty(&name.term) { + issues.push(format!("term `{id}`: textbook name {} is empty", k + 1)); + } + if let Some(key) = &name.reference { + cite(&mut issues, format!("term `{id}`"), "textbook", key); + } + } + } + + for (id, e) in &self.equations { + for (field, value) in [("name", &e.name), ("tex", &e.tex), ("says", &e.says)] { + if empty(value) { + issues.push(format!("equation `{id}`: empty {field}")); + } + } + for s in &e.symbols { + if empty(&s.symbol) { + issues.push(format!("equation `{id}`: a symbol has no `symbol`")); + } + match (&s.term, &s.meaning) { + (Some(term), _) if !self.terms.contains_key(term) => issues.push(format!( + "equation `{id}`: symbol `{}` names the term `{term}`, which is not \ + declared", + s.symbol + )), + (None, None) => issues.push(format!( + "equation `{id}`: symbol `{}` needs a `term` or a `meaning`", + s.symbol + )), + _ => {} + } + } + for imp in &e.implications { + if empty(&imp.statement) { + issues.push(format!( + "equation `{id}`: a {} implication has no statement", + imp.form.as_str() + )); + } + } + if let Some(source) = &e.source { + cite( + &mut issues, + format!("equation `{id}`"), + "source", + &source.reference, + ); + } + } + + for (id, v) in &self.values { + if empty(&v.name) { + issues.push(format!("value `{id}`: empty name")); + } + if empty(&v.say) { + issues.push(format!("value `{id}`: empty say")); + } + if v.value.is_none() && v.range.is_none() { + issues.push(format!("value `{id}`: give a `value` or a `range`")); + } + if let Some([low, high]) = v.range { + if low > high { + issues.push(format!( + "value `{id}`: range runs from {low} down to {high}; write the low end \ + first" + )); + } + } + if let Some(term) = &v.term { + if !self.terms.contains_key(term) { + issues.push(format!("value `{id}`: term `{term}` is not declared")); + } + } + if let Some(source) = &v.source { + cite( + &mut issues, + format!("value `{id}`"), + "source", + &source.reference, + ); + } + } + + for (id, m) in &self.misconceptions { + if empty(&m.belief) { + issues.push(format!("misconception `{id}`: empty belief")); + } + if empty(&m.correction) { + issues.push(format!("misconception `{id}`: empty correction")); + } + for evidence in &m.evidence { + if let Some(key) = &evidence.reference { + cite( + &mut issues, + format!("misconception `{id}`"), + "evidence", + key, + ); + } + } + } + + for (id, lecture) in &self.lectures { + for (k, material) in lecture.materials.iter().enumerate() { + if empty(&material.url) { + issues.push(format!("lecture `{id}`: material {} has no url", k + 1)); + } + } + for (k, reading) in lecture.readings.iter().enumerate() { + if reading.minutes == Some(0) { + issues.push(format!( + "lecture `{id}` reading {}: minutes must be at least 1", + k + 1 + )); + } + } + } + issues + } + + /// A target's `terms`, `equations`, `values`, and `misconceptions` each + /// name a declared entry of that registry (E2102). + fn target_link_problems(&self) -> Vec { + let mut issues = Vec::new(); + for (id, target) in &self.learning_targets { + let lists = [ + ("terms", &target.terms, RefKind::Term), + ("equations", &target.equations, RefKind::Equation), + ("values", &target.values, RefKind::Value), + ( + "misconceptions", + &target.misconceptions, + RefKind::Misconception, + ), + ]; + for (field, ids, kind) in lists { + for x in ids { + if RefKind::of(x) != Some(kind) || !self.resolves(x) { + issues.push(format!( + "target `{id}`: `{field}` names `{x}`, which is not a declared {}", + kind.noun() + )); + } + } + } + } + issues + } + + /// Brace references in student-facing prose resolve (E2106). + /// + /// An unresolved `{lo-...}` or `{t-...}` in a field 2.0 already had is + /// left to the linter, since 2.0 rendered it literally and accepted the + /// course; every other unresolved reference is an error. + fn brace_problems(&self) -> Vec { + let mut issues = Vec::new(); + for prose in self.student_prose() { + for (_, id) in brace_refs(prose.text) { + let kind = RefKind::of(id); + let legacy_kind = matches!(kind, Some(RefKind::Objective | RefKind::Target)); + if self.resolves(id) || (prose.legacy && legacy_kind) { + continue; + } + issues.push(format!( + "{}: {} refers to `{{{id}}}`, which is not a declared {}", + prose.owner, + prose.field, + kind.map(RefKind::noun).unwrap_or("entry") + )); + } + } + issues + } + + /// `conflicts` and `target_text_forbid` patterns compile (E2107). + fn pattern_problems(&self) -> Vec { + let mut issues = Vec::new(); + for (id, v) in &self.values { + for pattern in &v.conflicts { + if let Err(reason) = compile(pattern, false) { + issues.push(format!( + "value `{id}`: conflicts pattern `{pattern}` does not compile: {reason}" + )); + } + } + } + for pattern in &self.policy.target_text_forbid { + if let Err(reason) = compile(pattern, true) { + issues.push(format!( + "policy.target_text_forbid: pattern `{pattern}` does not compile: {reason}" + )); + } + } + issues + } + + /// The policy blocks 2.1 adds: level keys run 1 to 5, and durations are + /// positive. + fn policy_problems(&self) -> Vec { + let mut issues = Vec::new(); + let is_level = |k: &str| matches!(k, "1" | "2" | "3" | "4" | "5"); + for key in self.policy.levels.keys().filter(|k| !is_level(k)) { + issues.push(format!( + "policy.levels: `{key}` is not a level; levels run from 1 to 5" + )); + } + for (name, level) in &self.policy.levels { + if level.name.trim().is_empty() { + issues.push(format!("policy.levels: level {name} has an empty name")); + } + } + for (name, blueprint) in &self.policy.blueprints { + let keys = blueprint + .level_counts + .keys() + .chain(blueprint.bonus_counts.keys()); + for key in keys.filter(|k| !is_level(k)) { + issues.push(format!( + "policy.blueprints: `{name}` counts items at `{key}`, which is not a level" + )); + } + if blueprint.minutes.is_some_and(|m| m <= 0.0) { + issues.push(format!( + "policy.blueprints: `{name}` gives no time; minutes must be above 0" + )); + } + } + if let Some(conditions) = &self.policy.exam_conditions { + if conditions.seconds_per_item.is_some_and(|s| s <= 0.0) { + issues.push("policy.exam_conditions: seconds_per_item must be above 0".to_string()); + } + } + issues + } +} + #[cfg(test)] mod tests { use super::*; @@ -639,6 +1185,29 @@ mod tests { ); } + #[test] + fn the_example_patterns_compile_and_a_bad_one_says_why() { + for pattern in [r"5\.7\s*kJ", r"\bcomput", r"10\^\{?100\}?\s*alignments"] { + assert!(compile(pattern, false).is_ok(), "{pattern}"); + } + let reason = compile("5.7(", false).unwrap_err(); + assert!(reason.contains("unclosed group"), "{reason}"); + assert!(!reason.contains('\n')); + assert!( + compile(r"\bcalculat", true) + .unwrap() + .is_match("Calculate it.") + ); + } + + #[test] + fn whole_words_match_regardless_of_case() { + assert!(contains_word("Think in Decades.", "decades")); + assert!(!contains_word("decadent", "decade")); + assert!(contains_word("a gap penalty", "gap penalty")); + assert!(!contains_word("anything", "")); + } + #[test] fn a_reference_inside_latex_braces_is_still_found() { let ids: Vec<&str> = brace_refs("\\frac{a}{b} {val-x}") diff --git a/tests/common/mod.rs b/tests/common/mod.rs new file mode 100644 index 0000000..0e79640 --- /dev/null +++ b/tests/common/mod.rs @@ -0,0 +1,54 @@ +// SPDX-License-Identifier: Prosperity-3.0.0 +// Copyright Scienting +// Source: https://git.scient.ing/education/coursebank + +//! Fixtures shared by the 2.1 integration tests: the examples in +//! `docs/design/2.1/examples/`, copied into a scratch directory so a test may +//! change them. + +#![allow(dead_code)] + +use std::path::{Path, PathBuf}; + +/// `docs/design/2.1/`. +pub fn design() -> PathBuf { + PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("docs/design/2.1") +} + +/// A fresh, empty directory under the system temp dir. +pub fn scratch(tag: &str) -> PathBuf { + let dir = std::env::temp_dir().join(format!("coursebank-2-1-{tag}-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + dir +} + +/// Copies a directory tree. +pub fn copy_tree(from: &Path, to: &Path) { + std::fs::create_dir_all(to).unwrap(); + for entry in std::fs::read_dir(from).unwrap() { + let path = entry.unwrap().path(); + let target = to.join(path.file_name().unwrap()); + if path.is_dir() { + copy_tree(&path, &target); + } else { + std::fs::copy(&path, &target).unwrap(); + } + } +} + +/// The 2.1 example course, copied so a test may change it. +pub fn v21_course(tag: &str) -> PathBuf { + let root = scratch(tag); + copy_tree(&design().join("examples/v21"), &root); + let _ = std::fs::remove_file(root.join("lecture-page-preview.md")); + root +} + +/// Replaces the first occurrence of `from` with `to` in a file of a course. +pub fn edit(root: &Path, relative: &str, from: &str, to: &str) { + let path = root.join(relative); + let text = std::fs::read_to_string(&path).unwrap(); + assert!(text.contains(from), "{relative} does not contain {from:?}"); + std::fs::write(&path, text.replacen(from, to, 1)).unwrap(); +} diff --git a/tests/schema_2_1.rs b/tests/schema_2_1.rs index a875099..a085efa 100644 --- a/tests/schema_2_1.rs +++ b/tests/schema_2_1.rs @@ -9,40 +9,16 @@ //! `examples/v21/` uses every new field, and `schemas/` is the JSON Schema each //! generated file must equal. -use std::path::{Path, PathBuf}; +mod common; +use std::path::PathBuf; + +use common::{design, scratch, v21_course}; use coursebank::catalog::Catalog; use coursebank::course::CourseFile; use coursebank::jsonschema::{self, Kind}; use serde_json::Value; -/// `docs/design/2.1/`. -fn design() -> PathBuf { - PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("docs/design/2.1") -} - -/// A fresh, empty directory under the system temp dir. -fn scratch(tag: &str) -> PathBuf { - let dir = std::env::temp_dir().join(format!("coursebank-2-1-{tag}-{}", std::process::id())); - let _ = std::fs::remove_dir_all(&dir); - std::fs::create_dir_all(&dir).unwrap(); - dir -} - -/// Copies a directory tree. -fn copy_tree(from: &Path, to: &Path) { - std::fs::create_dir_all(to).unwrap(); - for entry in std::fs::read_dir(from).unwrap() { - let path = entry.unwrap().path(); - let target = to.join(path.file_name().unwrap()); - if path.is_dir() { - copy_tree(&path, &target); - } else { - std::fs::copy(&path, &target).unwrap(); - } - } -} - /// The JSON pointers at which two values differ. fn differences(path: &str, generated: &Value, target: &Value, out: &mut Vec) { match (generated, target) { @@ -166,14 +142,6 @@ fn the_2_0_fragments_load_and_validate_unchanged() { assert!(course.terms.is_empty() && course.equations.is_empty()); } -/// The 2.1 example course, copied so a test may change it. -pub fn v21_course(tag: &str) -> PathBuf { - let root = scratch(tag); - copy_tree(&design().join("examples/v21"), &root); - let _ = std::fs::remove_file(root.join("lecture-page-preview.md")); - root -} - #[test] fn the_2_1_example_loads_every_new_field() { let root = v21_course("v21-load"); diff --git a/tests/validate_2_1.rs b/tests/validate_2_1.rs new file mode 100644 index 0000000..6a2b884 --- /dev/null +++ b/tests/validate_2_1.rs @@ -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`"); +}