1 package net.pterodactylus.sone.template
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.*
12 * Unit test for [PostAccessor].
14 class PostAccessorTest {
16 private val core = mock<Core>()
17 private val accessor = PostAccessor(core)
18 private val post = mock<Post>()
19 private val now = System.currentTimeMillis()
23 whenever(post.id).thenReturn("post-id")
27 fun `accessor returns the correct replies`() {
29 createPostReply(2000),
30 createPostReply(-1000),
31 createPostReply(-2000),
32 createPostReply(-3000),
33 createPostReply(-4000)
35 whenever(core.getReplies("post-id")).thenReturn(replies)
36 val repliesForPost = accessor[null, post, "replies"] as Collection<PostReply>
37 assertThat(repliesForPost, contains(
45 private fun createPostReply(timeOffset: Long) = mock<PostReply>().apply {
46 whenever(time).thenReturn(now + timeOffset)
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))
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))
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))
75 fun `accessor returns false if there is no current sone`() {
76 val templateContext = TemplateContext()
77 assertThat(accessor[templateContext, post, "liked"], equalTo<Any>(false))
81 fun `accessor returns that not known post is new`() {
82 assertThat(accessor[null, post, "new"], equalTo<Any>(true))
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))
92 fun `accessor returns if post is bookmarked`() {
93 whenever(core.isBookmarked(post)).thenReturn(true)
94 assertThat(accessor[null, post, "bookmarked"], equalTo<Any>(true))
98 fun `accessor returns other properties`() {
99 assertThat(accessor[null, post, "hashCode"], equalTo<Any>(post.hashCode()))