Add unit test for substring filter and fix bug in filter
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 20 Nov 2016 11:56:54 +0000 (12:56 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 20 Nov 2016 13:47:32 +0000 (14:47 +0100)
src/main/java/net/pterodactylus/sone/template/SubstringFilter.java
src/test/kotlin/net/pterodactylus/sone/template/SubstringFilterTest.kt [new file with mode: 0644]

index 2104888..f14d0ff 100644 (file)
@@ -48,7 +48,7 @@ public class SubstringFilter implements Filter {
                }
                String dataString = String.valueOf(data);
                int dataLength = dataString.length();
-               int length = Integer.MAX_VALUE;
+               int length = dataLength;
                try {
                        length = Integer.parseInt(lengthString);
                } catch (NumberFormatException nfe1) {
diff --git a/src/test/kotlin/net/pterodactylus/sone/template/SubstringFilterTest.kt b/src/test/kotlin/net/pterodactylus/sone/template/SubstringFilterTest.kt
new file mode 100644 (file)
index 0000000..eebfe3c
--- /dev/null
@@ -0,0 +1,62 @@
+package net.pterodactylus.sone.template
+
+import org.hamcrest.MatcherAssert.assertThat
+import org.hamcrest.Matchers.equalTo
+import org.junit.Test
+
+/**
+ * Unit test for [SubstringFilter].
+ */
+class SubstringFilterTest {
+
+       private val filter = SubstringFilter()
+       private val string = "abcdefghijklmnopqrstuvwxyz"
+
+       private fun filterText(vararg parameters: Pair<String, Int>) = filter.format(null, string, mapOf(*parameters))
+
+       @Test
+       fun `filter returns the input string when no parameters are given`() {
+               assertThat(filterText(), equalTo<Any>(string))
+       }
+
+       @Test
+       fun `filter returns "abc" if start is omitted and length is three`() {
+           assertThat(filterText("length" to 3), equalTo<Any>("abc"))
+       }
+
+       @Test
+       fun `filter returns complete string if length is larger than length of string`() {
+           assertThat(filterText("length" to 3000), equalTo<Any>(string))
+       }
+
+       @Test
+       fun `filter returns part of the string if start is set to index within string`() {
+           assertThat(filterText("start" to 13), equalTo<Any>("nopqrstuvwxyz"))
+       }
+
+       @Test
+       fun `filter returns last three characters if start is set to minus three`() {
+               assertThat(filterText("start" to -3), equalTo<Any>("xyz"))
+       }
+
+       @Test
+       fun `filter returns center part of string with start and length set`() {
+           assertThat(filterText("start" to 13, "length" to 3), equalTo<Any>("nop"))
+       }
+
+       @Test
+       fun `filter returns end part of string with start and too-large length set`() {
+           assertThat(filterText("start" to 23, "length" to 30), equalTo<Any>("xyz"))
+       }
+
+       @Test
+       fun `filter returns end part of string with negative start and too-large length set`() {
+           assertThat(filterText("start" to -3, "length" to 30), equalTo<Any>("xyz"))
+       }
+
+       @Test
+       fun `filter returns part of end of string with negative start and small length set`() {
+           assertThat(filterText("start" to -6, "length" to 3), equalTo<Any>("uvw"))
+       }
+
+}