diff --git a/CHANGELOG.md b/CHANGELOG.md index 10c6a95..b8c2f4f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# 0.3.4 + +## Fixes + +* Fixed an issue where SingleWordArgument based classes could complete on blank or empty strings. + # 0.3.3 ## Fixes diff --git a/kordx-commands-runtime/src/main/kotlin/com/gitlab/kordlib/kordx/commands/argument/Argument.kt b/kordx-commands-runtime/src/main/kotlin/com/gitlab/kordlib/kordx/commands/argument/Argument.kt index 796ee6e..5eefb05 100644 --- a/kordx-commands-runtime/src/main/kotlin/com/gitlab/kordlib/kordx/commands/argument/Argument.kt +++ b/kordx-commands-runtime/src/main/kotlin/com/gitlab/kordlib/kordx/commands/argument/Argument.kt @@ -42,16 +42,21 @@ interface Argument { abstract class SingleWordArgument : StateArgument() { override suspend fun ParseState.parse(context: CONTEXT): ArgumentResult { + if(ended) return unexpectedEnd() + val cursorBefore = cursor val word = flush { consumeWord() } + if(word.isBlank()){ + return ArgumentResult.Failure("Expected word.", cursor) + } + val result = parse(word, context) require(result.wordsTaken <= 1) { "Single word arguments cannot take more than one word: $result" } - val argumentResult = result.toArgumentResult(cursorBefore, word) - return argumentResult + return result.toArgumentResult(cursorBefore, word) } /** diff --git a/kordx-commands-runtime/src/test/kotlin/com/gitlab/kordlib/kordx/commands/argument/text/WordArgumentTest.kt b/kordx-commands-runtime/src/test/kotlin/com/gitlab/kordlib/kordx/commands/argument/text/WordArgumentTest.kt index 951f1c2..127b910 100644 --- a/kordx-commands-runtime/src/test/kotlin/com/gitlab/kordlib/kordx/commands/argument/text/WordArgumentTest.kt +++ b/kordx-commands-runtime/src/test/kotlin/com/gitlab/kordlib/kordx/commands/argument/text/WordArgumentTest.kt @@ -1,5 +1,6 @@ package com.gitlab.kordlib.kordx.commands.argument.text +import com.gitlab.kordlib.kordx.commands.argument.requireFailure import com.gitlab.kordlib.kordx.commands.argument.requireItem import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.test.runBlockingTest @@ -19,11 +20,23 @@ class WordArgumentTest { argument.parse(text, 0, Unit).requireItem(text) } + @ParameterizedTest + @MethodSource("failingSources") + fun `correctly fails arguments`(text: String) = runBlockingTest { + argument.parse(text, 0, Unit).requireFailure() + } + companion object { @JvmStatic fun sources() = listOf( Arguments.of("word") ) + + @JvmStatic + fun failingSources() = listOf( + Arguments.of(""), + Arguments.of(" "), + ) } } \ No newline at end of file