aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/scala/com/iinteractive/test/ExceptionTest.scala
blob: 62c927855f94f5d7384beeb26ba130506400f797 (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
package com.iinteractive.test

import java.io.ByteArrayOutputStream

import com.iinteractive.test.tap.Parser

class ExceptionTest extends TestMore {
  val lineZero = Thread.currentThread.getStackTrace()(1).getLineNumber + 3
  def line (offset: Int) = lineZero + offset

  private class MyBasicTest extends TestMore {
    private case class MyException(msg: String)
      extends RuntimeException(msg)
    private case class OtherException(msg: String)
      extends RuntimeException(msg)

    lives_ok {
      val _ = "no exception"
    }
    lives_ok {
      throw MyException("foo")
    }

    dies_ok[MyException] {
      val _ = "no exception"
    }
    dies_ok[MyException] {
      throw OtherException("foo")
    }
    dies_ok[MyException] {
      throw MyException("foo")
    }

    throws_ok(MyException("foo")) {
      val _ = "no exception"
    }
    throws_ok(MyException("foo")) {
      throw MyException("bar")
    }
    throws_ok(MyException("foo")) {
      throw MyException("foo")
    }

    is(
      exception { val _ = "no exception" },
      None
    )
    is(
      exception { throw MyException("foo") },
      Some(MyException("foo"))
    )
  }

  val out = new ByteArrayOutputStream
  val exitCode = Console.withOut(out) {
    Console.withErr(out) {
      (new MyBasicTest).run
    }
  }

  is((new Parser).parse(out).exitCode, 5, "got the right plan")
  is(exitCode, 5, "got the right plan")

  val expected =
    ("""^ok 1 - didn't throw an exception
not ok 2 - didn't throw an exception
#   Failed test 'didn't throw an exception'
#   at ExceptionTest.scala line """ + line(9) + """.
#          got: [^\s]*MyException[^\n]*foo[^\n]*
#     expected: normal exit
not ok 3 - threw a [^\s]*MyException exception
#   Failed test 'threw a [^\s]*MyException exception'
#   at ExceptionTest.scala line """ + line(13) + """.
#          got: normal exit
#     expected: a [^\s]*MyException exception
not ok 4 - threw a [^\s]*MyException exception
#   Failed test 'threw a [^\s]*MyException exception'
#   at ExceptionTest.scala line """ + line(16) + """.
#          got: [^\s]*OtherException[^\n]*foo[^\n]*
#     expected: a [^\s]*MyException exception
ok 5 - threw a [^\s]*MyException exception
not ok 6 - threw [^\s]*MyException[^\n]*foo[^\n]*
#   Failed test 'threw [^\s]*MyException[^\n]*foo[^\n]*'
#   at ExceptionTest.scala line """ + line(23) + """.
#          got: normal exit
#     expected: [^\s]*MyException[^\n]*foo[^\n]*
not ok 7 - threw [^\s]*MyException[^\n]*foo[^\n]*
#   Failed test 'threw [^\s]*MyException[^\n]*foo[^\n]*'
#   at ExceptionTest.scala line """ + line(26) + """.
#          got: [^\s]*MyException[^\n]*bar[^\n]*
#     expected: [^\s]*MyException[^\n]*foo[^\n]*
ok 8 - threw [^\s]*MyException[^\n]*foo[^\n]*
ok 9
ok 10
1..10
# Looks like you failed 5 tests of 10.
$""").r

  like(out.toString, expected, "correct tap")
}