Skip to content
This repository has been archived by the owner on Apr 16, 2023. It is now read-only.

Commit

Permalink
Make WordResult fail on empty string
Browse files Browse the repository at this point in the history
  • Loading branch information
BartArys committed Sep 16, 2020
1 parent a9f5336 commit 5301906
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,21 @@ interface Argument<out T, in CONTEXT> {
abstract class SingleWordArgument<T, CONTEXT> : StateArgument<T, CONTEXT>() {

override suspend fun ParseState.parse(context: CONTEXT): ArgumentResult<T> {
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)
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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(" "),
)
}

}

0 comments on commit 5301906

Please sign in to comment.