[core] Canon, claims, hooks: strict parser, fail-closed claim checker, hook templates
This commit is contained in:
@@ -49,13 +49,19 @@ fn parses_the_sample_file() {
|
||||
assert_eq!(f.id, "tabula");
|
||||
assert_eq!(f.front.name, "Tabula");
|
||||
assert_eq!(f.front.one_liner, "Notes that stay on your own computer.");
|
||||
assert_eq!(f.front.audience, "People who want notes without an account.");
|
||||
assert_eq!(
|
||||
f.front.audience,
|
||||
"People who want notes without an account."
|
||||
);
|
||||
assert_eq!(f.front.price, "(assumption) one-time purchase");
|
||||
assert!(canon::is_assumption_value(&f.front.price));
|
||||
assert!(!canon::is_assumption_value(&f.front.name));
|
||||
assert_eq!(f.front.clicks, "New note in 1 click.");
|
||||
assert_eq!(f.front.status, Status::Released);
|
||||
assert_eq!(f.front.aula.as_deref(), Some("https://aula.marek-novak.cz/tabula/"));
|
||||
assert_eq!(
|
||||
f.front.aula.as_deref(),
|
||||
Some("https://aula.marek-novak.cz/tabula/")
|
||||
);
|
||||
assert_eq!(f.front.site, None);
|
||||
assert_eq!(f.facts.len(), 3);
|
||||
let f1 = f.fact(1).unwrap();
|
||||
@@ -63,7 +69,10 @@ fn parses_the_sample_file() {
|
||||
assert!(!f1.assumption);
|
||||
assert_eq!(f1.line, 16);
|
||||
let f2 = f.fact(2).unwrap();
|
||||
assert_eq!(f2.text, "It syncs between your devices without a cloud account.");
|
||||
assert_eq!(
|
||||
f2.text,
|
||||
"It syncs between your devices without a cloud account."
|
||||
);
|
||||
assert!(f2.assumption);
|
||||
assert_eq!(f2.line, 17);
|
||||
assert!(f.fact(4).is_none());
|
||||
@@ -84,14 +93,21 @@ fn urls_are_front_matter_plus_https_in_facts() {
|
||||
|
||||
#[test]
|
||||
fn http_urls_in_facts_are_not_allowed() {
|
||||
let text = with_front(FRONT, "- [F1] See http://example.com/x and https://example.com/y.\n");
|
||||
let text = with_front(
|
||||
FRONT,
|
||||
"- [F1] See http://example.com/x and https://example.com/y.\n",
|
||||
);
|
||||
let f = canon::parse("tabula", &text).unwrap();
|
||||
assert_eq!(f.urls(), vec!["https://example.com/y".to_string()]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn site_and_status_values() {
|
||||
for (s, st) in [("released", Status::Released), ("beta", Status::Beta), ("planned", Status::Planned)] {
|
||||
for (s, st) in [
|
||||
("released", Status::Released),
|
||||
("beta", Status::Beta),
|
||||
("planned", Status::Planned),
|
||||
] {
|
||||
let front = FRONT.replace("status: beta", &format!("status: {s}"));
|
||||
let text = with_front(&format!("{front}site: https://tabula.example/\n"), "");
|
||||
let f = canon::parse("tabula", &text).unwrap();
|
||||
@@ -104,30 +120,56 @@ fn site_and_status_values() {
|
||||
#[test]
|
||||
fn rejects_bad_status() {
|
||||
let front = FRONT.replace("status: beta", "status: Released");
|
||||
assert_eq!(code("tabula", &with_front(&front, "")), (8, CanonCode::BadStatus("Released".into())));
|
||||
assert_eq!(
|
||||
code("tabula", &with_front(&front, "")),
|
||||
(8, CanonCode::BadStatus("Released".into()))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_missing_unknown_duplicate_and_empty_keys() {
|
||||
let front = FRONT.replace("price: free\n", "");
|
||||
assert_eq!(code("tabula", &with_front(&front, "")).1, CanonCode::MissingKey("price".into()));
|
||||
assert_eq!(
|
||||
code("tabula", &with_front(&front, "")).1,
|
||||
CanonCode::MissingKey("price".into())
|
||||
);
|
||||
|
||||
let front = format!("{FRONT}colour: brass\n");
|
||||
assert_eq!(code("tabula", &with_front(&front, "")), (9, CanonCode::UnknownKey("colour".into())));
|
||||
assert_eq!(
|
||||
code("tabula", &with_front(&front, "")),
|
||||
(9, CanonCode::UnknownKey("colour".into()))
|
||||
);
|
||||
|
||||
let front = format!("{FRONT}name: Other\n");
|
||||
assert_eq!(code("tabula", &with_front(&front, "")), (9, CanonCode::DuplicateKey("name".into())));
|
||||
assert_eq!(
|
||||
code("tabula", &with_front(&front, "")),
|
||||
(9, CanonCode::DuplicateKey("name".into()))
|
||||
);
|
||||
|
||||
let front = FRONT.replace("price: free", "price:");
|
||||
assert_eq!(code("tabula", &with_front(&front, "")).1, CanonCode::EmptyValue("price".into()));
|
||||
assert_eq!(
|
||||
code("tabula", &with_front(&front, "")).1,
|
||||
CanonCode::EmptyValue("price".into())
|
||||
);
|
||||
|
||||
let front = FRONT.replace("price: free", "price: ");
|
||||
assert_eq!(code("tabula", &with_front(&front, "")).1, CanonCode::EmptyValue("price".into()));
|
||||
assert_eq!(
|
||||
code("tabula", &with_front(&front, "")).1,
|
||||
CanonCode::EmptyValue("price".into())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_non_flat_front_matter() {
|
||||
for bad in ["just text", " indented: yes", "list:\n - a", "", "# comment", "Name: Tabula", "key:value"] {
|
||||
for bad in [
|
||||
"just text",
|
||||
" indented: yes",
|
||||
"list:\n - a",
|
||||
"",
|
||||
"# comment",
|
||||
"Name: Tabula",
|
||||
"key:value",
|
||||
] {
|
||||
let front = format!("{FRONT}{bad}\n");
|
||||
let (line, c) = code("tabula", &with_front(&front, ""));
|
||||
assert_eq!(line, 9, "{bad:?}");
|
||||
@@ -140,19 +182,38 @@ fn rejects_non_flat_front_matter() {
|
||||
|
||||
#[test]
|
||||
fn rejects_missing_or_unclosed_front_matter() {
|
||||
assert_eq!(code("tabula", "# Tabula\n- [F1] x\n"), (1, CanonCode::NoFrontMatter));
|
||||
assert_eq!(
|
||||
code("tabula", "# Tabula\n- [F1] x\n"),
|
||||
(1, CanonCode::NoFrontMatter)
|
||||
);
|
||||
assert_eq!(code("tabula", ""), (1, CanonCode::NoFrontMatter));
|
||||
assert_eq!(code("tabula", " ---\n"), (1, CanonCode::NoFrontMatter));
|
||||
assert_eq!(code("tabula", &format!("---\n{FRONT}")).1, CanonCode::UnclosedFrontMatter);
|
||||
assert_eq!(
|
||||
code("tabula", &format!("---\n{FRONT}")).1,
|
||||
CanonCode::UnclosedFrontMatter
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn id_must_match_stem_and_be_valid() {
|
||||
assert_eq!(
|
||||
code("other", &with_front(FRONT, "")),
|
||||
(1, CanonCode::IdMismatch { id: "tabula".into(), stem: "other".into() })
|
||||
(
|
||||
2,
|
||||
CanonCode::IdMismatch {
|
||||
id: "tabula".into(),
|
||||
stem: "other".into()
|
||||
}
|
||||
)
|
||||
);
|
||||
for bad in ["Tabula", "-tabula", "tabula-", "tab_ula", "tab ula", "a234567890123456789012345678901234"] {
|
||||
for bad in [
|
||||
"Tabula",
|
||||
"-tabula",
|
||||
"tabula-",
|
||||
"tab_ula",
|
||||
"tab ula",
|
||||
"a234567890123456789012345678901234",
|
||||
] {
|
||||
let front = FRONT.replace("id: tabula", &format!("id: {bad}"));
|
||||
let c = code(bad, &with_front(&front, "")).1;
|
||||
assert_eq!(c, CanonCode::BadId(bad.trim().into()), "{bad:?}");
|
||||
@@ -167,9 +228,18 @@ fn id_must_match_stem_and_be_valid() {
|
||||
|
||||
#[test]
|
||||
fn urls_in_front_matter_must_be_https() {
|
||||
for bad in ["http://aula.example/", "aula.example", "https://a b", "https://"] {
|
||||
for bad in [
|
||||
"http://aula.example/",
|
||||
"aula.example",
|
||||
"https://a b",
|
||||
"https://",
|
||||
] {
|
||||
let text = with_front(&format!("{FRONT}aula: {bad}\n"), "");
|
||||
assert_eq!(code("tabula", &text).1, CanonCode::BadUrl(bad.into()), "{bad:?}");
|
||||
assert_eq!(
|
||||
code("tabula", &text).1,
|
||||
CanonCode::BadUrl(bad.into()),
|
||||
"{bad:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -190,7 +260,7 @@ fn rejects_malformed_fact_lines() {
|
||||
] {
|
||||
let text = with_front(FRONT, &format!("\n{bad}\n"));
|
||||
let (line, c) = code("tabula", &text);
|
||||
assert_eq!(line, 10, "{bad:?}");
|
||||
assert_eq!(line, 11, "{bad:?}");
|
||||
assert!(
|
||||
matches!(c, CanonCode::MalformedFact | CanonCode::EmptyFact),
|
||||
"{bad:?} gave {c:?}"
|
||||
@@ -202,21 +272,33 @@ fn rejects_malformed_fact_lines() {
|
||||
fn fact_ids_in_range_without_leading_zeros() {
|
||||
let ok = with_front(FRONT, "- [F1] a\n- [F9999] b\n");
|
||||
let f = canon::parse("tabula", &ok).unwrap();
|
||||
assert_eq!(f.facts.iter().map(|x| x.id).collect::<Vec<_>>(), vec![1, 9999]);
|
||||
for bad in ["- [F0] zero", "- [F10000] big", "- [F01] lead", "- [F99999999999999999999] huge"] {
|
||||
assert_eq!(
|
||||
f.facts.iter().map(|x| x.id).collect::<Vec<_>>(),
|
||||
vec![1, 9999]
|
||||
);
|
||||
for bad in [
|
||||
"- [F0] zero",
|
||||
"- [F10000] big",
|
||||
"- [F01] lead",
|
||||
"- [F99999999999999999999] huge",
|
||||
] {
|
||||
let text = with_front(FRONT, &format!("{bad}\n"));
|
||||
assert_eq!(code("tabula", &text), (9, CanonCode::FactIdOutOfRange), "{bad:?}");
|
||||
assert_eq!(
|
||||
code("tabula", &text),
|
||||
(10, CanonCode::FactIdOutOfRange),
|
||||
"{bad:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_duplicate_facts_and_empty_facts() {
|
||||
let text = with_front(FRONT, "- [F1] a\n## Limits\n- [F1] b\n");
|
||||
assert_eq!(code("tabula", &text), (11, CanonCode::DuplicateFact(1)));
|
||||
assert_eq!(code("tabula", &text), (12, CanonCode::DuplicateFact(1)));
|
||||
let text = with_front(FRONT, "- [F1] \n");
|
||||
assert_eq!(code("tabula", &text), (9, CanonCode::EmptyFact));
|
||||
assert_eq!(code("tabula", &text), (10, CanonCode::EmptyFact));
|
||||
let text = with_front(FRONT, "- [F1] (assumption)\n");
|
||||
assert_eq!(code("tabula", &text), (9, CanonCode::EmptyFact));
|
||||
assert_eq!(code("tabula", &text), (10, CanonCode::EmptyFact));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -227,7 +309,11 @@ fn assumption_marker() {
|
||||
- [F4] Inside (assumption) the text.\n- [F5] Trailing spaces (assumption) \n",
|
||||
);
|
||||
let f = canon::parse("tabula", &text).unwrap();
|
||||
let got: Vec<(&str, bool)> = f.facts.iter().map(|x| (x.text.as_str(), x.assumption)).collect();
|
||||
let got: Vec<(&str, bool)> = f
|
||||
.facts
|
||||
.iter()
|
||||
.map(|x| (x.text.as_str(), x.assumption))
|
||||
.collect();
|
||||
assert_eq!(
|
||||
got,
|
||||
vec![
|
||||
@@ -242,7 +328,10 @@ fn assumption_marker() {
|
||||
|
||||
#[test]
|
||||
fn other_body_lines_are_ignored() {
|
||||
let text = with_front(FRONT, "# T\n\nSome prose [F7] here.\n- plain bullet\n> - [F9] quoted\n- [F1] fact\n");
|
||||
let text = with_front(
|
||||
FRONT,
|
||||
"# T\n\nSome prose [F7] here.\n- plain bullet\n> - [F9] quoted\n- [F1] fact\n",
|
||||
);
|
||||
let f = canon::parse("tabula", &text).unwrap();
|
||||
assert_eq!(f.facts.len(), 1);
|
||||
}
|
||||
@@ -256,17 +345,31 @@ fn size_limit_is_64_kib() {
|
||||
assert_eq!(text.len(), canon::MAX_FILE_BYTES);
|
||||
assert!(canon::parse("tabula", &text).is_ok());
|
||||
text.push('\n');
|
||||
assert_eq!(code("tabula", &text), (0, CanonCode::TooLarge { bytes: canon::MAX_FILE_BYTES + 1 }));
|
||||
assert_eq!(
|
||||
code("tabula", &text),
|
||||
(
|
||||
0,
|
||||
CanonCode::TooLarge {
|
||||
bytes: canon::MAX_FILE_BYTES + 1
|
||||
}
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_huge_line_is_fine_under_the_limit_and_rejected_over_it() {
|
||||
let long = "a".repeat(60 * 1024);
|
||||
let text = with_front(FRONT, &format!("- [F1] {long}\n"));
|
||||
assert_eq!(canon::parse("tabula", &text).unwrap().facts[0].text.len(), long.len());
|
||||
assert_eq!(
|
||||
canon::parse("tabula", &text).unwrap().facts[0].text.len(),
|
||||
long.len()
|
||||
);
|
||||
let long = "a".repeat(70 * 1024);
|
||||
let text = with_front(FRONT, &format!("- [F1] {long}\n"));
|
||||
assert!(matches!(code("tabula", &text).1, CanonCode::TooLarge { .. }));
|
||||
assert!(matches!(
|
||||
code("tabula", &text).1,
|
||||
CanonCode::TooLarge { .. }
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -282,7 +385,7 @@ fn bom_and_crlf_are_tolerated() {
|
||||
#[test]
|
||||
fn lone_cr_nul_and_hidden_characters_are_rejected() {
|
||||
let text = with_front(FRONT, "- [F1] a\rb\n");
|
||||
assert_eq!(code("tabula", &text), (9, CanonCode::LoneCarriageReturn));
|
||||
assert_eq!(code("tabula", &text), (10, CanonCode::LoneCarriageReturn));
|
||||
for (ch, cp) in [
|
||||
('\0', 0u32),
|
||||
('\u{7}', 7),
|
||||
@@ -297,11 +400,18 @@ fn lone_cr_nul_and_hidden_characters_are_rejected() {
|
||||
('\u{e0041}', 0xe0041),
|
||||
] {
|
||||
let text = with_front(FRONT, &format!("- [F1] a{ch}b\n"));
|
||||
assert_eq!(code("tabula", &text), (9, CanonCode::ForbiddenCharacter { codepoint: cp }), "{cp:X}");
|
||||
assert_eq!(
|
||||
code("tabula", &text),
|
||||
(10, CanonCode::ForbiddenCharacter { codepoint: cp }),
|
||||
"{cp:X}"
|
||||
);
|
||||
}
|
||||
// In the front matter too.
|
||||
let front = FRONT.replace("name: Tabula", "name: Tab\u{202e}ula");
|
||||
assert_eq!(code("tabula", &with_front(&front, "")), (3, CanonCode::ForbiddenCharacter { codepoint: 0x202e }));
|
||||
assert_eq!(
|
||||
code("tabula", &with_front(&front, "")),
|
||||
(3, CanonCode::ForbiddenCharacter { codepoint: 0x202e })
|
||||
);
|
||||
// Tabs and ordinary non-ASCII text are fine.
|
||||
let text = with_front(FRONT, "- [F1] Poznámky\tžluťoučký kůň 📝\n");
|
||||
assert!(canon::parse("tabula", &text).is_ok());
|
||||
@@ -311,13 +421,26 @@ fn lone_cr_nul_and_hidden_characters_are_rejected() {
|
||||
fn canon_set() {
|
||||
let mut c = Canon::new();
|
||||
c.insert(canon::parse("tabula", TABULA).unwrap()).unwrap();
|
||||
let err = c.insert(canon::parse("tabula", TABULA).unwrap()).unwrap_err();
|
||||
let err = c
|
||||
.insert(canon::parse("tabula", TABULA).unwrap())
|
||||
.unwrap_err();
|
||||
assert_eq!(err.code, CanonCode::DuplicateApp("tabula".into()));
|
||||
c.insert(CanonFile::adhoc("release", "Tabula", vec![(1, "Tabula 1.2 is out.".into())])).unwrap();
|
||||
assert_eq!(c.apps().map(|f| f.id.as_str()).collect::<Vec<_>>(), vec!["release", "tabula"]);
|
||||
c.insert(CanonFile::adhoc(
|
||||
"release",
|
||||
"Tabula",
|
||||
vec![(1, "Tabula 1.2 is out.".into())],
|
||||
))
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
c.apps().map(|f| f.id.as_str()).collect::<Vec<_>>(),
|
||||
vec!["release", "tabula"]
|
||||
);
|
||||
assert!(c.get("tabula").is_some());
|
||||
assert!(c.get("nope").is_none());
|
||||
assert_eq!(c.fact("tabula", 1).unwrap().text, "Tabula keeps notes on your own computer.");
|
||||
assert_eq!(
|
||||
c.fact("tabula", 1).unwrap().text,
|
||||
"Tabula keeps notes on your own computer."
|
||||
);
|
||||
assert!(c.fact("tabula", 9).is_none());
|
||||
assert!(c.fact("nope", 1).is_none());
|
||||
let urls = c.allowed_urls();
|
||||
@@ -331,11 +454,17 @@ fn adhoc_release_file() {
|
||||
let f = CanonFile::adhoc(
|
||||
"release",
|
||||
"Tabula",
|
||||
vec![(1, "Tabula 1.2 is out.".into()), (2, "Faster search. (assumption)".into())],
|
||||
vec![
|
||||
(1, "Tabula 1.2 is out.".into()),
|
||||
(2, "Faster search. (assumption)".into()),
|
||||
],
|
||||
);
|
||||
assert_eq!(f.id, "release");
|
||||
assert_eq!(f.front.name, "Tabula");
|
||||
assert_eq!(f.facts.len(), 2);
|
||||
assert!(f.facts.iter().all(|x| !x.assumption), "release facts count as confirmed");
|
||||
assert!(
|
||||
f.facts.iter().all(|x| !x.assumption),
|
||||
"release facts count as confirmed"
|
||||
);
|
||||
assert_eq!(f.fact(2).unwrap().text, "Faster search. (assumption)");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user