aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/scala/com/iinteractive/test/harness/SummarizedTests.scala
blob: 1932af09e00a17dbc447e756ef2e01ef173e9aa0 (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
package com.iinteractive.test.harness

import java.io.{PipedInputStream,PipedOutputStream}
import scala.concurrent.Await
import scala.concurrent.duration.Duration
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import scala.util.{Try,Success,Failure}

import com.iinteractive.test.tap.{Parser,TAPEvent,TAPResult,TodoDirective}
import com.iinteractive.test.Test

/** This is a trait for classes that run tests and summarize the results. It
  * provides a single `runOneTest` method, which runs a test class and
  * produces a stream of [[com.iinteractive.test.tap.TAPEvent TAP events]]
  * which can be used to produce whatever summarized output you need.
  */
trait SummarizedTests {
  /** Runs a single [[com.iinteractive.test.Test test]] instance, calling `cb`
    * with each [[com.iinteractive.test.tap.TAPEvent TAP event]] as it is
    * produced.
    *
    * @return The overall result of the test instance.
    */
  protected def runOneTest (
    test:    Test,
    cb:      TAPEvent => Unit,
    combine: Boolean = false
  ): TAPResult = {
    val out = new PipedOutputStream
    val in  = new PipedInputStream(out)
    val err = if (combine) out else Console.err

    val testFuture = Future {
      val result = Try {
        Console.withOut(out) {
          Console.withErr(err) {
            if (combine) {
              test.run
            }
            else {
              test.runInHarness
            }
          }
        }
      }
      out.close
      result
    }

    val parser = new Parser(cb)
    val result = Try(parser.parse(in))
    in.close
    Await.result(testFuture, Duration.Inf) match {
      case Success(_) => ()
      case Failure(e) => throw e
    }

    result match {
      case Success(r) => r
      case Failure(e) => throw e
    }
  }
}