X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Ftest%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Ftemplate%2FPostAccessorTest.kt;h=72e1343fd59503a503946b57e8fab0205181e0fb;hp=000e50cae84291c572cffb992b2b016d1872e0d3;hb=d9cffbf70e8504180328964511303c1d70207b84;hpb=3ab186664b53925d8f9ee7a50a8dd9da2d9da172 diff --git a/src/test/kotlin/net/pterodactylus/sone/template/PostAccessorTest.kt b/src/test/kotlin/net/pterodactylus/sone/template/PostAccessorTest.kt index 000e50c..72e1343 100644 --- a/src/test/kotlin/net/pterodactylus/sone/template/PostAccessorTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/template/PostAccessorTest.kt @@ -95,8 +95,93 @@ class PostAccessorTest { } @Test + fun `reply sone for remote post without replies is current sone`() { + val post = mockPostFrom(remoteSone) + assertThat(accessor[templateContext, post, "replySone"], equalTo(currentSone)) + } + + @Test + fun `reply sone for remote post with remote replies is current sone`() { + val post = mockPostFrom(remoteSone) + val replies = listOf(mockReplyFrom(remoteSone), mockReplyFrom(remoteSone)) + whenever(core.getReplies("post-id")).thenReturn(replies) + assertThat(accessor[templateContext, post, "replySone"], equalTo(currentSone)) + } + + @Test + fun `reply sone for remote post with remote and one local replies is sone of local reply`() { + val post = mockPostFrom(remoteSone) + val localSone = mockLocalSone() + val replies = listOf(mockReplyFrom(remoteSone), mockReplyFrom(localSone)) + whenever(core.getReplies("post-id")).thenReturn(replies) + assertThat(accessor[templateContext, post, "replySone"], equalTo(localSone)) + } + + @Test + fun `reply sone for remote post with remote and several local replies is sone of latest local reply`() { + val post = mockPostFrom(remoteSone) + val localSone1 = mockLocalSone() + val localSone2 = mockLocalSone() + val replies = listOf(mockReplyFrom(remoteSone), mockReplyFrom(localSone1), mockReplyFrom(localSone2)) + whenever(core.getReplies("post-id")).thenReturn(replies) + assertThat(accessor[templateContext, post, "replySone"], equalTo(localSone2)) + } + + @Test + fun `reply sone for local post without replies is post sone`() { + val localSone = mockLocalSone() + val post = mockPostFrom(localSone) + assertThat(accessor[templateContext, post, "replySone"], equalTo(localSone)) + } + + @Test + fun `reply sone for local post with remote replies is local sone`() { + val localSone = mockLocalSone() + val post = mockPostFrom(localSone) + val replies = listOf(mockReplyFrom(remoteSone), mockReplyFrom(remoteSone)) + whenever(core.getReplies("post-id")).thenReturn(replies) + assertThat(accessor[templateContext, post, "replySone"], equalTo(localSone)) + } + + @Test + fun `reply sone for local post with remote and one local replies is local reply sone`() { + val localSone1 = mockLocalSone() + val post = mockPostFrom(localSone1) + val localSone2 = mockLocalSone() + val replies = listOf(mockReplyFrom(remoteSone), mockReplyFrom(localSone2)) + whenever(core.getReplies("post-id")).thenReturn(replies) + assertThat(accessor[templateContext, post, "replySone"], equalTo(localSone2)) + } + + @Test + fun `reply sone for local post with remote and several local replies is latest local reply sone`() { + val localSone1 = mockLocalSone() + val post = mockPostFrom(localSone1) + val localSone2 = mockLocalSone() + val localSone3 = mockLocalSone() + val replies = listOf(mockReplyFrom(remoteSone), mockReplyFrom(localSone2), mockReplyFrom(localSone3)) + whenever(core.getReplies("post-id")).thenReturn(replies) + assertThat(accessor[templateContext, post, "replySone"], equalTo(localSone3)) + } + + @Test fun `accessor returns other properties`() { assertThat(accessor[null, post, "hashCode"], equalTo(post.hashCode())) } } + +private val currentSone = mock() +private val remoteSone = mock() +private fun mockLocalSone() = mock().apply { whenever(isLocal).thenReturn(true) } + +private val templateContext = TemplateContext().apply { + this["currentSone"] = currentSone +} + +private fun mockPostFrom(sone: Sone) = mock().apply { + whenever(id).thenReturn("post-id") + whenever(this.sone).thenReturn(sone) +} + +private fun mockReplyFrom(sone: Sone) = mock().apply { whenever(this.sone).thenReturn(sone) }