X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Ftest%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Ftemplate%2FShortenFilterTest.kt;fp=src%2Ftest%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Ftemplate%2FShortenFilterTest.kt;h=c6990a7d5defee9a6f7822f8dfb19e7d9202e413;hp=0000000000000000000000000000000000000000;hb=b86b2cbef3b08619f99fb7aa68aaf55fab7bfcd2;hpb=de6d73a2f0718ff65832859a3ef7ba4f32299327 diff --git a/src/test/kotlin/net/pterodactylus/sone/template/ShortenFilterTest.kt b/src/test/kotlin/net/pterodactylus/sone/template/ShortenFilterTest.kt new file mode 100644 index 0000000..c6990a7 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/template/ShortenFilterTest.kt @@ -0,0 +1,96 @@ +package net.pterodactylus.sone.template + +import net.pterodactylus.sone.data.Profile +import net.pterodactylus.sone.data.Sone +import net.pterodactylus.sone.test.mock +import net.pterodactylus.sone.text.FreenetLinkPart +import net.pterodactylus.sone.text.Part +import net.pterodactylus.sone.text.PlainTextPart +import net.pterodactylus.sone.text.SonePart +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.contains +import org.junit.Test +import org.mockito.Mockito.`when` + +/** + * Unit test for [ShortenFilter]. + */ +class ShortenFilterTest { + + private val filter = ShortenFilter() + + @Suppress("UNCHECKED_CAST") + private fun shortenParts(length: Int, cutOffLength: Int, vararg parts: Part) = + filter.format(null, listOf(*parts), mapOf("cut-off-length" to cutOffLength, "length" to length)) as Iterable + + @Test + fun `plain text part is shortened if length exceeds maxl ength`() { + assertThat(shortenParts(15, 10, PlainTextPart("This is a long text.")), contains( + PlainTextPart("This is a …") + )) + } + + @Test + fun `plain text part is not shortened if length does not exceed max length`() { + assertThat(shortenParts(20, 10, PlainTextPart("This is a long text.")), contains( + PlainTextPart("This is a long text.") + )) + } + + @Test + fun `short parts are not shortened`() { + assertThat(shortenParts(15, 10, PlainTextPart("This.")), contains( + PlainTextPart("This.") + )) + } + + @Test + fun `multiple plain text parts are shortened`() { + assertThat(shortenParts(15, 10, PlainTextPart("This "), PlainTextPart("is a long text.")), contains( + PlainTextPart("This "), + PlainTextPart("is a …") + )) + } + + @Test + fun `parts after length has been reached are ignored`() { + assertThat(shortenParts(15, 10, PlainTextPart("This is a long text."), PlainTextPart(" And even more.")), contains( + PlainTextPart("This is a …") + )) + } + + @Test + fun `link parts are not shortened`() { + assertThat(shortenParts(15, 10, FreenetLinkPart("KSK@gpl.txt", "This is a long text.", false)), contains( + FreenetLinkPart("KSK@gpl.txt", "This is a long text.", false) + )) + } + + @Test + fun `additional link parts are ignored`() { + assertThat(shortenParts(15, 10, PlainTextPart("This is a long text."), FreenetLinkPart("KSK@gpl.txt", "This is a long text.", false)), contains( + PlainTextPart("This is a …") + )) + } + + @Test + fun `sone parts are added but their length is ignored`() { + val sone = mock() + `when`(sone.profile).thenReturn(Profile(sone)) + assertThat(shortenParts(15, 10, SonePart(sone), PlainTextPart("This is a long text.")), contains( + SonePart(sone), + PlainTextPart("This is a …") + )) + } + + @Test + fun `additional sone parts are ignored`() { + val sone = mock() + `when`(sone.profile).thenReturn(Profile(sone)) + assertThat(shortenParts(15, 10, PlainTextPart("This is a long text."), SonePart(sone)), contains( + PlainTextPart("This is a …") + )) + } + +} +