From: David ‘Bombe’ Roden Date: Thu, 20 Feb 2014 06:23:38 +0000 (+0100) Subject: Add test for javascript filter. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=a803c59dc90720f736f2c582f74b3616212c49db;p=Sone.git Add test for javascript filter. --- 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()); + } + +}