-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
assert.go
146 lines (118 loc) · 3.09 KB
/
assert.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
package main
import (
"fmt"
"regexp"
"runtime"
"sort"
"github.com/yuin/goldmark/ast"
)
// type for all linter assert methods
type T struct {
filename string
markdown []byte
newlineOffsets []int
sourceFlag bool
fails int
}
// called by all assert functions that involve a node
func (t *T) printFailedAssertf(n ast.Node, format string, args ...interface{}) {
t.printFile(n)
fmt.Printf(format+"\n", args...)
t.printRule(3)
t.fails++
}
// Assert function that doesnt involve a node, for example if something is missing
func (t *T) assertf(format string, args ...interface{}) {
t.assertLine2f(0, format, args...) // There's no line number associated, so use the first
}
func (t *T) assertNodef(n ast.Node, format string, args ...interface{}) {
t.printFailedAssertf(n, format, args...)
}
func (t *T) assertLinef(line int, format string, args ...interface{}) {
//this func only exists to make the call stack to t.printRule the same depth
//as when called through assertf
t.assertLine2f(line, format, args...)
}
func (t *T) assertLine2f(line int, format string, args ...interface{}) {
t.printFileLine(line)
fmt.Printf(format+"\n", args...)
t.printRule(3)
t.fails++
}
func (t *T) printRule(callers int) {
if !t.sourceFlag {
return
}
pc, codeFilename, codeLine, ok := runtime.Caller(callers)
if !ok {
panic("can not get caller")
}
f := runtime.FuncForPC(pc)
var funcName string
if f != nil {
funcName = f.Name()
}
fmt.Printf("%s:%d: ", codeFilename, codeLine)
if len(funcName) == 0 {
fmt.Printf("failed assert\n")
} else {
fmt.Printf("failed assert in function %s\n", funcName)
}
}
func (t *T) line(offset int) int {
return sort.SearchInts(t.newlineOffsets, offset)
}
func (t *T) printFile(n ast.Node) {
lines := n.Lines()
if lines == nil || lines.Len() == 0 {
t.printFileLine(0)
return
}
offset := lines.At(0).Start
line := t.line(offset)
t.printFileLine(line)
}
func (t *T) printFileLine(line int) {
fmt.Printf("%s:%d: ", t.filename, line+1) // Lines start with 1
}
func (t *T) printPassFail() {
if t.fails == 0 {
fmt.Printf("Pass %s\n", t.filename)
} else {
fmt.Printf("Fail %s, %d failed assertions\n", t.filename, t.fails)
}
}
func (t *T) assertKind(expected ast.NodeKind, n ast.Node) {
if n.Kind() == expected {
return
}
t.printFailedAssertf(n, "expected %s, have %s", expected.String(), n.Kind().String())
}
func (t *T) assertFirstChildRegexp(expectedPattern string, n ast.Node) {
var validRegexp = regexp.MustCompile(expectedPattern)
if !n.HasChildren() {
t.printFailedAssertf(n, "expected children")
return
}
c := n.FirstChild()
actual := string(c.Text(t.markdown))
if !validRegexp.MatchString(actual) {
t.printFailedAssertf(n, "%q does not match regexp %q", actual, expectedPattern)
return
}
}
func (t *T) assertHeadingLevel(expected int, n ast.Node) {
h, ok := n.(*ast.Heading)
if !ok {
fmt.Printf("failed Heading type assertion\n")
t.fails++
return
}
if h.Level == expected {
return
}
t.printFailedAssertf(n, "expected header level %d, have %d", expected, h.Level)
}
func (t *T) pass() bool {
return t.fails == 0
}