-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparse_test.go
More file actions
141 lines (124 loc) · 3.23 KB
/
Copy pathparse_test.go
File metadata and controls
141 lines (124 loc) · 3.23 KB
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
package pgtree_test
import (
"errors"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"testing"
"github.com/gofoji/pgtree"
)
func TestParseAndPretty(t *testing.T) {
ff, err := filepath.Glob("testdata/*.sql")
if err != nil {
panic(err)
}
for _, test := range ff {
if strings.HasSuffix(test, "_want.sql") {
continue
}
t.Run(test, func(t *testing.T) {
b, err := os.ReadFile(test)
if err != nil {
t.Errorf("ReadFile error = %v", err)
return
}
want, err := os.ReadFile(FileWithExt(test, "_want.sql"))
if err != nil {
t.Errorf("ReadFile error = %v", err)
return
}
got, err := testParse(string(b))
if err != nil {
if got == "" {
t.Errorf("Parse error = %v", err)
return
}
t.Errorf("Error: %v\ngot:\n`%v`\nwant:\n`%v`", err, got, string(want))
return
}
w := string(want)
if got != w {
t.Errorf("Mismatch diff:\n%s", diff(got, w))
t.Errorf("got:\n%s\nwant:\n%s", got, w)
return
}
})
}
ff, _ = filepath.Glob("temp/sql/*.sql")
for _, test := range ff {
t.Run(test, func(t *testing.T) {
b, err := os.ReadFile(test)
if err != nil {
t.Errorf("ReadFile error = %v", err)
return
}
got, err := testParse(string(b))
if err != nil {
if got == "" {
t.Errorf("Parse error = %v", err)
return
}
t.Errorf("Error: %v\ngot:\n`%v`", err, got)
return
}
})
}
}
func diff(got, want string) string {
var result []string
gg := strings.Split(got, "\n")
ww := strings.Split(want, "\n")
for i, g := range gg {
if i >= len(ww) {
result = append(result, "<<<<<got:line:"+strconv.Itoa(i), "`"+g+"`", ">>>>>", "=====")
} else if ww[i] != g {
result = append(result, "<<<<<got:line:"+strconv.Itoa(i), "`"+g+"`", ">>>>>", "`"+ww[i]+"`", "=====")
} else {
result = append(result, g)
}
}
if len(gg) < len(ww) {
result = append(result, "<<<<<got missing", ">>>>>", "`"+strings.Join(ww[len(gg):], "\n")+"`", "=====")
}
return strings.Join(result, "\n")
}
func FileWithExt(path, ext string) string {
return strings.TrimSuffix(path, filepath.Ext(path)) + ext
}
func testParse(sql string) (string, error) {
// We validate the parsing and printing by:
// first Parse the input SQL
// then Print it (concise)
// then Parse the concise (this ensures the generated syntax is valid)
// then Pretty Print
// then compare to the Pretty Print of the original parse
parseResult, err := pgtree.Parse(sql)
if err != nil {
return "", err
}
concise, err := pgtree.PrintParseResult(parseResult)
if err != nil {
return "", fmt.Errorf("concise:%w", err)
}
conciseParse, err := pgtree.Parse(concise)
if err != nil {
return "", fmt.Errorf("conciseParse:%w", err)
}
concisePretty, err := pgtree.PrettyPrintParseResult(conciseParse)
if err != nil {
return "", fmt.Errorf("concise:%w", err)
}
inputPretty, err := pgtree.PrettyPrintParseResult(parseResult)
if err != nil {
return "", fmt.Errorf("concise:%w", err)
}
if inputPretty != concisePretty {
s, ss, err := pgtree.Debug(parseResult.Stmts[0].Stmt)
fmt.Printf("Debug:\n%s\nGraph:\n%v\n,Error:%v\n", s, ss, err)
fmt.Printf("inputPretty:\n%s\nconcisePretty:\n%s\n", inputPretty, concisePretty)
return "", errors.New("re-parse mismatch")
}
return inputPretty, nil
}