1 package net.pterodactylus.sone.template
3 import net.pterodactylus.sone.core.Core
4 import net.pterodactylus.sone.data.PostReply
5 import net.pterodactylus.sone.data.Sone
6 import net.pterodactylus.sone.test.mock
7 import net.pterodactylus.sone.test.whenever
8 import net.pterodactylus.util.template.TemplateContext
9 import org.hamcrest.MatcherAssert.assertThat
10 import org.hamcrest.Matchers.equalTo
11 import org.junit.Before
15 * Unit test for [ReplyAccessor].
17 class ReplyAccessorTest {
19 private val core = mock<Core>()
20 private val accessor = ReplyAccessor(core)
21 private val templateContext = mock<TemplateContext>()
22 private val reply = mock<PostReply>()
23 private val currentSone = mock<Sone>()
27 whenever(reply.id).thenReturn("reply-id")
31 fun setupTemplateContext() {
32 whenever(templateContext.get("currentSone")).thenReturn(currentSone)
36 fun `returns the likes correctly`() {
37 val sones = setOf(mock<Sone>(), mock<Sone>(), mock<Sone>())
38 whenever(core.getLikes(reply)).thenReturn(sones)
39 assertThat(accessor.get(templateContext, reply, "likes"), equalTo<Any>(sones))
43 fun `returns that the reply is not liked if the current sone is null`() {
44 whenever(templateContext.get("currentSone")).thenReturn(null)
45 assertThat(accessor.get(templateContext, reply, "liked"), equalTo<Any>(false))
49 fun `returns that the reply is not liked if the current sone does not like the reply`() {
50 assertThat(accessor.get(templateContext, reply, "liked"), equalTo<Any>(false))
54 fun `returns that the reply is liked if the current sone does like the reply`() {
55 whenever(currentSone.isLikedReplyId("reply-id")).thenReturn(true)
56 assertThat(accessor.get(templateContext, reply, "liked"), equalTo<Any>(true))
60 fun `returns that the reply is new if the reply is not known`() {
61 assertThat(accessor.get(templateContext, reply, "new"), equalTo<Any>(true))
65 fun `returns that the reply is not new if the reply is known`() {
66 whenever(reply.isKnown).thenReturn(true)
67 assertThat(accessor.get(templateContext, reply, "new"), equalTo<Any>(false))
71 fun `return that a reply is not loaded if its sone is null`() {
72 assertThat(accessor.get(templateContext, reply, "loaded"), equalTo<Any>(false))
76 fun `return that a reply is loaded if its sone is not null`() {
77 whenever(reply.sone).thenReturn(mock<Sone>())
78 assertThat(accessor.get(templateContext, reply, "loaded"), equalTo<Any>(true))
82 fun `reflection accessor is used for all other members`() {
83 assertThat(accessor.get(templateContext, reply, "hashCode"), equalTo<Any>(reply.hashCode()))