-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser_test.go
139 lines (123 loc) · 3.33 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
package lunar
import (
"bytes"
"fmt"
"go/ast"
"go/parser"
"go/token"
"strings"
"testing"
"golang.org/x/tools/go/loader"
)
func ParseSnippet(snippet string) (string, string, error) {
src := fmt.Sprintf("package dummy\nfunc testFunc() {%s}", snippet)
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "dummy.go", src, parser.ParseComments)
if err != nil {
panic(fmt.Sprintf("Could not parse source %q: %v", src, err))
}
tree := &bytes.Buffer{}
node := f.Decls[0].(*ast.FuncDecl).Body
ast.Fprint(tree, fset, node, nil)
p := &Parser{testPkgName: "dummy"}
buf := &bytes.Buffer{}
err = p.ParseNode(buf, node)
if err != nil {
return "", tree.String(), err
}
return strings.TrimSpace(buf.String()), tree.String(), nil
}
func ParseFunc(src string) (string, string, error) {
src = fmt.Sprintf(`func testFunc() {%s}`, src)
get := func(f *ast.File) ast.Node {
return f.Decls[0].(*ast.FuncDecl).Body
}
return parseStr(src, get)
}
func ParsePackage(src string) (string, string, error) {
get := func(f *ast.File) ast.Node {
return f
}
return parseStr(src, get)
}
func parseStr(src string, get func(*ast.File) ast.Node) (string, string, error) {
src = "package dummy\n" + src
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "dummy.go", src, parser.ParseComments)
if err != nil {
panic(fmt.Sprintf("Could not parse source %q: %v", src, err))
}
tree := &bytes.Buffer{}
node := get(f)
ast.Fprint(tree, fset, node, nil)
var conf loader.Config
conf.Fset = fset
conf.CreateFromFiles("dummy", f)
prog, err := conf.Load()
if err != nil {
return "", tree.String(), err
}
p := NewParser(prog)
buf := &bytes.Buffer{}
err = p.ParseNode(buf, node)
if err != nil {
return "", tree.String(), err
}
return strings.TrimSpace(buf.String()), tree.String(), nil
}
type StringTest struct {
Go string
Lua string
}
func RunSnippetTests(t *testing.T, tests []StringTest) {
for i, test := range tests {
lua, tree, err := ParseSnippet(test.Go)
if err != nil {
t.Logf("Got tree: %s", tree)
t.Errorf("%d. Go %q resulted in error: %#v", i, test.Go, err)
continue
} else if lua != test.Lua {
t.Logf("Got tree: %s", tree)
t.Errorf("%d. Go %q resulted in Lua %q; want %q", i, test.Go, lua, test.Lua)
continue
}
}
}
func RunFuncTests(t *testing.T, tests []StringTest) {
for i, test := range tests {
lua, tree, err := ParseFunc(test.Go)
if err != nil {
t.Logf("Got tree: %s", tree)
t.Errorf("%d. Go %q resulted in error: %#v", i, test.Go, err)
continue
} else if lua != test.Lua {
t.Logf("Got tree: %s", tree)
t.Errorf("%d. Go %q resulted in Lua %q; want %q", i, test.Go, lua, test.Lua)
continue
}
}
}
func RunPackageTests(t *testing.T, tests []StringTest) {
const prelude = `-- Package declaration
local dummy = _G.dummy or {}
_G.dummy = dummy
local builtins = _G.lunar_go_builtins
-- Local declarations
`
for i, test := range tests {
lua, tree, err := ParsePackage(test.Go)
if err != nil {
t.Logf("Got tree: %s", tree)
t.Errorf("%d. Go %q resulted in error: %v", i, test.Go, err)
continue
} else if !strings.HasPrefix(lua, prelude) {
t.Errorf("%d. Expected prelude %q, got %q", prelude, lua)
}
pure := lua[len(prelude):]
if pure != test.Lua {
t.Logf("Got tree: %s", tree)
t.Errorf("%d. Go %q resulted in Lua %q; want %q", i, test.Go, pure, test.Lua)
continue
}
}
}