Add test for javascript filter.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 20 Feb 2014 06:23:38 +0000 (07:23 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 28 Feb 2014 21:26:09 +0000 (22:26 +0100)
src/test/java/net/pterodactylus/sone/template/JavascriptFilterTest.java [new file with mode: 0644]

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 (file)
index 0000000..e7223bc
--- /dev/null
@@ -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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+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.<String, Object>emptyMap());
+       }
+
+}