From 6c2ef0deae9b61821f419fd6d83ce3fba6696bb4 Mon Sep 17 00:00:00 2001 From: Kogia-sima Date: Mon, 28 Dec 2020 22:44:36 +0900 Subject: [PATCH] test: simplify filter tests --- sailfish/src/runtime/filter.rs | 46 ++++++++++++++++------------------ 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/sailfish/src/runtime/filter.rs b/sailfish/src/runtime/filter.rs index c57ce1b..828783b 100644 --- a/sailfish/src/runtime/filter.rs +++ b/sailfish/src/runtime/filter.rs @@ -353,37 +353,33 @@ cfg_json! { mod tests { use super::*; + fn assert_render(expr: &T, expected: &str) { + let mut buf = Buffer::new(); + Render::render(expr, &mut buf).unwrap(); + assert_eq!(buf.as_str(), expected); + } + + fn assert_render_escaped(expr: &T, expected: &str) { + let mut buf = Buffer::new(); + Render::render_escaped(expr, &mut buf).unwrap(); + assert_eq!(buf.as_str(), expected); + } + #[test] fn case() { - let mut buf = Buffer::new(); - upper(&"hElLO, WOrLd!").render(&mut buf).unwrap(); - assert_eq!(buf.as_str(), "HELLO, WORLD!"); - - buf.clear(); - lower(&"hElLO, WOrLd!").render(&mut buf).unwrap(); - assert_eq!(buf.as_str(), "hello, world!"); - - buf.clear(); - lower(&"

TITLE

").render_escaped(&mut buf).unwrap(); - assert_eq!(buf.as_str(), "<h1>title</h1>"); + assert_render(&upper("hElLo, WOrLd!"), "HELLO, WORLD!"); + assert_render(&lower("hElLo, WOrLd!"), "hello, world!"); + assert_render_escaped(&lower("

TITLE

"), "<h1>title</h1>"); } #[test] fn trim_test() { - let mut buf = Buffer::new(); - trim(&" hello ").render(&mut buf).unwrap(); - trim(&"hello ").render(&mut buf).unwrap(); - trim(&" hello").render(&mut buf).unwrap(); - assert_eq!(buf.as_str(), "hellohellohello"); + assert_render(&trim(""), ""); + assert_render(&trim("\n \t\r\x0C"), ""); - let mut buf = Buffer::new(); - trim(&"hello ").render(&mut buf).unwrap(); - trim(&" hello").render(&mut buf).unwrap(); - trim(&"hello").render(&mut buf).unwrap(); - assert_eq!(buf.as_str(), "hellohellohello"); - - let mut buf = Buffer::new(); - trim(&" hello").render(&mut buf).unwrap(); - assert_eq!(buf.as_str(), "hello"); + assert_render(&trim("hello world!"), "hello world!"); + assert_render(&trim("hello world!\n"), "hello world!"); + assert_render(&trim("\thello world!"), "hello world!"); + assert_render(&trim("\thello world!\r\n"), "hello world!"); } }