forked from rsc/grind
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.go
77 lines (72 loc) · 1.82 KB
/
test.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
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package grindtest
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"testing"
"github.com/jackspirou/grind/grinder"
)
var run = flag.String("grindrun", "", "only run golden tests for files with names matching this regexp")
func TestGlob(t *testing.T, pattern string, grinders []grinder.Func) {
matches, err := filepath.Glob(pattern)
if err != nil {
t.Errorf("%v", err)
return
}
if len(matches) == 0 {
t.Errorf("no matches for %s", pattern)
return
}
var re *regexp.Regexp
if *run != "" {
var err error
re, err = regexp.Compile(*run)
if err != nil {
t.Errorf("invalid regexp passed to -grindrun: %v", err)
return
}
}
for _, file := range matches {
if re != nil && !re.MatchString(file) {
continue
}
var buf bytes.Buffer
ctxt := &grinder.Context{
Grinders: grinders,
}
ctxt.Logf = func(format string, args ...interface{}) {
fmt.Fprintf(&buf, format, args...)
if buf.Len() > 0 && buf.Bytes()[buf.Len()-1] != '\n' {
buf.WriteByte('\n')
}
}
pkg := ctxt.GrindFiles(file)
if pkg == nil || ctxt.Errors {
t.Errorf("%s: grind failed:\n%s", file, buf.String())
continue
}
data, err := ioutil.ReadFile(file + ".out")
if err != nil {
if os.IsNotExist(err) {
if pkg.Modified(file) {
t.Errorf("%s: should not modify, but made changes:\n%s", file, grinder.Diff(pkg.OrigSrc(file), pkg.Src(file)))
}
continue
}
t.Errorf("%s: reading golden output: %v", file, err)
continue
}
want := string(data)
have := pkg.Src(file)
if have != want {
t.Errorf("%s: incorrect output\nhave:\n%s\nwant:\n%s\ndiff want have:\n%s", file, have, want, grinder.Diff(want, have))
}
}
}