-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_test.go
76 lines (69 loc) · 1.44 KB
/
generate_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
package scanlib
import (
"os"
"path/filepath"
"testing"
"git.furqansoftware.net/toph/scanlib/ast"
"git.furqansoftware.net/toph/scanlib/gen/cpp14"
"git.furqansoftware.net/toph/scanlib/gen/go1"
"git.furqansoftware.net/toph/scanlib/gen/py3"
"github.com/google/go-cmp/cmp"
)
type language struct {
key string
ext string
genFn func(*ast.Source) ([]byte, error)
}
var (
langs = []language{
{
key: "cpp14",
ext: ".cpp",
genFn: cpp14.Generate,
},
{
key: "go1",
ext: ".go",
genFn: go1.Generate,
},
{
key: "py3",
ext: ".py",
genFn: py3.Generate,
},
}
)
func TestGenerate(t *testing.T) {
fis, err := os.ReadDir("./testdata")
if err != nil {
t.Fatal(err)
}
for _, l := range langs {
t.Run(l.key, func(t *testing.T) {
for _, fi := range fis {
codesrc, err := os.ReadFile(filepath.Join("./testdata", fi.Name(), l.key+l.ext))
if os.IsNotExist(err) {
continue
}
t.Run(fi.Name(), func(t *testing.T) {
specsrc, err := os.ReadFile(filepath.Join("./testdata", fi.Name(), "scanspec"))
if err != nil {
t.Fatal(err)
}
n, err := ast.ParseString("inputspec", string(specsrc))
if err != nil {
t.Fatal(err)
}
code, err := l.genFn(n)
if err != nil {
t.Fatal(err)
}
diff := cmp.Diff(code, codesrc)
if diff != "" {
t.Errorf("want:\n\n%s\n\ngot:\n\n%sdiff:\n\n%s", codesrc, code, diff)
}
})
}
})
}
}