kite.kotliner-cli.1.0.5.source-code.CliKtTest.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotliner-cli Show documentation
Show all versions of kotliner-cli Show documentation
An easy way to set up a CLI
The newest version!
package com.lightningkite.kotlinercli
import org.junit.Assert.*
import org.junit.Test
import java.io.File
class CliKtTest {
@Test fun splits() {
assertEquals(listOf("asdf", "fdsa", "asdf"), "asdf fdsa asdf".cliSplit())
assertEquals(listOf("asdf", "fd\"sa", "asdf"), "asdf fd\\\"sa asdf".cliSplit())
assertEquals(listOf("asdf", "fdsa asdf"), "asdf \"fdsa asdf\"".cliSplit())
}
@Test fun testNoParams() {
success = false
cliReturning(arrayOf("commandA"), available = listOf(::commandA))
assertTrue(success)
}
@Test fun testBasicParam() {
success = false
cliReturning(arrayOf("commandB", "10", "Hello world!"), available = listOf(::commandB))
assertTrue(success)
success = false
cliReturning(arrayOf("commandB", "10", "--text", "Hello world!"), available = listOf(::commandB))
assertTrue(success)
success = false
cliReturning(arrayOf("commandB", "--number", "10", "--text", "Hello world!"), available = listOf(::commandB))
assertTrue(success)
}
@Test fun testAdvancedParam() {
success = false
cliReturning(arrayOf("commandC", "test.txt"), available = listOf(::commandC))
assertTrue(success)
success = false
cliReturning(arrayOf("commandC", "test.txt", "33"), available = listOf(::commandC))
assertTrue(success)
success = false
cliReturning(arrayOf("commandC", "test.txt", "--default", "33"), available = listOf(::commandC))
assertTrue(success)
success = false
cliReturning(arrayOf("commandC", "--file", "test.txt", "--default", "33"), available = listOf(::commandC))
assertTrue(success)
}
@Test fun testVararg() {
success = false
cliReturning(arrayOf("commandD", "test.txt"), available = listOf(::commandD))
assertTrue(success)
success = false
cliReturning(arrayOf("commandD", "test.txt", "33"), available = listOf(::commandD))
assertTrue(success)
success = false
cliReturning(arrayOf("commandD", "test.txt", "33", "22", "89"), available = listOf(::commandD))
assertTrue(success)
success = false
cliReturning(arrayOf("commandD", "--file", "test.txt", "22", "33"), available = listOf(::commandD))
assertTrue(success)
}
@Test fun testFlag() {
assertEquals(false, cliReturning(arrayOf("commandE", "test.txt"), available = listOf(::commandE)))
assertEquals(true, cliReturning(arrayOf("commandE", "test.txt", "--flag"), available = listOf(::commandE)))
assertEquals(true, cliReturning(arrayOf("commandE", "test.txt", "--flag", "true"), available = listOf(::commandE)))
assertEquals(false, cliReturning(arrayOf("commandE", "--file", "test.txt"), available = listOf(::commandE)))
assertEquals(true, cliReturning(arrayOf("commandE", "--file", "test.txt", "--flag", "true"), available = listOf(::commandE)))
assertEquals(true, cliReturning(arrayOf("commandE", "--file", "test.txt", "--flag"), available = listOf(::commandE)))
assertEquals(true, cliReturning(arrayOf("commandE", "--flag", "--file", "test.txt"), available = listOf(::commandE)))
assertEquals(true, cliReturning(arrayOf("commandE", "--flag", "true", "--file", "test.txt"), available = listOf(::commandE)))
}
@Test fun obj() {
success = false
cliReturning(arrayOf("test"), available = listOf(TestObject::test))
assertTrue(success)
}
@Test fun testSetup() {
success = false
cliReturning(arrayOf("doNothing"), setup = ::commandA, available = listOf(::doNothing))
assertTrue(success)
}
@Test fun testEnum() {
success = false
cliReturning(arrayOf("commandF", "ValueB"), setup = ::doNothing, available = listOf(::commandF))
assertTrue(success)
}
}
enum class TestEnum { ValueA, ValueB }
private var success = false
private fun commandA() {
success = true
}
private fun doNothing() {
}
private fun commandB(number: Int, text: String) {
success = true
}
private fun commandC(file: File, default: Int = 2) {
success = true
}
private fun commandD(file: File, vararg multiple: Int) {
success = true
}
private fun commandE(file: File, flag: Boolean = false): Boolean {
return flag
}
private fun commandF(enumValue: TestEnum): Boolean {
success = true
return enumValue == TestEnum.ValueA
}
object TestObject {
fun test() {
success = true
}
}