add parameters to function call in EntryKt

This commit is contained in:
az 2021-07-17 15:49:00 +02:00
parent 73b78352b9
commit 5b8bf87ef4
2 changed files with 80 additions and 13 deletions

View file

@ -6,9 +6,9 @@ package io.provs.entry
* Methods with a vararg parameter must be called with at least one argument.
*
* @param args specify class and (optionally) method and parameters, in detail:
* @param args[0] class to be called
* @param args[1] (optional) static method of the class; if not specified, the "main" method is used
* @param args[2...] String parameters that are passed to the method, only applicable if method has vararg parameter
* @param args[0] fully-qualified class name of the class to be called
* @param args[1] (optional) static method of the class with a vararg parameter of type String; if not specified, the "main" method is used
* @param args[2...] (optional) String parameters that are passed to the method; can be only used if method name (args[1]) is provided
*/
fun main(vararg args: String) {
@ -20,18 +20,18 @@ fun main(vararg args: String) {
val parameterTypeStringArray = arrayOf<Class<*>>(
Array<String>::class.java
)
val method = if (args.size == 1) jClass.getMethod("main", *parameterTypeStringArray) else
(if (args.size == 2 && args[1] != "main") jClass.getMethod(args[1])
else jClass.getMethod(args[1], *parameterTypeStringArray))
val method = if (args.size == 1) {
jClass.getMethod("main", *parameterTypeStringArray)
} else {
jClass.getMethod(args[1], *parameterTypeStringArray)
}
if ((args.size == 1) || (args.size == 2 && args[1] == "main")) {
if (args.size <= 2) {
method.invoke(null, emptyArray<String>())
} else if (args.size == 2) {
method.invoke(null)
} else {
method.invoke(null, args.drop(2).toTypedArray())
}
} else {
println("Usage: <packageName.className> <static methodName>")
println("Usage: <packageName.className> <optional static methodName> <optional parameters ... >")
}
}

View file

@ -1,18 +1,85 @@
package io.provs.entry
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import java.io.ByteArrayOutputStream
import java.io.PrintStream
@Suppress("unused")
fun testfun() {
println("test is fun")
fun testfun(args: Array<String>) {
println("test is fun " + args.joinToString(" "))
}
@Suppress("unused")
fun main(args: Array<String>) {
println("main is fun " + args.joinToString(" "))
}
internal class EntryKtTest {
private var outContent = ByteArrayOutputStream()
private var originalOut = System.out
@BeforeEach
fun redirectSystemOutStream() {
originalOut = System.out
// given
outContent = ByteArrayOutputStream()
System.setOut(PrintStream(outContent))
}
@AfterEach
fun restoreSystemOutStream() {
System.setOut(originalOut)
}
@Test
fun test_main_no_arg() {
fun test_without_method_argument() {
// when
main("io.provs.entry.EntryTestKt")
// then
assertEquals("main is fun \n", outContent.toString())
}
@Test
fun test_method_main_without_args() {
// when
main("io.provs.entry.EntryTestKt", "main")
// then
assertEquals("main is fun \n", outContent.toString())
}
@Test
fun test_named_method_without_args() {
// when
main("io.provs.entry.EntryTestKt", "testfun")
// then
assertEquals("test is fun \n", outContent.toString())
}
@Test
fun test_method_main_with_args() {
// when
main("io.provs.entry.EntryTestKt", "main", "arg1", "arg2")
// then
assertEquals("main is fun arg1 arg2\n", outContent.toString())
}
@Test
fun test_named_method_with_args() {
// when
main("io.provs.entry.EntryTestKt", "testfun", "arg1", "arg2")
// then
assertEquals("test is fun arg1 arg2\n", outContent.toString())
}
}