Merge branch 'release-0.9.7'
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / template / SubstringFilterTest.kt
1 package net.pterodactylus.sone.template
2
3 import org.hamcrest.MatcherAssert.assertThat
4 import org.hamcrest.Matchers.equalTo
5 import org.junit.Test
6
7 /**
8  * Unit test for [SubstringFilter].
9  */
10 class SubstringFilterTest {
11
12         private val filter = SubstringFilter()
13         private val string = "abcdefghijklmnopqrstuvwxyz"
14
15         private fun filterText(vararg parameters: Pair<String, Int>) = filter.format(null, string, mapOf(*parameters))
16
17         @Test
18         fun `filter returns the input string when no parameters are given`() {
19                 assertThat(filterText(), equalTo<Any>(string))
20         }
21
22         @Test
23         fun `filter returns "abc" if start is omitted and length is three`() {
24             assertThat(filterText("length" to 3), equalTo<Any>("abc"))
25         }
26
27         @Test
28         fun `filter returns complete string if length is larger than length of string`() {
29             assertThat(filterText("length" to 3000), equalTo<Any>(string))
30         }
31
32         @Test
33         fun `filter returns part of the string if start is set to index within string`() {
34             assertThat(filterText("start" to 13), equalTo<Any>("nopqrstuvwxyz"))
35         }
36
37         @Test
38         fun `filter returns last three characters if start is set to minus three`() {
39                 assertThat(filterText("start" to -3), equalTo<Any>("xyz"))
40         }
41
42         @Test
43         fun `filter returns center part of string with start and length set`() {
44             assertThat(filterText("start" to 13, "length" to 3), equalTo<Any>("nop"))
45         }
46
47         @Test
48         fun `filter returns end part of string with start and too-large length set`() {
49             assertThat(filterText("start" to 23, "length" to 30), equalTo<Any>("xyz"))
50         }
51
52         @Test
53         fun `filter returns end part of string with negative start and too-large length set`() {
54             assertThat(filterText("start" to -3, "length" to 30), equalTo<Any>("xyz"))
55         }
56
57         @Test
58         fun `filter returns part of end of string with negative start and small length set`() {
59             assertThat(filterText("start" to -6, "length" to 3), equalTo<Any>("uvw"))
60         }
61
62 }