-
Notifications
You must be signed in to change notification settings - Fork 6
/
out.go
105 lines (83 loc) · 2.1 KB
/
out.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
package mao
import (
"fmt"
"io/ioutil"
"os"
"path"
"runtime"
"strconv"
"strings"
)
type FailingLine struct {
content string
filename string
next string
number int
prev string
}
var lastTitle string
var (
testCounter = 0
failCounter = 0
startingLine = os.Getenv("MAO_LINENO_START")
reset = "\033[0m"
white = "\033[37m\033[1m"
grey = "\x1B[90m"
red = "\033[31m\033[1m"
)
func (test *Test) PrintTitle(title string) {
fmt.Printf("\033[37m \033[1m\n %s \n\n", title)
}
func (test *Test) PrintError(message string) {
failCounter += 1
if lastTitle != test.Title {
lastTitle = test.Title
test.PrintTitle(test.Title)
}
failingLine, err := getFailingLine()
if err != nil {
return
}
fmt.Printf("%s %s %s %s %s\n", red, message, grey, path.Base(failingLine.filename), reset)
test.PrintFailingLine(&failingLine)
}
func (test *Test) PrintFailingLine(failingLine *FailingLine) {
fmt.Printf("%s %d. %s\n", grey, failingLine.number-1, failingLine.prev)
fmt.Printf("%s %d. %s %s\n", white, failingLine.number, failingLine.content, reset)
fmt.Printf("%s %d. %s\n", grey, failingLine.number+1, failingLine.next)
fmt.Println(reset)
}
func PrintTestSummary() {
if failCounter > 0 {
fmt.Printf("\n Ran %d tests, %d assertions failed.\n\n", testCounter, failCounter)
return
}
fmt.Printf("\n Ran %d tests successfully.\n\n", testCounter)
}
func getFailingLine() (FailingLine, error) {
_, filename, ln, _ := runtime.Caller(3)
bf, err := ioutil.ReadFile(filename)
if err != nil {
return FailingLine{}, fmt.Errorf("Failed to open %s", filename)
}
lines := strings.Split(string(bf), "\n")[ln-2 : ln+2]
filename = strings.Replace(filename, "mao_", "", 1)
lineno := int(ln)
if len(startingLine) > 0 {
lndiff, _ := strconv.Atoi(startingLine)
lineno -= lndiff
}
return FailingLine{
softTabs(lines[1]),
filename,
softTabs(lines[2]),
lineno,
softTabs(lines[0]),
}, nil
}
func incTestCounter() {
testCounter += 1
}
func softTabs(text string) string {
return strings.Replace(text, "\t", " ", -1)
}