Skip to content

Commit 81a3433

Browse files
committed
core: Add tests for node-yielding tree-traversal functions
1 parent 335c6e8 commit 81a3433

File tree

3 files changed

+83
-43
lines changed

3 files changed

+83
-43
lines changed

core/tsc.el

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ e.g. automatically through escape analysis. How about porting ELisp to GraalVM?"
291291
(not (memq kw tsc-valid-node-props)))
292292
props)))
293293
(error "Invalid node properties %s" invalid-props)))
294+
((null props) nil)
294295
(t (error "Expected vectors or keyword %s" props))))
295296

296297
(defun tsc-traverse-mapc (func tree-or-node &optional props)

tests/tree-sitter-bench.el

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
(garbage-collect)
2727
(message "tsc-parse-chunks %6d %s" tsc--buffer-input-chunk-size
2828
(benchmark-run 10
29-
(tsc-parse-chunks parser #'tsc--buffer-input nil)))
29+
(tsc-parse-chunks parser #'tsc--buffer-input nil)))
3030
(cl-incf n)))))))
3131

3232
(ert-deftest cursor::bench ()
@@ -38,6 +38,11 @@
3838
(dolist (n '(1 10 100))
3939
(message "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
4040
(garbage-collect)
41+
(message "%10s %3d %s" :do n
42+
(eval `(benchmark-run-compiled ,n
43+
(tsc-traverse-do ([named-p type start-byte end-byte] tree-sitter-tree)
44+
named-p type start-byte end-byte))))
45+
(garbage-collect)
4146
(message "%10s %3d %s" :mapc n
4247
(eval `(benchmark-run-compiled ,n
4348
(tsc-traverse-mapc
@@ -50,10 +55,25 @@
5055
(iter-do (_ (tsc-traverse-iter tree-sitter-tree ,props))
5156
(tsc-test-no-op)))))
5257
(garbage-collect)
53-
(message "%10s %3d %s" :do n
58+
(message "%10s %3d %s" :node-mapc n
5459
(eval `(benchmark-run-compiled ,n
55-
(tsc-traverse-do ([named-p type start-byte end-byte] tree-sitter-tree)
56-
named-p type start-byte end-byte))))
60+
(tsc-traverse-mapc
61+
(lambda (node)
62+
(tsc-node-named-p node)
63+
(tsc-node-type node)
64+
(tsc-node-start-byte node)
65+
(tsc-node-end-byte node)
66+
(tsc-test-no-op))
67+
tree-sitter-tree))))
68+
(garbage-collect)
69+
(message "%10s %3d %s" :node-iter n
70+
(eval `(benchmark-run-compiled ,n
71+
(iter-do (node (tsc-traverse-iter tree-sitter-tree))
72+
(tsc-node-named-p node)
73+
(tsc-node-type node)
74+
(tsc-node-start-byte node)
75+
(tsc-node-end-byte node)
76+
(tsc-test-no-op)))))
5777
(garbage-collect)
5878
(message "%10s %3d %s" 'funcall n
5979
(eval `(benchmark-run-compiled ,(* 3429 n)

tests/tree-sitter-tests.el

Lines changed: 58 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,10 @@
101101
(tsc-test-tree-sexp '(source_file (ERROR)))
102102
(insert " foo() {}")
103103
(tsc-test-tree-sexp '(source_file
104-
(function_item
105-
name: (identifier)
106-
parameters: (parameters)
107-
body: (block))))
104+
(function_item
105+
name: (identifier)
106+
parameters: (parameters)
107+
body: (block))))
108108
(kill-region (point-min) (point-max))
109109
(tsc-test-tree-sexp '(source_file))))
110110

@@ -266,27 +266,46 @@ source_file (1 . 20)
266266
(tsc-test-render-node type named-p start-byte end-byte field depth)))))))))
267267

268268
(ert-deftest cursor::traverse:single-property ()
269-
(tsc-test-with rust parser
270-
(let* ((code "fn foo(x: usize) {}")
271-
(tree (tsc-parse-string parser code)))
272-
(ert-info ("")
273-
(let (mapc-result
274-
do-result
275-
iter-result)
276-
(ert-info ("Callback-based traversal should work with single property")
277-
(tsc-traverse-mapc
278-
(lambda (type)
279-
(cl-callf append mapc-result (list type)))
280-
tree :type))
281-
(ert-info ("Iterator-based traversal should work with single property")
282-
(setq iter-result (cl-loop for type
283-
iter-by (tsc-traverse-iter tree :type)
284-
collect type)))
285-
(tsc-traverse-do ([type] tree)
286-
(cl-callf append do-result (list type)))
287-
(ert-info ("All traversal methods should return the same result")
288-
(should (equal do-result mapc-result))
289-
(should (equal do-result iter-result))))))))
269+
(tsc-test-lang-with-file rust "data/types.rs"
270+
(let ((tree tree-sitter-tree)
271+
mapc-result
272+
do-result
273+
iter-result)
274+
(ert-info ("Callback-based traversal should work with single property")
275+
(tsc-traverse-mapc
276+
(lambda (type)
277+
(push type mapc-result))
278+
tree :type))
279+
(ert-info ("Iterator-based traversal should work with single property")
280+
(cl-loop for type
281+
iter-by (tsc-traverse-iter tree :type)
282+
do (push type iter-result)))
283+
(tsc-traverse-do ([type] tree)
284+
(push type do-result))
285+
(ert-info ("All traversal methods should return the same result")
286+
(should (equal do-result mapc-result))
287+
(should (equal do-result iter-result))))))
288+
289+
(ert-deftest cursor::traverse:node ()
290+
(tsc-test-lang-with-file rust "data/types.rs"
291+
(let ((tree tree-sitter-tree)
292+
mapc-result
293+
do-result
294+
iter-result)
295+
(ert-info ("Callback-based traversal should work with nodes")
296+
(tsc-traverse-mapc
297+
(lambda (node)
298+
(push (tsc-node-type node) mapc-result))
299+
tree))
300+
(ert-info ("Iterator-based traversal should work with nodes")
301+
(cl-loop for node
302+
iter-by (tsc-traverse-iter tree)
303+
do (push (tsc-node-type node) iter-result)))
304+
(tsc-traverse-do ([type] tree)
305+
(push type do-result))
306+
(ert-info ("All traversal methods should return the same result")
307+
(should (equal do-result mapc-result))
308+
(should (equal do-result iter-result))))))
290309

291310
(ert-deftest conversion::position<->tsc-point ()
292311
(tsc-test-with-file "tree-sitter-tests.el"
@@ -325,7 +344,7 @@ source_file (1 . 20)
325344
(ert-info ("Should work on vector")
326345
(should (= (tsc-query-count-patterns
327346
(tsc-make-query rust [(function_item (identifier) @function)
328-
(macro_definition (identifier) @macro)]))
347+
(macro_definition (identifier) @macro)]))
329348
2)))))
330349

331350
(ert-deftest query::basic ()
@@ -341,8 +360,8 @@ source_file (1 . 20)
341360
(tsc-node-text node)))
342361
captures))
343362
(capture-tags (mapcar (lambda (capture)
344-
(pcase-let ((`(,tag . _) capture)) tag))
345-
captures)))
363+
(pcase-let ((`(,tag . _) capture)) tag))
364+
captures)))
346365
(ert-info ("Should match specified functions and not more")
347366
(should (member "_make_query" node-texts))
348367
(should (member "make_query_cursor" node-texts))
@@ -355,10 +374,10 @@ source_file (1 . 20)
355374
(tsc-test-lang-with-file c "data/range-restriction-and-early-termination.c"
356375
(let ((cursor (tsc-make-query-cursor))
357376
(query (tsc-make-query tree-sitter-language
358-
[(call_expression
359-
function: (identifier) @function
360-
arguments: (argument_list (string_literal) @string.arg))
361-
(string_literal) @string]))
377+
[(call_expression
378+
function: (identifier) @function
379+
arguments: (argument_list (string_literal) @string.arg))
380+
(string_literal) @string]))
362381
(root-node (tsc-root-node tree-sitter-tree))
363382
(capture-names '(function string.arg string)))
364383
(ert-info ("Querying without range restriction")
@@ -469,13 +488,13 @@ source_file (1 . 20)
469488
We know it should since it is the `source_file' node."
470489
(tsc-test-lang-with-file rust "data/types.rs"
471490
(let ((buf-name (buffer-name)))
472-
(setq tree-sitter-debug-jump-buttons t)
473-
(tree-sitter-debug-mode)
474-
(goto-char (point-max))
475-
(should (> (point) 0)) ; Test if worthless if the file is empty
476-
(switch-to-buffer tree-sitter-debug--tree-buffer nil t)
477-
(tree-sitter-debug--button-node-lookup (button-at 1))
478-
(should (= (point) (point-min))))))
491+
(setq tree-sitter-debug-jump-buttons t)
492+
(tree-sitter-debug-mode)
493+
(goto-char (point-max))
494+
(should (> (point) 0)) ; Test if worthless if the file is empty
495+
(switch-to-buffer tree-sitter-debug--tree-buffer nil t)
496+
(tree-sitter-debug--button-node-lookup (button-at 1))
497+
(should (= (point) (point-min))))))
479498

480499
;; Local Variables:
481500
;; no-byte-compile: t

0 commit comments

Comments
 (0)