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())) } }