From 8fe7157cb42244371934b2dccd5bb25bf1c79e76 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 20 Nov 2016 12:56:54 +0100 Subject: [PATCH] Add unit test for substring filter and fix bug in filter --- .../sone/template/SubstringFilter.java | 2 +- .../sone/template/SubstringFilterTest.kt | 62 ++++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 src/test/kotlin/net/pterodactylus/sone/template/SubstringFilterTest.kt diff --git a/src/main/java/net/pterodactylus/sone/template/SubstringFilter.java b/src/main/java/net/pterodactylus/sone/template/SubstringFilter.java index 2104888..f14d0ff 100644 --- a/src/main/java/net/pterodactylus/sone/template/SubstringFilter.java +++ b/src/main/java/net/pterodactylus/sone/template/SubstringFilter.java @@ -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 index 0000000..eebfe3c --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/template/SubstringFilterTest.kt @@ -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) = filter.format(null, string, mapOf(*parameters)) + + @Test + fun `filter returns the input string when no parameters are given`() { + assertThat(filterText(), equalTo(string)) + } + + @Test + fun `filter returns "abc" if start is omitted and length is three`() { + assertThat(filterText("length" to 3), equalTo("abc")) + } + + @Test + fun `filter returns complete string if length is larger than length of string`() { + assertThat(filterText("length" to 3000), equalTo(string)) + } + + @Test + fun `filter returns part of the string if start is set to index within string`() { + assertThat(filterText("start" to 13), equalTo("nopqrstuvwxyz")) + } + + @Test + fun `filter returns last three characters if start is set to minus three`() { + assertThat(filterText("start" to -3), equalTo("xyz")) + } + + @Test + fun `filter returns center part of string with start and length set`() { + assertThat(filterText("start" to 13, "length" to 3), equalTo("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("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("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("uvw")) + } + +} -- 2.7.4