Debian Codegraph @

json/json_test.go

raw symbols SCIP
1 // Copyright 2019 Red Hat, Inc
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.)
14
15 package json
16
17 import (
18 "reflect"
19 "testing"
20
21 "github.com/coreos/vcontext/tree"
22
23 json "github.com/coreos/go-json"
24 )
25
26 func TestUnmarshalToContext(t *testing.T) {
27 tests := []struct {
28 in json.Node
29 out tree.Node
30 }{
31 // leaf
32 {
33 json.Node{
34 Start: 1,
35 End: 2,
36 Value: "foo",
37 },
38 tree.Leaf{
39 Marker: tree.MarkerFromIndices(1, 2),
40 },
41 },
42 // map
43 {
44 json.Node{
45 Start: 1,
46 End: 2,
47 Value: map[string]json.Node{},
48 },
49 tree.MapNode{
50 Marker: tree.MarkerFromIndices(1, 2),
51 Keys: map[string]tree.Leaf{},
52 Children: map[string]tree.Node{},
53 },
54 },
55 //slice
56 {
57 json.Node{
58 Start: 1,
59 End: 2,
60 Value: []json.Node{},
61 },
62 tree.SliceNode{
63 Marker: tree.MarkerFromIndices(1, 2),
64 Children: []tree.Node{},
65 },
66 },
67 // map of map, slice, leaf
68 {
69 json.Node{
70 Start: 1,
71 End: 2,
72 Value: map[string]json.Node{
73 "foo": {
74 KeyStart: 3,
75 KeyEnd: 4,
76 Start: 5,
77 End: 6,
78 Value: map[string]json.Node{},
79 },
80 "bar": {
81 KeyStart: 7,
82 KeyEnd: 8,
83 Start: 9,
84 End: 10,
85 Value: []json.Node{},
86 },
87 "baz": {
88 KeyStart: 11,
89 KeyEnd: 12,
90 Start: 13,
91 End: 14,
92 Value: "quux",
93 },
94 },
95 },
96 tree.MapNode{
97 Marker: tree.MarkerFromIndices(1, 2),
98 Keys: map[string]tree.Leaf{
99 "foo": {
100 Marker: tree.MarkerFromIndices(3, 4),
101 },
102 "bar": {
103 Marker: tree.MarkerFromIndices(7, 8),
104 },
105 "baz": {
106 Marker: tree.MarkerFromIndices(11, 12),
107 },
108 },
109 Children: map[string]tree.Node{
110 "foo": tree.MapNode{
111 Marker: tree.MarkerFromIndices(5, 6),
112 Children: map[string]tree.Node{},
113 Keys: map[string]tree.Leaf{},
114 },
115 "bar": tree.SliceNode{
116 Marker: tree.MarkerFromIndices(9, 10),
117 Children: []tree.Node{},
118 },
119 "baz": tree.Leaf{
120 Marker: tree.MarkerFromIndices(13, 14),
121 },
122 },
123 },
124 },
125 // slice of leaf
126 {
127 json.Node{
128 Start: 1,
129 End: 2,
130 Value: []json.Node{
131 {
132 Start: 3,
133 End: 4,
134 Value: "foo",
135 },
136 },
137 },
138 tree.SliceNode{
139 Marker: tree.MarkerFromIndices(1, 2),
140 Children: []tree.Node{
141 tree.Leaf{
142 Marker: tree.MarkerFromIndices(3, 4),
143 },
144 },
145 },
146 },
147 // slice of slice
148 {
149 json.Node{
150 Start: 1,
151 End: 2,
152 Value: []json.Node{
153 {
154 Start: 3,
155 End: 4,
156 Value: []json.Node{},
157 },
158 },
159 },
160 tree.SliceNode{
161 Marker: tree.MarkerFromIndices(1, 2),
162 Children: []tree.Node{
163 tree.SliceNode{
164 Marker: tree.MarkerFromIndices(3, 4),
165 Children: []tree.Node{},
166 },
167 },
168 },
169 },
170 // slice of map
171 {
172 json.Node{
173 Start: 1,
174 End: 2,
175 Value: []json.Node{
176 {
177 Start: 3,
178 End: 4,
179 Value: map[string]json.Node{},
180 },
181 },
182 },
183 tree.SliceNode{
184 Marker: tree.MarkerFromIndices(1, 2),
185 Children: []tree.Node{
186 tree.MapNode{
187 Marker: tree.MarkerFromIndices(3, 4),
188 Children: map[string]tree.Node{},
189 Keys: map[string]tree.Leaf{},
190 },
191 },
192 },
193 },
194 }
195 for i, test := range tests {
196 n := fromJsonNode(test.in)
197 if !reflect.DeepEqual(test.out, n) {
198 t.Errorf("test %d failed: expected: %v, got %v", i, test.out, n)
199 }
200 }
201 }