# HG changeset patch # User Tuomo Valkonen # Date 1697831955 18000 # Node ID 945a396340d20b1909a54489866ca4f61e3831dc # Parent 68538da191c76af75598d42a57784f78d2add8de Some unit tests diff -r 68538da191c7 -r 945a396340d2 src/main.rs --- a/src/main.rs Fri Oct 20 14:59:04 2023 -0500 +++ b/src/main.rs Fri Oct 20 14:59:15 2023 -0500 @@ -425,3 +425,90 @@ rule.flush(&ctx) } + + +#[cfg(test)] +mod tests { + use super::*; + use std::io::Cursor; + + fn process_str(config : Config, input : &str) -> String { + let b = process(config, Cursor::new(input.as_bytes()), Vec::new()); + String::from_utf8(b).unwrap() + } + + #[test] + fn test1_changes() { + let p = |s| process_str(Config { strip_comments : false, strip_whitespace : false }, s); + + assert_eq!(p("x\\added{y}z\n"), "xyz\n"); + assert_eq!(p("x\\deleted{y}z\n"), "xz\n"); + assert_eq!(p("x\\replaced{y}{q}z\n"), "xyz\n"); + + // TODO: working as intended, but not how would be good for LaTeX consistency + assert_eq!(p("x\\added{y{q}\\bar}z\n"), "xy{q}\\barz\n"); + assert_eq!(p("x\\deleted{y{q}\\bar}z\n"), "xz\n"); + + assert_eq!(p("x\\added{y\na}z\n"), "xy\naz\n"); + assert_eq!(p("x\\deleted{y\na}z\n"), "xz\n"); + assert_eq!(p("x\\replaced{y\na}{q}z\n"), "xy\naz\n"); + + assert_eq!(p("x\\added{y\\test{\na}}z\n"), "xy\\test{\na}z\n"); + assert_eq!(p("x\\deleted{y\\test{\na}}z\n"), "xz\n"); + + assert_eq!(p("x\\deleted{y\\added{a}}z\n"), "xz\n"); + + assert_eq!(p("\\added{%\n\\begin{a}\n x\n y\n z\\end{a}}\n"), + "%\n\\begin{a}\n x\n y\n z\\end{a}\n"); + + assert_eq!(p("\\added\n\n {q}\n"), "q\n"); + assert_eq!(p("\\replaced{\\{q}\n \n{z}\n"), "\\{q\n"); + assert_eq!(p("\\replaced{q\\}}\n \n{z}\n"), "q\\}\n"); + } + + #[test] + fn test2_comments() { + let p = |s| process_str(Config { strip_comments : true, strip_whitespace : false }, s); + + assert_eq!(p("\\added{%\n\\begin{a}\n x\n y\n z\\end{a}}\n"), + "\\begin{a}\n x\n y\n z\\end{a}\n"); + + assert_eq!(p(" test % comments"), " test \n"); + + assert_eq!(p(" test% comments"), " test%\n"); + + assert_eq!(p(" % comments\nline"), "line\n"); + } + + #[test] + fn test3_whitespace() { + let p = |s| process_str(Config { strip_comments : false, strip_whitespace : true }, s); + + assert_eq!(p("a\n\n\n\nb\n"), "a\n\nb\n"); + assert_eq!(p("a\n\n \n\nb\n"), "a\n\nb\n"); + assert_eq!(p("a \n\n\n\nb\n"), "a\n\nb\n"); + + assert_eq!(p("a\n\n% comment\n\nb\n"), "a\n\n% comment\n\nb\n"); + assert_eq!(p("a\n\n % comment \n\nb\n"), "a\n\n % comment\n\nb\n"); + assert_eq!(p("a % comment \n\n\n\nb\n"), "a % comment\n\nb\n"); + } + + #[test] + fn test4_comments_whitespace() { + let p = |s| process_str(Config { strip_comments : true, strip_whitespace : true }, s); + + assert_eq!(p("\\added{%\n\\begin{a}\n x\n y\n z\\end{a}}\n"), + "\\begin{a}\n x\n y\n z\\end{a}\n"); + + assert_eq!(p(" test % comments"), " test\n"); + + assert_eq!(p(" % comments\nline"), "line\n"); + + assert_eq!(p("a\n\n% comment\n\nb\n"), "a\n\nb\n"); + assert_eq!(p("a\n\n % comment \n\nb\n"), "a\n\nb\n"); + assert_eq!(p("a % comment \n\n\n\nb\n"), "a\n\nb\n"); + + assert_eq!(p("\\added{a % comment \n\n\n\nb}\n"), "a\n\nb\n"); + assert_eq!(p("c\\added{a % comment \n\n\n\nb}\n"), "ca\n\nb\n"); + } +} \ No newline at end of file