-
Notifications
You must be signed in to change notification settings - Fork 349
/
visitor_test.go
785 lines (691 loc) · 20.9 KB
/
visitor_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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
/*
* Copyright 2021 ByteDance Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ast
import (
`bufio`
`encoding/json`
`fmt`
`io`
`os`
`sort`
`strings`
`testing`
`github.com/stretchr/testify/assert`
`github.com/stretchr/testify/require`
)
var visitorTestCases = []struct {
name string
jsonStr string
} {
{"default", _TwitterJson},
{"issue_case01", "[1193.6419677734375]"},
{"issue653", `{"v0": 0, "m0": {}, "v1": 1, "a0": [], "v2": 2}`},
}
type visitorNodeDiffTest struct {
t *testing.T
str string
tracer io.Writer
cursor Node
stk visitorNodeStack
sp uint8
}
type visitorNodeStack = [256]struct {
Node Node
Object map[string]Node
Array []Node
ObjectKey string
}
func (self *visitorNodeDiffTest) incrSP() {
self.t.Helper()
self.sp++
require.NotZero(self.t, self.sp, "stack overflow")
}
func (self *visitorNodeDiffTest) debugStack() string {
var buf strings.Builder
buf.WriteString("[")
for i := uint8(0); i < self.sp; i++ {
if i != 0 {
buf.WriteString(", ")
}
if self.stk[i].Array != nil {
buf.WriteString("Array")
} else if self.stk[i].Object != nil {
buf.WriteString("Object")
} else {
fmt.Fprintf(&buf, "Key(%q)", self.stk[i].ObjectKey)
}
}
buf.WriteString("]")
return buf.String()
}
func (self *visitorNodeDiffTest) requireType(got int) {
self.t.Helper()
want := self.cursor.Type()
require.EqualValues(self.t, want, got)
}
func (self *visitorNodeDiffTest) toArrayIndex(array Node, i int) {
// set cursor to next Value if existed
self.t.Helper()
n, err := array.Len()
require.NoError(self.t, err)
if i < n {
self.cursor = *array.Index(i)
require.NoError(self.t, self.cursor.Check())
}
}
func (self *visitorNodeDiffTest) onValueEnd() {
if self.tracer != nil {
fmt.Fprintf(self.tracer, "OnValueEnd: %s\n", self.debugStack())
}
// cursor should point to the Value now
self.t.Helper()
if self.sp == 0 {
if self.tracer != nil {
fmt.Fprintf(self.tracer, "EOF\n\n")
}
return
}
// [..., Array, sp]
if array := self.stk[self.sp-1].Array; array != nil {
array = append(array, self.cursor)
self.stk[self.sp-1].Array = array
self.toArrayIndex(self.stk[self.sp-1].Node, len(array))
return
}
// [..., Object, ObjectKey, sp]
require.GreaterOrEqual(self.t, self.sp, uint8(2))
require.NotNil(self.t, self.stk[self.sp-2].Object)
require.Nil(self.t, self.stk[self.sp-1].Object)
require.Nil(self.t, self.stk[self.sp-1].Array)
self.stk[self.sp-2].Object[self.stk[self.sp-1].ObjectKey] = self.cursor
self.cursor = self.stk[self.sp-2].Node // reset cursor to Object
self.sp-- // pop ObjectKey
}
func (self *visitorNodeDiffTest) OnNull() error {
if self.tracer != nil {
fmt.Fprintf(self.tracer, "OnNull\n")
}
self.requireType(V_NULL)
self.onValueEnd()
return nil
}
func (self *visitorNodeDiffTest) OnBool(v bool) error {
if self.tracer != nil {
fmt.Fprintf(self.tracer, "OnBool: %t\n", v)
}
if v {
self.requireType(V_TRUE)
} else {
self.requireType(V_FALSE)
}
self.onValueEnd()
return nil
}
func (self *visitorNodeDiffTest) OnString(v string) error {
if self.tracer != nil {
fmt.Fprintf(self.tracer, "OnString: %q\n", v)
}
self.requireType(V_STRING)
want, err := self.cursor.StrictString()
require.NoError(self.t, err)
require.EqualValues(self.t, want, v)
self.onValueEnd()
return nil
}
func (self *visitorNodeDiffTest) OnInt64(v int64, n json.Number) error {
if self.tracer != nil {
fmt.Fprintf(self.tracer, "OnInt64: %d (%q)\n", v, n)
}
self.requireType(V_NUMBER)
want, err := self.cursor.StrictInt64()
require.NoError(self.t, err)
require.EqualValues(self.t, want, v)
nv, err := n.Int64()
require.NoError(self.t, err)
require.EqualValues(self.t, want, nv)
self.onValueEnd()
return nil
}
func (self *visitorNodeDiffTest) OnFloat64(v float64, n json.Number) error {
if self.tracer != nil {
fmt.Fprintf(self.tracer, "OnFloat64: %f (%q)\n", v, n)
}
self.requireType(V_NUMBER)
want, err := self.cursor.StrictFloat64()
require.NoError(self.t, err)
require.EqualValues(self.t, want, v)
nv, err := n.Float64()
require.NoError(self.t, err)
require.EqualValues(self.t, want, nv)
self.onValueEnd()
return nil
}
func (self *visitorNodeDiffTest) OnObjectBegin(capacity int) error {
if self.tracer != nil {
fmt.Fprintf(self.tracer, "OnObjectBegin: %d\n", capacity)
}
self.requireType(V_OBJECT)
self.stk[self.sp].Node = self.cursor
self.stk[self.sp].Object = make(map[string]Node, capacity)
self.incrSP()
return nil
}
func (self *visitorNodeDiffTest) OnObjectKey(key string) error {
if self.tracer != nil {
fmt.Fprintf(self.tracer, "OnObjectKey: %q %s\n", key, self.debugStack())
}
require.NotNil(self.t, self.stk[self.sp-1].Object)
node := self.stk[self.sp-1].Node
self.stk[self.sp].ObjectKey = key
self.incrSP()
self.cursor = *node.Get(key) // set cursor to Value
require.NoError(self.t, self.cursor.Check())
return nil
}
func (self *visitorNodeDiffTest) OnObjectEnd() error {
if self.tracer != nil {
fmt.Fprintf(self.tracer, "OnObjectEnd\n")
}
object := self.stk[self.sp-1].Object
require.NotNil(self.t, object)
node := self.stk[self.sp-1].Node
pairs, err := node.MapUseNode()
require.NoError(self.t, err)
keysGot := make([]string, 0, len(object))
for key := range object {
keysGot = append(keysGot, key)
}
keysWant := make([]string, 0, len(pairs))
for key := range pairs {
keysWant = append(keysWant, key)
}
sort.Strings(keysGot)
sort.Strings(keysWant)
require.EqualValues(self.t, keysWant, keysGot)
for key, pair := range pairs {
typeGot := object[key].Type()
typeWant := pair.Type()
require.EqualValues(self.t, typeWant, typeGot)
}
// pop Object
self.sp--
self.stk[self.sp].Node = Node{}
self.stk[self.sp].Object = nil
self.cursor = node // set cursor to this Object
self.onValueEnd()
return nil
}
func (self *visitorNodeDiffTest) OnArrayBegin(capacity int) error {
if self.tracer != nil {
fmt.Fprintf(self.tracer, "OnArrayBegin: %d\n", capacity)
}
self.requireType(V_ARRAY)
self.stk[self.sp].Node = self.cursor
self.stk[self.sp].Array = make([]Node, 0, capacity)
self.incrSP()
self.toArrayIndex(self.stk[self.sp-1].Node, 0)
return nil
}
func (self *visitorNodeDiffTest) OnArrayEnd() error {
if self.tracer != nil {
fmt.Fprintf(self.tracer, "OnArrayEnd\n")
}
array := self.stk[self.sp-1].Array
require.NotNil(self.t, array)
node := self.stk[self.sp-1].Node
values, err := node.ArrayUseNode()
require.NoError(self.t, err)
require.EqualValues(self.t, len(values), len(array))
for i, n := 0, len(values); i < n; i++ {
typeGot := array[i].Type()
typeWant := values[i].Type()
require.EqualValues(self.t, typeWant, typeGot)
}
// pop Array
self.sp--
self.stk[self.sp].Node = Node{}
self.stk[self.sp].Array = nil
self.cursor = node // set cursor to this Array
self.onValueEnd()
return nil
}
func (self *visitorNodeDiffTest) Run(t *testing.T, str string,
tracer io.Writer) {
self.t = t
self.str = str
self.tracer = tracer
self.t.Helper()
self.cursor = NewRaw(self.str)
require.NoError(self.t, self.cursor.LoadAll())
self.stk = visitorNodeStack{}
self.sp = 0
require.NoError(self.t, Preorder(self.str, self, nil))
}
func TestVisitor_NodeDiff(t *testing.T) {
var suite visitorNodeDiffTest
newTracer := func(t *testing.T) io.Writer {
const EnableTracer = false
if !EnableTracer {
return nil
}
basename := strings.ReplaceAll(t.Name(), "/", "_")
fp, err := os.Create(fmt.Sprintf("../output/%s.log", basename))
require.NoError(t, err)
writer := bufio.NewWriter(fp)
t.Cleanup(func() {
_ = writer.Flush()
_ = fp.Close()
})
return writer
}
for _, c := range visitorTestCases {
t.Run(c.name, func(t *testing.T) {
suite.Run(t, c.jsonStr, newTracer(t))
})
}
}
type visitorUserNode interface {
UserNode()
}
type (
visitorUserNull struct{}
visitorUserBool struct{ Value bool }
visitorUserInt64 struct{ Value int64 }
visitorUserFloat64 struct{ Value float64 }
visitorUserString struct{ Value string }
visitorUserObject struct{ Value map[string]visitorUserNode }
visitorUserArray struct{ Value []visitorUserNode }
)
func (*visitorUserNull) UserNode() {}
func (*visitorUserBool) UserNode() {}
func (*visitorUserInt64) UserNode() {}
func (*visitorUserFloat64) UserNode() {}
func (*visitorUserString) UserNode() {}
func (*visitorUserObject) UserNode() {}
func (*visitorUserArray) UserNode() {}
func compareUserNode(tb testing.TB, lhs, rhs visitorUserNode) bool {
switch lhs := lhs.(type) {
case *visitorUserNull:
_, ok := rhs.(*visitorUserNull)
return assert.True(tb, ok)
case *visitorUserBool:
rhs, ok := rhs.(*visitorUserBool)
return assert.True(tb, ok) && assert.Equal(tb, lhs.Value, rhs.Value)
case *visitorUserInt64:
rhs, ok := rhs.(*visitorUserInt64)
return assert.True(tb, ok) && assert.Equal(tb, lhs.Value, rhs.Value)
case *visitorUserFloat64:
rhs, ok := rhs.(*visitorUserFloat64)
return assert.True(tb, ok) && assert.Equal(tb, lhs.Value, rhs.Value)
case *visitorUserString:
rhs, ok := rhs.(*visitorUserString)
return assert.True(tb, ok) && assert.Equal(tb, lhs.Value, rhs.Value)
case *visitorUserObject:
rhs, ok := rhs.(*visitorUserObject)
if !(assert.True(tb, ok) && assert.Equal(tb, len(lhs.Value), len(rhs.Value))) {
return false
}
for key, lhs := range lhs.Value {
rhs, ok := rhs.Value[key]
if !(assert.True(tb, ok) && assert.True(tb, compareUserNode(tb, lhs, rhs))) {
return false
}
}
return true
case *visitorUserArray:
rhs, ok := rhs.(*visitorUserArray)
if !(assert.True(tb, ok) && assert.Equal(tb, len(lhs.Value), len(rhs.Value))) {
return false
}
for i, n := 0, len(lhs.Value); i < n; i++ {
if !assert.True(tb, compareUserNode(tb, lhs.Value[i], rhs.Value[i])) {
return false
}
}
return true
default:
tb.Fatalf("unexpected type of UserNode: %T", lhs)
return false
}
}
type visitorUserNodeDecoder interface {
Reset()
Decode(str string) (visitorUserNode, error)
}
var _ visitorUserNodeDecoder = (*visitorUserNodeASTDecoder)(nil)
type visitorUserNodeASTDecoder struct{}
func (self *visitorUserNodeASTDecoder) Reset() {}
func (self *visitorUserNodeASTDecoder) Decode(str string) (visitorUserNode, error) {
root := NewRaw(str)
if err := root.LoadAll(); err != nil {
return nil, err
}
return self.decodeValue(&root)
}
func (self *visitorUserNodeASTDecoder) decodeValue(root *Node) (visitorUserNode, error) {
switch typ := root.Type(); typ {
// embed (*Node).Check
case V_NONE:
return nil, ErrNotExist
case V_ERROR:
return nil, root
case V_NULL:
return &visitorUserNull{}, nil
case V_TRUE:
return &visitorUserBool{Value: true}, nil
case V_FALSE:
return &visitorUserBool{Value: false}, nil
case V_STRING:
value, err := root.StrictString()
if err != nil {
return nil, err
}
return &visitorUserString{Value: value}, nil
case V_NUMBER:
value, err := root.StrictNumber()
if err != nil {
return nil, err
}
i64, ierr := value.Int64()
if ierr == nil {
return &visitorUserInt64{Value: i64}, nil
}
f64, ferr := value.Float64()
if ferr == nil {
return &visitorUserFloat64{Value: f64}, nil
}
return nil, fmt.Errorf("invalid number: %v, ierr: %v, ferr: %v",
value, ierr, ferr)
case V_ARRAY:
nodes, err := root.ArrayUseNode()
if err != nil {
return nil, err
}
values := make([]visitorUserNode, len(nodes))
for i := 0; i<len(nodes); i++ {
n := &nodes[i]
value, err := self.decodeValue(n)
if err != nil {
return nil, err
}
values[i] = value
}
return &visitorUserArray{Value: values}, nil
case V_OBJECT:
pairs, err := root.MapUseNode()
if err != nil {
return nil, err
}
values := make(map[string]visitorUserNode, len(pairs))
for k, v := range pairs {
value, err := self.decodeValue(&v)
if err != nil {
return nil, err
}
values[k] = value
}
return &visitorUserObject{Value: values}, nil
case V_ANY:
fallthrough
default:
return nil, fmt.Errorf("unexpected Node type: %v", typ)
}
}
var _ visitorUserNodeDecoder = (*visitorUserNodeVisitorDecoder)(nil)
type visitorUserNodeVisitorDecoder struct {
stk visitorUserNodeStack
sp uint8
}
type visitorUserNodeStack = [256]struct {
val visitorUserNode
obj map[string]visitorUserNode
arr []visitorUserNode
key string
}
func (self *visitorUserNodeVisitorDecoder) Reset() {
self.stk = visitorUserNodeStack{}
self.sp = 0
}
func (self *visitorUserNodeVisitorDecoder) Decode(str string) (visitorUserNode, error) {
if err := Preorder(str, self, nil); err != nil {
return nil, err
}
return self.result()
}
func (self *visitorUserNodeVisitorDecoder) result() (visitorUserNode, error) {
if self.sp != 1 {
return nil, fmt.Errorf("incorrect sp: %d", self.sp)
}
return self.stk[0].val, nil
}
func (self *visitorUserNodeVisitorDecoder) incrSP() error {
self.sp++
if self.sp == 0 {
return fmt.Errorf("reached max depth: %d", len(self.stk))
}
return nil
}
func (self *visitorUserNodeVisitorDecoder) OnNull() error {
self.stk[self.sp].val = &visitorUserNull{}
if err := self.incrSP(); err != nil {
return err
}
return self.onValueEnd()
}
func (self *visitorUserNodeVisitorDecoder) OnBool(v bool) error {
self.stk[self.sp].val = &visitorUserBool{Value: v}
if err := self.incrSP(); err != nil {
return err
}
return self.onValueEnd()
}
func (self *visitorUserNodeVisitorDecoder) OnString(v string) error {
self.stk[self.sp].val = &visitorUserString{Value: v}
if err := self.incrSP(); err != nil {
return err
}
return self.onValueEnd()
}
func (self *visitorUserNodeVisitorDecoder) OnInt64(v int64, n json.Number) error {
self.stk[self.sp].val = &visitorUserInt64{Value: v}
if err := self.incrSP(); err != nil {
return err
}
return self.onValueEnd()
}
func (self *visitorUserNodeVisitorDecoder) OnFloat64(v float64, n json.Number) error {
self.stk[self.sp].val = &visitorUserFloat64{Value: v}
if err := self.incrSP(); err != nil {
return err
}
return self.onValueEnd()
}
func (self *visitorUserNodeVisitorDecoder) OnObjectBegin(capacity int) error {
self.stk[self.sp].obj = make(map[string]visitorUserNode, capacity)
return self.incrSP()
}
func (self *visitorUserNodeVisitorDecoder) OnObjectKey(key string) error {
self.stk[self.sp].key = key
return self.incrSP()
}
func (self *visitorUserNodeVisitorDecoder) OnObjectEnd() error {
self.stk[self.sp-1].val = &visitorUserObject{Value: self.stk[self.sp-1].obj}
self.stk[self.sp-1].obj = nil
return self.onValueEnd()
}
func (self *visitorUserNodeVisitorDecoder) OnArrayBegin(capacity int) error {
self.stk[self.sp].arr = make([]visitorUserNode, 0, capacity)
return self.incrSP()
}
func (self *visitorUserNodeVisitorDecoder) OnArrayEnd() error {
self.stk[self.sp-1].val = &visitorUserArray{Value: self.stk[self.sp-1].arr}
self.stk[self.sp-1].arr = nil
return self.onValueEnd()
}
func (self *visitorUserNodeVisitorDecoder) onValueEnd() error {
if self.sp == 1 {
return nil
}
// [..., Array, Value, sp]
if self.stk[self.sp-2].arr != nil {
self.stk[self.sp-2].arr = append(self.stk[self.sp-2].arr, self.stk[self.sp-1].val)
self.sp--
return nil
}
// [..., Object, ObjectKey, Value, sp]
self.stk[self.sp-3].obj[self.stk[self.sp-2].key] = self.stk[self.sp-1].val
self.sp -= 2
return nil
}
func testUserNodeDiff(t *testing.T, d1, d2 visitorUserNodeDecoder, str string) {
t.Helper()
d1.Reset()
n1, err := d1.Decode(_TwitterJson)
require.NoError(t, err)
d2.Reset()
n2, err := d2.Decode(_TwitterJson)
require.NoError(t, err)
require.True(t, compareUserNode(t, n1, n2))
}
func TestVisitor_UserNodeDiff(t *testing.T) {
var d1 visitorUserNodeASTDecoder
var d2 visitorUserNodeVisitorDecoder
for _, c := range visitorTestCases {
t.Run(c.name, func(t *testing.T) {
testUserNodeDiff(t, &d1, &d2, c.jsonStr)
})
}
}
type skipVisitor struct {
sp int
Skip int
inSkip bool
CountSkip int
}
func (self *skipVisitor) OnNull() error {
if self.sp == self.Skip+1 && self.inSkip {
panic("unexpected key")
}
return nil
}
func (self *skipVisitor) OnFloat64(v float64, n json.Number) error {
if self.sp == self.Skip+1 && self.inSkip {
panic("unexpected key")
}
return nil
}
func (self *skipVisitor) OnInt64(v int64, n json.Number) error {
if self.sp == self.Skip+1 && self.inSkip {
panic("unexpected key")
}
return nil
}
func (self *skipVisitor) OnBool(v bool) error {
if self.sp == self.Skip+1 && self.inSkip {
panic("unexpected key")
}
return nil
}
func (self *skipVisitor) OnString(v string) error {
if self.sp == self.Skip+1 && self.inSkip {
panic("unexpected key")
}
return nil
}
func (self *skipVisitor) OnObjectBegin(capacity int) error {
println("self.sp", self.sp)
if self.sp == self.Skip {
self.inSkip = true
self.CountSkip++
println("op skip")
self.sp++
return VisitOPSkip
}
self.sp++
return nil
}
func (self *skipVisitor) OnObjectKey(key string) error {
if self.sp == self.Skip+1 && self.inSkip {
panic("unexpected key")
}
return nil
}
func (self *skipVisitor) OnObjectEnd() error {
if self.sp == self.Skip + 1 {
if !self.inSkip {
panic("not in skip")
}
self.inSkip = false
println("finish op skip")
}
self.sp--
return nil
}
func (self *skipVisitor) OnArrayBegin(capacity int) error {
println("arr self.sp", self.sp)
if self.sp == self.Skip {
self.inSkip = true
self.CountSkip++
println("arr op skip")
self.sp++
return VisitOPSkip
}
self.sp++
return nil
}
func (self *skipVisitor) OnArrayEnd() error {
println("arr self.sp", self.sp)
if self.sp == self.Skip + 1 {
if !self.inSkip {
panic("arr not in skip")
}
self.inSkip = false
println("arr finish op skip")
}
self.sp--
return nil
}
func TestVisitor_OpSkip(t *testing.T) {
var suite skipVisitor
suite.Skip = 1
Preorder(`{ "a": [ null ] , "b": 1, "c": { "1" : 1 } }`, &suite, nil)
if suite.CountSkip != 2 {
t.Fatal(suite.CountSkip)
}
}
func BenchmarkVisitor_UserNode(b *testing.B) {
const str = _TwitterJson
b.Run("AST", func(b *testing.B) {
var d visitorUserNodeASTDecoder
b.ResetTimer()
for k := 0; k < b.N; k++ {
d.Reset()
_, err := d.Decode(str)
require.NoError(b, err)
b.SetBytes(int64(len(str)))
}
})
b.Run("Visitor", func(b *testing.B) {
var d visitorUserNodeVisitorDecoder
b.ResetTimer()
for k := 0; k < b.N; k++ {
d.Reset()
_, err := d.Decode(str)
require.NoError(b, err)
b.SetBytes(int64(len(str)))
}
})
}