From a908ee5ea0933a1841acf7a96ed37ff73568b26e Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Thu, 28 Feb 2013 11:37:48 -0600 Subject: refactor the harness application a bit --- .../scala/org/perl8/test/harness/TestHarness.scala | 68 ++++++++++++++++------ 1 file changed, 50 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/main/scala/org/perl8/test/harness/TestHarness.scala b/src/main/scala/org/perl8/test/harness/TestHarness.scala index 4865c2c..09f780b 100644 --- a/src/main/scala/org/perl8/test/harness/TestHarness.scala +++ b/src/main/scala/org/perl8/test/harness/TestHarness.scala @@ -4,34 +4,66 @@ object TestHarness { import org.perl8.test.Test def main (args: Array[String]) { - val (reporterName, idx, multi) = if (args.length >= 2 && args(0) == "-r") { - (args(1), 2, false) - } - else if (args.length >= 2 && args(0) == "-R") { - (args(1), 2, true) - } - else if (args.length > 1) { - ("org.perl8.test.harness.SummaryReporter", 0, true) - } - else if (args.length == 1) { - ("org.perl8.test.harness.TAPReporter", 0, false) - } - else { - println("No tests specified!") - sys.exit(1) - } + val opts = parseOpts(args.toList) + val multi = !opts("prefer-single").asInstanceOf[Boolean] val exitCode = if (multi) { - val testNames = args.drop(idx) + val reporterName = opts("multi-reporter").asInstanceOf[String] + val testNames = opts("test-classes").asInstanceOf[List[String]] val reporter = newInstance[MultiTestReporter](reporterName) reporter.run(testNames) } else { - val testName = args(idx) + val reporterName = opts("single-reporter").asInstanceOf[String] + val testName = opts("test-classes").asInstanceOf[List[String]].apply(0) val reporter = newInstance[Reporter](reporterName) reporter.run(testName) } sys.exit(exitCode) } + + def parseOpts (args: List[String]): Map[String, Any] = args match { + case "-r" :: singleReporter :: rest => + parseOpts(rest) + ("single-reporter" -> singleReporter) + + case "-R" :: multiReporter :: rest => + parseOpts(rest) ++ Map( + "multi-reporter" -> multiReporter, + "prefer-single" -> false + ) + + case "--help" :: rest => + usage(0) + + case Nil => Map( + "single-reporter" -> "org.perl8.test.harness.TAPReporter", + "multi-reporter" -> "org.perl8.test.harness.SummaryReporter", + "prefer-single" -> true, + "test-classes" -> Nil + ) + + case `unknownOption` => + usage(1) + + case testClass :: rest => { + val opts = parseOpts(rest) + val tests = opts("test-classes").asInstanceOf[List[String]] + opts ++ Map( + "test-classes" -> (testClass :: tests), + "prefer-single" -> tests.isEmpty + ) + } + } + + def usage (exitCode: Int) = { + val out = if (exitCode == 0) Console.out else Console.err + out.println("harness [-r ]\n" + + " [-R ]\n" + + " [--help]\n" + + " [...]\n") + sys.exit(exitCode) + } + + private val unknownOption = """^-.*""".r } -- cgit v1.2.3-54-g00ecf