-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvjson_test.go
183 lines (152 loc) · 5.07 KB
/
vjson_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
package vjson
import (
"os"
"os/exec"
"path/filepath"
"testing"
"log"
"regexp"
"strings"
"fmt"
"io/ioutil"
)
func TestGo(t *testing.T) {
tcaseList := []struct {
name string
in string
matches []string
} {
{name: "simple_map",in: `{"testkey":"testval"}`, matches: []string{`{"testkey":"testval"}`}},
{name: "simple_map_number",in: `{"teststr":"str1","testnum":15}`, matches: []string{`"teststr":"str1"`,`"testnum":15`}},
{name: "simple_map_bool",in: `{"testbool1":false,"testbool2":true}`, matches: []string{`"testbool1":false`,`"testbool2":true`}},
{name: "string_esc",in: `{"testkey":"test\"val"}`, matches: []string{`[{]"testkey":"test\\"val"[}]`}},
{name: "string_esc2",in: `{"testkey":"test\"val\""}`, matches: []string{`[{]"testkey":"test\\"val\\""[}]`}},
{name: "string_esc3",in: `{"testkey":"te\\st\"val\""}`, matches: []string{`[{]"testkey":"te\\\\st\\"val\\""[}]`}},
{name: "null1",in: `{"null1":null}`, matches: []string{`"null1":null`}},
{name: "array1",in: `{"array1":["s1","s2"]}`, matches: []string{`"array1":\["s1","s2"\]`}},
{name: "array2",in: `{"array2":[]}`, matches: []string{`"array2":\[\]`}},
{name: "array_of_obj1",in: `{"array1":[{},{"k1":"v1"}]}`, matches: []string{`"array1":...,."k1":"v1".`}},
}
for _, tcase := range tcaseList {
tcase := tcase
t.Run(tcase.name, func(t *testing.T) {
var m map[string]interface{}
err := unmarshal([]byte(tcase.in), &m)
if err != nil {
t.Fatal(err)
}
b, err := marshal(m)
if err != nil {
t.Fatal(err)
}
t.Logf("Marshal output: %s", b)
for _, ma := range tcase.matches {
if !regexp.MustCompile(ma).Match(b) {
t.Errorf("failed to match in output: %s", ma)
}
}
})
}
}
func TestTinygo(t *testing.T) {
dockerImage := "tinygo/tinygo:latest"
pwd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
pwd, err = filepath.EvalSymlinks(pwd)
if err != nil {
t.Fatal(err)
}
// now execute it inside a container for each input (since it will be built for that target, not necessarily for the host)
tcaseList := []struct {
name string
in string
matches []string
} {
{name: "simple_map",in: `{"testkey":"testval"}`, matches: []string{`{"testkey":"testval"}`}},
{name: "simple_map_number",in: `{"teststr":"str1","testnum":15}`, matches: []string{`"teststr":"str1"`,`"testnum":15`}},
{name: "simple_map_bool",in: `{"testbool1":false,"testbool2":true}`, matches: []string{`"testbool1":false`,`"testbool2":true`}},
{name: "string_esc",in: `{"testkey":"test\"val"}`, matches: []string{`[{]"testkey":"test\\"val"[}]`}},
{name: "string_esc2",in: `{"testkey":"test\"val\""}`, matches: []string{`[{]"testkey":"test\\"val\\""[}]`}},
{name: "string_esc3",in: `{"testkey":"te\\st\"val\""}`, matches: []string{`[{]"testkey":"te\\\\st\\"val\\""[}]`}},
{name: "null1",in: `{"null1":null}`, matches: []string{`"null1":null`}},
{name: "array1",in: `{"array1":["s1","s2"]}`, matches: []string{`"array1":\["s1","s2"\]`}},
{name: "array2",in: `{"array2":[]}`, matches: []string{`"array2":\[\]`}},
{name: "array_of_obj1",in: `{"array1":[{},{"k1":"v1"}]}`, matches: []string{`"array1":...,."k1":"v1".`}},
}
for _, tcase := range tcaseList {
tcase := tcase
t.Run(tcase.name, func(t *testing.T) {
pgmText := strings.Replace(testPgmTemplate, `__JSON_IN__`, fmt.Sprintf("%q", tcase.in), 1)
err := ioutil.WriteFile("vjson_test_pgm.go", []byte(pgmText), 0644)
if err != nil {
t.Fatal(err)
}
// build test program with test case stuff hard coded into it (os.Stdin doesn't seem to work, no args, no env)
cmd := exec.Command(
"docker", "run",
"--rm",
"-t",
// "-a","STDERR","-a","STDOUT",
"-eGOPATH=/root/go",
"-v"+pwd+":/root/go/src/github.com/vugu/vjson",
dockerImage,
"tinygo", "build",
"-o", "/root/go/src/github.com/vugu/vjson/vjson_test_pgm.out",
"/root/go/src/github.com/vugu/vjson/vjson_test_pgm.go",
)
b, err := cmd.CombinedOutput()
log.Printf("BUILD OUTPUT: %s", b)
if err != nil {
t.Fatal(err)
}
// run it
cmd = exec.Command(
"docker", "run",
"--rm",
"-t",
// "-a","STDERR","-a","STDOUT",
"-eGOPATH=/root/go",
"-v"+pwd+":/root/go/src/github.com/vugu/vjson",
dockerImage,
"/root/go/src/github.com/vugu/vjson/vjson_test_pgm.out",
)
// cmd.Stdin = bytes.NewReader([]byte(tcase.in))
b, err = cmd.CombinedOutput()
log.Printf("RUN OUTPUT for %s: %s", tcase.name, b)
if err != nil {
t.Fatal(err)
}
for _, ma := range tcase.matches {
if !regexp.MustCompile(ma).Match(b) {
t.Errorf("failed to match in output: %s", ma)
}
}
// time.Sleep(time.Second * 2)
})
}
}
var testPgmTemplate = `// +build ignore
package main
// This is a test program that is built and run with Tinygo. See vjson_test.go.
import (
"fmt"
//"io/ioutil"
//"os"
"github.com/vugu/vjson"
)
func main() {
var jsonIn = __JSON_IN__
var m map[string]interface{}
err := vjson.Unmarshal([]byte(jsonIn), &m)
if err != nil {
panic(err)
}
b, err := vjson.Marshal(m)
if err != nil {
panic(err)
}
fmt.Printf("%s", b)
}
`