2 * Sone - SoneMentionDetectorTest.kt - Copyright © 2019 David ‘Bombe’ Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.text
20 import com.google.common.eventbus.*
21 import net.pterodactylus.sone.core.event.*
22 import net.pterodactylus.sone.data.*
23 import net.pterodactylus.sone.database.*
24 import net.pterodactylus.sone.test.*
25 import org.hamcrest.MatcherAssert.*
26 import org.hamcrest.Matchers.*
30 * Unit test for [SoneMentionDetector].
32 class SoneMentionDetectorTest {
34 private val eventBus = EventBus()
35 private val soneProvider = TestSoneProvider()
36 private val postProvider = TestPostProvider()
37 private val soneTextParser = SoneTextParser(soneProvider, postProvider)
38 private val capturedEvents = mutableListOf<LocalSoneMentionedInPostEvent>()
41 eventBus.register(SoneMentionDetector(eventBus, soneTextParser))
42 eventBus.register(object : Any() {
44 fun captureEvent(localSoneMentionedInPostEvent: LocalSoneMentionedInPostEvent) {
45 capturedEvents += localSoneMentionedInPostEvent
51 fun `detector does not emit event on post that does not contain any sones`() {
52 val post = createPost()
53 eventBus.post(NewPostFoundEvent(post))
54 assertThat(capturedEvents, emptyIterable())
58 fun `detector does not emit event on post that does contain two remote sones`() {
59 val post = createPost("text mentions sone://${remoteSone1.id} and sone://${remoteSone2.id}.")
60 eventBus.post(NewPostFoundEvent(post))
61 assertThat(capturedEvents, emptyIterable())
65 fun `detector emits event on post that contains links to a remote and a local sone`() {
66 val post = createPost("text mentions sone://${localSone1.id} and sone://${remoteSone2.id}.")
67 eventBus.post(NewPostFoundEvent(post))
68 assertThat(capturedEvents, contains(LocalSoneMentionedInPostEvent(post)))
72 fun `detector emits one event on post that contains two links to the same local sone`() {
73 val post = createPost("text mentions sone://${localSone1.id} and sone://${localSone1.id}.")
74 eventBus.post(NewPostFoundEvent(post))
75 assertThat(capturedEvents, contains(LocalSoneMentionedInPostEvent(post)))
79 fun `detector emits one event on post that contains links to two local sones`() {
80 val post = createPost("text mentions sone://${localSone1.id} and sone://${localSone2.id}.")
81 eventBus.post(NewPostFoundEvent(post))
82 assertThat(capturedEvents, contains(LocalSoneMentionedInPostEvent(post)))
86 fun `detector does not emit event for post by local sone`() {
87 val post = createPost("text mentions sone://${localSone1.id} and sone://${localSone2.id}.", localSone1)
88 eventBus.post(NewPostFoundEvent(post))
89 assertThat(capturedEvents, emptyIterable())
94 private val remoteSone1 = createRemoteSone()
95 private val remoteSone2 = createRemoteSone()
97 private val localSone1 = createLocalSone()
98 private val localSone2 = createLocalSone()
100 private fun createPost(text: String = "", sone: Sone = remoteSone1): Post.EmptyPost {
101 return object : Post.EmptyPost("post-id") {
102 override fun getSone() = sone
103 override fun getText() = text
107 private class TestSoneProvider : SoneProvider {
109 override val sones: Collection<Sone> get() = remoteSones + localSones
110 override val localSones: Collection<Sone> get() = setOf(localSone1, localSone2)
111 override val remoteSones: Collection<Sone> get() = setOf(remoteSone1, remoteSone2)
112 override val soneLoader: (String) -> Sone? get() = this::getSone
113 override fun getSone(soneId: String): Sone? =
114 localSones.firstOrNull { it.id == soneId } ?: remoteSones.firstOrNull { it.id == soneId }
118 private class TestPostProvider : PostProvider {
120 override fun getPost(postId: String): Post? = null
121 override fun getPosts(soneId: String): Collection<Post> = emptyList()
122 override fun getDirectedPosts(recipientId: String): Collection<Post> = emptyList()