Add test for javascript filter
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 24 Jun 2016 05:12:47 +0000 (07:12 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 24 Jun 2016 05:12:47 +0000 (07:12 +0200)
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..6718c5a
--- /dev/null
@@ -0,0 +1,56 @@
+package net.pterodactylus.sone.template;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+
+import net.pterodactylus.util.number.Hex;
+
+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 filter = new JavascriptFilter();
+
+       @Test
+       public void filterEscapesAllCharactersBelowSpace() {
+               String source = buildStringWithAllCharactersToEscape();
+               String target = buildStringWithEscapedCharacters();
+               assertThat((String) filter.format(null, source, null), is("\"" + target + "\""));
+       }
+
+       private String buildStringWithAllCharactersToEscape() {
+               StringBuilder stringBuilder = new StringBuilder();
+               for (int i = 0; i < 32; i++) {
+                       stringBuilder.append((char) i);
+               }
+               stringBuilder.append('"').append("\\").append("!");
+               return stringBuilder.toString();
+       }
+
+       private String buildStringWithEscapedCharacters() {
+               StringBuilder stringBuilder = new StringBuilder();
+               for (int i = 0; i < 32; i++) {
+                       switch (i) {
+                               case 9:
+                                       stringBuilder.append("\\t");
+                                       break;
+                               case 10:
+                                       stringBuilder.append("\\n");
+                                       break;
+                               case 13:
+                                       stringBuilder.append("\\r");
+                                       break;
+                               default:
+                                       stringBuilder.append("\\x").append(Hex.toHex(i, 2));
+                       }
+               }
+               stringBuilder.append("\\\"").append("\\\\").append("!");
+               return stringBuilder.toString();
+       }
+
+}