From: David ‘Bombe’ Roden Date: Fri, 24 Jun 2016 05:12:47 +0000 (+0200) Subject: Add test for javascript filter X-Git-Tag: 0.9.5^2~23 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=1e253024817075e1d7537455c15db2fab1b78166;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..6718c5a --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/template/JavascriptFilterTest.java @@ -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 David ‘Bombe’ Roden + */ +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(); + } + +}