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
| | ;;; tree-sitter-tests.el --- tests for src/tree-sitter.c -*- lexical-binding: t; -*-
;; Copyright (C) 2021 Free Software Foundation, Inc.
;; This file is NOT 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 'tree-sitter)
(defsubst tree-sitter-testable ()
(when-let ((dylib (locate-file "bin/c" load-path (split-string ".so"))))
(tree-sitter--testable dylib)))
(defmacro tree-sitter-tests-with-load-path (&rest body)
(declare (indent defun))
`(let ((load-path (cons (expand-file-name "src/tree-sitter-resources")
load-path)))
(skip-unless (tree-sitter-testable))
,@body))
(defmacro tree-sitter-tests-doit (ext text &rest body)
"font-lock setup is highly circular, redundant, and difficult to isolate."
(declare (indent defun))
`(tree-sitter-tests-with-load-path
(let ((dir (make-temp-file "tree-sitter-tests" t))
(text (replace-regexp-in-string "^\n" "" ,text)))
(unwind-protect
(let ((font-lock-support-mode 'tree-sitter-lock-mode)
(file-name (expand-file-name (concat "tree-sitter-tests" ,ext) dir))
font-lock-global-modes ;; only works when not interactive
enable-dir-local-variables)
(with-temp-file file-name (insert text))
(find-file file-name)
(let (noninteractive)
(turn-on-font-lock))
(should font-lock-mode)
(should tree-sitter-lock-mode)
(should-not (text-property-any (point-min) (point-max) 'fontified nil))
,@body)
(let (kill-buffer-query-functions)
(kill-buffer))
(delete-directory dir t)))))
(ert-deftest tree-sitter-basic-parsing ()
"Test basic parsing routines."
(let ((text "
void main (void) {
return 0;
}
"))
(tree-sitter-tests-doit ".c" text
(should (equal (tree-sitter-highlights (point-min) (point-max))
'(font-lock-type-face (1 . 5) nil (5 . 6) font-lock-function-name-face (6 . 10) nil (10 . 12) font-lock-type-face (12 . 16) nil (16 . 22) font-lock-keyword-face (22 . 28) nil (28 . 29) font-lock-constant-face (29 . 30) nil (30 . 33))))
(goto-char (point-min))
(forward-line 1)
(insert "\n printf(\"hello world\");\n")
(should (equal (tree-sitter-highlights (point-min) (point-max))
'(font-lock-type-face (1 . 5) nil (5 . 6) font-lock-function-name-face (6 . 10) nil (10 . 12) font-lock-type-face (12 . 16) nil (16 . 23) font-lock-function-name-face (23 . 29) nil (29 . 30) font-lock-string-face (30 . 43) nil (43 . 48) font-lock-keyword-face (48 . 54) nil (54 . 55) font-lock-constant-face (55 . 56) nil (56 . 59)))))))
(ert-deftest tree-sitter-how-fast ()
"How fast can it fontify xdisp.c"
(tree-sitter-tests-with-load-path
(cl-flet ((bench
(file mode reps unfontify fontify)
(save-window-excursion
(find-file-literally file)
(let (font-lock-maximum-size
(font-lock-support-mode mode)
enable-dir-local-variables
font-lock-global-modes
font-lock-fontified)
(set-auto-mode)
(cl-letf (((symbol-function 'font-lock-initial-fontify) #'ignore))
(let (noninteractive)
(turn-on-font-lock)))
(cl-assert (null (text-property-any (point-min) (point-max) 'fontified t)))
(unwind-protect
(benchmark-run reps
(funcall unfontify (point-min) (point-max))
(funcall fontify (point-min) (point-max))
(cl-assert (null (text-property-any (point-min) (point-max) 'fontified nil))))
(let (kill-buffer-query-functions)
(kill-buffer)))))))
(let ((fast (car (bench (expand-file-name "src/xdisp.c" "..")
'tree-sitter-lock-mode 1
#'font-lock-unfontify-region
#'font-lock-fontify-region)))
(slow (car (bench (expand-file-name "src/xdisp.c" "..")
'jit-lock-mode 1
#'jit-lock-refontify
#'jit-lock-fontify-now))))
(message "tree-sitter-how-fast: %s versus %s" fast slow)
(should (< fast (/ slow 3)))))))
(provide 'tree-sitter-tests)
;;; tree-sitter-tests.el ends here
|