From: David ‘Bombe’ Roden Date: Thu, 2 Jan 2020 19:31:55 +0000 (+0100) Subject: 🎨 Replace post accessor test with Kotlin version X-Git-Tag: v81^2~11 X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=commitdiff_plain;h=74ef01c55e8581336625307fd63962eed4e818ca 🎨 Replace post accessor test with Kotlin version --- diff --git a/src/test/java/net/pterodactylus/sone/template/PostAccessorTest.java b/src/test/java/net/pterodactylus/sone/template/PostAccessorTest.java deleted file mode 100644 index e60dce9..0000000 --- a/src/test/java/net/pterodactylus/sone/template/PostAccessorTest.java +++ /dev/null @@ -1,118 +0,0 @@ -package net.pterodactylus.sone.template; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.contains; -import static org.hamcrest.Matchers.is; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.Set; - -import net.pterodactylus.sone.core.Core; -import net.pterodactylus.sone.data.Post; -import net.pterodactylus.sone.data.PostReply; -import net.pterodactylus.sone.data.Sone; -import net.pterodactylus.util.template.TemplateContext; - -import org.junit.Before; -import org.junit.Test; - -/** - * Unit test for {@link PostAccessor}. - */ -public class PostAccessorTest { - - private final Core core = mock(Core.class); - private final PostAccessor accessor = new PostAccessor(core); - private final Post post = mock(Post.class); - - private final long now = System.currentTimeMillis(); - - @Before - public void setupPost() { - when(post.getId()).thenReturn("post-id"); - } - - @Test - @SuppressWarnings("unchecked") - public void accessorReturnsTheCorrectReplies() { - List replies = new ArrayList<>(); - replies.add(createPostReply(2000)); - replies.add(createPostReply(-1000)); - replies.add(createPostReply(-2000)); - replies.add(createPostReply(-3000)); - replies.add(createPostReply(-4000)); - when(core.getReplies("post-id")).thenReturn(replies); - Collection repliesForPost = (Collection) accessor.get(null, post, "replies"); - assertThat(repliesForPost, contains( - replies.get(1), - replies.get(2), - replies.get(3), - replies.get(4) - )); - } - - private PostReply createPostReply(long timeOffset) { - PostReply postReply = mock(PostReply.class); - when(postReply.getTime()).thenReturn(now + timeOffset); - return postReply; - } - - @Test - @SuppressWarnings("unchecked") - public void accessorReturnsTheLikingSones() { - Set sones = mock(Set.class); - when(core.getLikes(post)).thenReturn(sones); - Set likingSones = (Set) accessor.get(null, post, "likes"); - assertThat(likingSones, is(sones)); - } - - @Test - public void accessorReturnsWhetherTheCurrentSoneLikedAPost() { - Sone sone = mock(Sone.class); - when(sone.isLikedPostId("post-id")).thenReturn(true); - TemplateContext templateContext = new TemplateContext(); - templateContext.set("currentSone", sone); - assertThat(accessor.get(templateContext, post, "liked"), is((Object) true)); - } - - @Test - public void accessorReturnsFalseIfPostIsNotLiked() { - Sone sone = mock(Sone.class); - TemplateContext templateContext = new TemplateContext(); - templateContext.set("currentSone", sone); - assertThat(accessor.get(templateContext, post, "liked"), is((Object) false)); - } - - @Test - public void accessorReturnsFalseIfThereIsNoCurrentSone() { - TemplateContext templateContext = new TemplateContext(); - assertThat(accessor.get(templateContext, post, "liked"), is((Object) false)); - } - - @Test - public void accessorReturnsThatNotKnownPostIsNew() { - assertThat(accessor.get(null, post, "new"), is((Object) true)); - } - - @Test - public void accessorReturnsThatKnownPostIsNotNew() { - when(post.isKnown()).thenReturn(true); - assertThat(accessor.get(null, post, "new"), is((Object) false)); - } - - @Test - public void accessorReturnsIfPostIsBookmarked() { - when(core.isBookmarked(post)).thenReturn(true); - assertThat(accessor.get(null, post, "bookmarked"), is((Object) true)); - } - - @Test - public void accessorReturnsOtherProperties() { - assertThat(accessor.get(null, post, "hashCode"), is((Object) post.hashCode())); - } - -} diff --git a/src/test/kotlin/net/pterodactylus/sone/template/PostAccessorTest.kt b/src/test/kotlin/net/pterodactylus/sone/template/PostAccessorTest.kt new file mode 100644 index 0000000..000e50c --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/template/PostAccessorTest.kt @@ -0,0 +1,102 @@ +package net.pterodactylus.sone.template + +import net.pterodactylus.sone.core.* +import net.pterodactylus.sone.data.* +import net.pterodactylus.sone.test.* +import net.pterodactylus.util.template.* +import org.hamcrest.MatcherAssert.* +import org.hamcrest.Matchers.* +import org.junit.* + +/** + * Unit test for [PostAccessor]. + */ +class PostAccessorTest { + + private val core = mock() + private val accessor = PostAccessor(core) + private val post = mock() + private val now = System.currentTimeMillis() + + @Before + fun setupPost() { + whenever(post.id).thenReturn("post-id") + } + + @Test + fun `accessor returns the correct replies`() { + val replies = listOf( + createPostReply(2000), + createPostReply(-1000), + createPostReply(-2000), + createPostReply(-3000), + createPostReply(-4000) + ) + whenever(core.getReplies("post-id")).thenReturn(replies) + val repliesForPost = accessor[null, post, "replies"] as Collection + assertThat(repliesForPost, contains( + replies[1], + replies[2], + replies[3], + replies[4] + )) + } + + private fun createPostReply(timeOffset: Long) = mock().apply { + whenever(time).thenReturn(now + timeOffset) + } + + @Test + fun `accessor returns the liking sones`() { + val sones = setOf() + whenever(core.getLikes(post)).thenReturn(sones) + val likingSones = accessor[null, post, "likes"] as Set + assertThat(likingSones, equalTo(sones)) + } + + @Test + fun `accessor returns whether the current sone liked a post`() { + val sone = mock() + whenever(sone.isLikedPostId("post-id")).thenReturn(true) + val templateContext = TemplateContext() + templateContext["currentSone"] = sone + assertThat(accessor[templateContext, post, "liked"], equalTo(true)) + } + + @Test + fun `accessor returns false if post is not liked`() { + val sone = mock() + val templateContext = TemplateContext() + templateContext["currentSone"] = sone + assertThat(accessor[templateContext, post, "liked"], equalTo(false)) + } + + @Test + fun `accessor returns false if there is no current sone`() { + val templateContext = TemplateContext() + assertThat(accessor[templateContext, post, "liked"], equalTo(false)) + } + + @Test + fun `accessor returns that not known post is new`() { + assertThat(accessor[null, post, "new"], equalTo(true)) + } + + @Test + fun `accessor returns that known post is not new`() { + whenever(post.isKnown).thenReturn(true) + assertThat(accessor[null, post, "new"], equalTo(false)) + } + + @Test + fun `accessor returns if post is bookmarked`() { + whenever(core.isBookmarked(post)).thenReturn(true) + assertThat(accessor[null, post, "bookmarked"], equalTo(true)) + } + + @Test + fun `accessor returns other properties`() { + assertThat(accessor[null, post, "hashCode"], equalTo(post.hashCode())) + } + +}