diff --git a/src/main/kotlin/io/provs/entry/Entry.kt b/src/main/kotlin/io/provs/entry/Entry.kt index 7655107..eab0bf9 100644 --- a/src/main/kotlin/io/provs/entry/Entry.kt +++ b/src/main/kotlin/io/provs/entry/Entry.kt @@ -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>( Array::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()) - } else if (args.size == 2) { - method.invoke(null) } else { method.invoke(null, args.drop(2).toTypedArray()) } } else { - println("Usage: ") + println("Usage: ") } } diff --git a/src/test/kotlin/io/provs/entry/EntryTest.kt b/src/test/kotlin/io/provs/entry/EntryTest.kt index 25cc628..193751b 100644 --- a/src/test/kotlin/io/provs/entry/EntryTest.kt +++ b/src/test/kotlin/io/provs/entry/EntryTest.kt @@ -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) { + println("test is fun " + args.joinToString(" ")) +} + +@Suppress("unused") +fun main(args: Array) { + 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()) } }