-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathformatter.go
135 lines (118 loc) · 3.31 KB
/
formatter.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
package gospel
import (
"fmt"
"io/ioutil"
"os"
"runtime"
"strings"
"github.com/mattn/go-colorable"
)
// Decides Formatter type.
var verboseMode bool
var Output = colorable.NewColorableStdout()
// Checks if -v option specified or not.
func init() {
for _, argument := range os.Args {
if argument == "-test.v=true" {
verboseMode = true
return
}
}
}
// Factory method to create a Formatter.
func newFormatter() Formatter {
if verboseMode {
return &DocumentFormatter{}
} else {
return &DotFormatter{}
}
}
type Formatter interface {
Started(*Example)
Failed(*Example, string)
Succeeded(*Example)
}
type DotFormatter struct {}
// Does nothing.
func (formatter *DotFormatter) Started(example *Example) {
}
func (formatter *DotFormatter) Failed(example *Example, message string) {
fmt.Fprintf(Output, red("F"))
root := example.ExampleGroup.Root()
if root.Result == "" {
root.Result += "\n\n"
}
_, filename, line, _ := runtime.Caller(3)
buffer, _ := ioutil.ReadFile(filename)
lines := strings.Split(string(buffer), "\n")[line-2:line+2]
root.Result += fmt.Sprintf(
red(" %s\n") +
grey(" %s\n") +
grey(" %s:%d\n") +
grey(" %4d.%s\n") +
grey(" %4d.%s\n") +
grey(" %4d.%s\n") +
"\n",
example.FullDescription(),
message,
filename, line,
line - 1, strings.Replace(lines[0], "\t", " ", -1),
line + 0, strings.Replace(lines[1], "\t", " ", -1),
line + 1, strings.Replace(lines[2], "\t", " ", -1),
)
}
func (formatter *DotFormatter) Succeeded(example *Example) {
fmt.Fprintf(Output, green("."))
}
type DocumentFormatter struct {}
func (formatter *DocumentFormatter) Started(example *Example) {
var previousDescriptions []string
if previousExample != nil {
previousDescriptions = previousExample.Descriptions()
}
currentDescriptions := example.Descriptions()
fullMessage := ""
for i, description := range currentDescriptions {
if previousExample == nil || i > len(previousDescriptions) - 1 || description != previousDescriptions[i] {
fullMessage += strings.Repeat(" ", i) + "\033[0m" + description + "\n"
}
}
fmt.Fprint(Output, fullMessage)
}
func (formatter *DocumentFormatter) Succeeded(example *Example) {
margin := strings.Repeat(" ", len(example.ExampleGroup.Ancestors()) + 1)
fmt.Fprintln(Output, margin + green(example.Message))
}
func (formatter *DocumentFormatter) Failed(example *Example, message string) {
_, filename, line, _ := runtime.Caller(3)
buffer, _ := ioutil.ReadFile(filename)
lines := strings.Split(string(buffer), "\n")[line-2:line+2]
margin := strings.Repeat(" ", len(example.ExampleGroup.Ancestors()) + 1)
result := fmt.Sprintf(
red("%s%s\n") +
grey("%s%s\n") +
grey("%s%s:%d\n") +
grey("%s%4d.%s\n") +
grey("%s%4d.%s\n") +
grey("%s%4d.%s\n"),
margin, example.Message,
margin, message,
margin, filename, line,
margin, line - 1, strings.Replace(lines[0], "\t", " ", -1),
margin, line + 0, strings.Replace(lines[1], "\t", " ", -1),
margin, line + 1, strings.Replace(lines[2], "\t", " ", -1),
)
fmt.Fprint(Output, result)
}
// Add red terminal ANSI color
func red(str string) string {
return "\033[31m\033[1m" + str + "\033[0m"
}
// Add green terminal ANSI color
func green(str string) string {
return "\033[32m\033[1m" + str + "\033[0m"
}
// Add grey terminal ANSI color
func grey(str string) string {
return "\x1B[90m" + str + "\033[0m"
}