1 package net.pterodactylus.sone.template
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
13 import org.mockito.Mockito.`when`
16 * Unit test for [ShortenFilter].
18 class ShortenFilterTest {
20 private val filter = ShortenFilter()
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>
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 …")
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.")
41 fun `short parts are not shortened`() {
42 assertThat(shortenParts(15, 10, PlainTextPart("This.")), contains<Part>(
43 PlainTextPart("This.")
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 …")
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 …")
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)
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 …")
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>(
82 PlainTextPart("This is a …")
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 …")