[core] Canon, claims, hooks: strict parser, fail-closed claim checker, hook templates
This commit is contained in:
@@ -26,23 +26,29 @@ fn canon() -> Canon {
|
||||
c.insert(CanonFile::adhoc(
|
||||
"release",
|
||||
"Tabula",
|
||||
vec![(1, "Tabula 1.2 is out.".into()), (2, "Search is faster.".into())],
|
||||
vec![
|
||||
(1, "Tabula 1.2 is out.".into()),
|
||||
(2, "Search is faster.".into()),
|
||||
],
|
||||
))
|
||||
.unwrap();
|
||||
c
|
||||
}
|
||||
|
||||
fn r(app: &str, fact: u32) -> Ref {
|
||||
Ref { app: app.into(), fact }
|
||||
Ref {
|
||||
app: app.into(),
|
||||
fact,
|
||||
}
|
||||
}
|
||||
|
||||
fn unsourced(text: &str) -> bool {
|
||||
let rep = claims::check(text, &canon());
|
||||
!rep.ok() && rep.problems.iter().all(|p| matches!(p, Problem::Unsourced { .. }))
|
||||
}
|
||||
|
||||
fn passes(text: &str) -> bool {
|
||||
claims::check(text, &canon()).ok()
|
||||
!rep.ok()
|
||||
&& rep
|
||||
.problems
|
||||
.iter()
|
||||
.all(|p| matches!(p, Problem::Unsourced { .. }))
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -54,7 +60,10 @@ fn sourced_sentences_pass_and_refs_are_collected() {
|
||||
&c,
|
||||
);
|
||||
assert!(rep.ok(), "{:?}", rep.problems);
|
||||
assert_eq!(rep.refs, vec![r("release", 1), r("tabula", 1), r("tabula", 7)]);
|
||||
assert_eq!(
|
||||
rep.refs,
|
||||
vec![r("release", 1), r("tabula", 1), r("tabula", 7)]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -136,22 +145,41 @@ fn a_plain_phrase_with_anything_extra_needs_a_ref() {
|
||||
#[test]
|
||||
fn every_sentence_is_checked_separately() {
|
||||
let c = canon();
|
||||
let rep = claims::check("Tabula keeps notes local [tabula#F1]. It runs on Linux. Try it today.", &c);
|
||||
assert_eq!(rep.problems, vec![Problem::Unsourced { sentence: "It runs on Linux.".into() }]);
|
||||
let rep = claims::check(
|
||||
"Tabula keeps notes local [tabula#F1]. It runs on Linux. Try it today.",
|
||||
&c,
|
||||
);
|
||||
assert_eq!(
|
||||
rep.problems,
|
||||
vec![Problem::Unsourced {
|
||||
sentence: "It runs on Linux.".into()
|
||||
}]
|
||||
);
|
||||
// Lines are sentences too.
|
||||
let rep = claims::check("Tabula 1.2 [release#F1]\nFaster search", &c);
|
||||
assert_eq!(rep.problems, vec![Problem::Unsourced { sentence: "Faster search".into() }]);
|
||||
assert_eq!(
|
||||
rep.problems,
|
||||
vec![Problem::Unsourced {
|
||||
sentence: "Faster search".into()
|
||||
}]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decimals_and_urls_do_not_split_sentences() {
|
||||
let c = canon();
|
||||
assert_eq!(claims::sentences("Tabula 1.2 is out. Try it."), vec!["Tabula 1.2 is out.", "Try it."]);
|
||||
assert_eq!(
|
||||
claims::sentences("Tabula 1.2 is out. Try it."),
|
||||
vec!["Tabula 1.2 is out.", "Try it."]
|
||||
);
|
||||
assert_eq!(
|
||||
claims::sentences("See https://aula.marek-novak.cz/tabula/?a=1. Thanks!"),
|
||||
vec!["See https://aula.marek-novak.cz/tabula/?a=1.", "Thanks!"]
|
||||
);
|
||||
assert_eq!(claims::sentences("A. [tabula#F1] B"), vec!["A. [tabula#F1]", "B"]);
|
||||
assert_eq!(
|
||||
claims::sentences("A. [tabula#F1] B"),
|
||||
vec!["A. [tabula#F1]", "B"]
|
||||
);
|
||||
assert!(claims::check("Tabula 1.2 is out. [release#F1]", &c).ok());
|
||||
}
|
||||
|
||||
@@ -159,17 +187,32 @@ fn decimals_and_urls_do_not_split_sentences() {
|
||||
fn bad_refs_are_rejected() {
|
||||
let c = canon();
|
||||
let rep = claims::check("Tabula keeps notes local. [tabula#F99]", &c);
|
||||
assert_eq!(rep.problems, vec![Problem::MissingFact { r: r("tabula", 99) }]);
|
||||
assert_eq!(
|
||||
rep.problems,
|
||||
vec![Problem::MissingFact { r: r("tabula", 99) }]
|
||||
);
|
||||
let rep = claims::check("Notes, done. [notapp#F1]", &c);
|
||||
assert_eq!(rep.problems, vec![Problem::UnknownApp { r: r("notapp", 1) }]);
|
||||
assert_eq!(
|
||||
rep.problems,
|
||||
vec![Problem::UnknownApp { r: r("notapp", 1) }]
|
||||
);
|
||||
let rep = claims::check("It syncs everywhere. [tabula#F2]", &c);
|
||||
assert_eq!(rep.problems, vec![Problem::Unconfirmed { r: r("tabula", 2) }]);
|
||||
assert_eq!(
|
||||
rep.problems,
|
||||
vec![Problem::Unconfirmed { r: r("tabula", 2) }]
|
||||
);
|
||||
// A bad ref fails even on a plain sentence.
|
||||
let rep = claims::check("Try it today. [tabula#F99]", &c);
|
||||
assert_eq!(rep.problems, vec![Problem::MissingFact { r: r("tabula", 99) }]);
|
||||
assert_eq!(
|
||||
rep.problems,
|
||||
vec![Problem::MissingFact { r: r("tabula", 99) }]
|
||||
);
|
||||
// One good ref does not excuse a bad one.
|
||||
let rep = claims::check("Local notes. [tabula#F1][tabula#F2]", &c);
|
||||
assert_eq!(rep.problems, vec![Problem::Unconfirmed { r: r("tabula", 2) }]);
|
||||
assert_eq!(
|
||||
rep.problems,
|
||||
vec![Problem::Unconfirmed { r: r("tabula", 2) }]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -194,7 +237,9 @@ fn malformed_refs_are_rejected() {
|
||||
let text = format!("Tabula keeps notes on your own computer. {bad}");
|
||||
let rep = claims::check(&text, &c);
|
||||
assert!(
|
||||
rep.problems.iter().any(|p| matches!(p, Problem::MalformedRef { .. })),
|
||||
rep.problems
|
||||
.iter()
|
||||
.any(|p| matches!(p, Problem::MalformedRef { .. })),
|
||||
"{bad:?}: {:?}",
|
||||
rep.problems
|
||||
);
|
||||
@@ -208,26 +253,53 @@ fn urls_must_come_from_the_canon() {
|
||||
assert!(claims::check("Get it here: https://aula.marek-novak.cz/tabula/", &c).ok());
|
||||
assert!(claims::check("Get it here: https://aula.marek-novak.cz/tabula", &c).ok());
|
||||
assert!(claims::check("Read more: https://aula.marek-novak.cz/tabula/windows.", &c).ok());
|
||||
assert!(claims::check("Read more: (https://aula.marek-novak.cz/tabula/windows)", &c).ok());
|
||||
assert!(
|
||||
claims::check(
|
||||
"Read more: (https://aula.marek-novak.cz/tabula/windows)",
|
||||
&c
|
||||
)
|
||||
.ok()
|
||||
);
|
||||
for (text, url) in [
|
||||
("Get it here: https://evil.example/tabula", "https://evil.example/tabula"),
|
||||
("Get it here: http://aula.marek-novak.cz/tabula/", "http://aula.marek-novak.cz/tabula/"),
|
||||
("Get it here: https://aula.marek-novak.cz/tabula/x", "https://aula.marek-novak.cz/tabula/x"),
|
||||
(
|
||||
"Get it here: https://evil.example/tabula",
|
||||
"https://evil.example/tabula",
|
||||
),
|
||||
(
|
||||
"Get it here: http://aula.marek-novak.cz/tabula/",
|
||||
"http://aula.marek-novak.cz/tabula/",
|
||||
),
|
||||
(
|
||||
"Get it here: https://aula.marek-novak.cz/tabula/x",
|
||||
"https://aula.marek-novak.cz/tabula/x",
|
||||
),
|
||||
("Get it here: www.evil.example", "www.evil.example"),
|
||||
("Get it here: evil.com", "evil.com"),
|
||||
("Get it here: evil.example/download", "evil.example/download"),
|
||||
(
|
||||
"Get it here: evil.example/download",
|
||||
"evil.example/download",
|
||||
),
|
||||
("Get it here: ftp://evil.example", "ftp://evil.example"),
|
||||
] {
|
||||
let rep = claims::check(text, &c);
|
||||
assert!(
|
||||
rep.problems.contains(&Problem::UrlNotInCanon { url: url.into() }),
|
||||
rep.problems
|
||||
.contains(&Problem::UrlNotInCanon { url: url.into() }),
|
||||
"{text:?}: {:?}",
|
||||
rep.problems
|
||||
);
|
||||
}
|
||||
// A URL in a sourced sentence is still checked.
|
||||
let rep = claims::check("It runs on Windows, see https://evil.example/ [tabula#F3]", &c);
|
||||
assert_eq!(rep.problems, vec![Problem::UrlNotInCanon { url: "https://evil.example/".into() }]);
|
||||
let rep = claims::check(
|
||||
"It runs on Windows, see https://evil.example/ [tabula#F3]",
|
||||
&c,
|
||||
);
|
||||
assert_eq!(
|
||||
rep.problems,
|
||||
vec![Problem::UrlNotInCanon {
|
||||
url: "https://evil.example/".into()
|
||||
}]
|
||||
);
|
||||
// A bare form of an allowed URL is fine.
|
||||
assert!(claims::check("Get it here: aula.marek-novak.cz/tabula/", &c).ok());
|
||||
}
|
||||
@@ -236,9 +308,15 @@ fn urls_must_come_from_the_canon() {
|
||||
fn hidden_characters_are_rejected() {
|
||||
let c = canon();
|
||||
let rep = claims::check("Try it today.\u{202e}", &c);
|
||||
assert_eq!(rep.problems, vec![Problem::HiddenCharacter { codepoint: 0x202e }]);
|
||||
assert_eq!(
|
||||
rep.problems,
|
||||
vec![Problem::HiddenCharacter { codepoint: 0x202e }]
|
||||
);
|
||||
let rep = claims::check("Try it\0 today.", &c);
|
||||
assert!(rep.problems.contains(&Problem::HiddenCharacter { codepoint: 0 }));
|
||||
assert!(
|
||||
rep.problems
|
||||
.contains(&Problem::HiddenCharacter { codepoint: 0 })
|
||||
);
|
||||
// CRLF and tabs are ordinary whitespace.
|
||||
assert!(claims::check("Try it today.\r\nThanks!\t", &c).ok());
|
||||
}
|
||||
@@ -251,12 +329,37 @@ fn reasons_are_plain_english() {
|
||||
&c,
|
||||
);
|
||||
let reasons: Vec<String> = rep.problems.iter().map(Problem::reason).collect();
|
||||
assert!(reasons.iter().any(|s| s.contains("It runs on Linux.") && s.contains("no source")), "{reasons:?}");
|
||||
assert!(reasons.iter().any(|s| s.contains("tabula#F99") && s.contains("does not exist")), "{reasons:?}");
|
||||
assert!(reasons.iter().any(|s| s.contains("unknown app") && s.contains("x")), "{reasons:?}");
|
||||
assert!(reasons.iter().any(|s| s.contains("unconfirmed fact") && s.contains("tabula#F2")), "{reasons:?}");
|
||||
assert!(
|
||||
reasons
|
||||
.iter()
|
||||
.any(|s| s.contains("It runs on Linux.") && s.contains("no source")),
|
||||
"{reasons:?}"
|
||||
);
|
||||
assert!(
|
||||
reasons
|
||||
.iter()
|
||||
.any(|s| s.contains("tabula#F99") && s.contains("does not exist")),
|
||||
"{reasons:?}"
|
||||
);
|
||||
assert!(
|
||||
reasons
|
||||
.iter()
|
||||
.any(|s| s.contains("unknown app") && s.contains("x")),
|
||||
"{reasons:?}"
|
||||
);
|
||||
assert!(
|
||||
reasons
|
||||
.iter()
|
||||
.any(|s| s.contains("unconfirmed fact") && s.contains("tabula#F2")),
|
||||
"{reasons:?}"
|
||||
);
|
||||
assert!(reasons.iter().any(|s| s.contains("[F3]")), "{reasons:?}");
|
||||
assert!(reasons.iter().any(|s| s.contains("https://evil.example/") && s.contains("not in the canon")), "{reasons:?}");
|
||||
assert!(
|
||||
reasons
|
||||
.iter()
|
||||
.any(|s| s.contains("https://evil.example/") && s.contains("not in the canon")),
|
||||
"{reasons:?}"
|
||||
);
|
||||
assert_eq!(
|
||||
Problem::HiddenCharacter { codepoint: 0x202e }.reason(),
|
||||
"Hidden or direction-changing character U+202E."
|
||||
@@ -267,21 +370,38 @@ fn reasons_are_plain_english() {
|
||||
fn problems_are_not_repeated() {
|
||||
let c = canon();
|
||||
let rep = claims::check("A. [tabula#F99] B. [tabula#F99]", &c);
|
||||
assert_eq!(rep.problems, vec![Problem::MissingFact { r: r("tabula", 99) }]);
|
||||
assert_eq!(
|
||||
rep.problems,
|
||||
vec![Problem::MissingFact { r: r("tabula", 99) }]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn strip_refs_for_display() {
|
||||
assert_eq!(
|
||||
claims::strip_refs("Tabula keeps notes local [tabula#F1]. One click.[tabula#F7][tabula#F1] Done [release#F1]"),
|
||||
claims::strip_refs(
|
||||
"Tabula keeps notes local [tabula#F1]. One click.[tabula#F7][tabula#F1] Done [release#F1]"
|
||||
),
|
||||
"Tabula keeps notes local. One click. Done"
|
||||
);
|
||||
assert_eq!(claims::strip_refs("Tabula [tabula#F1] keeps notes."), "Tabula keeps notes.");
|
||||
assert_eq!(
|
||||
claims::strip_refs("Tabula [tabula#F1] keeps notes."),
|
||||
"Tabula keeps notes."
|
||||
);
|
||||
assert_eq!(claims::strip_refs("A.[tabula#F1]B"), "A. B");
|
||||
assert_eq!(claims::strip_refs("Line one [tabula#F1]\nLine two"), "Line one\nLine two");
|
||||
assert_eq!(claims::strip_refs("Keep [beta] and [Tabula#F1]."), "Keep [beta] and [Tabula#F1].");
|
||||
assert_eq!(
|
||||
claims::strip_refs("Line one [tabula#F1]\nLine two"),
|
||||
"Line one\nLine two"
|
||||
);
|
||||
assert_eq!(
|
||||
claims::strip_refs("Keep [beta] and [Tabula#F1]."),
|
||||
"Keep [beta] and [Tabula#F1]."
|
||||
);
|
||||
assert_eq!(claims::strip_refs(""), "");
|
||||
assert_eq!(claims::strip_refs("Žluťoučký [tabula#F1]kůň"), "Žluťoučký kůň");
|
||||
assert_eq!(
|
||||
claims::strip_refs("Žluťoučký [tabula#F1]kůň"),
|
||||
"Žluťoučký kůň"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -292,3 +412,23 @@ fn stripped_text_of_a_passing_text_has_no_refs() {
|
||||
let stripped = claims::strip_refs(text);
|
||||
assert!(claims::check(&stripped, &c).refs.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn states_fact_is_fail_closed() {
|
||||
let c = canon();
|
||||
assert!(!claims::states_fact("Try it today.", &c));
|
||||
assert!(!claims::states_fact("Here's what's new [tabula#F1].", &c));
|
||||
assert!(claims::states_fact("Tabula is great.", &c));
|
||||
assert!(claims::states_fact("It is what it is.", &c));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_huge_text_is_checked_without_trouble() {
|
||||
let c = canon();
|
||||
let text = "Local notes [tabula#F1]. ".repeat(2000);
|
||||
let rep = claims::check(&text, &c);
|
||||
assert!(rep.ok());
|
||||
assert_eq!(rep.refs, vec![r("tabula", 1)]);
|
||||
let line = "a".repeat(100_000);
|
||||
assert!(!claims::check(&line, &c).ok());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user