1 ;;; context-coloring-test.el --- Tests for context coloring -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2014-2015 Free Software Foundation, Inc.
5 ;; This file is part of GNU Emacs.
7 ;; This program is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation, either version 3 of the License, or
10 ;; (at your option) any later version.
12 ;; This program is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
22 ;; Tests for context coloring.
24 ;; Use with `make test'.
29 (require 'context-coloring)
34 ;;; Test running utilities
36 (defconst context-coloring-test-path
37 (file-name-directory (or load-file-name buffer-file-name))
38 "This file's directory.")
40 (defun context-coloring-test-read-file (path)
41 "Return the file's contents from PATH as a string."
43 (insert-file-contents (expand-file-name path context-coloring-test-path))
46 (defmacro context-coloring-test-with-fixture (fixture &rest body)
47 "With the relative FIXTURE, evaluate BODY in a temporary
51 (insert (context-coloring-test-read-file ,fixture))
55 ;;; Test defining utilities
57 (cl-defmacro context-coloring-test-define-deftest (name
61 &key enable-context-coloring-mode
64 "Define a deftest defmacro for tests prefixed with NAME. MODE
65 is called to set up tests' environments. EXTENSION denotes the
66 suffix for tests' fixture files. If NO-FIXTURE is non-nil, don't
67 use a fixture. If ENABLE-CONTEXT-COLORING-MODE is non-nil,
68 `context-coloring-mode' is activated before tests. Functions
69 BEFORE-EACH and AFTER-EACH run before the major mode is activated
70 before each test, and after each test, even if an error is
72 (declare (indent defun))
73 (let ((macro-name (intern (format "context-coloring-test-deftest%s"
75 ;; No name means no dash.
77 (t (format "-%s" name)))))))
78 `(cl-defmacro ,macro-name (name
83 (declare (indent defun))
84 ;; Commas in nested backquotes are not evaluated. Binding the variables
85 ;; here is probably the cleanest workaround.
87 (before-each ',before-each)
88 (after-each ',after-each)
89 (test-name (intern (format ,(format "%s-%%s"
92 (t "generic"))) name)))
94 (fixture (format "./fixtures/%s" fixture))
95 (,no-fixture "./fixtures/empty")
96 (t (format ,(format "./fixtures/%%s.%s" extension) name)))))
97 ,@`((let ((enable-context-coloring-mode ,enable-context-coloring-mode))
98 `(ert-deftest ,test-name ()
99 (context-coloring-test-with-fixture
101 (when ,before-each (funcall ,before-each))
103 (when ,before (funcall ,before))
104 (when ,enable-context-coloring-mode (context-coloring-mode))
108 (when ,after (funcall ,after))
109 (when ,after-each (funcall ,after-each)))))))))))
111 (context-coloring-test-define-deftest nil
112 :mode #'fundamental-mode
115 (context-coloring-test-define-deftest javascript
118 :enable-context-coloring-mode t
119 :before-each (lambda ()
120 (setq js2-mode-show-parse-errors nil)
121 (setq js2-mode-show-strict-warnings nil)))
123 (context-coloring-test-define-deftest emacs-lisp
124 :mode #'emacs-lisp-mode
126 :enable-context-coloring-mode t)
128 (context-coloring-test-define-deftest eval-expression
129 :mode #'fundamental-mode
133 ;;; Assertion functions
135 (defun context-coloring-test-get-last-message ()
136 "Get the last message in the current messages bufffer."
137 (let ((messages (split-string
138 (buffer-substring-no-properties
142 (car (nthcdr (- (length messages) 2) messages))))
144 (defun context-coloring-test-assert-message (expected buffer)
145 "Assert that message EXPECTED is at the end of BUFFER."
146 (when (null (get-buffer buffer))
150 "Expected buffer `%s' to have message \"%s\", "
151 "but the buffer did not have any messages.")
153 (with-current-buffer buffer
154 (let ((message (context-coloring-test-get-last-message)))
155 (when (not (equal message expected))
159 "Expected buffer `%s' to have message \"%s\", "
160 "but instead it was \"%s\"")
164 (defun context-coloring-test-assert-not-message (expected buffer)
165 "Assert that message EXPECTED is not at the end of BUFFER."
166 (when (get-buffer buffer)
167 (with-current-buffer buffer
168 (let ((message (context-coloring-test-get-last-message)))
169 (when (equal message expected)
173 "Expected buffer `%s' not to have message \"%s\", "
175 buffer expected)))))))
177 (defun context-coloring-test-assert-error (body error-message)
178 "Assert that BODY signals ERROR-MESSAGE."
179 (let ((error-signaled-p nil))
184 (setq error-signaled-p t)
185 (when (not (string-equal (cadr err) error-message))
186 (ert-fail (format (concat "Expected the error \"%s\" to be thrown, "
187 "but instead it was \"%s\".")
190 (when (not error-signaled-p)
191 (ert-fail "Expected an error to be thrown, but there wasn't."))))
194 ;;; Miscellaneous tests
196 (defmacro context-coloring-test-define-derived-mode (name)
197 "Define a derived mode exclusively for any test with NAME."
198 (let ((name (intern (format "context-coloring-test-%s-mode" name))))
199 `(define-derived-mode ,name fundamental-mode "Testing")))
201 (defvar context-coloring-test-caused-p nil
202 "Dumb flag tracking for lambdas inside old advice definitions
203 which don't seem to have lexical binding.")
205 (defmacro context-coloring-test-assert-causes-coloring (&rest body)
206 "Assert that BODY causes coloring."
208 ;; Gross, but I want this to pass on 24.3.
209 (ad-add-advice #'context-coloring-colorize
210 '(assert-causes-coloring
213 (setq context-coloring-test-caused-p t))))
216 (ad-activate #'context-coloring-colorize)
218 (when (not context-coloring-test-caused-p)
219 (ert-fail "Expected to have colorized, but it didn't."))))
221 (defun context-coloring-test-cleanup-assert-causes-coloring ()
222 (ad-unadvise #'context-coloring-colorize)
223 (setq context-coloring-test-caused-p nil))
225 (context-coloring-test-define-derived-mode mode-startup)
227 (context-coloring-test-deftest mode-startup
229 (context-coloring-define-dispatch
231 :modes '(context-coloring-test-mode-startup-mode)
233 (context-coloring-test-mode-startup-mode)
234 (context-coloring-test-assert-causes-coloring
235 (context-coloring-mode)))
237 (context-coloring-test-cleanup-assert-causes-coloring)))
239 (context-coloring-test-define-derived-mode change-detection)
241 (context-coloring-test-deftest change-detection
243 (context-coloring-define-dispatch
245 :modes '(context-coloring-test-change-detection-mode)
247 :setup #'context-coloring-setup-idle-change-detection
248 :teardown #'context-coloring-teardown-idle-change-detection)
249 (context-coloring-test-change-detection-mode)
250 (context-coloring-mode)
251 (context-coloring-test-assert-causes-coloring
253 ;; Simply cannot figure out how to trigger an idle timer; would much rather
254 ;; test that. But (current-idle-time) always returns nil in these tests.
255 (context-coloring-maybe-colorize-with-buffer (current-buffer))))
257 (context-coloring-test-cleanup-assert-causes-coloring)))
259 (context-coloring-test-deftest unsupported-mode
261 (context-coloring-mode)
262 (context-coloring-test-assert-message
263 "Context coloring is not available for this major mode"
266 (context-coloring-test-deftest derived-mode
268 (lisp-interaction-mode)
269 (context-coloring-mode)
270 (context-coloring-test-assert-not-message
271 "Context coloring is not available for this major mode"
274 (context-coloring-test-define-derived-mode define-dispatch-error)
276 (context-coloring-test-deftest define-dispatch-error
278 (context-coloring-test-assert-error
280 (context-coloring-define-dispatch
281 'define-dispatch-no-modes))
282 "No mode or predicate defined for dispatch")
283 (context-coloring-test-assert-error
285 (context-coloring-define-dispatch
286 'define-dispatch-no-strategy
287 :modes '(context-coloring-test-define-dispatch-error-mode)))
288 "No colorizer defined for dispatch")))
290 (context-coloring-test-define-derived-mode disable-mode)
292 (context-coloring-test-deftest disable-mode
295 (context-coloring-define-dispatch
297 :modes '(context-coloring-test-disable-mode-mode)
301 (context-coloring-test-disable-mode-mode)
302 (context-coloring-mode)
303 (context-coloring-mode -1)
304 (when (not torn-down)
305 (ert-fail "Expected teardown function to have been called, but it wasn't.")))))
310 (defun context-coloring-test-assert-position-level (position level)
311 "Assert that POSITION has LEVEL."
312 (let ((face (get-text-property position 'face))
315 (let* ((face-string (symbol-name face))
316 (matches (string-match
317 "context-coloring-level-\\([[:digit:]]+\\)-face"
320 (setq actual-level (string-to-number
321 (substring face-string
324 (= level actual-level)))))
325 (ert-fail (format (concat "Expected level at position %s, "
326 "which is \"%s\", to be %s; "
329 (buffer-substring-no-properties position (1+ position)) level
332 (defun context-coloring-test-assert-position-face (position face-regexp)
333 "Assert that the face at POSITION satisfies FACE-REGEXP."
334 (let ((face (get-text-property position 'face)))
336 ;; Pass a non-string to do an `equal' check (against a symbol or nil).
337 (unless (stringp face-regexp)
338 (not (equal face-regexp face)))
339 ;; Otherwise do the matching.
340 (when (stringp face-regexp)
341 (not (string-match-p face-regexp (symbol-name face)))))
342 (ert-fail (format (concat "Expected face at position %s, "
343 "which is \"%s\", to be %s; "
346 (buffer-substring-no-properties position (1+ position)) face-regexp
349 (defun context-coloring-test-assert-position-comment (position)
350 "Assert that the face at POSITION is a comment."
351 (context-coloring-test-assert-position-face
352 position "\\`font-lock-comment\\(-delimiter\\)?-face\\'"))
354 (defun context-coloring-test-assert-position-constant-comment (position)
355 "Assert that the face at POSITION is a constant comment."
356 (context-coloring-test-assert-position-face position '(font-lock-constant-face
357 font-lock-comment-face)))
359 (defun context-coloring-test-assert-position-string (position)
360 "Assert that the face at POSITION is a string."
361 (context-coloring-test-assert-position-face position 'font-lock-string-face))
363 (defun context-coloring-test-assert-position-nil (position)
364 "Assert that the face at POSITION is nil."
365 (context-coloring-test-assert-position-face position nil))
367 (defun context-coloring-test-assert-coloring (map)
368 "Assert that the current buffer's coloring will match MAP.
370 MAP's newlines should correspond to the current fixture.
372 The following characters appearing in MAP assert coloring for
373 corresponding points in the fixture:
375 0-9: Level equals number.
376 C: Face is constant comment.
381 Any other characters are discarded. Characters \"x\" and any
382 other non-letters are guaranteed to always be discarded."
383 ;; Omit the superfluous, formatting-related leading newline. Can't use
384 ;; `save-excursion' here because if an assertion fails it will cause future
385 ;; tests to get messed up.
386 (goto-char (point-min))
387 (let* ((map (substring map 1))
391 (while (< index (length map))
392 (setq char-string (substring map index (1+ index)))
393 (setq char (string-to-char char-string))
402 (context-coloring-test-assert-position-level
403 (point) (string-to-number char-string))
405 ;; 'C' = Constant comment
407 (context-coloring-test-assert-position-constant-comment (point))
411 (context-coloring-test-assert-position-comment (point))
415 (context-coloring-test-assert-position-nil (point))
419 (context-coloring-test-assert-position-string (point))
423 (setq index (1+ index)))))
425 (context-coloring-test-deftest-javascript function-scopes
427 (context-coloring-test-assert-coloring "
428 000 0 0 11111111 11 110
430 111 1 1 22222222 22 221
434 (context-coloring-test-deftest-javascript global
436 (context-coloring-test-assert-coloring "
438 111 1 1 00000001xxx11
441 (context-coloring-test-deftest-javascript block-scopes
443 (context-coloring-test-assert-coloring "
451 (setq context-coloring-javascript-block-scopes t))
453 (setq context-coloring-javascript-block-scopes nil)))
455 (context-coloring-test-deftest-javascript catch
457 (context-coloring-test-assert-coloring "
467 (context-coloring-test-deftest-javascript key-names
469 (context-coloring-test-assert-coloring "
477 (context-coloring-test-deftest-javascript property-lookup
479 (context-coloring-test-assert-coloring "
486 (context-coloring-test-deftest-javascript key-values
488 (context-coloring-test-assert-coloring "
498 (context-coloring-test-deftest-javascript syntactic-comments-and-strings
500 (context-coloring-test-assert-coloring "
505 :fixture "comments-and-strings.js")
507 (context-coloring-test-deftest-javascript syntactic-comments
509 (context-coloring-test-assert-coloring "
514 :fixture "comments-and-strings.js"
516 (setq context-coloring-syntactic-strings nil))
518 (setq context-coloring-syntactic-strings t)))
520 (context-coloring-test-deftest-javascript syntactic-strings
522 (context-coloring-test-assert-coloring "
527 :fixture "comments-and-strings.js"
529 (setq context-coloring-syntactic-comments nil))
531 (setq context-coloring-syntactic-comments t)))
533 (context-coloring-test-deftest-javascript unterminated-comment
534 ;; As long as `add-text-properties' doesn't signal an error, this test passes.
537 (context-coloring-test-deftest-emacs-lisp defun
539 (context-coloring-test-assert-coloring "
540 111111 000 1111 111 111111111 1111
541 11 111 111 111 000011
549 (context-coloring-test-deftest-emacs-lisp defadvice
551 (context-coloring-test-assert-coloring "
552 1111111111 0 1111111 111111 11111 111 111111111
556 (context-coloring-test-deftest-emacs-lisp lambda
558 (context-coloring-test-assert-coloring "
559 00000000 1111111 1111
560 11111111 11 2222222 2222
561 222 22 12 2221 111 0 00")))
563 (context-coloring-test-deftest-emacs-lisp quote
565 (context-coloring-test-assert-coloring "
566 (xxxxx 0000000 00 00000)
567 (xxx () (xxxxxxxxx (,0000)))
572 111111 1 1111111111 11 111 1 111 1 00001 10000 11 00001 1 10000
580 (xxxxxx () 111111 11111)")))
582 (context-coloring-test-deftest-emacs-lisp splice
584 (context-coloring-test-assert-coloring "
586 111111 00001 100001)")))
588 (context-coloring-test-deftest-emacs-lisp comment
590 ;; Just check that the comment isn't parsed syntactically.
591 (context-coloring-test-assert-coloring "
593 (xx (x xxxxx-xxxx xx) cccccccccc
594 11 00000-0000 11))) cccccccccc")))
596 (context-coloring-test-deftest-emacs-lisp string
598 (context-coloring-test-assert-coloring "
600 (xxxxxx x x sss 1 0 sssss 0 1 sssssss11")))
602 (context-coloring-test-deftest-emacs-lisp ignored
604 (context-coloring-test-assert-coloring "
606 (x x 1 11 11 111 111 11 11 11 1 111 (1 1 1)))")))
608 (context-coloring-test-deftest-emacs-lisp sexp
610 (context-coloring-test-assert-coloring "
616 (context-coloring-test-deftest-emacs-lisp let
618 (context-coloring-test-assert-coloring "
631 (context-coloring-test-deftest-emacs-lisp let*
633 (context-coloring-test-assert-coloring "
643 2222 1 1 2 2 2 000022
644 1111 1 1 1 0 0 000011")))
646 (context-coloring-test-deftest-emacs-lisp cond
648 (context-coloring-test-assert-coloring "
658 (context-coloring-test-deftest-emacs-lisp condition-case
660 (context-coloring-test-assert-coloring "
664 1111111 111111 111 000011
666 (111111111-1111-111111-11111 111
672 (context-coloring-test-deftest-emacs-lisp dolist
674 (context-coloring-test-assert-coloring "
676 2222222 2222 1111 2222222
677 3333333 33 33 222 1111 2222223321")))
679 (defun context-coloring-test-insert-unread-space ()
680 "Simulate the insertion of a space as if by a user."
681 (setq unread-command-events (cons '(t . 32)
682 unread-command-events)))
684 (defun context-coloring-test-remove-faces ()
685 "Remove all faces in the current buffer."
686 (remove-text-properties (point-min) (point-max) '(face nil)))
688 (context-coloring-test-deftest-emacs-lisp iteration
690 (let ((context-coloring-elisp-sexps-per-pause 2))
691 (context-coloring-colorize)
692 (context-coloring-test-assert-coloring "
695 (context-coloring-test-remove-faces)
696 (context-coloring-test-insert-unread-space)
697 (context-coloring-colorize)
698 ;; Coloring is interrupted after the first "sexp" (the comment in this
700 (context-coloring-test-assert-coloring "
704 (context-coloring-test-deftest-emacs-lisp changed
706 (context-coloring-test-remove-faces)
708 (goto-char (point-min))
709 (forward-line (1- 3))
711 ;; Mock `pos-visible-in-window-p' because in batch mode `get-buffer-window'
712 ;; returns nil. Emacs must not have a window in that environment.
713 (cl-letf (((symbol-function 'pos-visible-in-window-p)
717 ;; First and third calls start from center. Second and
718 ;; fourth calls are made immediately after moving past
719 ;; the first defun in either direction "off screen".
725 (setq calls (1+ calls)))))))
726 (context-coloring-colorize))
727 (context-coloring-test-assert-coloring "
732 nnnnn n nnn nnnnnnnn")))
734 (context-coloring-test-deftest-emacs-lisp unbalanced-parenthesis
736 (context-coloring-test-assert-coloring "
740 (context-coloring-test-deftest-eval-expression let
742 (minibuffer-with-setup-hook
744 ;; Perform the test in a hook as it's the only way I know of examining
745 ;; the minibuffer's contents. The contents are implicitly submitted,
746 ;; so we have to ignore the errors in the arbitrary test subject code.
747 (insert "(ignore-errors (let (a) (message a free)))")
748 (context-coloring-colorize)
749 (context-coloring-test-assert-coloring "
750 xxxx: 0000000-000000 1111 111 11111111 1 0000110"))
751 ;; Simulate user input because `call-interactively' is blocking and
752 ;; doesn't seem to run the hook.
755 [?\C-u] ;; Don't output the result of the arbitrary test subject code.
758 (provide 'context-coloring-test)
760 ;;; context-coloring-test.el ends here