Skip to content

Commit

Permalink
Fix qualities with rating import
Browse files Browse the repository at this point in the history
  • Loading branch information
fmasa committed Sep 3, 2023
1 parent da8306b commit 6d4aa5c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ inline fun <reified T : Enum<T>> matchEnumSetOrNull(value: String, separator: St
return enums.toSet()
}

val FEATURE_REGEX = Regex("([a-zA-Z- ])+ ?\\(?\\+?([0-9])?A?\\)?")
val FEATURE_REGEX = Regex("([a-zA-Z- ]+) ?\\(?\\+?([0-9])?A?\\)?")
private val NAME_WITH_COUNT_PATTERN = Regex("(.*) \\((\\d+|dozen)\\)")

inline fun <reified T : Enum<T>> parseFeatures(value: String): Map<T, Int> {
Expand All @@ -47,11 +47,11 @@ inline fun <reified T : Enum<T>> parseFeatures(value: String): Map<T, Int> {
.splitToSequence(",")
.map { it.trim() }
.mapNotNull {
val (name, rating) = FEATURE_REGEX.matchEntire(it)?.groupValues
val (_, name, rating) = FEATURE_REGEX.matchEntire(it)?.groupValues
?: error("Invalid feature $it")
val feature = matchEnumOrNull<T>(name) ?: return@mapNotNull null
val feature = matchEnumOrNull<T>(name.trim()) ?: return@mapNotNull null

feature to (rating.toIntOrNull() ?: 0)
feature to (rating.toIntOrNull() ?: 1)
}.toMap()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package cz.frantisekmasa.wfrp_master.common.compendium.domain.importer.parsers.trappings

import cz.frantisekmasa.wfrp_master.common.core.domain.trappings.WeaponQuality
import kotlin.test.Test
import kotlin.test.assertEquals

class UtilsTest {

@Test
fun `matchEnum() parses all feature types`() {
assertEquals(
mapOf(
WeaponQuality.SLASH to 3,
WeaponQuality.DAMAGING to 1,
WeaponQuality.SHIELD to 2,
),
parseFeatures("Slash (3A), Damaging*, Shield 2, Unknown")
)
}
}

0 comments on commit 6d4aa5c

Please sign in to comment.