🔀 Merge branch 'release/v82'
[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.*
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
14 /**
15  * Unit test for [ShortenFilter].
16  */
17 class ShortenFilterTest {
18
19         private val filter = ShortenFilter()
20
21         @Suppress("UNCHECKED_CAST")
22         private fun shortenParts(length: Int, cutOffLength: Int, vararg parts: Part) =
23                         filter.format(null, listOf(*parts), mapOf("cut-off-length" to cutOffLength, "length" to length)) as Iterable<Part>
24
25         @Test
26         fun `plain text part is shortened if length exceeds maxl ength`() {
27                 assertThat(shortenParts(15, 10, PlainTextPart("This is a long text.")), contains<Part>(
28                                 PlainTextPart("This is a â€¦")
29                 ))
30         }
31
32         @Test
33         fun `plain text part is not shortened if length does not exceed max length`() {
34                 assertThat(shortenParts(20, 10, PlainTextPart("This is a long text.")), contains<Part>(
35                                 PlainTextPart("This is a long text.")
36                 ))
37         }
38
39         @Test
40         fun `short parts are not shortened`() {
41                 assertThat(shortenParts(15, 10, PlainTextPart("This.")), contains<Part>(
42                                 PlainTextPart("This.")
43                 ))
44         }
45
46         @Test
47         fun `multiple plain text parts are shortened`() {
48                 assertThat(shortenParts(15, 10, PlainTextPart("This "), PlainTextPart("is a long text.")), contains<Part>(
49                                 PlainTextPart("This "),
50                                 PlainTextPart("is a â€¦")
51                 ))
52         }
53
54         @Test
55         fun `parts after length has been reached are ignored`() {
56                 assertThat(shortenParts(15, 10, PlainTextPart("This is a long text."), PlainTextPart(" And even more.")), contains<Part>(
57                                 PlainTextPart("This is a â€¦")
58                 ))
59         }
60
61         @Test
62         fun `link parts are not shortened`() {
63                 assertThat(shortenParts(15, 10, FreenetLinkPart("KSK@gpl.txt", "This is a long text.", false)), contains<Part>(
64                                 FreenetLinkPart("KSK@gpl.txt", "This is a long text.", false)
65                 ))
66         }
67
68         @Test
69         fun `additional link parts are ignored`() {
70                 assertThat(shortenParts(15, 10, PlainTextPart("This is a long text."), FreenetLinkPart("KSK@gpl.txt", "This is a long text.", false)), contains<Part>(
71                                 PlainTextPart("This is a â€¦")
72                 ))
73         }
74
75         @Test
76         fun `sone parts are added but their length is ignored`() {
77                 val sone = mock<Sone>()
78                 whenever(sone.profile).thenReturn(Profile(sone))
79                 assertThat(shortenParts(15, 10, SonePart(sone), PlainTextPart("This is a long text.")), contains<Part>(
80                                 SonePart(sone),
81                                 PlainTextPart("This is a â€¦")
82                 ))
83         }
84
85         @Test
86         fun `additional sone parts are ignored`() {
87                 val sone = mock<Sone>()
88                 whenever(sone.profile).thenReturn(Profile(sone))
89                 assertThat(shortenParts(15, 10, PlainTextPart("This is a long text."), SonePart(sone)), contains<Part>(
90                                 PlainTextPart("This is a â€¦")
91                 ))
92         }
93
94 }
95