X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Ftest%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Ftemplate%2FSubstringFilterTest.kt;fp=src%2Ftest%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Ftemplate%2FSubstringFilterTest.kt;h=eebfe3c6dbf7f5251373062c7c35aa411ec3acc3;hb=8fe7157cb42244371934b2dccd5bb25bf1c79e76;hp=0000000000000000000000000000000000000000;hpb=9298284b0cf616691f42282d8098fc34d4853175;p=Sone.git 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")) + } + +}