unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
blob c61ad678d2da00c1991a8dc4f9e83e1d20a01108 6374 bytes (raw)
name: test/src/tree-sitter-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
 
;;; tree-sitter-tests.el --- tests for src/tree-sitter.c         -*- lexical-binding: t; -*-

;; Copyright (C) 2021 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 'tree-sitter-json)
(require 'tree-sitter)

(ert-deftest tree-sitter-basic-parsing ()
  "Test basic parsing routines."
  (with-temp-buffer
    (let ((parser (tree-sitter-create-parser
                   (current-buffer) (tree-sitter-json))))
      (should
       (eq parser (car tree-sitter-parser-list)))
      (should
       (equal (tree-sitter-node-string
               (tree-sitter-parser-root-node parser))
              "(ERROR)"))

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

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

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

(ert-deftest tree-sitter-query-api ()
  "Tests for query API."
  (with-temp-buffer
    (let (parser root-node pattern doc-node object-node pair-node)
      (progn
        (insert "[1,2,{\"name\": \"Bob\"},3]")
        (setq parser (tree-sitter-create-parser
                      (current-buffer) (tree-sitter-json)))
        (setq root-node (tree-sitter-parser-root-node
                         parser))
        (setq pattern "(string) @string
(pair key: (_) @keyword)
(number) @number"))

      (should
       (equal
        '((number . "1") (number . "2")
          (keyword . "\"name\"")
          (string . "\"name\"")
          (string . "\"Bob\"")
          (number . "3"))
        (mapcar (lambda (entry)
                  (cons (car entry)
                        (tree-sitter-node-content
                         (cdr entry))))
                (tree-sitter-query-capture root-node pattern)))))))

(provide 'tree-sitter-tests)
;;; tree-sitter-tests.el ends here

debug log:

solving c61ad678d2 ...
found c61ad678d2 in https://yhetil.org/emacs-devel/CF42CD83-02FF-4945-B3A5-0D71E8299EE2@gmail.com/
found cb1c464d3a in https://yhetil.org/emacs-devel/F95D0E9B-0B21-450A-B91D-87E9E05873CE@gmail.com/

applying [1/2] https://yhetil.org/emacs-devel/F95D0E9B-0B21-450A-B91D-87E9E05873CE@gmail.com/
diff --git a/test/src/tree-sitter-tests.el b/test/src/tree-sitter-tests.el
new file mode 100644
index 0000000000..cb1c464d3a


applying [2/2] https://yhetil.org/emacs-devel/CF42CD83-02FF-4945-B3A5-0D71E8299EE2@gmail.com/
diff --git a/test/src/tree-sitter-tests.el b/test/src/tree-sitter-tests.el
index cb1c464d3a..c61ad678d2 100644

Checking patch test/src/tree-sitter-tests.el...
Applied patch test/src/tree-sitter-tests.el cleanly.
Checking patch test/src/tree-sitter-tests.el...
Applied patch test/src/tree-sitter-tests.el cleanly.

index at:
100644 c61ad678d2da00c1991a8dc4f9e83e1d20a01108	test/src/tree-sitter-tests.el

(*) 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 public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).