From: David ‘Bombe’ Roden Date: Thu, 17 Nov 2016 19:36:08 +0000 (+0100) Subject: Add unit test for reply group filter X-Git-Tag: 0.9.7^2~429 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=51f51eca58cd8a8fca0c5b236f40d7899e87ac3b;p=Sone.git Add unit test for reply group filter --- diff --git a/src/test/kotlin/net/pterodactylus/sone/template/ReplyGroupFilterTest.kt b/src/test/kotlin/net/pterodactylus/sone/template/ReplyGroupFilterTest.kt new file mode 100644 index 0000000..4614454 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/template/ReplyGroupFilterTest.kt @@ -0,0 +1,64 @@ +package net.pterodactylus.sone.template + +import com.google.common.base.Optional +import net.pterodactylus.sone.data.Post +import net.pterodactylus.sone.data.PostReply +import net.pterodactylus.sone.data.Sone +import net.pterodactylus.sone.test.mock +import net.pterodactylus.sone.test.whenever +import net.pterodactylus.util.template.TemplateContext +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.containsInAnyOrder +import org.hamcrest.Matchers.equalTo +import org.junit.Before +import org.junit.Test + +/** + * Unit test for [ReplyGroupFilter]. + */ +class ReplyGroupFilterTest { + + private val filter = ReplyGroupFilter() + private val replies = mutableListOf() + private val posts = mutableListOf() + private val sones = mutableListOf() + private val templateContext = mock() + + @Before + fun setupReplies() { + (0..4).forEach { + sones += mock() + } + (0..7).forEach { + posts += mock() + whenever(posts[it].sone).thenReturn(sones[(it + 1) % sones.size]) + } + (0..10).forEach { + replies += mock() + whenever(replies[it].sone).thenReturn(sones[it % sones.size]) + whenever(replies[it].post).thenReturn(Optional.of(posts[it % posts.size])) + } + } + + @Test + @Suppress("UNCHECKED_CAST") + fun `replies are grouped correctly`() { + val groupReplies = filter.format(templateContext, replies, emptyMap()) as Map> + assertThat(groupReplies.keys, equalTo(posts.toSet())) + verifyPostRepliesAndSones(groupReplies, 0, listOf(0, 3), listOf(0, 8)) + verifyPostRepliesAndSones(groupReplies, 1, listOf(1, 4), listOf(1, 9)) + verifyPostRepliesAndSones(groupReplies, 2, listOf(2, 0), listOf(2, 10)) + verifyPostRepliesAndSones(groupReplies, 3, listOf(3), listOf(3)) + verifyPostRepliesAndSones(groupReplies, 4, listOf(4), listOf(4)) + verifyPostRepliesAndSones(groupReplies, 5, listOf(0), listOf(5)) + verifyPostRepliesAndSones(groupReplies, 6, listOf(1), listOf(6)) + verifyPostRepliesAndSones(groupReplies, 7, listOf(2), listOf(7)) + } + + @Suppress("UNCHECKED_CAST") + private fun verifyPostRepliesAndSones(groupReplies: Map>, postIndex: Int, soneIndices: List, replyIndices: List) { + assertThat(groupReplies[posts[postIndex]]!!["sones"] as Iterable, containsInAnyOrder(*soneIndices.map { sones[it] }.toTypedArray())) + assertThat(groupReplies[posts[postIndex]]!!["replies"] as Iterable, containsInAnyOrder(*replyIndices.map { replies[it] }.toTypedArray())) + } + +}