🎨 Replace post accessor test with Kotlin version
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / template / PostAccessorTest.kt
1 package net.pterodactylus.sone.template
2
3 import net.pterodactylus.sone.core.*
4 import net.pterodactylus.sone.data.*
5 import net.pterodactylus.sone.test.*
6 import net.pterodactylus.util.template.*
7 import org.hamcrest.MatcherAssert.*
8 import org.hamcrest.Matchers.*
9 import org.junit.*
10
11 /**
12  * Unit test for [PostAccessor].
13  */
14 class PostAccessorTest {
15
16         private val core = mock<Core>()
17         private val accessor = PostAccessor(core)
18         private val post = mock<Post>()
19         private val now = System.currentTimeMillis()
20
21         @Before
22         fun setupPost() {
23                 whenever(post.id).thenReturn("post-id")
24         }
25
26         @Test
27         fun `accessor returns the correct replies`() {
28                 val replies = listOf(
29                                 createPostReply(2000),
30                                 createPostReply(-1000),
31                                 createPostReply(-2000),
32                                 createPostReply(-3000),
33                                 createPostReply(-4000)
34                 )
35                 whenever(core.getReplies("post-id")).thenReturn(replies)
36                 val repliesForPost = accessor[null, post, "replies"] as Collection<PostReply>
37                 assertThat(repliesForPost, contains(
38                                 replies[1],
39                                 replies[2],
40                                 replies[3],
41                                 replies[4]
42                 ))
43         }
44
45         private fun createPostReply(timeOffset: Long) = mock<PostReply>().apply {
46                 whenever(time).thenReturn(now + timeOffset)
47         }
48
49         @Test
50         fun `accessor returns the liking sones`() {
51                 val sones = setOf<Sone>()
52                 whenever(core.getLikes(post)).thenReturn(sones)
53                 val likingSones = accessor[null, post, "likes"] as Set<Sone>
54                 assertThat(likingSones, equalTo(sones))
55         }
56
57         @Test
58         fun `accessor returns whether the current sone liked a post`() {
59                 val sone = mock<Sone>()
60                 whenever(sone.isLikedPostId("post-id")).thenReturn(true)
61                 val templateContext = TemplateContext()
62                 templateContext["currentSone"] = sone
63                 assertThat(accessor[templateContext, post, "liked"], equalTo<Any>(true))
64         }
65
66         @Test
67         fun `accessor returns false if post is not liked`() {
68                 val sone = mock<Sone>()
69                 val templateContext = TemplateContext()
70                 templateContext["currentSone"] = sone
71                 assertThat(accessor[templateContext, post, "liked"], equalTo<Any>(false))
72         }
73
74         @Test
75         fun `accessor returns false if there is no current sone`() {
76                 val templateContext = TemplateContext()
77                 assertThat(accessor[templateContext, post, "liked"], equalTo<Any>(false))
78         }
79
80         @Test
81         fun `accessor returns that not known post is new`() {
82                 assertThat(accessor[null, post, "new"], equalTo<Any>(true))
83         }
84
85         @Test
86         fun `accessor returns that known post is not new`() {
87                 whenever(post.isKnown).thenReturn(true)
88                 assertThat(accessor[null, post, "new"], equalTo<Any>(false))
89         }
90
91         @Test
92         fun `accessor returns if post is bookmarked`() {
93                 whenever(core.isBookmarked(post)).thenReturn(true)
94                 assertThat(accessor[null, post, "bookmarked"], equalTo<Any>(true))
95         }
96
97         @Test
98         fun `accessor returns other properties`() {
99                 assertThat(accessor[null, post, "hashCode"], equalTo<Any>(post.hashCode()))
100         }
101
102 }