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.*
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
15 * Unit test for [ShortenFilter].
17 class ShortenFilterTest {
19 private val filter = ShortenFilter()
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>
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 …")
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.")
40 fun `short parts are not shortened`() {
41 assertThat(shortenParts(15, 10, PlainTextPart("This.")), contains<Part>(
42 PlainTextPart("This.")
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 …")
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 …")
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)
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 …")
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>(
81 PlainTextPart("This is a …")
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 …")