Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backend: unit tests for replace roman numbers #3091

Open
wants to merge 7 commits into
base: beta
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ object ReplaceRomanNumerals {
"§o§a✔ §.* (?<roman>[IVXLCDM]+)§r",
"§5§o§7Purchase §a.* (?<roman>[IVXLCDM]+) §7.*",
"§5§o(?:§7)§.(?<roman>[IVXLCDM]+).*",
".*Heart of the Mountain (?<roman>[IVXLCDM]+) ?.*"
".*Heart of the Mountain (?<roman>[IVXLCDM]+) ?.*",
)

/**
Expand Down Expand Up @@ -96,17 +96,20 @@ object ReplaceRomanNumerals {
if (allowedPatterns.matches(this)) replace() else this
}

fun replaceLine(line: String): String {
if (!isEnabled()) return line
fun replaceLine(line: String, checkIfEnabled: Boolean = true): String {
if (checkIfEnabled && !isEnabled()) return line

return cachedStrings.getOrPut(line) {
line.replace()
}
}

private fun String.replace() = splitRegex.findAll(this).map { it.value }.joinToString("") {
it.takeIf { it.isValidRomanNumeral() && it.removeFormatting().romanToDecimal() != 2000 }?.coloredRomanToDecimal() ?: it
}
private fun String.replace() = splitRegex
.findAll(this)
.map { it.value }
.joinToString("") { part ->
part.takeIf { it.isValidRomanNumeral() }?.coloredRomanToDecimal() ?: part
}

private fun String.removeFormatting() = removeColor().replace(",", "")

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package at.hannibal2.skyhanni.test

import at.hannibal2.skyhanni.features.misc.ReplaceRomanNumerals
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test

class ReplaceRomanNumeralsTest {

@Test
fun `Regular words shouldn't be modified`() {
Assertions.assertEquals("hello", "hello".transformLine())
}

@Test
fun `'I' should never be converted`() {
Assertions.assertEquals("I", "I".transformLine())
}

@Test
fun `Single Roman numeral should be converted`() {
Assertions.assertEquals("5", "V".transformLine())
}

@Test
fun `Multiple Roman numerals separated by spaces should be converted`() {
Assertions.assertEquals("5 10 world", "V X world".transformLine())
}

@Test
fun `Roman numeral next to punctuation should be converted`() {
Assertions.assertEquals("5!", "V!".transformLine())
Assertions.assertEquals("hello 14 you?", "hello XIV you?".transformLine())
}

@Test
fun `Mixed with color codes should be converted`() {
Assertions.assertEquals("§c5!", "§cV!".transformLine())
Assertions.assertEquals("hello 2 is this 5 you?", "hello II is this V you?".transformLine())
}

@Test
fun `Invalid Roman numeral sequences are left unchanged`() {
Assertions.assertEquals("IIII", "IIII".transformLine())
}

@Test
fun `Sequences with punctuation and color codes interspersed should be converted`() {
Assertions.assertEquals("§d5 world", "§dV world".transformLine())
Assertions.assertEquals("hello 10 and then 1 more", "hello X and then I more".transformLine())
}

@Test
fun `Mixed complexity should be converted`() {
Assertions.assertEquals("Today 2023 was great!", "Today MMXXIII was great!".transformLine())
}

private fun String.transformLine(): String = ReplaceRomanNumerals.replaceLine(this, checkIfEnabled = false)
}
6 changes: 1 addition & 5 deletions versions/1.8.9/detekt/baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
<ID>MemberNameEqualsClassName:FirstMinionTier.kt$FirstMinionTier$fun firstMinionTier( otherItems: Map&lt;NEUInternalName, Int&gt;, minions: MutableMap&lt;String, NEUInternalName&gt;, tierOneMinions: MutableList&lt;NEUInternalName&gt;, tierOneMinionsDone: MutableSet&lt;NEUInternalName&gt;, )</ID>
<ID>MemberNameEqualsClassName:LastServers.kt$LastServers$private val lastServers = mutableMapOf&lt;String, SimpleTimeMark&gt;()</ID>
<ID>MemberNameEqualsClassName:PestSpawn.kt$PestSpawn$private fun pestSpawn(amount: Int, plotNames: List&lt;String&gt;, unknownAmount: Boolean)</ID>
<ID>MemberNameEqualsClassName:ReplaceRomanNumerals.kt$ReplaceRomanNumerals$fun replaceRomanNumerals(string: String): String</ID>
<ID>MemberNameEqualsClassName:Shimmy.kt$Shimmy.Companion$private fun shimmy(source: Any?, fieldName: String): Any?</ID>
<ID>MemberNameEqualsClassName:TestBingo.kt$TestBingo$var testBingo = false</ID>
<ID>MemberNameEqualsClassName:Text.kt$Text$fun text(text: String, init: IChatComponent.() -&gt; Unit = {})</ID>
Expand All @@ -72,7 +73,6 @@
<ID>NoNameShadowing:Renderable.kt$Renderable.Companion.&lt;no name provided&gt;$posY</ID>
<ID>NoNameShadowing:Renderable.kt$Renderable.Companion.&lt;no name provided&gt;${ it.value?.contains(textInput.textBox, ignoreCase = true) ?: true }</ID>
<ID>NoNameShadowing:RenderableUtils.kt$RenderableUtils${ it != null }</ID>
<ID>NoNameShadowing:ReplaceRomanNumerals.kt$ReplaceRomanNumerals${ it.isValidRomanNumeral() &amp;&amp; it.removeFormatting().romanToDecimal() != 2000 }</ID>
<ID>NoNameShadowing:RepoManager.kt$RepoManager${ unsuccessfulConstants.add(it) }</ID>
<ID>NoNameShadowing:RepoPatternManager.kt$RepoPatternManager${ it == '.' }</ID>
<ID>NoNameShadowing:Shimmy.kt$Shimmy.Companion$source</ID>
Expand Down Expand Up @@ -231,10 +231,6 @@
<ID>RepoPatternRegexTest:ScoreboardPattern.kt$ScoreboardPattern$by dungeonSb.pattern( "keys", "Keys: §.■ §.[✗✓] §.■ §a.x", )</ID>
<ID>RepoPatternRegexTest:ScoreboardPattern.kt$ScoreboardPattern$by dungeonSb.pattern( "teammates", "(?:§.)*(?&lt;classAbbv&gt;\\[\\w]) (?:§.)*(?&lt;username&gt;\\w{2,16}) (?:(?:§.)*(?&lt;classLevel&gt;\\[Lvl?(?&lt;level&gt;[\\w,.]+)?]?)|(?:§.)*(?&lt;health&gt;[\\w,.]+)(?:§.)*.?)", )</ID>
<ID>RepoPatternRegexTest:ShowMotesNpcSellPrice.kt$ShowMotesNpcSellPrice$by RepoPattern.pattern( "rift.everywhere.burger", ".*(?:§\\w)+You have (?:§\\w)+(?&lt;amount&gt;\\d) Grubber Stacks.*" )</ID>
<ID>RepoPatternRegexTest:SkillAPI.kt$SkillAPI$by patternGroup.pattern( "skill", "\\+(?&lt;gained&gt;[\\d.,]+) (?&lt;skillName&gt;\\w+) \\((?&lt;current&gt;[\\d.,]+)/(?&lt;needed&gt;[\\d.,]+)\\)", )</ID>
<ID>RepoPatternRegexTest:SkillAPI.kt$SkillAPI$by patternGroup.pattern( "skill.multiplier", "\\+(?&lt;gained&gt;[\\d.,]+) (?&lt;skillName&gt;.+) \\((?&lt;current&gt;[\\d.,]+)/(?&lt;needed&gt;[\\d,.]+[kmb])\\)", )</ID>
<ID>RepoPatternRegexTest:SkillAPI.kt$SkillAPI$by patternGroup.pattern( "skill.percent", "\\+(?&lt;gained&gt;[\\d.,]+) (?&lt;skillName&gt;.+) \\((?&lt;progress&gt;[\\d.]+)%\\)", )</ID>
<ID>RepoPatternRegexTest:SkillAPI.kt$SkillAPI$by patternGroup.pattern( "skill.tab", " (?&lt;type&gt;\\w+)(?: (?&lt;level&gt;\\d+))?: §r§a(?&lt;progress&gt;[0-9.]+)%", )</ID>
<ID>RepoPatternRegexTest:SkillAPI.kt$SkillAPI$by patternGroup.pattern( "skill.tab.max", " (?&lt;type&gt;\\w+) (?&lt;level&gt;\\d+): §r§c§lMAX", )</ID>
<ID>RepoPatternRegexTest:SkillAPI.kt$SkillAPI$by patternGroup.pattern( "skill.tab.nopercent", " §r§a(?&lt;type&gt;\\w+)(?: (?&lt;level&gt;\\d+))?: §r§e(?&lt;current&gt;[0-9,.]+)§r§6/§r§e(?&lt;needed&gt;[0-9kmb]+)", )</ID>
<ID>RepoPatternRegexTest:SkillExperience.kt$SkillExperience$by patternGroup.pattern( "actionbar", ".*§3\\+.* (?&lt;skill&gt;.*) \\((?&lt;overflow&gt;.*)/(?&lt;needed&gt;.*)\\).*" )</ID>
Expand Down
Loading