test: Filterのテストを追加

This commit is contained in:
usbharu 2024-06-17 16:10:02 +09:00
parent 845569a7f9
commit 571211f990
14 changed files with 287 additions and 11 deletions

View File

@ -17,4 +17,8 @@
package dev.usbharu.hideout.core.domain.model.application package dev.usbharu.hideout.core.domain.model.application
@JvmInline @JvmInline
value class ApplicationId(val id: Long) value class ApplicationId(val id: Long) {
init {
require(0 <= id)
}
}

View File

@ -17,4 +17,12 @@
package dev.usbharu.hideout.core.domain.model.application package dev.usbharu.hideout.core.domain.model.application
@JvmInline @JvmInline
value class ApplicationName(val name: String) value class ApplicationName(val name: String) {
init {
require(name.length <= LENGTH)
}
companion object {
const val LENGTH = 300
}
}

View File

@ -17,4 +17,8 @@
package dev.usbharu.hideout.core.domain.model.emoji package dev.usbharu.hideout.core.domain.model.emoji
@JvmInline @JvmInline
value class EmojiId(val emojiId: Long) value class EmojiId(val emojiId: Long) {
init {
require(0 <= emojiId)
}
}

View File

@ -21,6 +21,11 @@ class Filter(
this.filterKeywords = filterKeywords this.filterKeywords = filterKeywords
} }
/**
* フィルターを正規表現として表現したものを返します
*
* @return フィルターの正規表現
*/
fun compileFilter(): Regex { fun compileFilter(): Regex {
val words = mutableListOf<String>() val words = mutableListOf<String>()
val wholeWords = mutableListOf<String>() val wholeWords = mutableListOf<String>()
@ -33,10 +38,11 @@ class Filter(
NONE -> words.add(filterKeyword.keyword.keyword) NONE -> words.add(filterKeyword.keyword.keyword)
} }
} }
val wholeWordsRegex = wholeWords.takeIf { it.isNotEmpty() }?.joinToString("|", "\\b(", ")\\b")
val noneWordsRegex = words.takeIf { it.isNotEmpty() }?.joinToString("|", "(", ")")
val regex = regexes.takeIf { it.isNotEmpty() }?.joinToString("|", "(", ")")
return (wholeWords + regexes + wholeWords) return listOfNotNull(wholeWordsRegex, noneWordsRegex, regex).joinToString("|").toRegex()
.joinToString("|")
.toRegex()
} }
fun reconstructWith(filterKeywords: Set<FilterKeyword>): Filter { fun reconstructWith(filterKeywords: Set<FilterKeyword>): Filter {

View File

@ -1,4 +1,8 @@
package dev.usbharu.hideout.core.domain.model.filter package dev.usbharu.hideout.core.domain.model.filter
@JvmInline @JvmInline
value class FilterId(val id: Long) value class FilterId(val id: Long) {
init {
require(0 <= id)
}
}

View File

@ -1,4 +1,12 @@
package dev.usbharu.hideout.core.domain.model.filter package dev.usbharu.hideout.core.domain.model.filter
@JvmInline
value class FilterName(val name: String) class FilterName(name: String) {
val name = name.take(LENGTH)
companion object {
const val LENGTH = 300
}
}

View File

@ -19,7 +19,6 @@ package dev.usbharu.hideout
import com.fasterxml.jackson.module.kotlin.isKotlinClass import com.fasterxml.jackson.module.kotlin.isKotlinClass
import com.jparams.verifier.tostring.ToStringVerifier import com.jparams.verifier.tostring.ToStringVerifier
import com.jparams.verifier.tostring.preset.Presets import com.jparams.verifier.tostring.preset.Presets
import dev.usbharu.hideout.core.domain.model.emoji.UnicodeEmoji
import nl.jqno.equalsverifier.EqualsVerifier import nl.jqno.equalsverifier.EqualsVerifier
import nl.jqno.equalsverifier.Warning import nl.jqno.equalsverifier.Warning
import nl.jqno.equalsverifier.internal.reflection.PackageScanner import nl.jqno.equalsverifier.internal.reflection.PackageScanner
@ -115,7 +114,6 @@ class EqualsAndToStringTest {
.filterNot { .filterNot {
it.superclass.isSealed it.superclass.isSealed
} }
.filterNot { it == UnicodeEmoji::class.java }
.map { .map {
dynamicTest(it.name) { dynamicTest(it.name) {

View File

@ -0,0 +1,20 @@
package dev.usbharu.hideout.core.domain.model.application
import org.junit.jupiter.api.Assertions.assertDoesNotThrow
import org.junit.jupiter.api.Test
class ApplicationIdTest {
@Test
fun applicationIdは0以上である必要がある() {
org.junit.jupiter.api.assertThrows<IllegalArgumentException> {
ApplicationId(-1)
}
}
@Test
fun applicationIdが0以上なら設定できる() {
assertDoesNotThrow {
ApplicationId(1)
}
}
}

View File

@ -0,0 +1,20 @@
package dev.usbharu.hideout.core.domain.model.application
import org.junit.jupiter.api.Assertions.assertDoesNotThrow
import org.junit.jupiter.api.Test
class ApplicationNameTest {
@Test
fun applicationNameがlength以上の時エラー() {
org.junit.jupiter.api.assertThrows<IllegalArgumentException> {
ApplicationName("a".repeat(1000))
}
}
@Test
fun applicationNameがlength未満の時設定できる() {
assertDoesNotThrow {
ApplicationName("a".repeat(100))
}
}
}

View File

@ -0,0 +1,13 @@
package dev.usbharu.hideout.core.domain.model.application
import org.junit.jupiter.api.Test
class ApplicationTest {
@Test
fun インスタンスを生成できる() {
Application(
ApplicationId(1),
ApplicationName("aiueo")
)
}
}

View File

@ -0,0 +1,20 @@
package dev.usbharu.hideout.core.domain.model.emoji
import org.junit.jupiter.api.Assertions.assertDoesNotThrow
import org.junit.jupiter.api.Test
class EmojiIdTest {
@Test
fun emojiIdは0以上である必要がある() {
org.junit.jupiter.api.assertThrows<IllegalArgumentException> {
EmojiId(-1)
}
}
@Test
fun emojiIdは0以上なら設定できる() {
assertDoesNotThrow {
EmojiId(1)
}
}
}

View File

@ -0,0 +1,20 @@
package dev.usbharu.hideout.core.domain.model.filter
import org.junit.jupiter.api.Assertions.assertDoesNotThrow
import org.junit.jupiter.api.Test
class FilterIdTest {
@Test
fun filterIdは0以上である必要がある() {
org.junit.jupiter.api.assertThrows<IllegalArgumentException> {
FilterId(-1)
}
}
@Test
fun filterIdが0以上なら設定できる() {
assertDoesNotThrow {
FilterId(1)
}
}
}

View File

@ -0,0 +1,13 @@
package dev.usbharu.hideout.core.domain.model.filter
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals
class FilterNameTest {
@Test
fun FilterNameがlength以上のときは無視される() {
val filterName = FilterName("a".repeat(1000))
assertEquals(FilterName.LENGTH, filterName.name.length)
}
}

View File

@ -0,0 +1,138 @@
package dev.usbharu.hideout.core.domain.model.filter
import dev.usbharu.hideout.core.domain.model.actor.ActorId
import dev.usbharu.hideout.core.domain.model.userdetails.UserDetail
import dev.usbharu.hideout.core.domain.model.userdetails.UserDetailHashedPassword
import dev.usbharu.hideout.core.domain.model.userdetails.UserDetailId
import org.junit.jupiter.api.Assertions.assertDoesNotThrow
import org.junit.jupiter.api.Test
class FilterTest {
@Test
fun `setFilterKeywords 所有者のみ変更できる`() {
val filter = Filter.create(
id = FilterId(1),
userDetailId = UserDetailId(1),
name = FilterName("aiueo"),
filterContext = setOf(),
filterAction = FilterAction.HIDE,
filterKeywords = setOf()
)
val userDetail = UserDetail.create(
id = UserDetailId(1),
actorId = ActorId(1),
password = UserDetailHashedPassword(""),
autoAcceptFolloweeFollowRequest = false,
lastMigration = null
)
assertDoesNotThrow {
filter.setFilterKeywords(
setOf(
FilterKeyword(
FilterKeywordId(1),
FilterKeywordKeyword("keyword"),
FilterMode.NONE
)
), userDetail
)
}
}
@Test
fun compileFilterで正規表現として表すことができるNONE() {
val filter = Filter(
id = FilterId(1),
userDetailId = UserDetailId(1),
name = FilterName("aiueo"),
filterContext = setOf(),
filterAction = FilterAction.HIDE,
filterKeywords = setOf(
FilterKeyword(
FilterKeywordId(1),
FilterKeywordKeyword("hoge"),
FilterMode.NONE
)
)
)
kotlin.test.assertEquals("(hoge)", filter.compileFilter().pattern)
}
@Test
fun compileFilterで正規表現として表すことができるWHOLE_WORD() {
val filter = Filter(
id = FilterId(1),
userDetailId = UserDetailId(1),
name = FilterName("aiueo"),
filterContext = setOf(),
filterAction = FilterAction.HIDE,
filterKeywords = setOf(
FilterKeyword(
FilterKeywordId(1),
FilterKeywordKeyword("hoge"),
FilterMode.WHOLE_WORD
)
)
)
kotlin.test.assertEquals("\\b(hoge)\\b", filter.compileFilter().pattern)
}
@Test
fun compileFilterで正規表現として表すことができるREGEX() {
val filter = Filter(
id = FilterId(1),
userDetailId = UserDetailId(1),
name = FilterName("aiueo"),
filterContext = setOf(),
filterAction = FilterAction.HIDE,
filterKeywords = setOf(
FilterKeyword(
FilterKeywordId(1),
FilterKeywordKeyword("hoge"),
FilterMode.REGEX
)
)
)
kotlin.test.assertEquals("(hoge)", filter.compileFilter().pattern)
}
@Test
fun compileFilterで正規表現として表すことができる() {
val filter = Filter(
id = FilterId(1),
userDetailId = UserDetailId(1),
name = FilterName("aiueo"),
filterContext = setOf(),
filterAction = FilterAction.HIDE,
filterKeywords = setOf(
FilterKeyword(
FilterKeywordId(1),
FilterKeywordKeyword("hoge"),
FilterMode.WHOLE_WORD
),
FilterKeyword(
FilterKeywordId(2),
FilterKeywordKeyword("hoge"),
FilterMode.REGEX
),
FilterKeyword(
FilterKeywordId(3),
FilterKeywordKeyword("hoge"),
FilterMode.NONE
)
)
)
kotlin.test.assertEquals("\\b(hoge)\\b|(hoge)|(hoge)", filter.compileFilter().pattern)
}
}