159 lines
5.6 KiB
Rust
159 lines
5.6 KiB
Rust
// SPDX-License-Identifier: CPAL-1.0
|
|
// Copyright (c) 2026 Marek Novák. Exhibit A notice: LICENSE at the repository root.
|
|
|
|
use std::collections::BTreeMap;
|
|
|
|
use scaena_core::hooks::{self, Genome, HookError, Opening};
|
|
|
|
const HOOKS: &str = "# Hook templates
|
|
|
|
Prose is ignored.
|
|
|
|
- [H1] Tired of {pain}? {app} fixes it.
|
|
- [H2] {number} reasons {audience} love {app}.
|
|
- [H3] Try {feature} in {clicks}.
|
|
- [H4] {app} now runs on {platform}.
|
|
- [H5] Stop losing notes.
|
|
";
|
|
|
|
fn values(pairs: &[(&str, &str)]) -> BTreeMap<String, String> {
|
|
pairs.iter().map(|(k, v)| (k.to_string(), v.to_string())).collect()
|
|
}
|
|
|
|
#[test]
|
|
fn parses_templates_with_slots_in_order() {
|
|
let hs = hooks::parse(HOOKS).unwrap();
|
|
assert_eq!(hs.iter().map(|h| h.id.as_str()).collect::<Vec<_>>(), vec!["H1", "H2", "H3", "H4", "H5"]);
|
|
assert_eq!(hs[0].text, "Tired of {pain}? {app} fixes it.");
|
|
assert_eq!(hs[0].slots, vec!["pain", "app"]);
|
|
assert_eq!(hs[1].slots, vec!["number", "audience", "app"]);
|
|
assert!(hs[4].slots.is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn slots_are_unique() {
|
|
let hs = hooks::parse("- [H1] {app} and {app} and {feature}\n").unwrap();
|
|
assert_eq!(hs[0].slots, vec!["app", "feature"]);
|
|
}
|
|
|
|
#[test]
|
|
fn every_fixed_slot_is_allowed() {
|
|
let line = format!("- [H1] {}\n", hooks::SLOTS.map(|s| format!("{{{s}}}")).join(" "));
|
|
let hs = hooks::parse(&line).unwrap();
|
|
assert_eq!(hs[0].slots, hooks::SLOTS.to_vec());
|
|
}
|
|
|
|
#[test]
|
|
fn unknown_slots_are_errors() {
|
|
for (bad, slot) in [("{price}", "price"), ("{App}", "App"), ("{}", ""), ("{ app }", " app "), ("{url}", "url")] {
|
|
let md = format!("# x\n- [H1] Hello {bad}\n");
|
|
assert_eq!(
|
|
hooks::parse(&md),
|
|
Err(HookError::UnknownSlot { line: 2, slot: slot.into() }),
|
|
"{bad:?}"
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn unbalanced_braces_are_errors() {
|
|
for bad in ["{app", "app}", "{{app}}", "{a{pp}", "}{"] {
|
|
let md = format!("- [H1] Hi {bad}\n");
|
|
assert_eq!(hooks::parse(&md), Err(HookError::UnbalancedBrace { line: 1 }), "{bad:?}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn malformed_duplicate_and_empty_lines() {
|
|
for bad in ["- [H] x", "- [Hx] x", "- [H1]x", "- [H1 x", "- [h1] x", " - [H1] x", "- [H1] x"] {
|
|
assert_eq!(hooks::parse(&format!("{bad}\n")), Err(HookError::Malformed { line: 1 }), "{bad:?}");
|
|
}
|
|
for bad in ["- [H0] x", "- [H01] x", "- [H10000] x"] {
|
|
assert_eq!(hooks::parse(&format!("{bad}\n")), Err(HookError::IdOutOfRange { line: 1 }), "{bad:?}");
|
|
}
|
|
assert_eq!(
|
|
hooks::parse("- [H1] a\n- [H1] b\n"),
|
|
Err(HookError::Duplicate { line: 2, id: "H1".into() })
|
|
);
|
|
assert_eq!(hooks::parse("- [H1] \n"), Err(HookError::Empty { line: 1 }));
|
|
assert_eq!(hooks::parse("- [H1]\n"), Err(HookError::Malformed { line: 1 }));
|
|
}
|
|
|
|
#[test]
|
|
fn hostile_input() {
|
|
let big = format!("- [H1] {}\n", "a".repeat(hooks::MAX_FILE_BYTES));
|
|
assert!(matches!(hooks::parse(&big), Err(HookError::TooLarge { .. })));
|
|
assert_eq!(
|
|
hooks::parse("- [H1] a\u{202e}b\n"),
|
|
Err(HookError::ForbiddenCharacter { line: 1, codepoint: 0x202e })
|
|
);
|
|
assert_eq!(hooks::parse("x\n- [H1] a\0b\n"), Err(HookError::ForbiddenCharacter { line: 2, codepoint: 0 }));
|
|
let crlf = HOOKS.replace('\n', "\r\n");
|
|
assert_eq!(hooks::parse(&crlf).unwrap(), hooks::parse(HOOKS).unwrap());
|
|
let bom = format!("\u{feff}{HOOKS}");
|
|
assert_eq!(hooks::parse(&bom).unwrap(), hooks::parse(HOOKS).unwrap());
|
|
assert_eq!(hooks::parse("").unwrap(), vec![]);
|
|
}
|
|
|
|
#[test]
|
|
fn fill_replaces_every_slot() {
|
|
let hs = hooks::parse(HOOKS).unwrap();
|
|
let v = values(&[("pain", "lost notes"), ("app", "Tabula"), ("unused", "x")]);
|
|
assert_eq!(hs[0].fill(&v).unwrap(), "Tired of lost notes? Tabula fixes it.");
|
|
assert_eq!(hs[4].fill(&BTreeMap::new()).unwrap(), "Stop losing notes.");
|
|
}
|
|
|
|
#[test]
|
|
fn fill_missing_value_is_an_error() {
|
|
let hs = hooks::parse(HOOKS).unwrap();
|
|
let v = values(&[("app", "Tabula")]);
|
|
assert_eq!(hs[0].fill(&v), Err(HookError::MissingValue { slot: "pain".into() }));
|
|
}
|
|
|
|
#[test]
|
|
fn fill_does_not_re_expand_braces_in_values() {
|
|
let hs = hooks::parse(HOOKS).unwrap();
|
|
let v = values(&[("pain", "{app}"), ("app", "{pain} {x}")]);
|
|
assert_eq!(hs[0].fill(&v).unwrap(), "Tired of {app}? {pain} {x} fixes it.");
|
|
}
|
|
|
|
#[test]
|
|
fn genome_from_text() {
|
|
let hs = hooks::parse(HOOKS).unwrap();
|
|
assert_eq!(
|
|
hs[0].genome,
|
|
Genome { opening: Opening::Question, words: 6, has_number: false, lead_verb: None }
|
|
);
|
|
assert_eq!(
|
|
hs[1].genome,
|
|
Genome { opening: Opening::Number, words: 5, has_number: true, lead_verb: None }
|
|
);
|
|
assert_eq!(
|
|
hs[2].genome,
|
|
Genome { opening: Opening::Command, words: 4, has_number: false, lead_verb: Some("try".into()) }
|
|
);
|
|
assert_eq!(
|
|
hs[3].genome,
|
|
Genome { opening: Opening::Statement, words: 5, has_number: false, lead_verb: None }
|
|
);
|
|
assert_eq!(hs[4].genome.opening, Opening::Command);
|
|
assert_eq!(hs[4].genome.lead_verb.as_deref(), Some("stop"));
|
|
}
|
|
|
|
#[test]
|
|
fn genome_of_filled_text() {
|
|
let g = Genome::of("3 clicks to a new note.");
|
|
assert_eq!(g.opening, Opening::Number);
|
|
assert!(g.has_number);
|
|
assert_eq!(g.words, 6);
|
|
let g = Genome::of("Why do notes need an account?");
|
|
assert_eq!(g.opening, Opening::Question);
|
|
let g = Genome::of("Your notes, on your computer. Ever wondered why?");
|
|
assert_eq!(g.opening, Opening::Statement);
|
|
let g = Genome::of("");
|
|
assert_eq!(g, Genome { opening: Opening::Statement, words: 0, has_number: false, lead_verb: None });
|
|
let g = Genome::of("Meet Tabula 2.");
|
|
assert_eq!(g.opening, Opening::Command);
|
|
assert!(g.has_number);
|
|
}
|