all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
blob 65994ce608f4f4aa280993da55f447a11eef3ca0 44028 bytes (raw)
name: test/src/treesit-tests.el 	 # note: path name is non-authoritative(*)

   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
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
 
;;; treesit-tests.el --- tests for src/treesit.c         -*- lexical-binding: t; -*-

;; Copyright (C) 2021-2023 Free Software Foundation, Inc.

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.

;;; Code:

(require 'ert)
(require 'treesit)

(declare-function treesit-language-available-p "treesit.c")

(declare-function treesit-parser-root-node "treesit.c")
(declare-function treesit-parser-set-included-ranges "treesit.c")
(declare-function treesit-parser-included-ranges "treesit.c")

(declare-function treesit-parser-create "treesit.c")
(declare-function treesit-parser-delete "treesit.c")
(declare-function treesit-parser-list "treesit.c")
(declare-function treesit-parser-buffer "treesit.c")
(declare-function treesit-parser-language "treesit.c")

(declare-function treesit-pattern-expand "treesit.c")
(declare-function treesit-query-expand "treesit.c")
(declare-function treesit-query-compile "treesit.c")
(declare-function treesit-query-capture "treesit.c")

(declare-function treesit-node-type "treesit.c")
(declare-function treesit-node-start "treesit.c")
(declare-function treesit-node-end "treesit.c")
(declare-function treesit-node-string "treesit.c")
(declare-function treesit-node-parent "treesit.c")
(declare-function treesit-node-child "treesit.c")
(declare-function treesit-node-check "treesit.c")
(declare-function treesit-node-field-name-for-child "treesit.c")
(declare-function treesit-node-child-count "treesit.c")
(declare-function treesit-node-child-by-field-name "treesit.c")
(declare-function treesit-node-next-sibling "treesit.c")
(declare-function treesit-node-prev-sibling "treesit.c")
(declare-function treesit-node-first-child-for-pos "treesit.c")
(declare-function treesit-node-descendant-for-range "treesit.c")
(declare-function treesit-node-eq "treesit.c")

(declare-function treesit-search-forward "treesit.c")
(declare-function treesit-search-subtree "treesit.c")

;;; Basic API

(ert-deftest treesit-basic-parsing ()
  "Test basic parsing routines."
  (skip-unless (treesit-language-available-p 'json))
  (with-temp-buffer
    (let ((parser (treesit-parser-create 'json)))
      (should
       (eq parser (car (treesit-parser-list))))
      (should
       (equal (treesit-node-string
               (treesit-parser-root-node parser))
              "(document)"))

      (insert "[1,2,3]")
      (should
       (equal (treesit-node-string
               (treesit-parser-root-node parser))
              "(document (array (number) (number) (number)))"))

      (goto-char (point-min))
      (forward-char 3)
      (insert "{\"name\": \"Bob\"},")
      (should
       (equal
        (treesit-node-string
         (treesit-parser-root-node parser))
        "(document (array (number) (object (pair key: (string (string_content)) value: (string (string_content)))) (number) (number)))")))))

(ert-deftest treesit-node-api ()
  "Tests for node API."
  (skip-unless (treesit-language-available-p 'json))
  (with-temp-buffer
    (let (parser root-node doc-node object-node pair-node)
      (progn
        (insert "[1,2,{\"name\": \"Bob\"},3]")
        (setq parser (treesit-parser-create 'json))
        (setq root-node (treesit-parser-root-node
                         parser)))
      ;; `treesit-node-type'.
      (should (equal "document" (treesit-node-type root-node)))
      ;; `treesit-node-check'.
      (should (eq t (treesit-node-check root-node 'named)))
      (should (eq nil (treesit-node-check root-node 'missing)))
      (should (eq nil (treesit-node-check root-node 'extra)))
      (should (eq nil (treesit-node-check root-node 'has-error)))
      (should (eq t (treesit-node-check root-node 'live)))
      ;; `treesit-node-child'.
      (setq doc-node (treesit-node-child root-node 0))
      (should (equal "array" (treesit-node-type doc-node)))
      (should (equal (treesit-node-string doc-node)
                     "(array (number) (number) (object (pair key: (string (string_content)) value: (string (string_content)))) (number))"))
      ;; `treesit-node-child-count'.
      (should (eql 9 (treesit-node-child-count doc-node)))
      (should (eql 4 (treesit-node-child-count doc-node t)))
      ;; `treesit-node-field-name-for-child'.
      (setq object-node (treesit-node-child doc-node 2 t))
      (setq pair-node (treesit-node-child object-node 0 t))
      (should (equal "object" (treesit-node-type object-node)))
      (should (equal "pair" (treesit-node-type pair-node)))
      (should (equal "key"
                     (treesit-node-field-name-for-child
                      pair-node 0)))
      ;; `treesit-node-child-by-field-name'.
      (should (equal "(string (string_content))"
                     (treesit-node-string
                      (treesit-node-child-by-field-name
                       pair-node "key"))))
      ;; `treesit-node-next-sibling'.
      (should (equal "(number)"
                     (treesit-node-string
                      (treesit-node-next-sibling object-node t))))
      (should (equal "(\",\")"
                     (treesit-node-string
                      (treesit-node-next-sibling object-node))))
      ;; `treesit-node-prev-sibling'.
      (should (equal "(number)"
                     (treesit-node-string
                      (treesit-node-prev-sibling object-node t))))
      (should (equal "(\",\")"
                     (treesit-node-string
                      (treesit-node-prev-sibling object-node))))
      ;; `treesit-node-first-child-for-pos'.
      (should (equal "(number)"
                     (treesit-node-string
                      (treesit-node-first-child-for-pos
                       doc-node 3 t))))
      (should (equal "(\",\")"
                     (treesit-node-string
                      (treesit-node-first-child-for-pos
                       doc-node 3))))
      (should-error (treesit-node-first-child-for-pos doc-node 100)
                    :type 'args-out-of-range)
      ;; `treesit-node-descendant-for-range'.
      (should (equal "(\"{\")"
                     (treesit-node-string
                      (treesit-node-descendant-for-range
                       root-node 6 7))))
      (should (equal "(object (pair key: (string (string_content)) value: (string (string_content))))"
                     (treesit-node-string
                      (treesit-node-descendant-for-range
                       root-node 6 7 t))))
      (should-error (treesit-node-descendant-for-range
                     root-node 100 101)
                    :type 'args-out-of-range)
      ;; `treesit-node-eq'.
      (should (treesit-node-eq root-node root-node))
      (should (not (treesit-node-eq root-node doc-node)))

      ;; Further test for `treesit-node-check'.
      (treesit-parser-delete parser)
      (should (equal nil (treesit-node-check root-node 'live)))
      ;; Recreate parser.
      (setq parser (treesit-parser-create 'json))
      (setq root-node (treesit-parser-root-node
                       parser))
      (should (equal t (treesit-node-check root-node 'live)))
      (kill-buffer)
      (should (equal nil (treesit-node-check root-node 'live))))))

;;; Indirect buffer

(ert-deftest treesit-indirect-buffer ()
  "Tests for indirect buffers."
  (skip-unless (treesit-language-available-p 'json))
  (let ((base (get-buffer-create "*treesit test*"))
        parser indirect)
    (unwind-protect
        (progn
          (with-current-buffer base
            (setq indirect (clone-indirect-buffer "*treesit test 1*" nil)))
          (with-current-buffer indirect
            (setq parser (treesit-parser-create 'json)))
          ;; 1. Parser created in the indirect buffer should be
          ;; actually be created in the base buffer.
          (with-current-buffer base
            (should (equal (list parser)
                           (treesit-parser-list)))
            (insert "[1,2,3]"))
          ;; Change in the base buffer should be reflected in the
          ;; indirect buffer.
          (with-current-buffer indirect
            (should (eq (treesit-node-end
                         (treesit-buffer-root-node))
                        8))
            (erase-buffer))
          ;; Change in the indirect buffer should be reflected in the
          ;; base buffer.
          (with-current-buffer base
            (should (eq (treesit-node-end
                         (treesit-buffer-root-node))
                        1))
            (erase-buffer)))
      (kill-buffer base)
      (kill-buffer indirect))))

;;; Tree traversal

(ert-deftest treesit-search-subtree ()
  "Test `treesit-search-subtree'."
  (skip-unless (treesit-language-available-p 'json))
  (with-temp-buffer
    (let (parser root array)
      (progn
        (insert "[[1,2,3], [1,2,3], [1,2,3]]")
        (setq parser (treesit-parser-create 'json))
        (setq root (treesit-parser-root-node parser))
        (setq array (treesit-node-child root 0)))
      (dolist (subarray (treesit-node-children array t))
        ;; Find named node forward.
        (should (equal "1" (treesit-node-text
                            (treesit-search-subtree
                             subarray "number"))))
        ;; Find named node backward.
        (should (equal "3" (treesit-node-text
                            (treesit-search-subtree
                             subarray "number" t))))
        ;; Find anonymous node forward.
        (should (equal "[" (treesit-node-text
                            (treesit-search-subtree
                             subarray "\\[" nil t))))
        ;; Find anonymous node backward.
        (should (equal "]" (treesit-node-text
                            (treesit-search-subtree
                             subarray "\\]" t t))))
        ;; If ALL=nil, it shouldn't find anonymous node.
        (should (eq nil (treesit-node-text
                         (treesit-search-subtree
                          subarray "\\["))))
        ;; If ALL=nil, searching for number should still find the
        ;; numbers.
        (should (equal "1" (treesit-node-text
                            (treesit-search-subtree
                             subarray "number" nil t))))
        ;; Find named node backward.
        (should (equal "3" (treesit-node-text
                            (treesit-search-subtree
                             subarray "number" t t))))
        ))))

(defmacro treesit--ert-search-setup (&rest body)
  "Setup macro used by `treesit-search-forward' and friends.
BODY is the test body."
  (declare (debug (&rest form)))
  `(with-temp-buffer
     (let (parser root array)
       (progn
         (insert "[[1,2,3], [4,5,6], [7,8,9]]")
         (setq parser (treesit-parser-create 'json))
         (setq root (treesit-parser-root-node
                     parser))
         (setq array (treesit-node-child root 0)))
       ,@body)))

(ert-deftest treesit-search-forward ()
  "Test `treesit-search-forward'."
  (skip-unless (treesit-language-available-p 'json))
  (treesit--ert-search-setup
   (cl-loop for cursor = (treesit-node-child array 0)
            then (treesit-search-forward cursor "" nil t)
            for text in '("[" "[" "1" "," "2" "," "3" "]"
                          "[1,2,3]" ","
                          "[" "4" "," "5" "," "6" "]"
                          "[4,5,6]" ","
                          "[" "7" "," "8" "," "9" "]"
                          "[7,8,9]" "]"
                          "[[1,2,3], [4,5,6], [7,8,9]]")
            while cursor
            do (should (equal (treesit-node-text cursor)
                              text)))))

(ert-deftest treesit-search-forward-named-only ()
  "Test `treesit-search-forward'."
  (skip-unless (treesit-language-available-p 'json))
  (treesit--ert-search-setup
   (cl-loop for cursor = (treesit-node-child
                          (treesit-node-child array 1) 1)
            then (treesit-search-forward cursor "")
            for text in '("1" "2"  "3" "[1,2,3]"
                          "4" "5" "6" "[4,5,6]"
                          "7" "8"  "9" "[7,8,9]"
                          "[[1,2,3], [4,5,6], [7,8,9]]")
            while cursor
            do (should (equal (treesit-node-text cursor)
                              text)))))

(ert-deftest treesit-search-backward ()
  "Test `treesit-search-forward'."
  (skip-unless (treesit-language-available-p 'json))
  (treesit--ert-search-setup
   (cl-loop for cursor = (treesit-node-child array -1)
            then (treesit-search-forward cursor "" t t)
            for text in (reverse '("[[1,2,3], [4,5,6], [7,8,9]]"
                                   "[" "[1,2,3]"
                                   "[" "1" "," "2" "," "3" "]"
                                   "," "[4,5,6]"
                                   "[" "4" "," "5" "," "6" "]"
                                   "," "[7,8,9]"
                                   "[" "7" "," "8" "," "9" "]"
                                   "]"))
            while cursor
            do (should (equal (treesit-node-text cursor)
                              text)))))

(ert-deftest treesit-search-backward-named-only ()
  "Test `treesit-search-forward'."
  (skip-unless (treesit-language-available-p 'json))
  (treesit--ert-search-setup
   (cl-loop for cursor = (treesit-node-child
                          (treesit-node-child array -1 t) -1 t)
            then (treesit-search-forward cursor "" t)
            for text in (reverse '("[[1,2,3], [4,5,6], [7,8,9]]"
                                   "[1,2,3]" "1" "2"  "3"
                                   "[4,5,6]" "4" "5" "6"
                                   "[7,8,9]" "7" "8"  "9"))
            while cursor
            do (should (equal (treesit-node-text cursor)
                              text)))))

(ert-deftest treesit-search-forward-predicate ()
  "Test various form of supported predicates in search functions."
  (skip-unless (treesit-language-available-p 'json))
  (treesit--ert-search-setup
   ;; The following tests are adapted from `treesit-search-forward'.

   ;; Test `or'
   (cl-loop for cursor = (treesit-node-child array 0)
            then (treesit-search-forward cursor `(or "number" ,(rx "["))
                                         nil t)
            for text in '("[" "[" "1" "2" "3"
                          "[" "4" "5" "6"
                          "[" "7" "8" "9")
            while cursor
            do (should (equal (treesit-node-text cursor) text)))
   ;; Test `not' and `or'
   (cl-loop for cursor = (treesit-node-child array 0)
            then (treesit-search-forward cursor
                                         `(not (or "number" ,(rx "[")))
                                         nil t)
            for text in '("[" "," "," "]"
                          "[1,2,3]" ","
                          "," "," "]"
                          "[4,5,6]" ","
                          "," "," "]"
                          "[7,8,9]" "]"
                          "[[1,2,3], [4,5,6], [7,8,9]]")
            while cursor
            do (should (equal (treesit-node-text cursor) text)))
   ;; Test (regexp . function)
   (let ((is-odd (lambda (node)
                   (let ((string (treesit-node-text node)))
                     (and (eq 1 (length string))
                          (cl-oddp (string-to-number string)))))))
     (cl-loop for cursor = (treesit-node-child array 0)
              then (treesit-search-forward cursor `("number" . ,is-odd)
                                           nil t)
              for text in '("[" "1" "3" "5" "7" "9")
              while cursor
              do (should (equal (treesit-node-text cursor) text))))))

(ert-deftest treesit-search-forward-predicate-invalid-predicate ()
  "Test tree-sitter's ability to detect invalid predicates."
  (skip-unless (treesit-language-available-p 'json))
  (treesit--ert-search-setup
   (dolist (pred '( 1 (not 1) (not "2" "3") (or) (or 1) 'a))
     (should-error (treesit-search-forward (treesit-node-child array 0)
                                           pred)
                   :type 'treesit-invalid-predicate))
   (should-error (treesit-search-forward (treesit-node-child array 0)
                                         (lambda (node) (car node)))
                 :type 'wrong-type-argument)))

(ert-deftest treesit-cursor-helper-with-missing-node ()
  "Test treesit_cursor_helper with a missing node."
  (skip-unless (treesit-language-available-p 'json))
  (treesit--ert-search-setup
   (delete-char -1)
   (setq root (treesit-buffer-root-node))
   (setq array (treesit-node-child root 0))
   ;; If everything works, this should not hang.
   (let ((missing-bracket (treesit-node-child array -1)))
     (treesit-search-forward missing-bracket "" t))))

;;; Query

(defun treesit--ert-pred-last-sibling (node)
  (null (treesit-node-next-sibling node t)))

(ert-deftest treesit-query-api ()
  "Tests for query API."
  (skip-unless (treesit-language-available-p 'json))
  (with-temp-buffer
    (let (parser root-node)
      (progn
        (insert "[1,2,{\"name\": \"Bob\"},3]")
        (setq parser (treesit-parser-create 'json))
        (setq root-node (treesit-parser-root-node
                         parser)))

      (should-error (treesit-query-capture root-node "" 100 101)
                    :type 'args-out-of-range)

      ;; Test `treesit-query-capture' on string, sexp and compiled
      ;; queries.
      (dolist (query1
               ;; String query.
               '("(string) @string
(pair key: (_) @keyword)
((_) @bob (#match \"\\\\`B.b\\\\'\" @bob))
(number) @number
((number) @n3 (#equal \"3\" @n3))
((number) @n3p (#pred treesit--ert-pred-last-sibling @n3p))"
                 ;; Sexp query.
                 ((string) @string
                  (pair key: (_) @keyword)
                  ((_) @bob (:match "\\`B.b\\'" @bob))
                  (number) @number
                  ((number) @n3 (:equal "3" @n3))
                  ((number) @n3p (:pred treesit--ert-pred-last-sibling
                                        @n3p)))))
        ;; Test `treesit-query-compile'.
        (dolist (query (list query1
                             (treesit-query-compile 'json query1)))
          (should
           (equal
            '((number . "1") (number . "2")
              (keyword . "\"name\"")
              (string . "\"name\"")
              (string . "\"Bob\"")
              (bob . "Bob")
              (number . "3")
              (n3 . "3")
              (n3p . "3"))
            (mapcar (lambda (entry)
                      (cons (car entry)
                            (treesit-node-text
                             (cdr entry))))
                    (treesit-query-capture root-node query))))))
      ;; Test `treesit-query-expand'.
      (should
       (equal
        "(type field: (_) @capture .) ? * + \"return\""
        (treesit-query-expand
         '((type field: (_) @capture :anchor)
           :? :* :+ "return"))))

      ;; Test string conversion in `treesit-pattern-expand'.
      (should (equal
               (treesit-pattern-expand "a\nb\rc\td\0e\"f\1g\\h\fi")
               "\"a\\nb\\rc\\td\\0e\\\"f\1g\\\\h\fi\"")))))

;;; Narrow

(ert-deftest treesit-narrow ()
  "Tests if narrowing works."
  (skip-unless (treesit-language-available-p 'json))
  (with-temp-buffer
    (let (parser)
      (progn
        (insert "xxx[1,{\"name\": \"Bob\"},2,3]xxx")
        (narrow-to-region (+ (point-min) 3) (- (point-max) 3))
        (setq parser (treesit-parser-create 'json))
        (treesit-parser-root-node parser))
      ;; This test is from the basic test.
      (should
       (equal
        (treesit-node-string
         (treesit-parser-root-node parser))
        "(document (array (number) (object (pair key: (string (string_content)) value: (string (string_content)))) (number) (number)))"))

      (widen)
      (goto-char (point-min))
      (insert "ooo")
      (should (equal "oooxxx[1,{\"name\": \"Bob\"},2,3]xxx"
                     (buffer-string)))
      (delete-region 10 26)
      (should (equal "oooxxx[1,2,3]xxx"
                     (buffer-string)))
      (narrow-to-region (+ (point-min) 6) (- (point-max) 3))
      ;; This test is also from the basic test.
      (should
       (equal (treesit-node-string
               (treesit-parser-root-node parser))
              "(document (array (number) (number) (number)))"))
      (widen)
      (goto-char (point-max))
      (insert "[1,2]")
      (should (equal "oooxxx[1,2,3]xxx[1,2]"
                     (buffer-string)))
      (narrow-to-region (- (point-max) 5) (point-max))
      (should
       (equal (treesit-node-string
               (treesit-parser-root-node parser))
              "(document (array (number) (number)))"))
      (widen)
      (goto-char (point-min))
      (insert "[1]")
      (should (equal "[1]oooxxx[1,2,3]xxx[1,2]"
                     (buffer-string)))
      (narrow-to-region (point-min) (+ (point-min) 3))
      (should
       (equal (treesit-node-string
               (treesit-parser-root-node parser))
              "(document (array (number)))")))))

(ert-deftest treesit-cross-boundary ()
  "Tests for cross-boundary edits.
Cross-boundary means crossing visible_beg and visible_end.  We
don't test if parser parses correctly, instead we just check
edits like this don't produce assertion errors.  (I inserted a
bunch of assertions that checks e.g. visible_beg <=
visible_end.)"
  (skip-unless (treesit-language-available-p 'json))
  (with-temp-buffer
    (let (parser)
      (progn
        (insert "xxx[1,{\"name\": \"Bob\"},2,3]xxx")
        (narrow-to-region (+ (point-min) 3) (- (point-max) 3))
        (setq parser (treesit-parser-create 'json))
        ;; Now visible_beg/end = visible boundary.
        (treesit-parser-root-node parser))
      ;; Now parser knows the content of the visible region.
      (widen)
      ;; Now visible_beg/end don't change, but visible region expanded.
      (delete-region 1 7)
      ;; (1) This change is across visible_beg.  I expect
      ;; ts_record_change to receive (start=1, old_end=7, new_end=1).
      (treesit-parser-root-node parser)
      ;; Above form forces a parse which calls
      ;; `ts_ensure_position_synced'. Now visible_beg/end matches the
      ;; visible region (whole buffer).  We want to test that this
      ;; doesn't cause assertion error.

      (should (equal "{\"name\": \"Bob\"},2,3]xxx" (buffer-string)))
      (narrow-to-region 1 16)
      (should (equal "{\"name\": \"Bob\"}" (buffer-string)))
      (treesit-parser-root-node parser)
      ;; Call `ts_ensure_position_synced' again to update visible_beg/end.
      (widen)
      (goto-char 14)
      (insert "by")
      ;; (2) This change is inside [visible_beg, visible_end].
      (should (equal "{\"name\": \"Bobby\"},2,3]xxx" (buffer-string)))
      (delete-region 14 23)
      ;; This delete is across visible_end.
      (should (equal "{\"name\": \"Bobxxx" (buffer-string)))
      (treesit-parser-root-node parser)
      ;; visible_beg/end synced.

      (narrow-to-region 3 7)
      (should (equal "name" (buffer-string)))
      (treesit-parser-root-node parser)
      ;; visible_beg/end synced.
      (widen)
      (goto-char (point-min))
      (insert "zzz")
      (should (equal "zzz{\"name\": \"Bobxxx" (buffer-string)))
      ;; (3) Test inserting before visible_beg.
      (treesit-parser-root-node parser)
      ;; visible_beg/end synced.

      (narrow-to-region 4 11)
      (should (equal "{\"name\"" (buffer-string)))
      (treesit-parser-root-node parser)
      ;; visible_beg/end synced.
      (widen)
      (goto-char (point-max))
      (insert "yyy")
      ;; (4) This change is after visible_end.
      (treesit-parser-root-node parser)
      ;; Sync up visible_beg/end.
      (should (equal "zzz{\"name\": \"Bobxxxyyy" (buffer-string)))

      (narrow-to-region 1 17)
      (should (equal "zzz{\"name\": \"Bob" (buffer-string)))
      (treesit-parser-root-node parser)
      ;; Sync up visible_beg/end.
      (widen)
      (delete-region 13 (point-max))
      (treesit-parser-root-node parser)
      ;; Sync up visible_beg/end.
      (should (equal "zzz{\"name\": " (buffer-string)))
      ;; Ideally we want to also test the case where we delete and
      ;; insert simultaneously, but the only such use is in
      ;; `casify_region', all others either only inserts or only
      ;; deletes.  I'll leave it to someone to try to write a test
      ;; that calls that.
      )))

;;; Range

(ert-deftest treesit-range ()
  "Tests if range works."
  (skip-unless (treesit-language-available-p 'json))
  (with-temp-buffer
    (let (parser)
      (progn
        (insert "[[1],oooxxx[1,2,3],xxx[1,2]]")
        (setq parser (treesit-parser-create 'json))
        (treesit-parser-root-node parser))

      (should (eq (treesit-parser-included-ranges parser) nil))

      (should-error
       (treesit-parser-set-included-ranges
        parser '((1 . 6) (5 . 20)))
       :type '(treesit-range-invalid))

      (treesit-parser-set-included-ranges
       parser '((1 . 6) (12 . 20) (23 . 29)))
      (should (equal '((1 . 6) (12 . 20) (23 . 29))
                     (treesit-parser-included-ranges parser)))
      (should (equal "(document (array (array (number)) (array (number) (number) (number)) (array (number) (number))))"
                     (treesit-node-string
                      (treesit-parser-root-node parser))))

      (treesit-parser-set-included-ranges parser nil)
      (should (eq (treesit-parser-included-ranges parser) nil))

      ;; `treesit--merge-ranges'.
      (let ((old-ranges '((1 . 10) ; (1) -- before (a)
                          (20 . 30); (2) -- intersect with (b)
                          (42 . 46) (47 . 48) ; (3) -- inside (c)
                          (55 . 65) (70 . 75) ; (4) -- intersect start-end
                          (80 . 90) ; (4)
                          ))
            (new-ranges '((10 . 15) ; (a)
                          (18 . 25) (26 . 28) ; (b)
                          (40 . 50) ; (c)
                          (90 . 100) ; (d) -- after (4)
                          ))
            (result '((1 . 10) ; (1)
                      (10 . 15) ; (a)
                      (18 . 25) (26 . 28) ; (b)
                      (40 . 50) ; (c)
                      (80 . 90) ; (4)
                      (90 . 100) ; (d)
                      )))
        (should (equal (treesit--merge-ranges
                        old-ranges new-ranges 60 75)
                       result)))
      ;; TODO: More tests.
      )))

;;; Multiple language

(ert-deftest treesit-multi-lang ()
  "Tests if parsing multiple language works."
  (skip-unless (and (treesit-language-available-p 'html)
                    (treesit-language-available-p 'css)
                    (treesit-language-available-p 'javascript)))
  (with-temp-buffer
    (let (css js css-range js-range)
      (progn
        (insert "<html><script>1</script><style>body {}</style></html>")
        (treesit-parser-create 'html)
        (setq css (treesit-parser-create 'css))
        (setq js (treesit-parser-create 'javascript)))
      ;; JavaScript.
      (setq js-range
            (treesit-query-range
             'html
             '((script_element (raw_text) @capture))))
      (should (equal '((15 . 16)) js-range))
      (treesit-parser-set-included-ranges js js-range)
      (should (equal "(program (expression_statement (number)))"
                     (treesit-node-string
                      (treesit-parser-root-node js))))
      ;; CSS.
      (setq css-range
            (treesit-query-range
             'html
             '((style_element (raw_text) @capture))))
      (should (equal '((32 . 39)) css-range))
      (treesit-parser-set-included-ranges css css-range)
      (should
       (equal "(stylesheet (rule_set (selectors (tag_name)) (block)))"
              (treesit-node-string
               (treesit-parser-root-node css))))
      ;; TODO: More tests.
      )))

;;; Supplemental functions

(ert-deftest treesit-parser-supplemental ()
  "Supplemental node functions."
  (skip-unless (treesit-language-available-p 'json))
  ;; `treesit-parse-string'.
  (should (equal (treesit-node-string
                  (treesit-parse-string
                   "[1,2,{\"name\": \"Bob\"},3]"
                   'json))
                 "(document (array (number) (number) (object (pair key: (string (string_content)) value: (string (string_content)))) (number)))"))
  (with-temp-buffer
    (let (parser root-node)
      (progn
        (insert "[1,2,{\"name\": \"Bob\"},3]")
        (setq parser (treesit-parser-create 'json))
        (setq root-node (treesit-parser-root-node
                         parser))
        (treesit-node-child root-node 0))
      )))

(ert-deftest treesit-node-supplemental ()
  "Supplemental node functions."
  (skip-unless (treesit-language-available-p 'json))
  (let (parser root-node doc-node)
    (progn
      (insert "[1,2,{\"name\": \"Bob\"},3]")
      (setq parser (treesit-parser-create 'json))
      (setq root-node (treesit-parser-root-node
                       parser))
      (setq doc-node (treesit-node-child root-node 0)))
    ;; `treesit-node-buffer'.
    (should (equal (treesit-node-buffer root-node)
                   (current-buffer)))
    ;; `treesit-node-language'.
    (should (eq (treesit-node-language root-node)
                'json))
    ;; `treesit-node-at'.
    (should (equal (treesit-node-string
                    (treesit-node-at 1 'json))
                   "(\"[\")"))
    ;; `treesit-node-on'
    (should (equal (treesit-node-string
                    (treesit-node-on 1 2 'json))
                   "(\"[\")"))
    ;; `treesit-buffer-root-node'.
    (should (treesit-node-eq
             (treesit-buffer-root-node 'json)
             root-node))
    ;; `treesit-filter-child'.
    (should (equal (mapcar
                    (lambda (node)
                      (treesit-node-type node))
                    (treesit-filter-child
                     doc-node (lambda (node)
                                (treesit-node-check node 'named))))
                   '("number" "number" "object" "number")))
    ;; `treesit-node-text'.
    (should (equal (treesit-node-text doc-node)
                   "[1,2,{\"name\": \"Bob\"},3]"))
    ;; `treesit-node-index'.
    (should (eq (treesit-node-index doc-node)
                0))
    ;; TODO:
    ;; `treesit-parent-until'
    ;; `treesit-parent-while'
    ;; `treesit-node-children'
    ;; `treesit-node-field-name'
    ;; `treesit-search-forward-goto'
    ))

(ert-deftest treesit-node-at ()
  "Test `treesit-node-at'."
  (skip-unless (treesit-language-available-p 'json))
  (let (parser)
    (progn
      (insert "[1,  2, 3,4]  ")
      (setq parser (treesit-parser-create 'json))
      (treesit-parser-root-node parser))
    ;; Point at ",", should return ",".
    (goto-char (point-min))
    (search-forward "1")
    (should (equal (treesit-node-text
                    (treesit-node-at (point)))
                   ","))
    ;; Point behind ",", should still return the ",".
    (search-forward ",")
    (should (equal (treesit-node-text
                    (treesit-node-at (point)))
                   ","))
    ;; Point between "," and "2", should return 2.
    (forward-char)
    (should (equal (treesit-node-text
                    (treesit-node-at (point)))
                   "2"))
    ;; EOF, should return the last leaf node "]".
    (goto-char (point-max))
    (should (equal (treesit-node-text
                    (treesit-node-at (point)))
                   "]"))))

(ert-deftest treesit-node-check ()
  "Test `treesit-node-check'."
  (skip-unless (treesit-language-available-p 'json))
  (let (parser root-node array-node comment-node)
    (progn
      (insert "/* comment */ [1,  2, 3,4  ")
      (setq parser (treesit-parser-create 'json))
      (setq root-node (treesit-parser-root-node
                       parser))
      (setq comment-node (treesit-node-child root-node 0))
      (setq array-node (treesit-node-child root-node 1)))

    (should (treesit-node-check comment-node 'extra))
    (should (treesit-node-check array-node 'has-error))
    (should-error (treesit-node-check array-node 'xxx))
    (should (treesit-node-check (treesit-node-child array-node -1)
                                'missing))
    (goto-char (point-max))
    (insert "]")
    (should (treesit-node-check array-node 'outdated))))

;;; Defun navigation
;;
;; I've setup a framework for easier testing of defun navigation.
;;
;; To use it for a particular language, first write a test program
;; similar to `treesit--ert-defun-navigation-python-program', and
;; insert markers.  Markers that marks BOLs are defined as follows:
;;
;; 100 Before 1st parent
;; 101 Beg of 1st parent
;; 102 End of 1st parent
;; 103 Beg of 2nd parent
;; 104 Beg of 1st method
;; 105 End of 1st method
;; 106 Beg of 2nd method
;; 107 End of 2nd method
;; 108 End of 2nd parent
;; 109 Beg of 3rd parent
;; 110 End of 3rd parent
;; 999 Dummy markers
;;
;; Then add marker 0-9 following the definition given in
;; `treesit--ert-defun-navigation-nested-master'.  Then you can use
;; `treesit--ert-test-defun-navigation', pass the test program you
;; just wrote, and the appropriate master:
;;
;; - `treesit--ert-defun-navigation-nested-master' for nested defun
;; - `treesit--ert-defun-navigation-top-level-master' for top-level


(defun treesit--ert-insert-and-parse-marker (opening closing text)
  "Insert TEXT and parse the marker positions in it.

TEXT should be a string in which contains position markings
like (1).  OPENING and CLOSING are position marking's delimiters,
for (1), OPENING and CLOSING should be \"(\" and \")\",
respectively.

This function inserts TEXT, parses and removes all the markings,
and returns an alist of (NUMBER . POS), where number is each
marking's number, and POS is each marking's position."
  (declare (indent 2))
  (let (result)
    (insert text)
    (goto-char (point-min))
    (while (re-search-forward
            (rx-to-string `(seq ,opening (group (+ digit)) ,closing))
            nil t)
      (let ((idx (string-to-number (match-string 1))))
        (push (cons idx (match-beginning 0)) result)
        (delete-region (match-beginning 0) (match-end 0))))
    (nreverse result)))

(defun treesit--ert-collect-positions (positions functions)
  "Collect positions after calling each function in FUNCTIONS.

POSITIONS should be a list of buffer positions, FUNCTIONS should
be a list of functions.  This function collects the return value
of each function in FUNCTIONS starting at each position in
POSITIONS.

Return a list of (POS...) where each POS corresponds to a
function in FUNCTIONS.  For example, if buffer content is
\"123\", POSITIONS is (2 3), FUNCTIONS is (point-min point-max),
the return value is ((1 3) (1 3))."
  (cl-loop for pos in positions
           collect (cl-loop for fn in functions
                            collect (progn
                                      (goto-char pos)
                                      (funcall fn)))))

(defun treesit--ert-test-defun-navigation
    (init program master tactic &optional opening closing)
  "Run defun navigation tests on PROGRAM and MASTER.

INIT is a setup function that runs right after this function
creates a temporary buffer.  It should take no argument.

PROGRAM is a program source in string, MASTER is a list of
\(START PREV-BEG NEXT-END PREV-END NEXT-BEG), where START is the
starting marker position, and the rest are marker positions the
corresponding navigation should stop at (after running
`treesit-defun-skipper').

TACTIC is the same as in `treesit--navigate-thing'.

OPENING and CLOSING are the same as in
`treesit--ert-insert-and-parse-marker', by default they are \"[\"
and \"]\"."
  (with-temp-buffer
    (funcall init)
    (pcase-let*
        ((opening (or opening "["))
         (closing (or closing "]"))
         ;; Insert program and parse marker positions.
         (marker-alist (treesit--ert-insert-and-parse-marker
                           opening closing program))
         ;; Translate marker positions into buffer positions.
         (decoded-master
          (cl-loop for record in master
                   collect
                   (cl-loop for pos in record
                            collect (alist-get pos marker-alist))))
         ;; Collect positions each function returns.
         (positions
          (treesit--ert-collect-positions
           ;; The first column of DECODED-MASTER.
           (mapcar #'car decoded-master)
           ;; Four functions: next-end, prev-beg, next-beg, prev-end.
           (mapcar (lambda (conf)
                     (lambda ()
                       (if-let ((pos (funcall
                                      #'treesit--navigate-thing
                                      (point) (car conf) (cdr conf)
                                      treesit-defun-type-regexp tactic)))
                           (save-excursion
                             (goto-char pos)
                             (funcall treesit-defun-skipper)
                             (point)))))
                   '((-1 . beg)
                     (1 . end)
                     (-1 . end)
                     (1 . beg))))))
      ;; Verify each position.
      (cl-loop for record in decoded-master
               for orig-record in master
               for poss in positions
               for name = (format "marker %d" (car orig-record))
               do (should (equal (cons name (cdr record))
                                 (cons name poss)))))))

(defvar treesit--ert-defun-navigation-python-program
  "[100]
[101]class Class1():
[999]    prop = 0
[102]
[103]class Class2():[0]
[104]    [1]def method1():
[999]        [2]return 0[3]
[105]    [4]
[106]    [5]def method2():
[999]        [6]return 0[7]
[107]    [8]
[999]    prop = 0[9]
[108]
[109]class Class3():
[999]    prop = 0[10]
[110]
"
  "Python source for navigation test.")

(defvar treesit--ert-defun-navigation-js-program
  "[100]
[101]class Class1 {
[999]}
[102]
[103]class Class2 {[0]
[104]  [1]method1() {
[999]    [2]return 0;
[999]  }[3]
[105]  [4]
[106]  [5]method2() {
[999]    [6]return 0;
[999]  }[7]
[107][8]
[999]}[9]
[108]
[109]class class3 {
[999]}[10]
[110]
"
  "Javascript source for navigation test.")

(defvar treesit--ert-defun-navigation-bash-program
  "[100]
[101]parent1 () {
[999]}
[102]
[103]parent2 () {[0]
[104]    [1]sibling1 () {
[999]        [2]echo hi
[999]    }[3]
[105]    [4]
[106]    [5]sibling2 () {
[999]        [6]echo hi
[999]    }[7]
[107][8]
[999]}[9]
[108]
[109]parent3 () {
[999]}
[110]
"
  "Bash source for navigation test.")

(defvar treesit--ert-defun-navigation-elixir-program
  "[100]
[101]def bar() do
[999]end
[102]
[103]defmodule Example do[0]
[999] @impl true
[104] [1]def bar() do[2]
[999] end[3]
[105] [4]
[106] [5]def baz() do[6]
[999] end[7]
[107] [8]
[999]end[9]
[108]
[109]def bar() do
[999]end
[110]
"
  "Elixir source for navigation test.")

(defvar treesit--ert-defun-navigation-nested-master
  ;; START PREV-BEG NEXT-END PREV-END NEXT-BEG
  '((0 103 105 102 104) ; Between Beg of parent & 1st sibling.
    (1 103 105 102 106) ; Beg of 1st sibling.
    (2 104 105 102 106) ; Inside 1st sibling.
    (3 104 107 102 106) ; End of 1st sibling.
    (4 104 107 105 106) ; Between 1st sibling & 2nd sibling.
    (5 104 107 105 109) ; Beg of 2nd sibling.
    (6 106 107 105 109) ; Inside 2nd sibling.
    (7 106 108 105 109) ; End of 2nd sibling.
    (8 106 108 107 109) ; Between 2nd sibling & end of parent.
    (9 103 110 102 109) ; End of parent.

    (100 nil 102 nil 101) ; Before 1st parent.
    (101 nil 102 nil 103) ; Beg of 1st parent.
    (102 101 108 102 103) ; Between 1st & 2nd parent.
    (103 101 108 102 109) ; Beg of 2nd parent.
    (110 109 nil 110 nil) ; After 3rd parent.
    )
  "Master of nested navigation test.

This basically says, e.g., \"start with point on marker 0, go to
the prev-beg, now point should be at marker 103\", etc.")

(defvar treesit--ert-defun-navigation-top-level-master
  ;; START PREV-BEG NEXT-END PREV-END NEXT-BEG
  '((0 103 108 102 109) ; Between Beg of parent & 1st sibling.
    (1 103 108 102 109) ; Beg of 1st sibling.
    (2 103 108 102 109) ; Inside 1st sibling.
    (3 103 108 102 109) ; End of 1st sibling.
    (4 103 108 102 109) ; Between 1st sibling & 2nd sibling.
    (5 103 108 102 109) ; Beg of 2nd sibling.
    (6 103 108 102 109) ; Inside 2nd sibling.
    (7 103 108 102 109) ; End of 2nd sibling.
    (8 103 108 102 109) ; Between 2nd sibling & end of parent.
    (9 103 110 102 109) ; End of parent.

    ;; Top-level defuns should be identical to the nested test.
    (100 nil 102 nil 101) ; Before 1st parent.
    (101 nil 102 nil 103) ; Beg of 1st parent.
    (102 101 108 102 103) ; Between 1st & 2nd parent.
    (103 101 108 102 109) ; Beg of 2nd parent.
    (110 109 nil 110 nil) ; After 3rd parent.
    )
  "Master of top-level navigation test.")

(ert-deftest treesit-defun-navigation-nested-1 ()
  "Test defun navigation."
  (skip-unless (treesit-language-available-p 'python))
  ;; Nested defun navigation
  (require 'python)
  (treesit--ert-test-defun-navigation
   'python-ts-mode
   treesit--ert-defun-navigation-python-program
   treesit--ert-defun-navigation-nested-master
   'nested))

(ert-deftest treesit-defun-navigation-nested-2 ()
  "Test defun navigation using `js-ts-mode'."
  (skip-unless (treesit-language-available-p 'javascript))
  ;; Nested defun navigation
  (require 'js)
  (treesit--ert-test-defun-navigation
   'js-ts-mode
   treesit--ert-defun-navigation-js-program
   treesit--ert-defun-navigation-nested-master
   'nested))

(ert-deftest treesit-defun-navigation-nested-3 ()
  "Test defun navigation using `bash-ts-mode'."
  (skip-unless (treesit-language-available-p 'bash))
  ;; Nested defun navigation
  (treesit--ert-test-defun-navigation
   (lambda ()
     (treesit-parser-create 'bash)
     (setq-local treesit-defun-type-regexp "function_definition"))
   treesit--ert-defun-navigation-bash-program
   treesit--ert-defun-navigation-nested-master
   'nested))

(ert-deftest treesit-defun-navigation-nested-4 ()
  "Test defun navigation using Elixir.
This tests bug#60355."
  (skip-unless (treesit-language-available-p 'elixir))
  ;; Nested defun navigation
  (let ((pred (lambda (node)
                (member (treesit-node-text
                         (treesit-node-child-by-field-name node "target"))
                        '("def" "defmodule")))))
    (treesit--ert-test-defun-navigation
     (lambda ()
       (treesit-parser-create 'elixir)
       (setq-local treesit-defun-type-regexp `("call" . ,pred)))
     treesit--ert-defun-navigation-elixir-program
     treesit--ert-defun-navigation-nested-master
     'nested)))

(ert-deftest treesit-defun-navigation-top-level ()
  "Test top-level only defun navigation."
  (skip-unless (treesit-language-available-p 'python))
  ;; Nested defun navigation
  (require 'python)
  (treesit--ert-test-defun-navigation
   'python-ts-mode
   treesit--ert-defun-navigation-python-program
   treesit--ert-defun-navigation-top-level-master
   'top-level))

;; TODO
;; - Functions in treesit.el
;; - treesit-load-name-override-list
;; - treesit-search-subtree
;; - treesit-search-forward
;; - treesit-induce-sparse-tree
;; - treesit-search-forward


(provide 'treesit-tests)
;;; treesit-tests.el ends here

debug log:

solving 65994ce608f ...
found 65994ce608f in https://git.savannah.gnu.org/cgit/emacs.git

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.