From 8da4aed98f8c7b098e553bc9ed06b870be709d20 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 17 Nov 2016 19:26:17 +0100 Subject: [PATCH] Add unit test for reply accessor --- .../sone/template/ReplyAccessorTest.kt | 86 ++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 src/test/kotlin/net/pterodactylus/sone/template/ReplyAccessorTest.kt diff --git a/src/test/kotlin/net/pterodactylus/sone/template/ReplyAccessorTest.kt b/src/test/kotlin/net/pterodactylus/sone/template/ReplyAccessorTest.kt new file mode 100644 index 0000000..f06069b --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/template/ReplyAccessorTest.kt @@ -0,0 +1,86 @@ +package net.pterodactylus.sone.template + +import net.pterodactylus.sone.core.Core +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.equalTo +import org.junit.Before +import org.junit.Test + +/** + * Unit test for [ReplyAccessor]. + */ +class ReplyAccessorTest { + + private val core = mock() + private val accessor = ReplyAccessor(core) + private val templateContext = mock() + private val reply = mock() + private val currentSone = mock() + + @Before + fun setupReply() { + whenever(reply.id).thenReturn("reply-id") + } + + @Before + fun setupTemplateContext() { + whenever(templateContext.get("currentSone")).thenReturn(currentSone) + } + + @Test + fun `returns the likes correctly`() { + val sones = setOf(mock(), mock(), mock()) + whenever(core.getLikes(reply)).thenReturn(sones) + assertThat(accessor.get(templateContext, reply, "likes"), equalTo(sones)) + } + + @Test + fun `returns that the reply is not liked if the current sone is null`() { + whenever(templateContext.get("currentSone")).thenReturn(null) + assertThat(accessor.get(templateContext, reply, "liked"), equalTo(false)) + } + + @Test + fun `returns that the reply is not liked if the current sone does not like the reply`() { + assertThat(accessor.get(templateContext, reply, "liked"), equalTo(false)) + } + + @Test + fun `returns that the reply is liked if the current sone does like the reply`() { + whenever(currentSone.isLikedReplyId("reply-id")).thenReturn(true) + assertThat(accessor.get(templateContext, reply, "liked"), equalTo(true)) + } + + @Test + fun `returns that the reply is new if the reply is not known`() { + assertThat(accessor.get(templateContext, reply, "new"), equalTo(true)) + } + + @Test + fun `returns that the reply is not new if the reply is known`() { + whenever(reply.isKnown).thenReturn(true) + assertThat(accessor.get(templateContext, reply, "new"), equalTo(false)) + } + + @Test + fun `return that a reply is not loaded if its sone is null`() { + assertThat(accessor.get(templateContext, reply, "loaded"), equalTo(false)) + } + + @Test + fun `return that a reply is loaded if its sone is not null`() { + whenever(reply.sone).thenReturn(mock()) + assertThat(accessor.get(templateContext, reply, "loaded"), equalTo(true)) + } + + @Test + fun `reflection accessor is used for all other members`() { + assertThat(accessor.get(templateContext, reply, "hashCode"), equalTo(reply.hashCode())) + } + +} -- 2.7.4