Skip to content

Commit

Permalink
fix enum argument
Browse files Browse the repository at this point in the history
  • Loading branch information
makeevrserg committed Dec 22, 2024
1 parent e1c34e9 commit 220c33b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
3 changes: 3 additions & 0 deletions command/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ dependencies {
// Kotlin
compileOnly(libs.bundles.kotlin)
implementation(projects.core)
// Test-Core
testImplementation(libs.bundles.testing.kotlin)
testImplementation(libs.tests.kotlin.test)
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ interface EnumArgument {
*/
class EnumArgumentType<T>(
private val entries: EnumEntries<T>
) : PrimitiveArgumentType<Enum<T>> where T : Enum<T>, T : EnumArgument {
) : ArgumentType<Enum<T>> where T : Enum<T>, T : EnumArgument {
override val key: String = "ENUM"

override fun transform(value: String): Enum<T> {
override fun transform(value: String): T {
return entries.firstOrNull { entry -> entry.value.equals(value, ignoreCase = true) }
?.ordinal
?.let(entries::getOrNull)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ru.astrainteractive.astralibs.command.api.argumenttype

import org.junit.Test
import org.junit.jupiter.api.assertThrows
import ru.astrainteractive.astralibs.command.api.exception.ArgumentTypeException
import kotlin.test.assertEquals

class EnumArgumentTypeTest {
enum class TestType(override val value: String) : EnumArgument {
QS("qs"), SHOP("shop")
}

@Test
fun `GIVEN_enum_WHEN_parse_THEN_parsed`() {
TestType.entries.forEach { entry ->
EnumArgumentType(TestType.entries).transform(entry.value).let { result ->
assertEquals(entry, result)
}
}
assertThrows<ArgumentTypeException> {
EnumArgumentType(TestType.entries).transform("no_value")
}
}
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ makeevrserg.java.ktarget=21
# Project
makeevrserg.project.name=AstraLibs
makeevrserg.project.group=ru.astrainteractive.astralibs
makeevrserg.project.version.string=3.18.0
makeevrserg.project.version.string=3.18.1
makeevrserg.project.description=Core utilities for spigot development
makeevrserg.project.developers=makeevrserg|Makeev Roman|[email protected]
makeevrserg.project.url=https://empireprojekt.ru
Expand Down

0 comments on commit 220c33b

Please sign in to comment.