🔖 Update version and latest edition
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / template / ShortenFilterTest.kt
1 package net.pterodactylus.sone.template
2
3 import net.pterodactylus.sone.data.Profile
4 import net.pterodactylus.sone.data.Sone
5 import net.pterodactylus.sone.test.mock
6 import net.pterodactylus.sone.text.FreenetLinkPart
7 import net.pterodactylus.sone.text.Part
8 import net.pterodactylus.sone.text.PlainTextPart
9 import net.pterodactylus.sone.text.SonePart
10 import org.hamcrest.MatcherAssert.assertThat
11 import org.hamcrest.Matchers.contains
12 import org.junit.Test
13 import org.mockito.Mockito.`when`
14
15 /**
16  * Unit test for [ShortenFilter].
17  */
18 class ShortenFilterTest {
19
20         private val filter = ShortenFilter()
21
22         @Suppress("UNCHECKED_CAST")
23         private fun shortenParts(length: Int, cutOffLength: Int, vararg parts: Part) =
24                         filter.format(null, listOf(*parts), mapOf("cut-off-length" to cutOffLength, "length" to length)) as Iterable<Part>
25
26         @Test
27         fun `plain text part is shortened if length exceeds maxl ength`() {
28                 assertThat(shortenParts(15, 10, PlainTextPart("This is a long text.")), contains<Part>(
29                                 PlainTextPart("This is a â€¦")
30                 ))
31         }
32
33         @Test
34         fun `plain text part is not shortened if length does not exceed max length`() {
35                 assertThat(shortenParts(20, 10, PlainTextPart("This is a long text.")), contains<Part>(
36                                 PlainTextPart("This is a long text.")
37                 ))
38         }
39
40         @Test
41         fun `short parts are not shortened`() {
42                 assertThat(shortenParts(15, 10, PlainTextPart("This.")), contains<Part>(
43                                 PlainTextPart("This.")
44                 ))
45         }
46
47         @Test
48         fun `multiple plain text parts are shortened`() {
49                 assertThat(shortenParts(15, 10, PlainTextPart("This "), PlainTextPart("is a long text.")), contains<Part>(
50                                 PlainTextPart("This "),
51                                 PlainTextPart("is a â€¦")
52                 ))
53         }
54
55         @Test
56         fun `parts after length has been reached are ignored`() {
57                 assertThat(shortenParts(15, 10, PlainTextPart("This is a long text."), PlainTextPart(" And even more.")), contains<Part>(
58                                 PlainTextPart("This is a â€¦")
59                 ))
60         }
61
62         @Test
63         fun `link parts are not shortened`() {
64                 assertThat(shortenParts(15, 10, FreenetLinkPart("KSK@gpl.txt", "This is a long text.", false)), contains<Part>(
65                                 FreenetLinkPart("KSK@gpl.txt", "This is a long text.", false)
66                 ))
67         }
68
69         @Test
70         fun `additional link parts are ignored`() {
71                 assertThat(shortenParts(15, 10, PlainTextPart("This is a long text."), FreenetLinkPart("KSK@gpl.txt", "This is a long text.", false)), contains<Part>(
72                                 PlainTextPart("This is a â€¦")
73                 ))
74         }
75
76         @Test
77         fun `sone parts are added but their length is ignored`() {
78                 val sone = mock<Sone>()
79                 `when`(sone.profile).thenReturn(Profile(sone))
80                 assertThat(shortenParts(15, 10, SonePart(sone), PlainTextPart("This is a long text.")), contains<Part>(
81                                 SonePart(sone),
82                                 PlainTextPart("This is a â€¦")
83                 ))
84         }
85
86         @Test
87         fun `additional sone parts are ignored`() {
88                 val sone = mock<Sone>()
89                 `when`(sone.profile).thenReturn(Profile(sone))
90                 assertThat(shortenParts(15, 10, PlainTextPart("This is a long text."), SonePart(sone)), contains<Part>(
91                                 PlainTextPart("This is a â€¦")
92                 ))
93         }
94
95 }
96