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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
package org.perl8.test.tap
import org.perl8.test.Utils._
import scala.util.parsing.combinator._
import java.io.OutputStream
object Consumer {
def parse (input: String): TAPResult = {
import TAPParser.{parseAll,tap,Success,NoSuccess}
parseAll(tap(), input) match {
case Success(result, _) => result
case failure: NoSuccess => throw new ParseException(failure.msg)
}
}
def parse (input: OutputStream): TAPResult =
parse(input.toString)
private object TAPParser extends RegexParsers {
def tap (indent: String = ""): Parser[TAPResult] =
planFirst(indent) | planLast(indent)
def planFirst (indent: String): Parser[TAPResult] =
(line(plan, indent) ~ rep(line(result(indent), indent))) ^^ {
case plan ~ results => new TAPResult(plan, results)
}
def planLast (indent: String): Parser[TAPResult] =
(rep(line(result(indent), indent)) ~ line(plan, indent)) ^^ {
case results ~ plan => new TAPResult(plan, results)
}
def comment: Parser[String] =
"""#[^\n]*""".r
def plan: Parser[Plan] =
(planValue <~ ws) ~ opt(planDirective) ^^ {
case planValue ~ Some(SkipDirective(d)) => SkipAll(d)
case planValue ~ None => NumericPlan(planValue)
}
def result (indent: String): Parser[TestResult] =
simpleResult | subtestResult(indent)
def simpleResult: Parser[TestResult] =
(ok <~ ws) ~
(testNumber <~ ws) ~
(testDescription <~ ws) ~
opt(testDirective) ^^ {
case ok ~ testNumber ~ testDescription ~ testDirective =>
new TestResult(ok, testNumber, testDescription, testDirective, None)
}
def subtestResult (indent: String): Parser[TestResult] =
(new Parser[TAPResult] {
def apply (in: Input) = {
val source = in.source
val offset = in.offset
val str = source.subSequence(offset, source.length)
val newIndent = ws.findPrefixMatchOf(str).getOrElse("").toString
newIndent match {
case "" => Failure("subtests must be indented", in)
case _ => {
tap(indent + newIndent)(in.drop(-indent.length))
}
}
}
} <~ indent <~ rep(comment ~ "\n" ~ indent)) ~ simpleResult ^^ {
case tapResult ~ testResult =>
new TestResult(
testResult.passed,
testResult.number,
testResult.description,
testResult.directive,
Some(tapResult)
)
}
def planValue: Parser[Int] =
"1.." ~> """\d+""".r ^^ { _.toInt }
def planDirective: Parser[Directive] =
skipDirective
def ok: Parser[Boolean] =
opt("not ") <~ "ok" ^^ { _.isEmpty }
def testNumber: Parser[Int] =
"""\d+""".r ^^ { _.toInt }
def testDescription: Parser[String] =
"""[^#\n]*""".r ^^ { _.trim }
def testDirective: Parser[Directive] =
todoDirective | skipDirective
def skipDirective: Parser[Directive] =
"#" ~> ws ~> """(?i:skip)""".r ~> ws ~> opt("""[^\n]*""".r) ^^ {
case desc => new SkipDirective(desc.map(s => s.trim))
}
def todoDirective: Parser[Directive] =
"#" ~> ws ~> """(?i:todo)""".r ~> ws ~> opt("""[^\n]*""".r) ^^ {
case desc => new TodoDirective(desc.map(s => s.trim))
}
def line[T] (p: => Parser[T], indent: String): Parser[T] =
rep(indent ~ comment ~ "\n") ~>
indent ~> p <~ "\n" <~
rep(indent ~ comment ~ "\n")
override def skipWhitespace = false
val ws = """[ \t]*""".r
}
}
trait Directive {
val message: Option[String]
}
case class SkipDirective (
override val message: Option[String]
) extends Directive
case class TodoDirective (
override val message: Option[String]
) extends Directive
case class TestResult (
val passed: Boolean,
val number: Int,
val description: String,
val directive: Option[Directive],
val subtest: Option[TAPResult]
)
class TAPResult (val plan: Plan, val results: Seq[TestResult]) {
val correctPlan = plan match {
case NumericPlan(n) => results.length == n
case SkipAll(_) => results.length == 0
}
val fails = results.count { r =>
!r.passed && !r.directive.isDefined
}
val testsPassed = fails == 0
val success =
correctPlan && testsPassed
val exitCode =
if (success) {
0
}
else if (!correctPlan) {
255
}
else {
fails
}
}
case class ParseException (
val message: String
) extends RuntimeException(message)
|