From 571211f99055f782d88418bd44d389d42326b570 Mon Sep 17 00:00:00 2001 From: usbharu Date: Mon, 17 Jun 2024 16:10:02 +0900 Subject: [PATCH] =?UTF-8?q?test:=20Filter=E3=81=AE=E3=83=86=E3=82=B9?= =?UTF-8?q?=E3=83=88=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/model/application/ApplicationId.kt | 6 +- .../model/application/ApplicationName.kt | 10 +- .../core/domain/model/emoji/EmojiId.kt | 6 +- .../core/domain/model/filter/Filter.kt | 12 +- .../core/domain/model/filter/FilterId.kt | 6 +- .../core/domain/model/filter/FilterName.kt | 12 +- .../usbharu/hideout/EqualsAndToStringTest.kt | 2 - .../model/application/ApplicationIdTest.kt | 20 +++ .../model/application/ApplicationNameTest.kt | 20 +++ .../model/application/ApplicationTest.kt | 13 ++ .../core/domain/model/emoji/EmojiIdTest.kt | 20 +++ .../core/domain/model/filter/FilterIdTest.kt | 20 +++ .../domain/model/filter/FilterNameTest.kt | 13 ++ .../core/domain/model/filter/FilterTest.kt | 138 ++++++++++++++++++ 14 files changed, 287 insertions(+), 11 deletions(-) create mode 100644 hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/application/ApplicationIdTest.kt create mode 100644 hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/application/ApplicationNameTest.kt create mode 100644 hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/application/ApplicationTest.kt create mode 100644 hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/emoji/EmojiIdTest.kt create mode 100644 hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterIdTest.kt create mode 100644 hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterNameTest.kt create mode 100644 hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterTest.kt diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/application/ApplicationId.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/application/ApplicationId.kt index 6daadcf0..41007237 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/application/ApplicationId.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/application/ApplicationId.kt @@ -17,4 +17,8 @@ package dev.usbharu.hideout.core.domain.model.application @JvmInline -value class ApplicationId(val id: Long) +value class ApplicationId(val id: Long) { + init { + require(0 <= id) + } +} diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/application/ApplicationName.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/application/ApplicationName.kt index 6b9f9a77..c57f4755 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/application/ApplicationName.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/application/ApplicationName.kt @@ -17,4 +17,12 @@ package dev.usbharu.hideout.core.domain.model.application @JvmInline -value class ApplicationName(val name: String) +value class ApplicationName(val name: String) { + init { + require(name.length <= LENGTH) + } + + companion object { + const val LENGTH = 300 + } +} \ No newline at end of file diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/emoji/EmojiId.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/emoji/EmojiId.kt index 0ba25e1a..5cf4284d 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/emoji/EmojiId.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/emoji/EmojiId.kt @@ -17,4 +17,8 @@ package dev.usbharu.hideout.core.domain.model.emoji @JvmInline -value class EmojiId(val emojiId: Long) +value class EmojiId(val emojiId: Long) { + init { + require(0 <= emojiId) + } +} diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/Filter.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/Filter.kt index 41a835b3..dc26a715 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/Filter.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/Filter.kt @@ -21,6 +21,11 @@ class Filter( this.filterKeywords = filterKeywords } + /** + * フィルターを正規表現として表現したものを返します + * + * @return フィルターの正規表現 + */ fun compileFilter(): Regex { val words = mutableListOf() val wholeWords = mutableListOf() @@ -33,10 +38,11 @@ class Filter( 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) - .joinToString("|") - .toRegex() + return listOfNotNull(wholeWordsRegex, noneWordsRegex, regex).joinToString("|").toRegex() } fun reconstructWith(filterKeywords: Set): Filter { diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterId.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterId.kt index ce42a4c4..a9ee5ce4 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterId.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterId.kt @@ -1,4 +1,8 @@ package dev.usbharu.hideout.core.domain.model.filter @JvmInline -value class FilterId(val id: Long) +value class FilterId(val id: Long) { + init { + require(0 <= id) + } +} diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterName.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterName.kt index 09e424de..4f398bd3 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterName.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterName.kt @@ -1,4 +1,12 @@ 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 + } +} diff --git a/hideout-core/src/test/kotlin/dev/usbharu/hideout/EqualsAndToStringTest.kt b/hideout-core/src/test/kotlin/dev/usbharu/hideout/EqualsAndToStringTest.kt index 96f3655f..d6325826 100644 --- a/hideout-core/src/test/kotlin/dev/usbharu/hideout/EqualsAndToStringTest.kt +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/EqualsAndToStringTest.kt @@ -19,7 +19,6 @@ package dev.usbharu.hideout import com.fasterxml.jackson.module.kotlin.isKotlinClass import com.jparams.verifier.tostring.ToStringVerifier 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.Warning import nl.jqno.equalsverifier.internal.reflection.PackageScanner @@ -115,7 +114,6 @@ class EqualsAndToStringTest { .filterNot { it.superclass.isSealed } - .filterNot { it == UnicodeEmoji::class.java } .map { dynamicTest(it.name) { diff --git a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/application/ApplicationIdTest.kt b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/application/ApplicationIdTest.kt new file mode 100644 index 00000000..e8c08c9d --- /dev/null +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/application/ApplicationIdTest.kt @@ -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 { + ApplicationId(-1) + } + } + + @Test + fun applicationIdが0以上なら設定できる() { + assertDoesNotThrow { + ApplicationId(1) + } + } +} \ No newline at end of file diff --git a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/application/ApplicationNameTest.kt b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/application/ApplicationNameTest.kt new file mode 100644 index 00000000..a9fddfa1 --- /dev/null +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/application/ApplicationNameTest.kt @@ -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 { + ApplicationName("a".repeat(1000)) + } + } + + @Test + fun applicationNameがlength未満の時設定できる() { + assertDoesNotThrow { + ApplicationName("a".repeat(100)) + } + } +} diff --git a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/application/ApplicationTest.kt b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/application/ApplicationTest.kt new file mode 100644 index 00000000..af51c836 --- /dev/null +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/application/ApplicationTest.kt @@ -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") + ) + } +} \ No newline at end of file diff --git a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/emoji/EmojiIdTest.kt b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/emoji/EmojiIdTest.kt new file mode 100644 index 00000000..c304294b --- /dev/null +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/emoji/EmojiIdTest.kt @@ -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 { + EmojiId(-1) + } + } + + @Test + fun emojiIdは0以上なら設定できる() { + assertDoesNotThrow { + EmojiId(1) + } + } +} \ No newline at end of file diff --git a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterIdTest.kt b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterIdTest.kt new file mode 100644 index 00000000..f91e4d5f --- /dev/null +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterIdTest.kt @@ -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 { + FilterId(-1) + } + } + + @Test + fun filterIdが0以上なら設定できる() { + assertDoesNotThrow { + FilterId(1) + } + } +} \ No newline at end of file diff --git a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterNameTest.kt b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterNameTest.kt new file mode 100644 index 00000000..00bd6c84 --- /dev/null +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterNameTest.kt @@ -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) + } +} \ No newline at end of file diff --git a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterTest.kt b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterTest.kt new file mode 100644 index 00000000..9512f9c7 --- /dev/null +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterTest.kt @@ -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) + + } +} +