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 `reply sone for remote post without replies is current sone`() {
99 val post = mockPostFrom(remoteSone)
100 assertThat(accessor[templateContext, post, "replySone"], equalTo<Any>(currentSone))
104 fun `reply sone for remote post with remote replies is current sone`() {
105 val post = mockPostFrom(remoteSone)
106 val replies = listOf(mockReplyFrom(remoteSone), mockReplyFrom(remoteSone))
107 whenever(core.getReplies("post-id")).thenReturn(replies)
108 assertThat(accessor[templateContext, post, "replySone"], equalTo<Any>(currentSone))
112 fun `reply sone for remote post with remote and one local replies is sone of local reply`() {
113 val post = mockPostFrom(remoteSone)
114 val localSone = mockLocalSone()
115 val replies = listOf(mockReplyFrom(remoteSone), mockReplyFrom(localSone))
116 whenever(core.getReplies("post-id")).thenReturn(replies)
117 assertThat(accessor[templateContext, post, "replySone"], equalTo<Any>(localSone))
121 fun `reply sone for remote post with remote and several local replies is sone of latest local reply`() {
122 val post = mockPostFrom(remoteSone)
123 val localSone1 = mockLocalSone()
124 val localSone2 = mockLocalSone()
125 val replies = listOf(mockReplyFrom(remoteSone), mockReplyFrom(localSone1), mockReplyFrom(localSone2))
126 whenever(core.getReplies("post-id")).thenReturn(replies)
127 assertThat(accessor[templateContext, post, "replySone"], equalTo<Any>(localSone2))
131 fun `reply sone for local post without replies is post sone`() {
132 val localSone = mockLocalSone()
133 val post = mockPostFrom(localSone)
134 assertThat(accessor[templateContext, post, "replySone"], equalTo<Any>(localSone))
138 fun `reply sone for local post with remote replies is local sone`() {
139 val localSone = mockLocalSone()
140 val post = mockPostFrom(localSone)
141 val replies = listOf(mockReplyFrom(remoteSone), mockReplyFrom(remoteSone))
142 whenever(core.getReplies("post-id")).thenReturn(replies)
143 assertThat(accessor[templateContext, post, "replySone"], equalTo<Any>(localSone))
147 fun `reply sone for local post with remote and one local replies is local reply sone`() {
148 val localSone1 = mockLocalSone()
149 val post = mockPostFrom(localSone1)
150 val localSone2 = mockLocalSone()
151 val replies = listOf(mockReplyFrom(remoteSone), mockReplyFrom(localSone2))
152 whenever(core.getReplies("post-id")).thenReturn(replies)
153 assertThat(accessor[templateContext, post, "replySone"], equalTo<Any>(localSone2))
157 fun `reply sone for local post with remote and several local replies is latest local reply sone`() {
158 val localSone1 = mockLocalSone()
159 val post = mockPostFrom(localSone1)
160 val localSone2 = mockLocalSone()
161 val localSone3 = mockLocalSone()
162 val replies = listOf(mockReplyFrom(remoteSone), mockReplyFrom(localSone2), mockReplyFrom(localSone3))
163 whenever(core.getReplies("post-id")).thenReturn(replies)
164 assertThat(accessor[templateContext, post, "replySone"], equalTo<Any>(localSone3))
168 fun `accessor returns other properties`() {
169 assertThat(accessor[null, post, "hashCode"], equalTo<Any>(post.hashCode()))
174 private val currentSone = mock<Sone>()
175 private val remoteSone = mock<Sone>()
176 private fun mockLocalSone() = mock<Sone>().apply { whenever(isLocal).thenReturn(true) }
178 private val templateContext = TemplateContext().apply {
179 this["currentSone"] = currentSone
182 private fun mockPostFrom(sone: Sone) = mock<Post>().apply {
183 whenever(id).thenReturn("post-id")
184 whenever(this.sone).thenReturn(sone)
187 private fun mockReplyFrom(sone: Sone) = mock<PostReply>().apply { whenever(this.sone).thenReturn(sone) }