This repository has been archived by the owner on Mar 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
testutil.go
127 lines (112 loc) · 2.9 KB
/
testutil.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
package testutil
import (
"flag"
"fmt"
"os"
"path/filepath"
"testing"
"github.com/demizer/go-rst/pkg/log"
klog "github.com/go-kit/kit/log"
jd "github.com/josephburnett/jd/lib"
)
func init() { SetDebug() }
var (
StdLogger = klog.NewNopLogger()
debug bool
LogExcludes log.LoggerExcludes
LoggerConfig log.Config
)
// SetDebug is typically called from the init() function in a test file. SetDebug parses debug flags passed to the test
// binary and also sets the template for logging output.
func SetDebug() {
flag.Var(&LogExcludes, "exclude", "Exclude context from output.")
flag.BoolVar(&debug, "debug", false, "Enable debug output.")
flag.Parse()
if debug {
StdLogger = klog.NewLogfmtLogger(os.Stdout)
}
LoggerConfig = log.Config{
Name: "test",
Logger: StdLogger,
Caller: true,
CallDepth: 4,
Excludes: LogExcludes,
}
}
func LogRun(name string) {
if testing.Verbose() {
fmt.Printf("+++ RUN %s\n", name)
}
}
func LogPass(name string) {
if testing.Verbose() {
fmt.Printf("+++ PASS %s\n", name)
}
}
func Log(vals ...interface{}) {
if testing.Verbose() {
fmt.Println(vals...)
}
}
func LogDebug(vals ...interface{}) {
if debug {
fmt.Println(vals...)
}
}
func Logf(s string, vals ...interface{}) {
if testing.Verbose() {
fmt.Printf(s, vals...)
}
}
func JsonDiff(expectedData string, parsedData string) (string, error) {
a, _ := jd.ReadJsonString(expectedData)
b, _ := jd.ReadJsonString(parsedData)
return a.Diff(b).Render(), nil
}
// Contains a single test with data loaded from test files in the testdata directory
type Test struct {
Path string // The path including directory and basename
Data string // The input data to be parsed
ExpectItemData string // The expected lex items output in json
ExpectParseData string // The expected parse nodes in json
}
// Contains absolute file paths for the test data
var TestDataFiles []string
// testPathsFromDirectory walks through the file tree in the testdata directory containing all of the tests and returns a
// string slice of all the discovered paths.
func TestPathsFromDirectory(dir string) ([]string, error) {
var paths []string
wFunc := func(p string, info os.FileInfo, err error) error {
path, err := filepath.Abs(p)
if err != nil {
return err
}
if filepath.Ext(path) == ".rst" {
paths = append(paths, path[:len(path)-4])
}
return nil
}
err := filepath.Walk(dir, wFunc)
if err != nil {
return nil, err
}
return paths, nil
}
// TestPathFromName loops through TestDataFiles until name is matched.
func TestPathFromName(name string) string {
var err error
if len(TestDataFiles) < 1 {
TestDataFiles, err = TestPathsFromDirectory("../../testdata")
if err != nil {
panic(err)
}
}
for _, p := range TestDataFiles {
if len(p)-len(name) > 0 {
if p[len(p)-len(name):] == name {
return p
}
}
}
panic(fmt.Sprintf("Could not find test for %q\n", name))
}