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
| | ;;; treesit-tests.el --- tests for src/treesit.c -*- lexical-binding: t; -*-
;; Copyright (C) 2021-2022 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-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")
(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))
"(ERROR)"))
(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)))
;; `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))))
;; `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))))
;; `treesit-node-eq'.
(should (treesit-node-eq root-node root-node))
(should (not (treesit-node-eq root-node doc-node))))))
(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)))
;; 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)) "
;; Sexp query.
((string) @string
(pair key: (_) @keyword)
((_) @bob (:match "^B.b$" @bob))
(number) @number
((number) @n3 (:equal "3" @n3)))))
;; 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"))
(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")))))))
(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.
)))
(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.
)))
(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.
)))
(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))))
;; 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
|