-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjson2ast_test.go
255 lines (204 loc) · 5.08 KB
/
json2ast_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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package json2go_test
import (
"bytes"
"fmt"
"go/ast"
"go/parser"
"go/token"
"reflect"
"testing"
)
import (
. "launchpad.net/gocheck"
)
import (
"github.com/modcloth-labs/json2go"
)
func Test(t *testing.T) { TestingT(t) }
type Json2AstSuite struct{}
var _ = Suite(&Json2AstSuite{})
var json2AstTests = []struct {
Input string
Output string
}{
{
`{"a": 5, "b": true, "c": 64.5, "d": "foo"}`,
`package main
type Top struct {
a float64
b bool
c float64
d string
}`,
},
{
`{"foo": {"bar": true}}`,
`package main
type Foo struct {
bar bool
}
type Top struct {
foo *Foo
}`,
},
{
`{"foo": [{"bar": true}, {"bar": false}]}`,
`package main
type Foo struct {
bar bool
}
type Top struct {
foo []*Foo
}`,
},
{
`{"foo": [{"bar": true}, {"bar": false}]}`,
`package main
type Foo struct {
bar bool
}
type Top struct {
foo []*Foo
}`,
},
{
`{"foo": [{"bar": {"foobar": 46.5}}]}`,
`package main
type Bar struct {
foobar float64
}
type Foo struct {
bar *Bar
}
type Top struct {
foo []*Foo
}`,
},
}
type astEquals struct {
*CheckerInfo
}
var AstEquals Checker = &astEquals{
&CheckerInfo{Name: "AST Equals", Params: []string{"obtained", "expected"}},
}
func (this *astEquals) Check(params []interface{}, names []string) (bool, string) {
var obtainedNodes, expectedNodes chan *NodeWithBreadcrumbs
var obtainedNode, expectedNode *NodeWithBreadcrumbs
obtainedNodes = make(chan *NodeWithBreadcrumbs)
expectedNodes = make(chan *NodeWithBreadcrumbs)
go ast.Walk(&AstChannelWalker{Out: obtainedNodes}, params[0].(ast.Node))
go ast.Walk(&AstChannelWalker{Out: expectedNodes}, params[1].(ast.Node))
for {
obtainedNode = <-obtainedNodes
expectedNode = <-expectedNodes
if obtainedNode == nil && expectedNode == nil {
break
}
if obtainedNode == nil || expectedNode == nil {
return false, fmt.Sprintf("\n%+v\ndid not match\n%+v", obtainedNode, expectedNode)
}
if !this.nodeEquals(obtainedNode.Node, expectedNode.Node) {
return false, fmt.Sprintf("\n%+v\ndid not match\n%+v", obtainedNode, expectedNode)
}
}
return true, ""
}
func (s *Json2AstSuite) TestJson2Ast(c *C) {
var fset *token.FileSet
var obtainedAst, expectedAst *ast.File
var err error
fset = token.NewFileSet()
for i, tt := range json2AstTests {
if expectedAst, err = parser.ParseFile(fset, "", tt.Output, 0); err != nil {
c.Error(err)
continue
}
if obtainedAst, err = json2go.Json2Ast(bytes.NewBufferString(tt.Input)); err != nil {
c.Error(err)
continue
}
c.Assert(obtainedAst, AstEquals, expectedAst, Commentf("Test Case %d failed", i))
}
}
/* Partial AST comparison logic (for nodes and values we care about) */
type AstChannelWalker struct {
Out chan *NodeWithBreadcrumbs
breadcrumbs Breadcrumbs
walkCount int
}
type Breadcrumbs []ast.Node
func (this Breadcrumbs) String() string {
var buffer bytes.Buffer
for _, node := range this {
fmt.Fprintf(&buffer, " -> %+v", node)
}
return buffer.String()
}
type NodeWithBreadcrumbs struct {
Node ast.Node
Breadcrumbs
}
func (this *AstChannelWalker) Visit(node ast.Node) ast.Visitor {
this.walkCount += 1
if node != nil {
this.Out <- &NodeWithBreadcrumbs{
Node: node,
Breadcrumbs: this.breadcrumbs,
}
this.breadcrumbs = append(this.breadcrumbs, node)
} else {
this.breadcrumbs = this.breadcrumbs[0:len(this.breadcrumbs)]
this.walkCount -= 2
}
//Walked all nodes
if this.walkCount == 0 {
close(this.Out)
}
return this
}
func valueEquals(obtained reflect.Value, expected reflect.Value) bool {
if obtained.Kind() != expected.Kind() {
return false
}
var result bool
switch obtained.Kind() {
case reflect.Bool:
result = obtained.Bool() == expected.Bool()
case reflect.String:
result = obtained.String() == expected.String()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
result = obtained.Int() == expected.Int()
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
result = obtained.Uint() == expected.Uint()
default:
panic("Unknown kind: " + obtained.Kind().String())
}
return result
}
//Fields we care to compare in AST nodes
var fieldsForStruct = map[string][]string{
"*ast.File": []string{},
"*ast.Ident": []string{"Name"},
"*ast.GenDecl": []string{"Tok"},
"*ast.TypeSpec": []string{},
"*ast.StructType": []string{"Incomplete"},
"*ast.FieldList": []string{},
"*ast.Field": []string{},
"*ast.StarExpr": []string{},
"*ast.ArrayType": []string{},
}
func (this *astEquals) nodeEquals(obtained ast.Node, expected ast.Node) bool {
if reflect.TypeOf(obtained) != reflect.TypeOf(expected) {
return false
}
var fields []string
if fields = fieldsForStruct[reflect.TypeOf(obtained).String()]; fields == nil {
return false
}
for _, field := range fields {
if !valueEquals(reflect.Indirect(reflect.ValueOf(obtained)).FieldByName(field), reflect.Indirect(reflect.ValueOf(expected)).FieldByName(field)) {
return false
}
}
return true
}