Skip to content

Commit

Permalink
test: ReplacerProcessorTestの分割、Replacer複合テストの追加 (#187)
Browse files Browse the repository at this point in the history
* test: ReplacerProcessorTestの分割、Replacer複合テストの追加

* Update src/test/kotlin/replacers/UrlReplacerTest.kt

Co-authored-by: yuuaHP <identity@yuua.dev>

* Update src/test/kotlin/processors/ReplacerProcessorTest.kt

Co-authored-by: yuuaHP <identity@yuua.dev>

* Update src/test/kotlin/processors/ReplacerProcessorTest.kt

Co-authored-by: yuuaHP <identity@yuua.dev>

---------

Co-authored-by: yuuaHP <identity@yuua.dev>
  • Loading branch information
book000 and yuuahp authored Jul 19, 2024
1 parent d053ca2 commit 8d4a1f5
Show file tree
Hide file tree
Showing 9 changed files with 1,793 additions and 1,349 deletions.
1,456 changes: 107 additions & 1,349 deletions src/test/kotlin/processors/ReplacerProcessorTest.kt

Large diffs are not rendered by default.

92 changes: 92 additions & 0 deletions src/test/kotlin/replacers/AliasReplacerTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package replacers

import com.jaoafa.vcspeaker.VCSpeaker
import com.jaoafa.vcspeaker.stores.*
import com.jaoafa.vcspeaker.tts.Token
import com.jaoafa.vcspeaker.tts.replacers.AliasReplacer
import dev.kord.common.entity.Snowflake
import dev.kord.core.entity.Message
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import io.mockk.*
import java.io.File

class AliasReplacerTest : FunSpec({
// テスト前にモックを初期化
beforeTest {
mockkObject(VCSpeaker)
every { VCSpeaker.storeFolder } returns File(System.getProperty("java.io.tmpdir") + File.separator + "vcspeaker")

val storeStruct = mockk<StoreStruct<IgnoreData>>()
every { storeStruct.write() } returns Unit

mockkObject(IgnoreStore)
every { IgnoreStore.write() } returns Unit

IgnoreStore.data.clear()

mockkObject(AliasStore)
every { AliasStore.write() } returns Unit

AliasStore.data.clear()
}

// テスト後にモックを削除
afterTest {
clearAllMocks()
}

// 全てのテスト後にフォルダを削除
finalizeSpec {
VCSpeaker.storeFolder.deleteRecursively()
}

// テキストエイリアスを設定した場合、正しく置き換えられる
test("If a text alias matches the message, the replaced text should be returned.") {
val message = mockk<Message>()
coEvery { message.getGuild() } returns mockk {
every { id } returns Snowflake(0)
}

AliasStore.create(
AliasData(
guildId = Snowflake(0),
userId = Snowflake(0),
type = AliasType.Text,
search = "world",
replace = "Kotlin"
)
)

val tokens = mutableListOf(Token("Hello, world!"))
val expectedTokens = mutableListOf(Token("Hello, "), Token("Kotlin", "Text Alias「world」→「Kotlin」"), Token("!"))

val processedTokens = AliasReplacer.replace(tokens, Snowflake(0))

processedTokens shouldBe expectedTokens
}

// テキストエイリアスを設定していても合致しない場合、変更されない
test("If a text alias does not match the content, the text should remain unchanged.") {
val message = mockk<Message>()
coEvery { message.getGuild() } returns mockk {
every { id } returns Snowflake(0)
}

AliasStore.create(
AliasData(
guildId = Snowflake(0),
userId = Snowflake(0),
type = AliasType.Text,
search = "Java",
replace = "Kotlin"
)
)

val tokens = mutableListOf(Token("Hello, world!"))

val processedTokens = AliasReplacer.replace(tokens, Snowflake(0))

processedTokens shouldBe tokens
}
})
88 changes: 88 additions & 0 deletions src/test/kotlin/replacers/ChannelMentionReplacerTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package replacers

import com.jaoafa.vcspeaker.VCSpeaker
import com.jaoafa.vcspeaker.tts.Token
import com.jaoafa.vcspeaker.tts.replacers.ChannelMentionReplacer
import dev.kord.common.entity.Snowflake
import dev.kord.core.ClientResources
import dev.kord.core.entity.Message
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import io.mockk.*
import java.io.File

class ChannelMentionReplacerTest : FunSpec({
// テスト前にモックを初期化
beforeTest {
mockkObject(VCSpeaker)
every { VCSpeaker.storeFolder } returns File(System.getProperty("java.io.tmpdir") + File.separator + "vcspeaker")
}

// テスト後にモックを削除
afterTest {
clearAllMocks()
}

// 全てのテスト後にフォルダを削除
finalizeSpec {
VCSpeaker.storeFolder.deleteRecursively()
}

// 既知のチャンネルメンションを置き換える
test("Mentions of known channels should be replaced with their associated name.") {
every { VCSpeaker.kord } returns mockk {
every { resources } returns mockk<ClientResources>() // kordをmock化するために必要
coEvery { getChannel(Snowflake(123456789012345678)) } returns mockk {
every { data } returns mockk {
every { name } returns mockk {
every { value } returns "test-channel" // テスト用のチャンネル名
}
}
}
}

val message = mockk<Message>()
coEvery { message.getGuild() } returns mockk {
every { id } returns Snowflake(0)
}

val tokens = mutableListOf(Token("Hello, <#123456789012345678>!"))
val expectedTokens = mutableListOf(
Token("Hello, "),
Token("#test-channel", "Mentionable `123456789012345678` →「test-channel」"),
Token("!")
)

val processedTokens = ChannelMentionReplacer.replace(
tokens, Snowflake(0)
)

processedTokens shouldBe expectedTokens
}

// 未知のチャンネルメンションを置き換える
test("Mentions of unknown channels should be replaced as unknown channels.") {
every { VCSpeaker.kord } returns mockk {
every { resources } returns mockk<ClientResources>() // kordをmock化するために必要
coEvery { getChannel(Snowflake(123456789012345678)) } returns null
}

val message = mockk<Message>()
coEvery { message.getGuild() } returns mockk {
every { id } returns Snowflake(0)
}

val tokens = mutableListOf(Token("Hello, <#123456789012345678>!"))
val expectedTokens = mutableListOf(
Token("Hello, "),
Token("#不明なチャンネル", "Mentionable `123456789012345678` →「不明なチャンネル」"),
Token("!")
)

val processedTokens = ChannelMentionReplacer.replace(
tokens, Snowflake(0)
)

processedTokens shouldBe expectedTokens
}
})
97 changes: 97 additions & 0 deletions src/test/kotlin/replacers/EmojiReplacerTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package replacers

import com.jaoafa.vcspeaker.VCSpeaker
import com.jaoafa.vcspeaker.stores.*
import com.jaoafa.vcspeaker.tts.Token
import com.jaoafa.vcspeaker.tts.replacers.EmojiReplacer
import com.jaoafa.vcspeaker.tts.replacers.RegexReplacer
import dev.kord.common.entity.Snowflake
import dev.kord.core.entity.Message
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import io.mockk.*
import java.io.File

class EmojiReplacerTest : FunSpec({
// テスト前にモックを初期化
beforeTest {
mockkObject(VCSpeaker)
every { VCSpeaker.storeFolder } returns File(System.getProperty("java.io.tmpdir") + File.separator + "vcspeaker")

val storeStruct = mockk<StoreStruct<IgnoreData>>()
every { storeStruct.write() } returns Unit

mockkObject(IgnoreStore)
every { IgnoreStore.write() } returns Unit

IgnoreStore.data.clear()

mockkObject(AliasStore)
every { AliasStore.write() } returns Unit

AliasStore.data.clear()
}

// テスト後にモックを削除
afterTest {
clearAllMocks()
}

// 全てのテスト後にフォルダを削除
finalizeSpec {
VCSpeaker.storeFolder.deleteRecursively()
}

// 絵文字エイリアスを設定した場合、正しく置き換えられる
test("If an emoji alias match the content, the replaced text should be returned.") {
val message = mockk<Message>()
coEvery { message.getGuild() } returns mockk {
every { id } returns Snowflake(0)
}

AliasStore.create(
AliasData(
guildId = Snowflake(0),
userId = Snowflake(0),
type = AliasType.Emoji,
search = "<:world:123456789012345678>",
replace = "world"
)
)

val tokens = mutableListOf(Token("Hello, <:world:123456789012345678>!"))
val expectedTokens = mutableListOf(
Token("Hello, "),
Token("world", "Emoji Alias「<:world:123456789012345678>」→「world」"),
Token("!")
)

val processedTokens = EmojiReplacer.replace(tokens, Snowflake(0))

processedTokens shouldBe expectedTokens
}

// 絵文字エイリアスを設定していても合致しない場合、変更されない
test("If a emoji alias does not match the content, the text should remain unchanged.") {
val message = mockk<Message>()
coEvery { message.getGuild() } returns mockk {
every { id } returns Snowflake(0)
}

AliasStore.create(
AliasData(
guildId = Snowflake(0),
userId = Snowflake(0),
type = AliasType.Emoji,
search = "<:kotlin:876543210987654321>",
replace = "Kotlin"
)
)

val tokens = mutableListOf(Token("Hello, <:world:123456789012345678>!"))

val processedTokens = RegexReplacer.replace(tokens, Snowflake(0))

processedTokens shouldBe tokens
}
})
84 changes: 84 additions & 0 deletions src/test/kotlin/replacers/GuildEmojiReplacerTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package replacers

import com.jaoafa.vcspeaker.VCSpeaker
import com.jaoafa.vcspeaker.stores.AliasStore
import com.jaoafa.vcspeaker.stores.IgnoreData
import com.jaoafa.vcspeaker.stores.IgnoreStore
import com.jaoafa.vcspeaker.stores.StoreStruct
import com.jaoafa.vcspeaker.tts.Token
import com.jaoafa.vcspeaker.tts.replacers.GuildEmojiReplacer
import dev.kord.common.entity.Snowflake
import dev.kord.core.entity.Message
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import io.mockk.*
import java.io.File

class GuildEmojiReplacerTest : FunSpec({
// テスト前にモックを初期化
beforeTest {
mockkObject(VCSpeaker)
every { VCSpeaker.storeFolder } returns File(System.getProperty("java.io.tmpdir") + File.separator + "vcspeaker")

val storeStruct = mockk<StoreStruct<IgnoreData>>()
every { storeStruct.write() } returns Unit

mockkObject(IgnoreStore)
every { IgnoreStore.write() } returns Unit

IgnoreStore.data.clear()

mockkObject(AliasStore)
every { AliasStore.write() } returns Unit

AliasStore.data.clear()
}

// テスト後にモックを削除
afterTest {
clearAllMocks()
}

// 全てのテスト後にフォルダを削除
finalizeSpec {
VCSpeaker.storeFolder.deleteRecursively()
}

// サーバ絵文字が絵文字名に置き換わること
test("If an server emoji found, the replaced text should be returned.") {
val message = mockk<Message>()
coEvery { message.getGuild() } returns mockk {
every { id } returns Snowflake(0)
}

val tokens = mutableListOf(Token("Hello, <:world:123456789012345678>!"))
val expectedTokens = mutableListOf(
Token("Hello, "),
Token("world", "Guild Emoji `<:world:123456789012345678>` →「world」"),
Token("!")
)

val processedTokens = GuildEmojiReplacer.replace(tokens, Snowflake(0))

processedTokens shouldBe expectedTokens
}

// サーバGIF絵文字が絵文字名に置き換わること
test("If an server GIF emoji found, the replaced text should be returned.") {
val message = mockk<Message>()
coEvery { message.getGuild() } returns mockk {
every { id } returns Snowflake(0)
}

val tokens = mutableListOf(Token("Hello, <a:world:123456789012345678>!"))
val expectedTokens = mutableListOf(
Token("Hello, "),
Token("world", "Guild Emoji `<a:world:123456789012345678>` →「world」"),
Token("!")
)

val processedTokens = GuildEmojiReplacer.replace(tokens, Snowflake(0))

processedTokens shouldBe expectedTokens
}
})
Loading

0 comments on commit 8d4a1f5

Please sign in to comment.