aboutsummaryrefslogtreecommitdiffstats
path: root/test/src
diff options
context:
space:
mode:
authorAlan Mackenzie2020-10-01 20:35:40 +0000
committerAlan Mackenzie2020-10-01 20:35:40 +0000
commitda591df90ae95a6bc34f43b1594fc58a91967304 (patch)
treeea1d997f7a393626e25fc373435ab29c6218f798 /test/src
parent306fcc59dcd7d2a6ec8338dfff520a89d7a9121d (diff)
downloademacs-da591df90ae95a6bc34f43b1594fc58a91967304.tar.gz
emacs-da591df90ae95a6bc34f43b1594fc58a91967304.zip
Enhance syntax-tests.el to test some comment character handling.
* test/src/syntax-tests: Add a new section testing some aspects of comment handling in syntax.c. This needs further enhancement. It uses .... * test/data/syntax-comments.txt: A new test file.
Diffstat (limited to 'test/src')
-rw-r--r--test/src/syntax-tests.el190
1 files changed, 190 insertions, 0 deletions
diff --git a/test/src/syntax-tests.el b/test/src/syntax-tests.el
index 65c56b3b29d..4bd8a8519c0 100644
--- a/test/src/syntax-tests.el
+++ b/test/src/syntax-tests.el
@@ -82,4 +82,194 @@ also has open paren syntax (see Bug#24870)."
82 (should (equal (parse-partial-sexp pointC pointX nil nil ppsC) 82 (should (equal (parse-partial-sexp pointC pointX nil nil ppsC)
83 ppsX))))) 83 ppsX)))))
84 84
85
86;;; Commentary:
87;; The next bit tests the handling of comments in syntax.c, in
88;; particular the function `forward-comment'.
89
90;; It is intended to enhance this bit to test nested comments and also
91;; the interaction of `parse-partial-sexp' and `scan-lists' with
92;; comments (2020-10-01).
93
94;; This bit uses the data file test/data/syntax-comments.txt.
95
96(defun syntax-comments-point (n forw)
97 "Return the buffer offset corresponding to the \"label\" N.
98N is a decimal number which appears in the data file, usually
99twice, as \"labels\". It can also be a negative number or zero.
100FORW is t when we're using the label at BOL, nil for the one at EOL.
101
102If the label N doesn't exist in the current buffer, an exception
103is thrown.
104
105When FORW is t and N positive, we return the position after the
106first occurrence of label N at BOL in the data file. With FORW
107nil, we return the position before the last occurrence of the
108label at EOL in the data file.
109
110When N is negative, we return instead the position of the end of
111line that the -N label is on. When it is zero, we return POINT."
112 (if (zerop n)
113 (point)
114 (let ((str (format "%d" (abs n))))
115 (save-excursion
116 (if forw
117 (progn
118 (goto-char (point-min))
119 (re-search-forward
120 (concat "^\\(" str "\\)\\([^0-9\n]\\|$\\)"))
121 (if (< n 0)
122 (progn (end-of-line) (point))
123 (match-end 1)))
124 (goto-char (point-max))
125 (re-search-backward
126 (concat "\\(^\\|[^0-9]\\)\\(" str "\\)$"))
127 (if (< n 0)
128 (progn (end-of-line) (point))
129 (match-beginning 2)))))))
130
131(eval-and-compile
132 (defvar syntax-comments-section))
133
134(defmacro syntax-comments (-type- -dir- res start &optional stop)
135 "Create an ERT test to test (forward-comment 1/-1).
136The test uses a fixed name data file, which it visits. It calls
137entry and exit functions to set up and tear down syntax entries
138for comment characters. The test is given a name based on the
139global variable `syntax-comments-section', the direction of
140movement and the value of START.
141
142-TYPE- (unquoted) is a symbol from whose name the entry and exit
143function names are derived by appending \"-in\" and \"-out\".
144
145-DIR- (unquoted) is `forward' or `backward', the direction
146`forward-comment' is attempted.
147
148RES, t or nil, is the expected result from `forward-comment'.
149
150START and STOP are decimal numbers corresponding to labels in the
151data file marking the start and expected stop positions. See
152`syntax-comments-point' for a precise specification. If STOP is
153missing or nil, the value of START is assumed for it."
154 (declare (debug t))
155 (let ((forw
156 (cond
157 ((eq -dir- 'forward) t)
158 ((eq -dir- 'backward) nil)
159 (t (error "Invalid -dir- argument \"%s\" to `syntax-comments'" -dir-))))
160 (start-str (format "%d" (abs start)))
161 (type -type-)
162 )
163 `(ert-deftest ,(intern (concat "syntax-comments-"
164 syntax-comments-section
165 (if forw "-f" "-b") start-str))
166 ()
167 (with-current-buffer
168 (find-file
169 ,(expand-file-name "data/syntax-comments.txt"
170 (getenv "EMACS_TEST_DIRECTORY")))
171 (,(intern (concat (symbol-name type) "-in")))
172 (goto-char (syntax-comments-point ,start ,forw))
173 (let ((stop (syntax-comments-point ,(or stop start) ,(not forw))))
174 (should (eq (forward-comment ,(if forw 1 -1)) ,res))
175 (should (eq (point) stop)))
176 (,(intern (concat (symbol-name type) "-out")))))))
177
178;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
179;; "Pascal" style comments - single character delimiters, the closing
180;; delimiter not being newline.
181;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
182(defun {-in ()
183 (setq comment-end-can-be-escaped nil)
184 (modify-syntax-entry ?{ "<")
185 (modify-syntax-entry ?} ">"))
186(defun {-out ()
187 (modify-syntax-entry ?{ "(}")
188 (modify-syntax-entry ?} "){"))
189(eval-and-compile
190 (setq syntax-comments-section "pascal"))
191
192(syntax-comments { forward nil 20 0)
193(syntax-comments { backward nil 20 0)
194(syntax-comments { forward t 21)
195(syntax-comments { backward t 21)
196(syntax-comments { forward t 22)
197(syntax-comments { backward t 22)
198
199(syntax-comments { forward t 23)
200(syntax-comments { backward t 23)
201(syntax-comments { forward t 24)
202(syntax-comments { backward t 24)
203(syntax-comments { forward t 26)
204(syntax-comments { backward t 26)
205
206;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
207;; "Lisp" style comments - single character opening delimiters on line
208;; comments.
209;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
210(defun \;-in ()
211 (setq comment-end-can-be-escaped nil)
212 (modify-syntax-entry ?\n ">")
213 (modify-syntax-entry ?\; "<"))
214(defun \;-out ()
215 (modify-syntax-entry ?\n " ")
216 (modify-syntax-entry ?\; "."))
217(eval-and-compile
218 (setq syntax-comments-section "lisp"))
219
220(syntax-comments \; backward nil 30 30)
221(syntax-comments \; forward t 31)
222(syntax-comments \; backward t 31)
223(syntax-comments \; forward t 32)
224(syntax-comments \; backward t 32)
225(syntax-comments \; forward t 33)
226(syntax-comments \; backward t 33)
227
228;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
229;; Emacs 27 "C" style comments - `comment-end-can-be-escaped' is non-nil.
230;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
231(defun /*-in ()
232 (setq comment-end-can-be-escaped t)
233 (modify-syntax-entry ?/ ". 124b")
234 (modify-syntax-entry ?* ". 23")
235 (modify-syntax-entry ?\n "> b"))
236(defun /*-out ()
237 (setq comment-end-can-be-escaped nil)
238 (modify-syntax-entry ?/ ".")
239 (modify-syntax-entry ?* ".")
240 (modify-syntax-entry ?\n " "))
241(eval-and-compile
242 (setq syntax-comments-section "c"))
243
244(syntax-comments /* forward t 1)
245(syntax-comments /* backward t 1)
246(syntax-comments /* forward t 2)
247(syntax-comments /* backward t 2)
248(syntax-comments /* forward t 3)
249(syntax-comments /* backward t 3)
250
251(syntax-comments /* forward t 4)
252(syntax-comments /* backward t 4)
253(syntax-comments /* forward t 5 6)
254(syntax-comments /* backward nil 5 0)
255(syntax-comments /* forward nil 6 0)
256(syntax-comments /* backward t 6 5)
257
258(syntax-comments /* forward t 7 8)
259(syntax-comments /* backward nil 7 0)
260(syntax-comments /* forward nil 8 0)
261(syntax-comments /* backward t 8 7)
262(syntax-comments /* forward t 9)
263(syntax-comments /* backward t 9)
264
265(syntax-comments /* forward nil 10 0)
266(syntax-comments /* backward nil 10 0)
267(syntax-comments /* forward t 11)
268(syntax-comments /* backward t 11)
269
270(syntax-comments /* forward t 13 14)
271(syntax-comments /* backward nil 13 -14)
272(syntax-comments /* forward t 15)
273(syntax-comments /* backward t 15)
274
85;;; syntax-tests.el ends here 275;;; syntax-tests.el ends here