423 rule = rule.consume('\n', &ctx); |
423 rule = rule.consume('\n', &ctx); |
424 } |
424 } |
425 |
425 |
426 rule.flush(&ctx) |
426 rule.flush(&ctx) |
427 } |
427 } |
|
428 |
|
429 |
|
430 #[cfg(test)] |
|
431 mod tests { |
|
432 use super::*; |
|
433 use std::io::Cursor; |
|
434 |
|
435 fn process_str(config : Config, input : &str) -> String { |
|
436 let b = process(config, Cursor::new(input.as_bytes()), Vec::new()); |
|
437 String::from_utf8(b).unwrap() |
|
438 } |
|
439 |
|
440 #[test] |
|
441 fn test1_changes() { |
|
442 let p = |s| process_str(Config { strip_comments : false, strip_whitespace : false }, s); |
|
443 |
|
444 assert_eq!(p("x\\added{y}z\n"), "xyz\n"); |
|
445 assert_eq!(p("x\\deleted{y}z\n"), "xz\n"); |
|
446 assert_eq!(p("x\\replaced{y}{q}z\n"), "xyz\n"); |
|
447 |
|
448 // TODO: working as intended, but not how would be good for LaTeX consistency |
|
449 assert_eq!(p("x\\added{y{q}\\bar}z\n"), "xy{q}\\barz\n"); |
|
450 assert_eq!(p("x\\deleted{y{q}\\bar}z\n"), "xz\n"); |
|
451 |
|
452 assert_eq!(p("x\\added{y\na}z\n"), "xy\naz\n"); |
|
453 assert_eq!(p("x\\deleted{y\na}z\n"), "xz\n"); |
|
454 assert_eq!(p("x\\replaced{y\na}{q}z\n"), "xy\naz\n"); |
|
455 |
|
456 assert_eq!(p("x\\added{y\\test{\na}}z\n"), "xy\\test{\na}z\n"); |
|
457 assert_eq!(p("x\\deleted{y\\test{\na}}z\n"), "xz\n"); |
|
458 |
|
459 assert_eq!(p("x\\deleted{y\\added{a}}z\n"), "xz\n"); |
|
460 |
|
461 assert_eq!(p("\\added{%\n\\begin{a}\n x\n y\n z\\end{a}}\n"), |
|
462 "%\n\\begin{a}\n x\n y\n z\\end{a}\n"); |
|
463 |
|
464 assert_eq!(p("\\added\n\n {q}\n"), "q\n"); |
|
465 assert_eq!(p("\\replaced{\\{q}\n \n{z}\n"), "\\{q\n"); |
|
466 assert_eq!(p("\\replaced{q\\}}\n \n{z}\n"), "q\\}\n"); |
|
467 } |
|
468 |
|
469 #[test] |
|
470 fn test2_comments() { |
|
471 let p = |s| process_str(Config { strip_comments : true, strip_whitespace : false }, s); |
|
472 |
|
473 assert_eq!(p("\\added{%\n\\begin{a}\n x\n y\n z\\end{a}}\n"), |
|
474 "\\begin{a}\n x\n y\n z\\end{a}\n"); |
|
475 |
|
476 assert_eq!(p(" test % comments"), " test \n"); |
|
477 |
|
478 assert_eq!(p(" test% comments"), " test%\n"); |
|
479 |
|
480 assert_eq!(p(" % comments\nline"), "line\n"); |
|
481 } |
|
482 |
|
483 #[test] |
|
484 fn test3_whitespace() { |
|
485 let p = |s| process_str(Config { strip_comments : false, strip_whitespace : true }, s); |
|
486 |
|
487 assert_eq!(p("a\n\n\n\nb\n"), "a\n\nb\n"); |
|
488 assert_eq!(p("a\n\n \n\nb\n"), "a\n\nb\n"); |
|
489 assert_eq!(p("a \n\n\n\nb\n"), "a\n\nb\n"); |
|
490 |
|
491 assert_eq!(p("a\n\n% comment\n\nb\n"), "a\n\n% comment\n\nb\n"); |
|
492 assert_eq!(p("a\n\n % comment \n\nb\n"), "a\n\n % comment\n\nb\n"); |
|
493 assert_eq!(p("a % comment \n\n\n\nb\n"), "a % comment\n\nb\n"); |
|
494 } |
|
495 |
|
496 #[test] |
|
497 fn test4_comments_whitespace() { |
|
498 let p = |s| process_str(Config { strip_comments : true, strip_whitespace : true }, s); |
|
499 |
|
500 assert_eq!(p("\\added{%\n\\begin{a}\n x\n y\n z\\end{a}}\n"), |
|
501 "\\begin{a}\n x\n y\n z\\end{a}\n"); |
|
502 |
|
503 assert_eq!(p(" test % comments"), " test\n"); |
|
504 |
|
505 assert_eq!(p(" % comments\nline"), "line\n"); |
|
506 |
|
507 assert_eq!(p("a\n\n% comment\n\nb\n"), "a\n\nb\n"); |
|
508 assert_eq!(p("a\n\n % comment \n\nb\n"), "a\n\nb\n"); |
|
509 assert_eq!(p("a % comment \n\n\n\nb\n"), "a\n\nb\n"); |
|
510 |
|
511 assert_eq!(p("\\added{a % comment \n\n\n\nb}\n"), "a\n\nb\n"); |
|
512 assert_eq!(p("c\\added{a % comment \n\n\n\nb}\n"), "ca\n\nb\n"); |
|
513 } |
|
514 } |