From a803c59dc90720f736f2c582f74b3616212c49db Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 20 Feb 2014 07:23:38 +0100 Subject: [PATCH] Add test for javascript filter. --- .../sone/template/JavascriptFilterTest.java | 58 ++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/test/java/net/pterodactylus/sone/template/JavascriptFilterTest.java diff --git a/src/test/java/net/pterodactylus/sone/template/JavascriptFilterTest.java b/src/test/java/net/pterodactylus/sone/template/JavascriptFilterTest.java new file mode 100644 index 0000000..e7223bc --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/template/JavascriptFilterTest.java @@ -0,0 +1,58 @@ +package net.pterodactylus.sone.template; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; + +import java.util.Collections; + +import org.junit.Test; + +/** + * Unit test for {@link JavascriptFilter}. + * + * @author David ‘Bombe’ Roden + */ +public class JavascriptFilterTest { + + private final JavascriptFilter javascriptFilter = new JavascriptFilter(); + + @Test + public void enclosesAStringInDoubleQuotes() { + assertThat(formatString("Some String"), is("\"Some String\"")); + } + + @Test + public void escapesDoubleQuotes() { + assertThat(formatString("Some \" Quote"), is("\"Some \\\" Quote\"")); + } + + @Test + public void escapesBackslash() { + assertThat(formatString("Some \\ Quote"), is("\"Some \\\\ Quote\"")); + } + + @Test + public void escapesCarriageReturn() { + assertThat(formatString("Some \r Quote"), is("\"Some \\r Quote\"")); + } + + @Test + public void escapesLineFeed() { + assertThat(formatString("Some \n Quote"), is("\"Some \\n Quote\"")); + } + + @Test + public void escapesTab() { + assertThat(formatString("Some \t Quote"), is("\"Some \\t Quote\"")); + } + + @Test + public void escapesControlCharacters() { + assertThat(formatString("\u0001a\u0002"), is("\"\\x01a\\x02\"")); + } + + private String formatString(String string) { + return (String) javascriptFilter.format(null, string, Collections.emptyMap()); + } + +} -- 2.7.4