forked from go-check/check
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reporter.go
178 lines (154 loc) · 4.45 KB
/
reporter.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
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
package check
import (
"bytes"
"fmt"
"io"
"sync"
"time"
)
// TODO: start test suite
type outputWriter interface {
Write(content []byte) (n int, err error)
WriteCallStarted(label string, c *C)
WriteCallProblem(label string, c *C)
WriteCallSuccess(label string, c *C)
StreamEnabled() bool
}
/*************** Plain writer *****************/
type plainWriter struct {
outputWriter
m sync.Mutex
writer io.Writer
wroteCallProblemLast bool
stream bool
verbose bool
}
func newPlainWriter(writer io.Writer, stream, verbose bool) *plainWriter {
return &plainWriter{writer: writer, stream: stream, verbose: verbose}
}
func (w *plainWriter) StreamEnabled() bool { return w.stream }
func (w *plainWriter) Write(content []byte) (n int, err error) {
w.m.Lock()
n, err = w.writer.Write(content)
w.m.Unlock()
return
}
func (w *plainWriter) WriteCallStarted(label string, c *C) {
if w.stream {
header := renderCallHeader(label, c, "", "\n")
w.m.Lock()
w.writer.Write([]byte(header))
w.m.Unlock()
}
}
func (w *plainWriter) WriteCallProblem(label string, c *C) {
var prefix string
if !w.stream {
prefix = "\n-----------------------------------" +
"-----------------------------------\n"
}
header := renderCallHeader(label, c, prefix, "\n\n")
w.m.Lock()
w.wroteCallProblemLast = true
w.writer.Write([]byte(header))
if !w.stream {
c.logb.WriteTo(w.writer)
}
w.m.Unlock()
}
func (w *plainWriter) WriteCallSuccess(label string, c *C) {
if w.stream || (w.verbose && c.kind == testKd) {
// TODO Use a buffer here.
var suffix string
if c.reason != "" {
suffix = " (" + c.reason + ")"
}
if c.status == succeededSt {
suffix += "\t" + c.timerString()
}
suffix += "\n"
if w.stream {
suffix += "\n"
}
header := renderCallHeader(label, c, "", suffix)
w.m.Lock()
// Resist temptation of using line as prefix above due to race.
if !w.stream && w.wroteCallProblemLast {
header = "\n-----------------------------------" +
"-----------------------------------\n" +
header
}
w.wroteCallProblemLast = false
w.writer.Write([]byte(header))
w.m.Unlock()
}
}
func renderCallHeader(label string, c *C, prefix, suffix string) string {
pc := c.method.PC()
return fmt.Sprintf("%s%s: %s: %s%s", prefix, label, niceFuncPath(pc),
niceFuncName(pc), suffix)
}
/*************** xUnit writer *****************/
// TODO: Write mrthod can collect data for std-out
type xunitReport struct {
suites []xunitSuite `xml:"testsuites>testsuite,omitempty"`
}
type xunitSuite struct {
Package string `xml:"package,attr,omitempty"`
Name string `xml:"name,attr,omitempty"`
Classname string `xml:"classname,attr,omitempty"`
Time float64 `xml:"time,attr"`
Timestamp time.Time `xml:"timestamp,attr"`
Tests uint64 `xml:"tests,attr"`
Failures uint64 `xml:"failures,attr"`
Errors uint64 `xml:"errors,attr"`
Skipped uint64 `xml:"skipped,attr"`
Properties []xunitSuiteProperty `xml:"properties>property"` //TODO: test
Testcases []xunitTestcase `xml:"testcase"`
SystemOut string `xml:"system-out,omitempty"`
SystemErr string `xml:"system-err,omitempty"`
}
type xunitSuiteProperty struct {
Name string `xml:"name,attr"`
Value string `xml:"value,attr"`
}
type xunitTestcase struct {
Name string `xml:"name,attr,omitempty"`
Classname string `xml:"classname,attr,omitempty"`
Time float64 `xml:"time,attr"`
Failure *xunitTestcaseResult `xml:"failure,omitempty"`
Error *xunitTestcaseResult `xml:"error,omitempty"`
}
type xunitTestcaseResult struct {
Message string `xml:"message,attr,omitempty"`
Type string `xml:"type,attr,omitempty"`
Value string `xml:",chardata"`
}
type xunitWriter struct {
outputWriter
m sync.Mutex
writer io.Writer
stream bool
verbose bool
systemOut io.Writer
}
func newXunitWriter(writer io.Writer, stream, verbose bool) *plainWriter {
return &xunitWriter{
writer: writer,
systemOut: bytes.Buffer{},
stream: stream,
verbose: verbose,
}
}
func (w *xunitWriter) GetReport() string {
}
func (w *xunitWriter) Write(content []byte) (n int, err error) {
w.m.Lock()
n, err = w.systemOut.Write(content)
w.m.Unlock()
return
}
func (w *xunitWriter) WriteCallStarted(label string, c *C) {}
func (w *xunitWriter) WriteCallProblem(label string, c *C) {}
func (w *xunitWriter) WriteCallSuccess(label string, c *C) {}
func (w *xunitWriter) StreamEnabled() bool {}