-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreport.go
202 lines (175 loc) · 5.08 KB
/
report.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package qac
import (
"fmt"
"testing"
)
// ReportEntryType represents the type of a report entry.
type ReportEntryType int8
const (
reportTypeNone ReportEntryType = iota
// ErrorType is report entry error
ErrorType
// InfoType is report entry info
InfoType
// SuccessType is report entry success
SuccessType
)
// ReportEntry is a single unit of information in a report.
type ReportEntry struct {
kind ReportEntryType
description string
errors []error
}
func (r *ReportEntry) addError(err error) {
r.errors = append(r.errors, err)
}
// Description is the textual representation of a report entry.
func (r *ReportEntry) Description() string {
return r.description
}
// Errors returns the errors list in a report entry.
func (r *ReportEntry) Errors() []error {
return r.errors
}
// Kind returns the type of a report entry: error, info, success...
func (r *ReportEntry) Kind() ReportEntryType {
return r.kind
}
// ReportBlock is an aggregate of report entries classified on the phase.
type ReportBlock struct {
phase string
entries []ReportEntry
}
// Phase returns the phase of a block.
func (r *ReportBlock) Phase() string {
return r.phase
}
// Entries returns the entries of a block.
func (r *ReportBlock) Entries() []ReportEntry {
return r.entries
}
func newReportEntryFromAssertionResult(ar AssertionResult) ReportEntry {
k := ErrorType
if ar.Success() {
k = SuccessType
}
return ReportEntry{description: ar.description, kind: k, errors: ar.errors}
}
func newReportEntryFromError(err error) ReportEntry {
return ReportEntry{description: `error`, kind: ErrorType, errors: []error{err}}
}
func newReportEntryInfo(msg string) ReportEntry {
return ReportEntry{description: msg, kind: InfoType, errors: []error{}}
}
// TestExecutionReport is the full report on a test execution
type TestExecutionReport struct {
// used to keep report entries ordered
blocks []*ReportBlock
}
func (r *TestExecutionReport) addEntryAsErrorString(phase string, message string) {
r.addEntryAsError(phase, fmt.Errorf(message))
}
func (r *TestExecutionReport) addEntryAsError(phase string, err error) {
entry := newReportEntryFromError(err)
r.addEntry(phase, entry)
}
func (r *TestExecutionReport) addEntryAsAssertionResult(phase string, ar AssertionResult) {
entry := newReportEntryFromAssertionResult(ar)
r.addEntry(phase, entry)
}
func (r *TestExecutionReport) addEntryInfo(phase string, msg string) {
entry := newReportEntryInfo(msg)
r.addEntry(phase, entry)
}
func (r *TestExecutionReport) addEntry(phase string, entry ReportEntry) {
for _, block := range r.blocks {
if block.phase == phase {
block.entries = append(block.entries, entry)
return
}
}
r.blocks = append(r.blocks, &ReportBlock{phase: phase, entries: []ReportEntry{entry}})
}
// Blocks returns the blocks list in a full report.
func (r *TestExecutionReport) Blocks() []*ReportBlock {
return r.blocks
}
// AllErrors returns all errors in a report, without considering blocks or phases.
func (r *TestExecutionReport) AllErrors() []error {
errors := []error{}
for _, block := range r.Blocks() {
for _, entry := range block.Entries() {
for _, err := range entry.Errors() {
errors = append(errors, err)
}
}
}
return errors
}
// Reporter is the interface for components publishing the report.
type Reporter interface {
Publish(report *TestExecutionReport) error
}
// NewTestLogsReporter returns a Reporter implementation using the testing log.
func NewTestLogsReporter(t *testing.T) Reporter {
return &testLogsReporter{t: t}
}
type testLogsReporter struct {
t *testing.T
}
func (r *testLogsReporter) Publish(report *TestExecutionReport) error {
for _, block := range report.Blocks() {
r.t.Logf("Phase <%s>", block.Phase())
for _, entry := range block.Entries() {
// r.ui.Lifecyclef(" - %s", entry.Description())
switch entry.Kind() {
case ErrorType:
r.t.Logf(" | - KO %s", entry.Description())
for i, err := range entry.Errors() {
r.t.Logf(" %d %s", (i + 1), err.Error())
}
break
case InfoType:
r.t.Logf(" | INFO %s", entry.Description())
break
case SuccessType:
r.t.Logf(" | - OK %s", entry.Description())
break
default:
r.t.Logf("unexpected kind %v", entry.Kind())
}
}
}
return nil
}
// NewConsoleReporter returns a Reporter implementation writing to the stdout.
func NewConsoleReporter() Reporter {
return &consoleReporter{}
}
type consoleReporter struct {
}
func (r *consoleReporter) Publish(report *TestExecutionReport) error {
for _, block := range report.Blocks() {
fmt.Printf("Phase <%s>\n", block.Phase())
for _, entry := range block.Entries() {
// r.ui.Lifecyclef(" - %s", entry.Description())
switch entry.Kind() {
case ErrorType:
fmt.Printf(" | - KO %s\n", entry.Description())
for i, err := range entry.Errors() {
fmt.Printf(" (%d) %s\n", (i + 1), err.Error())
}
break
case InfoType:
fmt.Printf(" | INFO %s\n", entry.Description())
break
case SuccessType:
fmt.Printf(" | - OK %s\n", entry.Description())
break
default:
fmt.Printf("unexpected kind %v\n", entry.Kind())
}
}
}
return nil
}