aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/scala/org/perl8/test/harness/TestHarness.scala
blob: cd779010a70bd25ebe3c4ebe213421a3feaec601 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package org.perl8.test.harness

object TestHarness {
  import org.perl8.test.Test

  def main (args: Array[String]) {
    val opts = parseOpts(args.toList)
    val single = opts("prefer-single").asInstanceOf[Boolean]

    val exitCode = if (single) {
      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)
    }
    else {
      val reporterName = opts("multi-reporter").asInstanceOf[String]
      val testNames = opts("test-classes").asInstanceOf[List[String]]
      val reporter = newInstance[MultiTestReporter](reporterName)
      reporter.run(testNames)
    }

    sys.exit(exitCode)
  }

  def parseOpts (args: List[String]): Map[String, Any] = args match {
    case Nil => Map(
      "single-reporter" -> "org.perl8.test.harness.TAPReporter",
      "multi-reporter"  -> "org.perl8.test.harness.SummaryReporter",
      "prefer-single"   -> true,
      "test-classes"    -> Nil
    )

    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 `unknownOption` :: rest =>
      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 <single-reporter-class>]\n" +
                "        [-R <multi-reporter-class>]\n" +
                "        [--help]\n" +
                "        <test-class> [<test-class>...]\n")
    sys.exit(exitCode)
  }

  private val unknownOption = """^-.*""".r
}