aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/scala/org/perl8/test/TestMore.scala
blob: b4122151f550a7340b943516785621617795713a (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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package org.perl8.test

import scala.util.matching.Regex

import org.perl8.test.tap.TestBuilder

class TestMore (plan: Plan = NoPlan) extends Test with DelayedInit {
  def delayedInit (body: => Unit) {
    testBody = { terminalInUse =>
      todo    = None
      builder = new TestBuilder(plan, terminalInUse)
      plan match {
        case SkipAll(_) => ()
        case _          => body
      }
    }
  }

  def runTests (terminalInUse: Boolean): Int = {
    if (testBody == null) {
      delayedInit { }
    }

    testBody(terminalInUse)
    builder.doneTesting
    builder.exitCode
  }

  def ok (cond: Boolean): Boolean =
    test(cond)

  def ok (cond: Boolean, desc: String): Boolean =
    testWithDesc(cond, desc)

  def is[T] (got: T, expected: T): Boolean =
    test(got == expected, isMessage(got, expected))

  def is[T] (got: T, expected: T, desc: String): Boolean =
    testWithDesc(got == expected, desc, isMessage(got, expected))

  def isnt[T] (got: T, expected: T): Boolean =
    test(got != expected, isntMessage(got))

  def isnt[T] (got: T, expected: T, desc: String): Boolean =
    testWithDesc(got != expected, desc, isntMessage(got))

  def like (got: String, rx: Regex): Boolean =
    test(rx.findFirstIn(got).nonEmpty, likeMessage(got, rx))

  def like (got: String, rx: Regex, desc: String): Boolean =
    testWithDesc(rx.findFirstIn(got).nonEmpty, desc, likeMessage(got, rx))

  def unlike (got: String, rx: Regex): Boolean =
    test(rx.findFirstIn(got).isEmpty, unlikeMessage(got, rx))

  def unlike (got: String, rx: Regex, desc: String): Boolean =
    testWithDesc(rx.findFirstIn(got).isEmpty, desc, unlikeMessage(got, rx))

  def pass: Boolean =
    ok(true)

  def pass (desc: String): Boolean =
    ok(true, desc)

  def fail: Boolean =
    ok(false)

  def fail (desc: String): Boolean =
    ok(false, desc)

  def diag (message: String) {
    builder.diag(message)
  }

  def BAIL_OUT {
    builder.bailOut
  }

  def BAIL_OUT (desc: String) {
    builder.bailOut(desc)
  }

  def todo (reason: String)(body: => Unit) {
    val oldTodo = todo
    try {
      todo = Some(reason)
      body
    }
    finally {
      todo = oldTodo
    }
  }

  def skip (count: Int)(body: => Unit) {
    for (i <- 1 to count) {
      builder.skip
    }
  }

  def skip (count: Int, reason: String)(body: => Unit) {
    for (i <- 1 to count) {
      builder.skip(reason)
    }
  }

  def subtest (
    name: String,
    plan: Plan = NoPlan
  )(body: => Unit): Boolean = {
    val oldBuilder = builder
    val success = try {
      builder = oldBuilder.cloneForSubtest(plan)
      body
      builder.doneTesting
    }
    finally {
      builder = oldBuilder
    }
    ok(success, name)
  }

  private def isMessage[T] (got: T, expected: T): String =
    "         got: '" + got + "'\n" +
    "    expected: '" + expected + "'\n"

  private def isntMessage[T] (got: T): String =
    "         got: '" + got + "'\n" +
    "    expected: anything else\n"

  private def likeMessage (got: String, rx: Regex): String =
    "                  '" + got + "'\n" +
    "    doesn't match '" + rx + "'\n"

  private def unlikeMessage (got: String, rx: Regex): String =
    "                  '" + got + "'\n" +
    "          matches '" + rx + "'\n"

  private def testWithDesc (
    cond:   Boolean,
    desc:   String
  ): Boolean = {
    todo match {
      case Some(t) => builder.todo(t, cond, "- " + desc)
      case None    => builder.ok(cond, "- " + desc)
    }
    if (!cond) {
      failed(Some(desc), None)
    }
    cond
  }

  private def testWithDesc (
    cond:   Boolean,
    desc:   String,
    reason: => String
  ): Boolean = {
    todo match {
      case Some(t) => builder.todo(t, cond, "- " + desc)
      case None    => builder.ok(cond, "- " + desc)
    }
    if (!cond) {
      failed(Some(desc), Some(reason))
    }
    cond
  }

  private def test (cond: Boolean): Boolean = {
    todo match {
      case Some(t) => builder.todo(t, cond)
      case None    => builder.ok(cond)
    }
    if (!cond) {
      failed(None, None)
    }
    cond
  }

  private def test (cond: Boolean, reason: => String): Boolean = {
    todo match {
      case Some(t) => builder.todo(t, cond)
      case None    => builder.ok(cond)
    }
    if (!cond) {
      failed(None, Some(reason))
    }
    cond
  }

  private def failed (desc: Option[String], reason: Option[String]) {
    val stack = Thread.currentThread.getStackTrace.drop(1).filter { frame =>
      !ignoreFrame(frame)
    }
    val idx = stack.lastIndexWhere { frame =>
      frame.getClassName == "org.perl8.test.TestMore" &&
      frame.getMethodName == "hideTestMethod"
    }
    val caller = idx match {
      case -1 => stack.headOption
      // one level to jump out of hideTestMethod and one level to jump out of
      // the method that called hideTestMethod
      case i  => stack.drop(i + 2).headOption

    }
    val (file, line) = caller match {
      case Some(frame) => (frame.getFileName, frame.getLineNumber)
      case None        => ("<unknown file>", "<unknown line>")
    }
    val message = "  " + (todo match {
      case Some(_) => "Failed (TODO) test"
      case None    => "Failed test"
    }) + (desc match {
      case Some(m) => " '" + m + "'\n  "
      case None    => " "
    })
    val trace = "at " + file + " line " + line + "."
    val explanation = message + trace + reason.map("\n" + _).getOrElse("")
    if (todo.isDefined) {
      builder.note(explanation)
    }
    else {
      builder.diag(explanation)
    }
  }

  // this just adds a method call with a known name to the stack trace, so
  // that we can detect it later
  def hideTestMethod[T] (body: => T): T = {
    body
  }

  protected def ignoreFrame (frame: StackTraceElement): Boolean = {
    val className = frame.getClassName
    val methodName = frame.getMethodName

    // ignore everything in this class, except the hideTestMethod call which we
    // use as a stack trace marker
    (className == "org.perl8.test.TestMore" &&
      methodName != "hideTestMethod") ||
    // when you call a method in a class when the method is defined in a
    // trait, it calls a stub which calls the real definition in the trait.
    // the trait is represented under the hood as a class with the same name
    // as the trait, except with $class appended. this is a gross reliance on
    // implementation details that could change at any moment, but i don't
    // really see any better options.
    """\$class$""".r.findFirstIn(className).nonEmpty
  }

  private var todo:     Option[String]  = _
  private var builder:  TestBuilder     = _
  private var testBody: Boolean => Unit = _
}