forked from smartystreets/goconvey
/
stack_trace_test.go
137 lines (107 loc) · 3.13 KB
/
stack_trace_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
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
package convey
import (
"fmt"
"strings"
"testing"
"github.com/smartystreets/goconvey/convey/reporting"
)
func TestStackTrace(t *testing.T) {
file, test := setupFileReporter()
Convey("A", test, func() {
So(1, ShouldEqual, 2)
})
if !strings.Contains(file.String(), "Failures:\n") {
t.Errorf("Expected errors, found none.")
}
if strings.Contains(file.String(), "goroutine ") {
t.Errorf("Found stack trace, expected none.")
}
Convey("A", test, StackFail, func() {
So(1, ShouldEqual, 2)
})
if !strings.Contains(file.String(), "goroutine ") {
t.Errorf("Expected stack trace, found none.")
}
}
func TestSetDefaultStackMode(t *testing.T) {
file, test := setupFileReporter()
SetDefaultStackMode(StackFail) // the default is normally StackError
defer SetDefaultStackMode(StackError)
Convey("A", test, func() {
So(1, ShouldEqual, 2)
})
if !strings.Contains(file.String(), "goroutine ") {
t.Errorf("Expected stack trace, found none.")
}
}
func TestStackModeMultipleInvocationInheritance(t *testing.T) {
file, test := setupFileReporter()
// initial convey should default to StaskError, so no stack trace
Convey("A", test, FailureContinues, func() {
So(1, ShouldEqual, 2)
// nested convey has explicit StaskFail, so should emit stack trace
Convey("B", StackFail, func() {
So(1, ShouldEqual, 2)
})
})
stackCount := strings.Count(file.String(), "goroutine ")
if stackCount != 1 {
t.Errorf("Expected 1 stack trace, found %d.", stackCount)
fmt.Printf("RESULT: %s \n", file.String())
}
}
func TestStackModeMultipleInvocationInheritance2(t *testing.T) {
file, test := setupFileReporter()
// Explicit StackFail, expect stack trace
Convey("A", test, FailureContinues, StackFail, func() {
So(1, ShouldEqual, 2)
// Nested Convey inherits StackFail, expect stack trace
Convey("B", func() {
So(1, ShouldEqual, 2)
})
})
stackCount := strings.Count(file.String(), "goroutine ")
if stackCount != 2 {
t.Errorf("Expected 2 stack traces, found %d.", stackCount)
}
}
func TestStackModeMultipleInvocationInheritance3(t *testing.T) {
file, test := setupFileReporter()
// Explicit StackFail, expect stack trace
Convey("A", test, FailureContinues, StackFail, func() {
So(1, ShouldEqual, 2)
// Nested Convey explicitly sets StackError, so no stack trace
Convey("B", StackError, func() {
So(1, ShouldEqual, 2)
})
})
stackCount := strings.Count(file.String(), "goroutine ")
if stackCount != 1 {
t.Errorf("Expected 1 stack trace1, found %d.", stackCount)
}
}
func setupFileReporter() (*memoryFile, *fakeGoTest) {
//monochrome()
file := newMemoryFile()
printer := reporting.NewPrinter(file)
reporter := reporting.NewProblemReporter(printer)
testReporter = reporter
return file, new(fakeGoTest)
}
////////////////// memoryFile ////////////////////
type memoryFile struct {
buffer string
}
func (self *memoryFile) Write(p []byte) (n int, err error) {
self.buffer += string(p)
return len(p), nil
}
func (self *memoryFile) String() string {
return self.buffer
}
func newMemoryFile() *memoryFile {
return new(memoryFile)
}
// func monochrome() {
// greenColor, yellowColor, redColor, resetColor = "", "", "", ""
// }