/
parser-go_test.go
224 lines (189 loc) · 4.53 KB
/
parser-go_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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package vugu
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"sort"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
// parserGoBuildAndRunMulti will build and run multiple single-file components in the same package (requiring a main that prints to stdout) and return the captured output.
// pgmMap is the component struct name as the key and the program source as the value.
func parserGoBuildAndRunMulti(pgmMap map[string]string, debug bool) (string, error) {
tmpDir, err := ioutil.TempDir("", "parserGoBuildAndRun")
if err != nil {
return "", err
}
defer os.RemoveAll(tmpDir)
keys := make([]string, 0, len(pgmMap))
for k := range pgmMap {
keys = append(keys, k)
}
sort.Strings(keys)
// log.Printf("keys = %#v", keys)
for _, k := range keys {
p := &ParserGo{
PackageName: "main",
ComponentType: k,
DataType: k + "Data",
OutDir: tmpDir,
OutFile: k + ".go",
}
err = p.Parse(bytes.NewReader([]byte(pgmMap[k])))
if err != nil {
return "", fmt.Errorf("error parsing for %q: %v", k, err)
}
if debug {
b, err := ioutil.ReadFile(filepath.Join(tmpDir, k+".go"))
if err != nil {
return "", err
}
log.Printf("OUT PROGRAM (%s.go):\n%s", k, b)
}
}
wd, err := os.Getwd()
// log.Printf("test working dir = %q", wd)
if err != nil {
return "", err
}
if !filepath.IsAbs(wd) {
panic(fmt.Errorf("wd is not absolute: %s", wd))
}
err = ioutil.WriteFile(filepath.Join(tmpDir, "go.mod"), []byte(`
module main
replace github.com/vugu/vugu => `+wd+`
`), 0644)
if err != nil {
return "", err
}
cmd := exec.Command("go", "build", "-o", "a.exe", ".")
cmd.Dir = tmpDir
b, err := cmd.CombinedOutput()
if err != nil {
log.Printf("BUILD OUTPUT: %s", b)
return "", err
}
if debug {
log.Printf("BUILD OUTPUT: %s", b)
}
cmd = exec.Command("./a.exe")
cmd.Dir = tmpDir
b, err = cmd.CombinedOutput()
if err != nil {
log.Printf("RUN OUTPUT: %s", b)
return "", err
}
if debug {
log.Printf("RUN OUTPUT: %s", b)
}
return string(b), nil
}
// parserGoBuildAndRun will build an run a single-file component (requiring a main that prints to stdout) and return the captured output
func parserGoBuildAndRun(pgm string, debug bool) (string, error) {
tmpDir, err := ioutil.TempDir("", "parserGoBuildAndRun")
if err != nil {
return "", err
}
defer os.RemoveAll(tmpDir)
p := &ParserGo{
PackageName: "main",
ComponentType: "DemoComp",
// TagName: "demo-comp",
DataType: "DemoCompData",
OutDir: tmpDir,
OutFile: "demo-component.go",
}
err = p.Parse(bytes.NewReader([]byte(pgm)))
if err != nil {
return "", err
}
b, err := ioutil.ReadFile(filepath.Join(tmpDir, "demo-component.go"))
if err != nil {
log.Printf("OUT PROGRAM:\n%s", b)
return "", err
}
if debug {
log.Printf("OUT PROGRAM:\n%s", b)
}
wd, err := os.Getwd()
// log.Printf("test working dir = %q", wd)
if err != nil {
return "", err
}
if !filepath.IsAbs(wd) {
panic(fmt.Errorf("wd is not absolute: %s", wd))
}
err = ioutil.WriteFile(filepath.Join(tmpDir, "go.mod"), []byte(`
module main
replace github.com/vugu/vugu => `+wd+`
`), 0644)
if err != nil {
return "", err
}
cmd := exec.Command("go", "build", "-o", "a.exe", ".")
cmd.Dir = tmpDir
b, err = cmd.CombinedOutput()
if err != nil {
log.Printf("BUILD OUTPUT: %s", b)
return "", err
}
if debug {
log.Printf("BUILD OUTPUT: %s", b)
}
cmd = exec.Command("./a.exe")
cmd.Dir = tmpDir
b, err = cmd.CombinedOutput()
if err != nil {
log.Printf("RUN OUTPUT: %s", b)
return "", err
}
if debug {
log.Printf("RUN OUTPUT: %s", b)
}
return string(b), nil
}
func TestParserGo(t *testing.T) {
assert := assert.New(t)
out, err := parserGoBuildAndRun(`
<div id="whatever">
<ul id="ul1" vg-if="data.ShowFirstUL">
<li vg-range=".Test2" :testbind="data.TestBound">Blah1</li>
<li>Blah2</li>
</ul>
<ul id="ul2">
<li class="li3" vg-for="_, item := range data.SecondULItems" vg-html="item"></li>
</ul>
<ul id="ul3">
<!-- shorthand version -->
<li class="li4" vg-for="data.SecondULItems" vg-html="value"></li>
</ul>
</div>
<script type="application/x-go">
func main() {
_ = &vugu.VGNode{}
_ = &DemoComp{}
fmt.Println("OK")
}
type DemoComp struct {
}
type DemoCompData struct {
ShowFirstUL bool
SecondULItems []string
TestBound bool
}
func (ct *DemoComp) NewData(props vugu.Props) (interface{}, error) {
return &DemoCompData{
ShowFirstUL: true,
SecondULItems: []string{"a","b","c"},
}, nil
}
</script>
`, false)
assert.NoError(err)
assert.Equal("OK", strings.TrimSpace(out))
}