From 1e253024817075e1d7537455c15db2fab1b78166 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 24 Jun 2016 07:12:47 +0200 Subject: [PATCH] Add test for javascript filter --- .../sone/template/JavascriptFilterTest.java | 56 ++++++++++++++++++++++ 1 file changed, 56 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..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(); + } + +} -- 2.7.4