-
Notifications
You must be signed in to change notification settings - Fork 10
/
golden_test.go
109 lines (91 loc) · 2.34 KB
/
golden_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
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
package main
import (
"fmt"
"io/ioutil"
"path"
"strings"
"testing"
"github.com/sergi/go-diff/diffmatchpatch"
"github.com/stretchr/testify/assert"
)
var (
golden = []string{
"Array",
"Channeler",
"Embedder",
"Funcer",
"Identifier",
"Interfacer",
"Importer",
"Mapper",
"Multireturner",
"Namedvaluer",
"Pointer",
"Qualifier",
"Structer",
"Variadic",
"Voider",
}
unsupported = []string{
"_",
"Emptier",
}
dmp = diffmatchpatch.New()
)
func TestUnsupported(t *testing.T) {
for _, interfaceName := range unsupported {
t.Run(interfaceName, CheckOneUnsupported)
}
}
func CheckOneUnsupported(t *testing.T) {
name := path.Base(t.Name())
lname := strings.ToLower(name)
inputFilename := fmt.Sprintf("testdata/%s/%s_def.go", lname, lname)
g, err := parsePackage("testdata/"+lname, []string{inputFilename})
if err != nil {
t.Fatalf("parsePackage error: %s", err)
}
got, err := g.Generate([]string{name})
if err == nil {
t.Fatalf("expected error for %s", name)
}
if got != nil {
t.Fatalf("Unexpected output result for %s", name)
}
}
func TestGolden(t *testing.T) {
for _, interfaceName := range golden {
t.Run(interfaceName, CheckOneGolden)
}
}
func CheckOneGolden(t *testing.T) {
name := path.Base(t.Name())
lname := strings.ToLower(name)
symGen.reset()
identSymGen.reset()
inputFilename := fmt.Sprintf("./testdata/%s/%s_def.go", lname, lname)
outputFilename := fmt.Sprintf("./testdata/%s/%s.go", lname, lname)
outputFile, err := ioutil.ReadFile(outputFilename)
if err != nil {
outputFile = []byte{}
}
g, err := parsePackage("testdata/"+lname, []string{inputFilename})
if err != nil {
t.Fatalf("parsePackage error: %s", err)
}
got, err := g.Generate([]string{name})
if err != nil {
t.Fatalf("Generator.Generate error for %s: %s", name, err)
}
if len(got) == 0 {
t.Fatalf("%q resulted in an empty file when the contents of %q were expected", name, outputFilename)
}
readableOutput := string(outputFile)
readableResult := string(got)
// Only compare everything after the first line to avoid
// comparing the generation commands
outputStart := strings.Index(readableOutput, "\n")
resultStart := strings.Index(readableResult, "\n")
diffs := dmp.DiffMain(readableOutput, readableResult, false)
assert.Equal(t, readableOutput[outputStart:], readableResult[resultStart:], dmp.DiffPrettyText(diffs))
}