-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcursor_test.go
More file actions
838 lines (712 loc) · 23.6 KB
/
Copy pathcursor_test.go
File metadata and controls
838 lines (712 loc) · 23.6 KB
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
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
package xref
import (
"testing"
)
func TestTreeCursorNew(t *testing.T) {
lang := queryTestLanguage()
tree := buildSimpleTree(lang)
root := tree.RootNode()
c := NewTreeCursor(root, tree)
if c.CurrentNode() != root {
t.Fatal("CurrentNode should be root")
}
if c.Depth() != 0 {
t.Fatalf("Depth at root should be 0, got %d", c.Depth())
}
}
func TestTreeCursorNewWithNilNodeIsSafe(t *testing.T) {
lang := queryTestLanguage()
tree := buildSimpleTree(lang)
c := NewTreeCursor(nil, tree)
if c.CurrentNode() != nil {
t.Fatal("CurrentNode should be nil")
}
if c.GotoFirstChild() {
t.Fatal("GotoFirstChild should fail on nil node cursor")
}
if c.GotoNextSibling() {
t.Fatal("GotoNextSibling should fail on nil node cursor")
}
if c.GotoChildByFieldID(1) {
t.Fatal("GotoChildByFieldID should fail on nil node cursor")
}
}
func TestTreeCursorFromNilTreeIsSafe(t *testing.T) {
c := NewTreeCursorFromTree(nil)
if c.CurrentNode() != nil {
t.Fatal("CurrentNode should be nil")
}
if c.Depth() != 0 {
t.Fatalf("Depth should be 0, got %d", c.Depth())
}
if c.GotoFirstChild() {
t.Fatal("GotoFirstChild should fail for nil tree cursor")
}
}
func TestTreeCursorGotoFirstChild(t *testing.T) {
lang := queryTestLanguage()
tree := buildSimpleTree(lang)
c := NewTreeCursor(tree.RootNode(), tree)
if !c.GotoFirstChild() {
t.Fatal("GotoFirstChild should succeed on program node")
}
// First child of program is function_declaration
if c.CurrentNode().Symbol() != Symbol(5) {
t.Fatalf("expected function_declaration (5), got %d", c.CurrentNode().Symbol())
}
if c.Depth() != 1 {
t.Fatalf("Depth should be 1, got %d", c.Depth())
}
// Descend again: first child of function_declaration is "func" keyword
if !c.GotoFirstChild() {
t.Fatal("GotoFirstChild should succeed on function_declaration")
}
if c.CurrentNode().Symbol() != Symbol(8) {
t.Fatalf("expected func keyword (8), got %d", c.CurrentNode().Symbol())
}
if c.Depth() != 2 {
t.Fatalf("Depth should be 2, got %d", c.Depth())
}
}
func TestTreeCursorGotoNextSibling(t *testing.T) {
lang := queryTestLanguage()
tree := buildSimpleTree(lang)
c := NewTreeCursor(tree.RootNode(), tree)
c.GotoFirstChild() // function_declaration
c.GotoFirstChild() // "func" keyword
// Traverse siblings: func -> identifier -> parameter_list -> block
expected := []Symbol{1, 13, 14}
for i, sym := range expected {
if !c.GotoNextSibling() {
t.Fatalf("GotoNextSibling should succeed at step %d", i)
}
if c.CurrentNode().Symbol() != sym {
t.Fatalf("step %d: expected symbol %d, got %d", i, sym, c.CurrentNode().Symbol())
}
}
// No more siblings
if c.GotoNextSibling() {
t.Fatal("GotoNextSibling should return false at last sibling")
}
}
func TestTreeCursorGotoPrevSibling(t *testing.T) {
lang := queryTestLanguage()
tree := buildSimpleTree(lang)
c := NewTreeCursor(tree.RootNode(), tree)
c.GotoFirstChild() // function_declaration
c.GotoFirstChild() // "func" keyword
// Move to last sibling
for c.GotoNextSibling() {
}
// Now at block (14)
if c.CurrentNode().Symbol() != Symbol(14) {
t.Fatalf("expected block (14), got %d", c.CurrentNode().Symbol())
}
// Go back: block -> parameter_list -> identifier -> func
expected := []Symbol{13, 1, 8}
for i, sym := range expected {
if !c.GotoPrevSibling() {
t.Fatalf("GotoPrevSibling should succeed at step %d", i)
}
if c.CurrentNode().Symbol() != sym {
t.Fatalf("step %d: expected symbol %d, got %d", i, sym, c.CurrentNode().Symbol())
}
}
// No more prev siblings
if c.GotoPrevSibling() {
t.Fatal("GotoPrevSibling should return false at first sibling")
}
}
func TestTreeCursorGotoParent(t *testing.T) {
lang := queryTestLanguage()
tree := buildSimpleTree(lang)
c := NewTreeCursor(tree.RootNode(), tree)
c.GotoFirstChild() // function_declaration
c.GotoFirstChild() // "func" keyword
if !c.GotoParent() {
t.Fatal("GotoParent should succeed")
}
if c.CurrentNode().Symbol() != Symbol(5) {
t.Fatalf("expected function_declaration (5), got %d", c.CurrentNode().Symbol())
}
if c.Depth() != 1 {
t.Fatalf("Depth should be 1, got %d", c.Depth())
}
if !c.GotoParent() {
t.Fatal("GotoParent should succeed to root")
}
if c.CurrentNode().Symbol() != Symbol(7) {
t.Fatalf("expected program (7), got %d", c.CurrentNode().Symbol())
}
if c.Depth() != 0 {
t.Fatalf("Depth should be 0, got %d", c.Depth())
}
}
func TestTreeCursorGotoLastChild(t *testing.T) {
lang := queryTestLanguage()
tree := buildSimpleTree(lang)
c := NewTreeCursor(tree.RootNode(), tree)
c.GotoFirstChild() // function_declaration
if !c.GotoLastChild() {
t.Fatal("GotoLastChild should succeed")
}
// Last child of function_declaration is block (14)
if c.CurrentNode().Symbol() != Symbol(14) {
t.Fatalf("expected block (14), got %d", c.CurrentNode().Symbol())
}
}
func TestTreeCursorAtLeaf(t *testing.T) {
lang := queryTestLanguage()
tree := buildSimpleTree(lang)
c := NewTreeCursor(tree.RootNode(), tree)
c.GotoFirstChild() // function_declaration
c.GotoFirstChild() // "func" keyword (leaf)
if c.GotoFirstChild() {
t.Fatal("GotoFirstChild should return false on leaf node")
}
if c.GotoLastChild() {
t.Fatal("GotoLastChild should return false on leaf node")
}
}
func TestTreeCursorBoundary(t *testing.T) {
lang := queryTestLanguage()
tree := buildSimpleTree(lang)
c := NewTreeCursor(tree.RootNode(), tree)
// At root: parent should fail
if c.GotoParent() {
t.Fatal("GotoParent at root should return false")
}
// At root: siblings should fail
if c.GotoNextSibling() {
t.Fatal("GotoNextSibling at root should return false")
}
if c.GotoPrevSibling() {
t.Fatal("GotoPrevSibling at root should return false")
}
}
func TestTreeCursorFromTree(t *testing.T) {
lang := queryTestLanguage()
tree := buildSimpleTree(lang)
c := NewTreeCursorFromTree(tree)
if c.CurrentNode() != tree.RootNode() {
t.Fatal("NewTreeCursorFromTree should start at root")
}
if c.Depth() != 0 {
t.Fatalf("Depth should be 0, got %d", c.Depth())
}
}
func TestTreeCursorCurrentFieldID(t *testing.T) {
lang := queryTestLanguage()
tree := buildSimpleTree(lang)
// buildSimpleTree: function_declaration children have fields:
// [0: func(no field), 1: identifier(name=1), 2: parameter_list(parameters=5), 3: block(body=2)]
c := NewTreeCursor(tree.RootNode(), tree)
c.GotoFirstChild() // function_declaration
c.GotoFirstChild() // "func" keyword — no field
if fid := c.CurrentFieldID(); fid != 0 {
t.Fatalf("func keyword should have field ID 0, got %d", fid)
}
c.GotoNextSibling() // identifier — field "name" (1)
if fid := c.CurrentFieldID(); fid != FieldID(1) {
t.Fatalf("identifier should have field ID 1 (name), got %d", fid)
}
c.GotoNextSibling() // parameter_list — field "parameters" (5)
if fid := c.CurrentFieldID(); fid != FieldID(5) {
t.Fatalf("parameter_list should have field ID 5 (parameters), got %d", fid)
}
c.GotoNextSibling() // block — field "body" (2)
if fid := c.CurrentFieldID(); fid != FieldID(2) {
t.Fatalf("block should have field ID 2 (body), got %d", fid)
}
}
func TestTreeCursorCurrentFieldName(t *testing.T) {
lang := queryTestLanguage()
tree := buildSimpleTree(lang)
c := NewTreeCursor(tree.RootNode(), tree)
c.GotoFirstChild() // function_declaration
c.GotoFirstChild() // "func" keyword
if name := c.CurrentFieldName(); name != "" {
t.Fatalf("func keyword should have empty field name, got %q", name)
}
c.GotoNextSibling() // identifier
if name := c.CurrentFieldName(); name != "name" {
t.Fatalf("identifier field name should be 'name', got %q", name)
}
c.GotoNextSibling() // parameter_list
if name := c.CurrentFieldName(); name != "parameters" {
t.Fatalf("parameter_list field name should be 'parameters', got %q", name)
}
c.GotoNextSibling() // block
if name := c.CurrentFieldName(); name != "body" {
t.Fatalf("block field name should be 'body', got %q", name)
}
}
func TestTreeCursorGotoChildByFieldName(t *testing.T) {
lang := queryTestLanguage()
tree := buildSimpleTree(lang)
c := NewTreeCursor(tree.RootNode(), tree)
c.GotoFirstChild() // function_declaration
if !c.GotoChildByFieldName("body") {
t.Fatal("GotoChildByFieldName('body') should succeed")
}
if c.CurrentNode().Symbol() != Symbol(14) {
t.Fatalf("expected block (14), got %d", c.CurrentNode().Symbol())
}
// Go back to function_declaration and try "name"
c.GotoParent()
if !c.GotoChildByFieldName("name") {
t.Fatal("GotoChildByFieldName('name') should succeed")
}
if c.CurrentNode().Symbol() != Symbol(1) {
t.Fatalf("expected identifier (1), got %d", c.CurrentNode().Symbol())
}
// Non-existent field
c.GotoParent()
if c.GotoChildByFieldName("nonexistent") {
t.Fatal("GotoChildByFieldName('nonexistent') should return false")
}
}
func TestTreeCursorGotoChildByFieldIDZeroSentinel(t *testing.T) {
lang := queryTestLanguage()
tree := buildSimpleTree(lang)
c := NewTreeCursor(tree.RootNode(), tree)
c.GotoFirstChild() // function_declaration
if c.GotoChildByFieldID(0) {
t.Fatal("GotoChildByFieldID(0) should fail; 0 is the no-field sentinel")
}
}
func TestTreeCursorFieldIDAtRoot(t *testing.T) {
lang := queryTestLanguage()
tree := buildSimpleTree(lang)
c := NewTreeCursor(tree.RootNode(), tree)
if fid := c.CurrentFieldID(); fid != 0 {
t.Fatalf("field ID at root should be 0, got %d", fid)
}
if name := c.CurrentFieldName(); name != "" {
t.Fatalf("field name at root should be empty, got %q", name)
}
}
func TestTreeCursorGotoFirstNamedChild(t *testing.T) {
lang := queryTestLanguage()
tree := buildSimpleTree(lang)
c := NewTreeCursor(tree.RootNode(), tree)
c.GotoFirstChild() // function_declaration
// function_declaration children: func(anon), identifier(named), parameter_list(named), block(named)
// GotoFirstNamedChild should skip "func" keyword and land on identifier
if !c.GotoFirstNamedChild() {
t.Fatal("GotoFirstNamedChild should succeed")
}
if c.CurrentNode().Symbol() != Symbol(1) {
t.Fatalf("expected identifier (1), got %d", c.CurrentNode().Symbol())
}
}
func TestTreeCursorGotoNextNamedSibling(t *testing.T) {
lang := queryTestLanguage()
tree := buildSimpleTree(lang)
c := NewTreeCursor(tree.RootNode(), tree)
c.GotoFirstChild() // function_declaration
c.GotoFirstNamedChild() // identifier
// Next named sibling: parameter_list (13)
if !c.GotoNextNamedSibling() {
t.Fatal("GotoNextNamedSibling should succeed")
}
if c.CurrentNode().Symbol() != Symbol(13) {
t.Fatalf("expected parameter_list (13), got %d", c.CurrentNode().Symbol())
}
// Next named sibling: block (14)
if !c.GotoNextNamedSibling() {
t.Fatal("GotoNextNamedSibling should succeed")
}
if c.CurrentNode().Symbol() != Symbol(14) {
t.Fatalf("expected block (14), got %d", c.CurrentNode().Symbol())
}
// No more named siblings
if c.GotoNextNamedSibling() {
t.Fatal("GotoNextNamedSibling should return false at end")
}
}
func TestTreeCursorGotoPrevNamedSibling(t *testing.T) {
lang := queryTestLanguage()
tree := buildSimpleTree(lang)
c := NewTreeCursor(tree.RootNode(), tree)
c.GotoFirstChild() // function_declaration
c.GotoLastChild() // block (14, named)
// Prev named sibling: parameter_list (13)
if !c.GotoPrevNamedSibling() {
t.Fatal("GotoPrevNamedSibling should succeed")
}
if c.CurrentNode().Symbol() != Symbol(13) {
t.Fatalf("expected parameter_list (13), got %d", c.CurrentNode().Symbol())
}
// Prev named sibling: identifier (1) — skips "func" keyword
if !c.GotoPrevNamedSibling() {
t.Fatal("GotoPrevNamedSibling should succeed")
}
if c.CurrentNode().Symbol() != Symbol(1) {
t.Fatalf("expected identifier (1), got %d", c.CurrentNode().Symbol())
}
// No more prev named siblings (func keyword is anonymous)
if c.GotoPrevNamedSibling() {
t.Fatal("GotoPrevNamedSibling should return false — func keyword is anonymous")
}
}
func TestTreeCursorGotoLastNamedChild(t *testing.T) {
lang := queryTestLanguage()
tree := buildSimpleTree(lang)
c := NewTreeCursor(tree.RootNode(), tree)
c.GotoFirstChild() // function_declaration
// Last named child: block (14)
if !c.GotoLastNamedChild() {
t.Fatal("GotoLastNamedChild should succeed")
}
if c.CurrentNode().Symbol() != Symbol(14) {
t.Fatalf("expected block (14), got %d", c.CurrentNode().Symbol())
}
// Test on parameter_list which has only anonymous children: "(" and ")"
c.GotoParent() // back to function_declaration
c.GotoChildByFieldName("parameters") // parameter_list
if c.GotoFirstNamedChild() {
t.Fatal("GotoFirstNamedChild should return false on parameter_list with only anonymous children")
}
if c.GotoLastNamedChild() {
t.Fatal("GotoLastNamedChild should return false on parameter_list with only anonymous children")
}
}
func TestTreeCursorGotoFirstChildForByte(t *testing.T) {
lang := queryTestLanguage()
tree := buildSimpleTree(lang)
// Tree: func main() { 42 }
// function_declaration children:
// func(0-4), identifier(5-9), parameter_list(9-11), block(14-16? endByte from tree)
c := NewTreeCursor(tree.RootNode(), tree)
c.GotoFirstChild() // function_declaration
// Byte 6 is inside "main" (5-9), so first child where endByte > 6 is identifier
if idx := c.GotoFirstChildForByte(6); idx != 1 {
t.Fatalf("GotoFirstChildForByte(6) index: got %d, want 1", idx)
}
if c.CurrentNode().Symbol() != Symbol(1) {
t.Fatalf("expected identifier (1), got %d", c.CurrentNode().Symbol())
}
// Go back and try byte 0, should land on "func" keyword
c.GotoParent()
if idx := c.GotoFirstChildForByte(0); idx != 0 {
t.Fatalf("GotoFirstChildForByte(0) index: got %d, want 0", idx)
}
if c.CurrentNode().Symbol() != Symbol(8) {
t.Fatalf("expected func keyword (8), got %d", c.CurrentNode().Symbol())
}
// Try byte 15, should land on block (which contains number at 14-16)
c.GotoParent()
if idx := c.GotoFirstChildForByte(15); idx != 3 {
t.Fatalf("GotoFirstChildForByte(15) index: got %d, want 3", idx)
}
if c.CurrentNode().Symbol() != Symbol(14) {
t.Fatalf("expected block (14), got %d", c.CurrentNode().Symbol())
}
}
func TestTreeCursorGotoFirstChildForByteOutOfRange(t *testing.T) {
lang := queryTestLanguage()
tree := buildSimpleTree(lang)
c := NewTreeCursor(tree.RootNode(), tree)
c.GotoFirstChild() // function_declaration
// Byte way past end of all children
if idx := c.GotoFirstChildForByte(9999); idx != -1 {
t.Fatalf("GotoFirstChildForByte with out-of-range byte should return -1, got %d", idx)
}
// Leaf node
c.GotoFirstChild() // "func" keyword
if idx := c.GotoFirstChildForByte(0); idx != -1 {
t.Fatalf("GotoFirstChildForByte on leaf should return -1, got %d", idx)
}
}
func TestTreeCursorGotoFirstChildForPoint(t *testing.T) {
lang := queryTestLanguage()
tree := buildSimpleTree(lang)
// All nodes in buildSimpleTree are on row 0 with column == byte offset
c := NewTreeCursor(tree.RootNode(), tree)
c.GotoFirstChild() // function_declaration
// Point at column 6 (inside "main") should land on identifier
if idx := c.GotoFirstChildForPoint(Point{Row: 0, Column: 6}); idx != 1 {
t.Fatalf("GotoFirstChildForPoint index: got %d, want 1", idx)
}
if c.CurrentNode().Symbol() != Symbol(1) {
t.Fatalf("expected identifier (1), got %d", c.CurrentNode().Symbol())
}
// Point at column 0 should land on "func" keyword
c.GotoParent()
if idx := c.GotoFirstChildForPoint(Point{Row: 0, Column: 0}); idx != 0 {
t.Fatalf("GotoFirstChildForPoint(0,0) index: got %d, want 0", idx)
}
if c.CurrentNode().Symbol() != Symbol(8) {
t.Fatalf("expected func keyword (8), got %d", c.CurrentNode().Symbol())
}
}
func TestTreeCursorGotoFirstChildForByteExactBoundary(t *testing.T) {
lang := queryTestLanguage()
tree := buildSimpleTree(lang)
c := NewTreeCursor(tree.RootNode(), tree)
c.GotoFirstChild() // function_declaration
// targetByte == endByte of "func" (4) should pick the next child (identifier).
if idx := c.GotoFirstChildForByte(4); idx != 1 {
t.Fatalf("GotoFirstChildForByte(4) index: got %d, want 1", idx)
}
if c.CurrentNode().Symbol() != Symbol(1) {
t.Fatalf("expected identifier (1), got %d", c.CurrentNode().Symbol())
}
}
func buildMultiRowCursorTree() *Tree {
c0 := NewLeafNode(Symbol(1), true, 0, 3, Point{Row: 0, Column: 0}, Point{Row: 0, Column: 3})
c1 := NewLeafNode(Symbol(2), true, 4, 8, Point{Row: 1, Column: 0}, Point{Row: 1, Column: 4})
c2 := NewLeafNode(Symbol(3), true, 9, 14, Point{Row: 2, Column: 0}, Point{Row: 2, Column: 5})
root := NewParentNode(Symbol(4), true, []*Node{c0, c1, c2}, []FieldID{0, 0, 0}, 0)
return NewTree(root, []byte("aaa\nbbbb\nccccc"), nil)
}
func TestTreeCursorGotoFirstChildForPointMultiRow(t *testing.T) {
tree := buildMultiRowCursorTree()
c := NewTreeCursorFromTree(tree)
if idx := c.GotoFirstChildForPoint(Point{Row: 1, Column: 1}); idx != 1 {
t.Fatalf("GotoFirstChildForPoint(Row1) index: got %d, want 1", idx)
}
if c.CurrentNode().Symbol() != Symbol(2) {
t.Fatalf("expected symbol 2, got %d", c.CurrentNode().Symbol())
}
c.GotoParent()
if idx := c.GotoFirstChildForPoint(Point{Row: 0, Column: 3}); idx != 1 {
t.Fatalf("GotoFirstChildForPoint(boundary) index: got %d, want 1", idx)
}
}
func TestTreeCursorGotoParentThenGotoNextSibling(t *testing.T) {
leafA := NewLeafNode(Symbol(1), true, 0, 1, Point{Row: 0, Column: 0}, Point{Row: 0, Column: 1})
leafB := NewLeafNode(Symbol(2), true, 1, 2, Point{Row: 0, Column: 1}, Point{Row: 0, Column: 2})
child0 := NewParentNode(Symbol(3), true, []*Node{leafA}, []FieldID{0}, 0)
child1 := NewParentNode(Symbol(4), true, []*Node{leafB}, []FieldID{0}, 0)
root := NewParentNode(Symbol(5), true, []*Node{child0, child1}, []FieldID{0, 0}, 0)
tree := NewTree(root, []byte("ab"), nil)
c := NewTreeCursorFromTree(tree)
if !c.GotoFirstChild() { // child0
t.Fatal("GotoFirstChild should succeed")
}
if !c.GotoFirstChild() { // leafA
t.Fatal("GotoFirstChild on child0 should succeed")
}
if !c.GotoParent() { // back to child0
t.Fatal("GotoParent should succeed")
}
if !c.GotoNextSibling() { // child1
t.Fatal("GotoNextSibling after GotoParent should succeed")
}
if c.CurrentNode().Symbol() != Symbol(4) {
t.Fatalf("expected symbol 4, got %d", c.CurrentNode().Symbol())
}
}
func TestTreeCursorReset(t *testing.T) {
lang := queryTestLanguage()
tree := buildSimpleTree(lang)
c := NewTreeCursorFromTree(tree)
c.GotoFirstChild() // function_declaration
c.GotoFirstChild() // "func" keyword
if c.Depth() != 2 {
t.Fatalf("expected depth 2, got %d", c.Depth())
}
// Reset to root
c.Reset(tree.RootNode())
if c.Depth() != 0 {
t.Fatalf("after Reset, depth should be 0, got %d", c.Depth())
}
if c.CurrentNode() != tree.RootNode() {
t.Fatal("after Reset, CurrentNode should be the new root")
}
// Verify navigation still works after reset
if !c.GotoFirstChild() {
t.Fatal("GotoFirstChild should work after Reset")
}
}
func TestTreeCursorResetZeroValue(t *testing.T) {
var c TreeCursor
c.Reset(nil)
if c.CurrentNode() != nil {
t.Fatal("CurrentNode should be nil after Reset(nil)")
}
if c.Depth() != 0 {
t.Fatalf("Depth should be 0, got %d", c.Depth())
}
if c.GotoFirstChild() {
t.Fatal("GotoFirstChild should fail after Reset(nil)")
}
}
func TestTreeCursorResetTree(t *testing.T) {
lang := queryTestLanguage()
tree1 := buildSimpleTree(lang)
tree2 := buildSimpleTree(lang)
c := NewTreeCursorFromTree(tree1)
c.GotoFirstChild()
c.GotoFirstChild()
c.ResetTree(tree2)
if c.CurrentNode() != tree2.RootNode() {
t.Fatal("ResetTree should place cursor at new tree root")
}
if c.Depth() != 0 {
t.Fatalf("Depth should be 0 after ResetTree, got %d", c.Depth())
}
if !c.GotoFirstChild() {
t.Fatal("GotoFirstChild should work after ResetTree")
}
}
func TestTreeCursorResetTreeNil(t *testing.T) {
lang := queryTestLanguage()
tree := buildSimpleTree(lang)
c := NewTreeCursorFromTree(tree)
c.GotoFirstChild()
c.ResetTree(nil)
if c.CurrentNode() != nil {
t.Fatal("CurrentNode should be nil after ResetTree(nil)")
}
if c.GotoFirstChild() {
t.Fatal("GotoFirstChild should fail after ResetTree(nil)")
}
}
func TestTreeCursorCopy(t *testing.T) {
lang := queryTestLanguage()
tree := buildSimpleTree(lang)
c := NewTreeCursorFromTree(tree)
c.GotoFirstChild() // function_declaration
c.GotoFirstChild() // "func" keyword
cp := c.Copy()
// Both should be at the same node
if cp.CurrentNode() != c.CurrentNode() {
t.Fatal("Copy should point to same node")
}
if cp.Depth() != c.Depth() {
t.Fatal("Copy should have same depth")
}
// Moving the copy should not affect the original
cp.GotoNextSibling() // identifier
if cp.CurrentNode().Symbol() == c.CurrentNode().Symbol() {
t.Fatal("moving copy should not affect original")
}
if c.CurrentNode().Symbol() != Symbol(8) {
t.Fatalf("original should still be at func (8), got %d", c.CurrentNode().Symbol())
}
}
func TestTreeCursorDFSTraversal(t *testing.T) {
lang := queryTestLanguage()
tree := buildSimpleTree(lang)
// Collect nodes via Walk
var walkSymbols []Symbol
Walk(tree.RootNode(), func(n *Node, depth int) WalkAction {
walkSymbols = append(walkSymbols, n.Symbol())
return WalkContinue
})
// Collect nodes via cursor DFS
var cursorSymbols []Symbol
c := NewTreeCursorFromTree(tree)
// Iterative DFS using cursor
reachedRoot := false
for !reachedRoot {
cursorSymbols = append(cursorSymbols, c.CurrentNode().Symbol())
// Try to go deeper
if c.GotoFirstChild() {
continue
}
// Try next sibling
if c.GotoNextSibling() {
continue
}
// Go up until we can go to a sibling or reach root
for {
if !c.GotoParent() {
reachedRoot = true
break
}
if c.GotoNextSibling() {
break
}
}
}
if len(cursorSymbols) != len(walkSymbols) {
t.Fatalf("cursor DFS found %d nodes, Walk found %d", len(cursorSymbols), len(walkSymbols))
}
for i := range walkSymbols {
if cursorSymbols[i] != walkSymbols[i] {
t.Fatalf("node %d: cursor got symbol %d, Walk got %d", i, cursorSymbols[i], walkSymbols[i])
}
}
}
func TestTreeCursorConvenienceAccessors(t *testing.T) {
lang := queryTestLanguage()
tree := buildSimpleTree(lang)
c := NewTreeCursorFromTree(tree)
c.GotoFirstChild() // function_declaration
if typ := c.CurrentNodeType(); typ != "function_declaration" {
t.Fatalf("expected 'function_declaration', got %q", typ)
}
if !c.CurrentNodeIsNamed() {
t.Fatal("function_declaration should be named")
}
c.GotoFirstChild() // "func" keyword
if typ := c.CurrentNodeType(); typ != "func" {
t.Fatalf("expected 'func', got %q", typ)
}
if c.CurrentNodeIsNamed() {
t.Fatal("func keyword should not be named")
}
if text := c.CurrentNodeText(); text != "func" {
t.Fatalf("expected text 'func', got %q", text)
}
c.GotoNextSibling() // identifier "main"
if text := c.CurrentNodeText(); text != "main" {
t.Fatalf("expected text 'main', got %q", text)
}
}
func TestTreeCursorBoundTree(t *testing.T) {
lang := queryTestLanguage()
tree := buildSimpleTree(lang)
bt := Bind(tree)
c := bt.TreeCursor()
if c.CurrentNode() != tree.RootNode() {
t.Fatal("BoundTree.TreeCursor should start at root")
}
c.GotoFirstChild() // function_declaration
if typ := c.CurrentNodeType(); typ != "function_declaration" {
t.Fatalf("expected 'function_declaration', got %q", typ)
}
}
func BenchmarkTreeCursorDFS(b *testing.B) {
lang := queryTestLanguage()
tree := buildSimpleTree(lang)
c := NewTreeCursorFromTree(tree)
b.ResetTimer()
for i := 0; i < b.N; i++ {
c.Reset(tree.RootNode())
reachedRoot := false
for !reachedRoot {
_ = c.CurrentNode()
if c.GotoFirstChild() {
continue
}
if c.GotoNextSibling() {
continue
}
for {
if !c.GotoParent() {
reachedRoot = true
break
}
if c.GotoNextSibling() {
break
}
}
}
}
}
func BenchmarkWalkDFS(b *testing.B) {
lang := queryTestLanguage()
tree := buildSimpleTree(lang)
b.ResetTimer()
for i := 0; i < b.N; i++ {
Walk(tree.RootNode(), func(n *Node, depth int) WalkAction {
_ = n
return WalkContinue
})
}
}