🎨 Rename method to create replies for tests
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / text / SoneMentionDetectorTest.kt
index 24d2335..1c08480 100644 (file)
@@ -1,5 +1,5 @@
 /**
- * Sone - SoneMentionDetectorTest.kt - Copyright Â© 2019 David â€˜Bombe’ Roden
+ * Sone - SoneMentionDetectorTest.kt - Copyright Â© 2019–2020 David â€˜Bombe’ Roden
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -17,8 +17,6 @@
 
 package net.pterodactylus.sone.text
 
-import com.google.common.base.*
-import com.google.common.base.Optional.*
 import com.google.common.eventbus.*
 import net.pterodactylus.sone.core.event.*
 import net.pterodactylus.sone.data.*
@@ -34,7 +32,8 @@ import kotlin.test.*
 @Suppress("UnstableApiUsage")
 class SoneMentionDetectorTest {
 
-       private val eventBus = EventBus()
+       private val caughtExceptions = mutableListOf<Throwable>()
+       private val eventBus = EventBus { exception, _ -> caughtExceptions += exception }
        private val soneProvider = TestSoneProvider()
        private val postProvider = TestPostProvider()
        private val soneTextParser = SoneTextParser(soneProvider, postProvider)
@@ -101,14 +100,14 @@ class SoneMentionDetectorTest {
 
        @Test
        fun `detector does not emit event for reply that contains no sones`() {
-               val reply = emptyPostReply()
+               val reply = createPostReply()
                eventBus.post(NewPostReplyFoundEvent(reply))
                assertThat(capturedFoundEvents, emptyIterable())
        }
 
        @Test
        fun `detector does not emit event for reply that contains two links to remote sones`() {
-               val reply = emptyPostReply("text mentions sone://${remoteSone1.id} and sone://${remoteSone2.id}.")
+               val reply = createPostReply("text mentions sone://${remoteSone1.id} and sone://${remoteSone2.id}.")
                eventBus.post(NewPostReplyFoundEvent(reply))
                assertThat(capturedFoundEvents, emptyIterable())
        }
@@ -116,7 +115,7 @@ class SoneMentionDetectorTest {
        @Test
        fun `detector emits event on reply that contains links to a remote and a local sone`() {
                val post = createPost()
-               val reply = emptyPostReply("text mentions sone://${remoteSone1.id} and sone://${localSone1.id}.", post)
+               val reply = createPostReply("text mentions sone://${remoteSone1.id} and sone://${localSone1.id}.", post)
                eventBus.post(NewPostReplyFoundEvent(reply))
                assertThat(capturedFoundEvents, contains(MentionOfLocalSoneFoundEvent(post)))
        }
@@ -124,7 +123,7 @@ class SoneMentionDetectorTest {
        @Test
        fun `detector emits one event on reply that contains two links to the same local sone`() {
                val post = createPost()
-               val reply = emptyPostReply("text mentions sone://${localSone1.id} and sone://${localSone1.id}.", post)
+               val reply = createPostReply("text mentions sone://${localSone1.id} and sone://${localSone1.id}.", post)
                eventBus.post(NewPostReplyFoundEvent(reply))
                assertThat(capturedFoundEvents, contains(MentionOfLocalSoneFoundEvent(post)))
        }
@@ -132,19 +131,27 @@ class SoneMentionDetectorTest {
        @Test
        fun `detector emits one event on reply that contains two links to local sones`() {
                val post = createPost()
-               val reply = emptyPostReply("text mentions sone://${localSone1.id} and sone://${localSone2.id}.", post)
+               val reply = createPostReply("text mentions sone://${localSone1.id} and sone://${localSone2.id}.", post)
                eventBus.post(NewPostReplyFoundEvent(reply))
                assertThat(capturedFoundEvents, contains(MentionOfLocalSoneFoundEvent(post)))
        }
 
        @Test
        fun `detector does not emit event for reply by local sone`() {
-               val reply = emptyPostReply("text mentions sone://${localSone1.id} and sone://${localSone2.id}.", sone = localSone1)
+               val reply = createPostReply("text mentions sone://${localSone1.id} and sone://${localSone2.id}.", sone = localSone1)
                eventBus.post(NewPostReplyFoundEvent(reply))
                assertThat(capturedFoundEvents, emptyIterable())
        }
 
        @Test
+       fun `detector does not emit event for reply without post`() {
+               val reply = createPostReply("text mentions sone://${localSone1.id} and sone://${localSone2.id}.", post = null)
+               eventBus.post(NewPostReplyFoundEvent(reply))
+               assertThat(caughtExceptions, emptyIterable())
+               assertThat(capturedFoundEvents, emptyIterable())
+       }
+
+       @Test
        fun `detector does not emit removed event when a post without mention is removed`() {
                val post = createPost()
                eventBus.post(PostRemovedEvent(post))
@@ -177,7 +184,7 @@ class SoneMentionDetectorTest {
        @Test
        fun `detector does emit removed event when reply with mention is removed and no more mentions in that post exist`() {
                val post = createPost()
-               val reply = emptyPostReply("sone://${localSone1.id}", post)
+               val reply = createPostReply("sone://${localSone1.id}", post)
                postReplyProvider.postReplies[post.id] = listOf(reply)
                eventBus.post(NewPostReplyFoundEvent(reply))
                eventBus.post(PostReplyRemovedEvent(reply))
@@ -187,7 +194,7 @@ class SoneMentionDetectorTest {
        @Test
        fun `detector does not emit removed event when reply with mention is removed and post mentions local sone`() {
                val post = createPost("sone://${localSone1.id}")
-               val reply = emptyPostReply("sone://${localSone1.id}", post)
+               val reply = createPostReply("sone://${localSone1.id}", post)
                eventBus.post(NewPostReplyFoundEvent(reply))
                eventBus.post(PostReplyRemovedEvent(reply))
                assertThat(capturedRemovedEvents, emptyIterable())
@@ -196,7 +203,7 @@ class SoneMentionDetectorTest {
        @Test
        fun `detector does emit removed event when reply with mention is removed and post mentions local sone but is known`() {
                val post = createPost("sone://${localSone1.id}", known = true)
-               val reply = emptyPostReply("sone://${localSone1.id}", post)
+               val reply = createPostReply("sone://${localSone1.id}", post)
                eventBus.post(NewPostReplyFoundEvent(reply))
                eventBus.post(PostReplyRemovedEvent(reply))
                assertThat(capturedRemovedEvents, contains(MentionOfLocalSoneRemovedEvent(post)))
@@ -205,8 +212,8 @@ class SoneMentionDetectorTest {
        @Test
        fun `detector does not emit removed event when reply with mention is removed and post has other replies with mentions`() {
                val post = createPost()
-               val reply1 = emptyPostReply("sone://${localSone1.id}", post)
-               val reply2 = emptyPostReply("sone://${localSone1.id}", post)
+               val reply1 = createPostReply("sone://${localSone1.id}", post)
+               val reply2 = createPostReply("sone://${localSone1.id}", post)
                postReplyProvider.postReplies[post.id] = listOf(reply1, reply2)
                eventBus.post(NewPostReplyFoundEvent(reply1))
                eventBus.post(PostReplyRemovedEvent(reply1))
@@ -216,8 +223,8 @@ class SoneMentionDetectorTest {
        @Test
        fun `detector does emit removed event when reply with mention is removed and post has other replies with mentions which are known`() {
                val post = createPost()
-               val reply1 = emptyPostReply("sone://${localSone1.id}", post)
-               val reply2 = emptyPostReply("sone://${localSone1.id}", post, known = true)
+               val reply1 = createPostReply("sone://${localSone1.id}", post)
+               val reply2 = createPostReply("sone://${localSone1.id}", post, known = true)
                postReplyProvider.postReplies[post.id] = listOf(reply1, reply2)
                eventBus.post(NewPostReplyFoundEvent(reply1))
                eventBus.post(PostReplyRemovedEvent(reply1))
@@ -226,20 +233,6 @@ class SoneMentionDetectorTest {
 
 }
 
-private val remoteSone1 = createRemoteSone()
-private val remoteSone2 = createRemoteSone()
-
-private val localSone1 = createLocalSone()
-private val localSone2 = createLocalSone()
-
-private fun createPost(text: String = "", sone: Sone = remoteSone1, known: Boolean = false): Post.EmptyPost {
-       return object : Post.EmptyPost("post-id") {
-               override fun getSone() = sone
-               override fun getText() = text
-               override fun isKnown() = known
-       }
-}
-
 private class TestSoneProvider : SoneProvider {
 
        override val sones: Collection<Sone> get() = remoteSones + localSones
@@ -268,14 +261,3 @@ private class TestPostReplyProvider : PostReplyProvider {
        override fun getReplies(postId: String) = postReplies[postId] ?: emptyList()
 
 }
-
-private fun emptyPostReply(text: String = "", post: Post = createPost(), sone: Sone = remoteSone1, known: Boolean = false) = object : PostReply {
-       override val id = "reply-id"
-       override fun getSone() = sone
-       override fun getPostId() = post.id
-       override fun getPost(): Optional<Post> = of(post)
-       override fun getTime() = 1L
-       override fun getText() = text
-       override fun isKnown() = known
-       override fun setKnown(known: Boolean): PostReply = this
-}