-
Notifications
You must be signed in to change notification settings - Fork 13
/
formatters.go
178 lines (145 loc) · 4.82 KB
/
formatters.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 prettytest
import (
"fmt"
"path/filepath"
"strings"
)
const formatTag = "\t%s\t"
var (
labelFAIL = red("F")
labelMUSTFAIL = green("EF")
labelPASS = green("OK")
labelPENDING = yellow("PE")
labelNOASSERTIONS = yellow("NA")
)
func green(text string) string {
return "\033[32m" + text + "\033[0m"
}
func red(text string) string {
return "\033[31m" + text + "\033[0m"
}
func yellow(text string) string {
return "\033[33m" + text + "\033[0m"
}
type FinalReport struct {
Passed, Failed, ExpectedFailures, Pending, NoAssertions int
}
func (r *FinalReport) Total() int {
return r.Passed + r.Failed + r.ExpectedFailures + r.Pending + r.NoAssertions
}
// Formatter is the interface each formatter should implement.
type Formatter interface {
PrintSuiteInfo(suite *Suite)
PrintStatus(testFunc *TestFunc)
PrintFinalReport(report *FinalReport)
PrintErrorLog(errorLog []*Error)
// AllowedMethodPattern returns a regexp for the allowed
// method name (e.g. "^Test.*" for the TDDFormatter)
AllowedMethodsPattern() string
}
/*TDDFormatter is a very simple TDD-like formatter.
Legend:
* F - Test Failed
* OK - Test Passed
* EF - An Expected Failure occured
* NA - Not Assertions found
* PE - Pending test
*/
type TDDFormatter struct{}
func (formatter *TDDFormatter) PrintSuiteInfo(suite *Suite) {
fmt.Printf("\n%s.%s:\n\n", suite.Package, suite.Name)
}
func (formatter *TDDFormatter) PrintStatus(testFunc *TestFunc) {
callerName := testFunc.Name
switch testFunc.Status {
case STATUS_FAIL:
fmt.Printf(formatTag+"%-30s(%d assertion(s))\n", labelFAIL, callerName, len(testFunc.Assertions))
case STATUS_MUST_FAIL:
fmt.Printf(formatTag+"%-30s(%d assertion(s))\n", labelMUSTFAIL, callerName, len(testFunc.Assertions))
case STATUS_PASS:
fmt.Printf(formatTag+"%-30s(%d assertion(s))\n", labelPASS, callerName, len(testFunc.Assertions))
case STATUS_PENDING:
fmt.Printf(formatTag+"%-30s(%d assertion(s))\n", labelPENDING, callerName, len(testFunc.Assertions))
case STATUS_NO_ASSERTIONS:
fmt.Printf(formatTag+"%-30s(%d assertion(s))\n", labelNOASSERTIONS, callerName, len(testFunc.Assertions))
}
}
func (formatter *TDDFormatter) PrintErrorLog(logs []*Error) {
if len(logs) > 0 {
currentTestFuncHeader := ""
for _, error := range logs {
if currentTestFuncHeader != error.TestFunc.Name {
fmt.Printf("\n%s:\n", error.TestFunc.Name)
}
filename := filepath.Base(error.Assertion.Filename)
fmt.Printf("\t(%s:%d) %s\n", filename, error.Assertion.Line, error.Assertion.ErrorMessage)
currentTestFuncHeader = error.TestFunc.Name
}
}
}
func (formatter *TDDFormatter) PrintFinalReport(report *FinalReport) {
fmt.Printf("\n%d tests, %d passed, %d failed, %d expected failures, %d pending, %d with no assertions\n",
report.Total(), report.Passed, report.Failed, report.ExpectedFailures, report.Pending, report.NoAssertions)
}
func (formatter *TDDFormatter) AllowedMethodsPattern() string {
return "^Test.*"
}
// BDDFormatter is a formatter à la rspec.
type BDDFormatter struct {
Description string
}
func (formatter *BDDFormatter) PrintSuiteInfo(suite *Suite) {
fmt.Printf("\n%s:\n", formatter.Description)
}
func (formatter *BDDFormatter) PrintStatus(testFunc *TestFunc) {
shouldText := strings.Replace(testFunc.Name, "_", " ", -1)
switch testFunc.Status {
case STATUS_FAIL:
fmt.Printf("- %s\n", red(shouldText))
case STATUS_PASS:
fmt.Printf("- %s\n", green(shouldText))
case STATUS_MUST_FAIL:
fmt.Printf("- %s\n", green(shouldText))
case STATUS_PENDING:
fmt.Printf("- %s\t(Not Yet Implemented)\n", yellow(shouldText))
case STATUS_NO_ASSERTIONS:
fmt.Printf("- %s\t(No assertions found)\n", yellow(shouldText))
}
}
func (formatter *BDDFormatter) PrintFinalReport(report *FinalReport) {
fmt.Printf("\n%d examples, %d passed, %d failed, %d expected failures, %d pending, %d with no assertions\n",
report.Total(),
report.Passed,
report.Failed,
report.ExpectedFailures,
report.Pending,
report.NoAssertions)
}
func (formatter *BDDFormatter) PrintErrorLog(logs []*Error) {
if len(logs) > 0 {
currentTestFuncHeader := ""
for _, error := range logs {
if currentTestFuncHeader != error.TestFunc.Name {
fmt.Printf("\n%s:\n", error.TestFunc.Name)
}
filename := filepath.Base(error.Assertion.Filename)
fmt.Printf("\t(%s:%d) %s\n", filename, error.Assertion.Line, error.Assertion.ErrorMessage)
currentTestFuncHeader = error.TestFunc.Name
}
}
}
func (formatter *BDDFormatter) AllowedMethodsPattern() string {
return "^Should_.*"
}
func (formatter *BDDFormatter) splitString(text, sep string) (result string) {
s := strings.Split(text, sep)
if len(s) < 2 {
panic("Can't use BDD formatter!")
}
stringWithUnderscores := s[2]
splittedByUnderscores := strings.Split(stringWithUnderscores, "_")
for _, v := range splittedByUnderscores {
result += v + " "
}
return strings.TrimSpace(result)
}