Some unit tests

Fri, 20 Oct 2023 14:59:15 -0500

author
Tuomo Valkonen <tuomov@iki.fi>
date
Fri, 20 Oct 2023 14:59:15 -0500
changeset 8
945a396340d2
parent 7
68538da191c7
child 9
87d632a18fc2

Some unit tests

src/main.rs file | annotate | diff | comparison | revisions
--- 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

mercurial