// SPDX-License-Identifier: Apache-2.0 // Copyright Scienting // Source: https://git.scient.ing/education/coursebank //! The lint rules 2.1 adds, one test each. //! //! Each test breaks the example course the way the prototype's self-test does //! (`docs/design/2.1/prototype/check21.py --selftest`) and checks that the rule //! fires. `docs/design/2.1/CODES.md` maps the specification's codes to these //! rule codes. mod common; use std::path::PathBuf; use std::process::Command; use common::v21_course; use coursebank::catalog::{Catalog, Severity}; use coursebank::course::fragment::Section; use coursebank::course::glossary::{Term, TermKind}; use coursebank::lint::{self, Finding, Rule, Thresholds}; /// The example course, loaded. fn catalog(tag: &str) -> Catalog { Catalog::load(&v21_course(tag)).expect("the example loads") } /// The glossary findings for a rule. fn fired(catalog: &Catalog, rule: Rule) -> Vec { lint::lint_glossary(catalog) .into_iter() .filter(|f| f.rule == rule) .collect() } /// Asserts a rule fires once, on `subject`, with `needle` in its message. fn fires(catalog: &Catalog, rule: Rule, subject: &str, needle: &str) { let found = fired(catalog, rule); assert!( found .iter() .any(|f| f.subject == subject && f.message.contains(needle)), "{} did not fire on {subject} with {needle:?}: {found:#?}", rule.code() ); } /// The example item, for the rules that read items. fn item(catalog: &mut Catalog) -> &mut coursebank::item::Item { &mut catalog.entries[0].item } #[test] fn the_example_draws_exactly_the_two_expected_glossary_findings() { let findings = lint::lint_glossary(&catalog("baseline")); let summary: Vec<(String, &str)> = findings .iter() .map(|f| (f.subject.clone(), f.rule.code())) .collect(); assert_eq!( summary, vec![ ("val-per-decade".to_string(), "glossary-unused"), ("glossary".to_string(), "glossary-proposed"), ], "{findings:#?}" ); let proposed = &findings[1]; assert_eq!(proposed.severity, Severity::Note); assert!(proposed.message.starts_with("2 proposed entries")); } #[test] fn w2101_an_entry_nothing_needs() { let mut c = catalog("w2101"); c.course.terms.insert( "term-unused".into(), Term { term: "unused".into(), definition: "Not needed.".into(), kind: TermKind::Standard, textbook: Vec::new(), variants: Vec::new(), symbol: None, units: None, exam_wording: None, status: None, tags: Vec::new(), notes_private: None, }, ); fires( &c, Rule::GlossaryUnused, "term-unused", "no target lists this term", ); } #[test] fn w2102_a_renamed_term_with_no_textbook_name() { let mut c = catalog("w2102"); c.course.terms.get_mut("term-gap").unwrap().kind = TermKind::Renamed; fires(&c, Rule::NoTextbookName, "term-gap", "a renamed term"); } #[test] fn w2103_a_coined_term_with_no_exam_wording() { let mut c = catalog("w2103"); c.course.terms.get_mut("term-decade").unwrap().exam_wording = None; fires(&c, Rule::NoExamWording, "term-decade", "exam_wording"); } #[test] fn w2104_a_definition_that_uses_its_term() { let mut c = catalog("w2104"); c.course.terms.get_mut("term-gap").unwrap().definition = "A gap is a dash.".into(); fires(&c, Rule::CircularDefinition, "term-gap", "uses 'gap'"); } #[test] fn w2105_a_coined_term_in_an_item() { let mut c = catalog("w2105"); item(&mut c).stem.push_str(" Think in decades."); fires( &c, Rule::CoinedTermInItem, "q-align-which-scores-higher", "stem uses the coined term 'decades' (term-decade); write 'ten-fold'", ); } #[test] fn w2106_a_value_with_no_source() { let mut c = catalog("w2106"); c.course .values .get_mut("val-alignment-count-100") .unwrap() .source = None; fires( &c, Rule::UnsourcedValue, "val-alignment-count-100", "no `source`", ); } #[test] fn w2107_a_value_variant_in_student_prose_and_in_an_item() { let mut c = catalog("w2107"); c.course .learning_targets .get_mut("t-align-fill-matrix") .unwrap() .explanation = Some("It costs 5.7 kJ.".into()); item(&mut c).options[0].text = "About 5.8 kJ per decade.".into(); fires( &c, Rule::ValueConflict, "t-align-fill-matrix", "explanation writes '5.7 kJ', a variant of val-per-decade", ); fires( &c, Rule::ValueConflict, "q-align-which-scores-higher", "state it as 'about 6 kJ/mol'", ); } #[test] fn w2108_target_text_the_policy_forbids() { let mut c = catalog("w2108"); c.course .learning_targets .get_mut("t-align-fill-matrix") .unwrap() .text = "Calculate the score.".into(); fires( &c, Rule::ForbiddenTargetWording, "t-align-fill-matrix", "the text says 'Calculat'", ); } #[test] fn w2109_an_explanation_using_a_term_its_target_does_not_list() { let mut c = catalog("w2109"); c.course .learning_targets .get_mut("t-align-gap-penalty-effect") .unwrap() .explanation = Some("Unlike a {term-local-alignment}, the path changes.".into()); fires( &c, Rule::UnlistedTerm, "t-align-gap-penalty-effect", "{term-local-alignment}", ); } #[test] fn w2109_a_term_inherited_from_a_prerequisite_target_is_listed() { // t-align-gap-penalty-effect has t-align-fill-matrix as a prerequisite, // which lists the traceback. let mut c = catalog("w2109-inherited"); c.course .learning_targets .get_mut("t-align-gap-penalty-effect") .unwrap() .explanation = Some("Follow the {term-traceback}.".into()); assert!(fired(&c, Rule::UnlistedTerm).is_empty()); } #[test] fn w2110_an_item_prints_a_recall_value() { let mut c = catalog("w2110"); item(&mut c).stem = "Given about 6 kJ/mol per ten-fold ratio, which is larger?".into(); fires( &c, Rule::RecallValuePrinted, "q-align-which-scores-higher", "stem prints 'about 6 kJ/mol' (val-per-decade)", ); } #[test] fn w2111_a_deep_target_on_an_equation_with_no_implications() { let mut c = catalog("w2111"); c.course .equations .get_mut("eq-global-recurrence") .unwrap() .implications .clear(); fires( &c, Rule::EquationWithoutImplications, "t-align-fill-matrix", "turns on eq-global-recurrence", ); } #[test] fn i2112_an_entry_defined_away_from_the_objective_that_first_needs_it() { let mut c = catalog("i2112"); c.course.origins.insert( (Section::Terms, "term-gap".into()), PathBuf::from("objectives/lo-alignment-recurrence.yaml"), ); let found = fired(&c, Rule::DefinedElsewhere); assert_eq!(found.len(), 1, "{found:#?}"); assert_eq!(found[0].subject, "term-gap"); assert_eq!(found[0].severity, Severity::Note); assert!( found[0] .message .contains("first needed by t-align-matrix-terms in l09"), "{found:#?}" ); } #[test] fn i2113_counts_proposed_entries() { let mut c = catalog("i2113"); c.course.terms.get_mut("term-decade").unwrap().status = None; fires( &c, Rule::ProposedEntries, "glossary", "1 proposed entry waiting for the instructor: val-per-decade", ); } #[test] fn an_unresolved_target_reference_in_an_old_reading_field_is_a_warning() { let mut c = catalog("legacy-ref"); c.course.lectures.get_mut("l09").unwrap().readings[0].focus = Some("See {t-typo}.".into()); fires( &c, Rule::UnresolvedReference, "l09", "reading 1 focus refers to {t-typo}", ); } #[test] fn a_registry_misconception_designs_a_distractor_and_gives_it_feedback() { // Approved items are held to the completeness rules. The option that names // a registry misconception and nothing else passes both of them. let mut c = catalog("misconception-id"); item(&mut c).status = coursebank::taxonomy::Status::InReview; let findings = lint::lint_catalog(&c, &Thresholds::default()); let about_option = |rule: Rule| { findings .iter() .any(|f| f.rule == rule && f.message.contains("o-matches-only")) }; assert!(!about_option(Rule::UndesignedDistractor), "{findings:#?}"); assert!(!about_option(Rule::NoStudentFeedback), "{findings:#?}"); } #[test] fn every_glossary_rule_has_a_glossary_code() { let glossary: Vec = Rule::ALL .into_iter() .filter(|r| r.family() == "glossary") .collect(); assert_eq!(glossary.len(), 14); assert!(glossary.iter().all(|r| r.code().starts_with("glossary-"))); } /// Runs the binary on a course. fn coursebank(root: &std::path::Path, args: &[&str]) -> std::process::Output { Command::new(env!("CARGO_BIN_EXE_coursebank")) .arg("-C") .arg(root) .args(args) .output() .expect("the binary runs") } #[test] fn notes_are_printed_but_do_not_fail_lint() { let root = v21_course("cli-notes"); let only_notes = coursebank(&root, &["lint", "--only", "glossary-proposed"]); let stdout = String::from_utf8_lossy(&only_notes.stdout); assert!(stdout.contains("[note] glossary-proposed"), "{stdout}"); assert!(only_notes.status.success(), "{stdout}"); let with_a_warning = coursebank( &root, &["lint", "--only", "glossary-proposed,glossary-unused"], ); assert!(!with_a_warning.status.success()); }