forked from luontola/gospec
/
recover_test.go
55 lines (47 loc) · 1.52 KB
/
recover_test.go
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
// Copyright © 2009-2011 Esko Luontola <www.orfjackal.net>
// This software is released under the Apache License 2.0.
// The license text is at http://www.apache.org/licenses/LICENSE-2.0
package gospec
import (
"fmt"
"github.com/orfjackal/nanospec.go/src/nanospec"
)
func boom2() {
boom1()
}
func boom1() {
boom0()
}
func boom0() {
panic("boom!") // line 19
}
func noBoom() {
}
func RecoverSpec(c nanospec.Context) {
c.Specify("When the called function panics", func() {
err := recoverOnPanic(boom2)
c.Specify("the cause is returned", func() {
c.Expect(err.Cause).Equals("boom!")
})
c.Specify("the stack trace begins with the panicking line", func() {
c.Expect(err.StackTrace[0].Name()).Equals(fmt.Sprintf("%v.boom0", pkgPath))
})
c.Specify("the stack trace includes all parent functions", func() {
c.Expect(err.StackTrace[1].Name()).Equals(fmt.Sprintf("%v.boom1", pkgPath))
})
c.Specify("the stack trace ends with the called function", func() {
lastEntry := err.StackTrace[len(err.StackTrace)-1]
c.Expect(lastEntry.Name()).Equals(fmt.Sprintf("%v.boom2", pkgPath))
})
c.Specify("the stack trace line numbers are the line of the call; not where the call will return", func() {
// For an explanation, see the comments at http://code.google.com/p/go/issues/detail?id=1100
c.Expect(err.StackTrace[0].Line()).Equals(19)
})
})
c.Specify("When the called function does not panic", func() {
err := recoverOnPanic(noBoom)
c.Specify("there is no error", func() {
c.Expect(err == nil).IsTrue()
})
})
}