diff options
author | Linnea Gräf <nea@nea.moe> | 2024-04-23 08:41:33 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-04-23 08:41:33 +0200 |
commit | 68175690b3109f8e94c2a0f93a7a38b9ad993510 (patch) | |
tree | 68ad1ebd5338d064a9836f4591242664e6a513f1 /src/test | |
parent | b40ca6837bd362be9a14b1f027ac176220040f9c (diff) | |
download | skyhanni-68175690b3109f8e94c2a0f93a7a38b9ad993510.tar.gz skyhanni-68175690b3109f8e94c2a0f93a7a38b9ad993510.tar.bz2 skyhanni-68175690b3109f8e94c2a0f93a7a38b9ad993510.zip |
Add component regex matcher API (#1512)
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/java/at/hannibal2/skyhanni/test/ComponentSpanTest.kt | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/src/test/java/at/hannibal2/skyhanni/test/ComponentSpanTest.kt b/src/test/java/at/hannibal2/skyhanni/test/ComponentSpanTest.kt new file mode 100644 index 000000000..9b5f40829 --- /dev/null +++ b/src/test/java/at/hannibal2/skyhanni/test/ComponentSpanTest.kt @@ -0,0 +1,53 @@ +package at.hannibal2.skyhanni.test + +import at.hannibal2.skyhanni.utils.ComponentMatcherUtils.findStyledMatcher +import at.hannibal2.skyhanni.utils.ComponentMatcherUtils.intoSpan +import at.hannibal2.skyhanni.utils.ComponentMatcherUtils.matchStyledMatcher +import net.minecraft.util.ChatComponentText +import net.minecraft.util.ChatStyle +import net.minecraft.util.EnumChatFormatting +import org.junit.jupiter.api.Test +import java.util.regex.Pattern + +class ComponentSpanTest { + private fun text(string: String, init: ChatComponentText.() -> Unit = {}) = ChatComponentText(string).also(init) + + @Test + fun testComponent() { + val component = text("12345") { + appendSibling(text("12345") { + chatStyle = ChatStyle().setColor(EnumChatFormatting.RED) + }) + appendSibling(text("12345")) + } + val span = component.intoSpan() + require(span.sampleStyleAtStart()?.isEmpty == true) + require(span.slice(5, 8).sampleStyleAtStart()?.color == EnumChatFormatting.RED) + require(span.slice(8, 12).sampleStyleAtStart()?.color == EnumChatFormatting.RED) + require(span.slice(10, 12).sampleStyleAtStart()?.isEmpty == true) + require(span.slice(4, 11).intoComponent().formattedText == "§r5§r§c12345§r1§r") + } + + @Test + fun testRegex() { + val component = text("12345") { + appendSibling(text("abcdef") { + chatStyle = ChatStyle().setColor(EnumChatFormatting.RED) + }) + appendSibling(text("12345")) + } + Pattern.compile("[0-9]*(?<middle>[a-z]+)[0-9]*").matchStyledMatcher(component) { + require(group("middle")?.sampleStyleAtStart()?.color == EnumChatFormatting.RED) + } + val middlePartExtracted = + Pattern.compile("[0-9]*(?<middle>[0-9][a-z]+[0-9])[0-9]*").matchStyledMatcher(component) { + require(group("middle")?.sampleComponents()?.size == 3) + require(group("middle")?.sampleStyles()?.find { it.color != null }?.color == EnumChatFormatting.RED) + group("middle") + }!! + Pattern.compile("(?<whole>c)").findStyledMatcher(middlePartExtracted) { + require(group("whole")?.sampleStyleAtStart()?.color == EnumChatFormatting.RED) + } + } + +} |