-
Notifications
You must be signed in to change notification settings - Fork 19
/
parser_test.go
178 lines (147 loc) · 4.04 KB
/
parser_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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package memefish_test
import (
"bytes"
"flag"
"fmt"
"log"
"os"
"path/filepath"
"testing"
"github.com/cloudspannerecosystem/memefish"
"github.com/cloudspannerecosystem/memefish/ast"
"github.com/cloudspannerecosystem/memefish/token"
"github.com/k0kubun/pp"
"github.com/pmezard/go-difflib/difflib"
)
var update = flag.Bool("update", false, "update result files")
func testParser(t *testing.T, inputPath, resultPath string, parse func(p *memefish.Parser) (ast.Node, error)) {
printer := pp.New()
printer.SetColoringEnabled(false)
if *update {
_, err := os.Stat(resultPath)
if err == nil {
err = os.RemoveAll(resultPath)
if err != nil {
log.Fatalf("error on remove result path: %v", err)
}
}
err = os.MkdirAll(resultPath, 0777)
if err != nil {
log.Fatalf("error on create result path: %v", err)
}
}
inputs, err := os.ReadDir(inputPath)
if err != nil {
t.Fatalf("error on reading input path: %v", err)
}
for _, in := range inputs {
in := in
t.Run(in.Name(), func(t *testing.T) {
t.Parallel()
inputFilePath := filepath.Join(inputPath, in.Name())
b, err := os.ReadFile(inputFilePath)
if err != nil {
t.Fatalf("error on reading input file: %v", err)
}
p := &memefish.Parser{
Lexer: &memefish.Lexer{
File: &token.File{FilePath: inputFilePath, Buffer: string(b)},
},
}
node, err := parse(p)
if err != nil {
log.Fatalf("error on parsing input file: %v", err)
}
var buf bytes.Buffer
fmt.Fprintf(&buf, "--- %s\n", in.Name())
fmt.Fprint(&buf, string(b))
fmt.Fprintln(&buf)
fmt.Fprintf(&buf, "--- AST\n")
_, _ = printer.Fprintln(&buf, node)
fmt.Fprintln(&buf)
fmt.Fprintf(&buf, "--- SQL\n")
fmt.Fprintln(&buf, node.SQL())
actual := buf.Bytes()
if *update {
t.Log("update " + in.Name() + ".txt")
err = os.WriteFile(filepath.Join(resultPath, in.Name()+".txt"), buf.Bytes(), 0666)
if err != nil {
log.Fatalf("error on writing result file: %v", err)
}
return
}
expected, err := os.ReadFile(filepath.Join(resultPath, in.Name()+".txt"))
if err != nil {
t.Fatalf("error on reading result file: %v", err)
}
if !bytes.Equal(actual, expected) {
diff := difflib.UnifiedDiff{
A: difflib.SplitLines(string(expected)),
B: difflib.SplitLines(string(actual)),
Context: 5,
}
d, err := difflib.GetUnifiedDiffString(diff)
if err != nil {
t.Fatalf("error on diff: %v", err)
}
t.Error(d)
return
}
s1 := node.SQL()
p1 := &memefish.Parser{
Lexer: &memefish.Lexer{
File: &token.File{FilePath: in.Name() + " (SQL)", Buffer: s1},
},
}
node1, err := parse(p1)
if err != nil {
log.Fatalf("error on parsing unparsed SQL: %v", err)
}
s2 := node1.SQL()
if s1 != s2 {
t.Errorf("%q != %q", s1, s2)
}
})
}
}
func TestParseQuery(t *testing.T) {
inputPath := "./testdata/input/query"
resultPath := "./testdata/result/query"
testParser(t, inputPath, resultPath, func(p *memefish.Parser) (ast.Node, error) {
return p.ParseQuery()
})
}
func TestParseDDL(t *testing.T) {
inputPath := "./testdata/input/ddl"
resultPath := "./testdata/result/ddl"
testParser(t, inputPath, resultPath, func(p *memefish.Parser) (ast.Node, error) {
return p.ParseDDL()
})
}
func TestParseDML(t *testing.T) {
inputPath := "./testdata/input/dml"
resultPath := "./testdata/result/dml"
testParser(t, inputPath, resultPath, func(p *memefish.Parser) (ast.Node, error) {
return p.ParseDML()
})
}
func TestParseExpr(t *testing.T) {
inputPath := "./testdata/input/expr"
resultPath := "./testdata/result/expr"
testParser(t, inputPath, resultPath, func(p *memefish.Parser) (ast.Node, error) {
return p.ParseExpr()
})
}
func TestParseStatement(t *testing.T) {
inputPaths := []string{
"./testdata/input/query",
"./testdata/input/ddl",
"./testdata/input/dml",
}
resultPath := "./testdata/result/statement"
for _, inputPath := range inputPaths {
testParser(t, inputPath, resultPath, func(p *memefish.Parser) (ast.Node, error) {
return p.ParseStatement()
})
}
}