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 relative FIXTURE, evaluate BODY in a temporary buffer."
50 (insert (context-coloring-test-read-file ,fixture))
54 ;;; Test defining utilities
56 (cl-defmacro context-coloring-test-define-deftest (name
60 &key enable-context-coloring-mode
63 "Define a deftest defmacro for tests prefixed with NAME. MODE
64 is called to set up tests' environments. EXTENSION denotes the
65 suffix for tests' fixture files. If NO-FIXTURE is non-nil, don't
66 use a fixture. If ENABLE-CONTEXT-COLORING-MODE is non-nil,
67 `context-coloring-mode' is activated before tests. Functions
68 BEFORE-EACH and AFTER-EACH run before the major mode is activated
69 before each test, and after each test, even if an error is
71 (declare (indent defun))
72 (let ((macro-name (intern (format "context-coloring-test-deftest%s"
74 ;; No name means no dash.
76 (t (format "-%s" name)))))))
77 `(cl-defmacro ,macro-name (name
82 (declare (indent defun))
83 ;; Commas in nested backquotes are not evaluated. Binding the variables
84 ;; here is probably the cleanest workaround.
86 (before-each ',before-each)
87 (after-each ',after-each)
88 (test-name (intern (format ,(format "%s-%%s"
91 (t "generic"))) name)))
93 (fixture (format "./fixtures/%s" fixture))
94 (,no-fixture "./fixtures/empty")
95 (t (format ,(format "./fixtures/%%s.%s" extension) name)))))
96 ,@`((let ((enable-context-coloring-mode ,enable-context-coloring-mode))
97 `(ert-deftest ,test-name ()
98 (context-coloring-test-with-fixture
100 (when ,before-each (funcall ,before-each))
102 (when ,before (funcall ,before))
103 (when ,enable-context-coloring-mode (context-coloring-mode))
107 (when ,after (funcall ,after))
108 (when ,after-each (funcall ,after-each)))))))))))
110 (context-coloring-test-define-deftest nil
111 :mode #'fundamental-mode
114 (context-coloring-test-define-deftest javascript
117 :enable-context-coloring-mode t
118 :before-each (lambda ()
119 (setq js2-mode-show-parse-errors nil)
120 (setq js2-mode-show-strict-warnings nil)))
122 (context-coloring-test-define-deftest emacs-lisp
123 :mode #'emacs-lisp-mode
125 :enable-context-coloring-mode t)
127 (context-coloring-test-define-deftest eval-expression
128 :mode #'fundamental-mode
132 ;;; Assertion functions
134 (defun context-coloring-test-get-last-message ()
135 "Get the last message in the current messages bufffer."
136 (let ((messages (split-string
137 (buffer-substring-no-properties
141 (car (nthcdr (- (length messages) 2) messages))))
143 (defun context-coloring-test-assert-message (expected buffer)
144 "Assert that message EXPECTED is at the end of BUFFER."
145 (when (null (get-buffer buffer))
149 "Expected buffer `%s' to have message \"%s\", "
150 "but the buffer did not have any messages.")
152 (with-current-buffer buffer
153 (let ((message (context-coloring-test-get-last-message)))
154 (when (not (equal message expected))
158 "Expected buffer `%s' to have message \"%s\", "
159 "but instead it was \"%s\"")
163 (defun context-coloring-test-assert-not-message (expected buffer)
164 "Assert that message EXPECTED is not at the end of BUFFER."
165 (when (get-buffer buffer)
166 (with-current-buffer buffer
167 (let ((message (context-coloring-test-get-last-message)))
168 (when (equal message expected)
172 "Expected buffer `%s' not to have message \"%s\", "
174 buffer expected)))))))
176 (defun context-coloring-test-assert-error (body error-message)
177 "Assert that BODY signals ERROR-MESSAGE."
178 (let ((error-signaled-p nil))
183 (setq error-signaled-p t)
184 (when (not (string-equal (cadr err) error-message))
185 (ert-fail (format (concat "Expected the error \"%s\" to be thrown, "
186 "but instead it was \"%s\".")
189 (when (not error-signaled-p)
190 (ert-fail "Expected an error to be thrown, but there wasn't."))))
193 ;;; Miscellaneous tests
195 (defmacro context-coloring-test-define-derived-mode (name)
196 "Define a derived mode exclusively for any test with NAME."
197 (let ((name (intern (format "context-coloring-test-%s-mode" name))))
198 `(define-derived-mode ,name fundamental-mode "Testing")))
200 (defvar context-coloring-test-caused-p nil
201 "If non-nil, coloring was caused.")
203 (defmacro context-coloring-test-assert-causes-coloring (&rest body)
204 "Assert that BODY causes coloring."
206 ;; Gross, but I want this to pass on 24.3.
207 (ad-add-advice #'context-coloring-colorize
208 '(assert-causes-coloring
211 (setq context-coloring-test-caused-p t))))
214 (ad-activate #'context-coloring-colorize)
216 (when (not context-coloring-test-caused-p)
217 (ert-fail "Expected to have colorized, but it didn't."))))
219 (defun context-coloring-test-cleanup-assert-causes-coloring ()
220 "Undo `context-coloring-test-assert-causes-coloring'."
221 (ad-unadvise #'context-coloring-colorize)
222 (setq context-coloring-test-caused-p nil))
224 (context-coloring-test-define-derived-mode mode-startup)
226 (context-coloring-test-deftest mode-startup
228 (context-coloring-define-dispatch
230 :modes '(context-coloring-test-mode-startup-mode)
232 (context-coloring-test-mode-startup-mode)
233 (context-coloring-test-assert-causes-coloring
234 (context-coloring-mode)))
236 (context-coloring-test-cleanup-assert-causes-coloring)))
238 (context-coloring-test-define-derived-mode change-detection)
240 (context-coloring-test-deftest change-detection
242 (context-coloring-define-dispatch
244 :modes '(context-coloring-test-change-detection-mode)
246 :setup #'context-coloring-setup-idle-change-detection
247 :teardown #'context-coloring-teardown-idle-change-detection)
248 (context-coloring-test-change-detection-mode)
249 (context-coloring-mode)
250 (context-coloring-test-assert-causes-coloring
252 ;; Simply cannot figure out how to trigger an idle timer; would much rather
253 ;; test that. But (current-idle-time) always returns nil in these tests.
254 (context-coloring-maybe-colorize-with-buffer (current-buffer))))
256 (context-coloring-test-cleanup-assert-causes-coloring)))
258 (context-coloring-test-deftest unsupported-mode
260 (context-coloring-mode)
261 (context-coloring-test-assert-message
262 "Context coloring is unavailable here"
265 (context-coloring-test-deftest derived-mode
267 (lisp-interaction-mode)
268 (context-coloring-mode)
269 (context-coloring-test-assert-not-message
270 "Context coloring is unavailable here"
273 (context-coloring-test-deftest unavailable-message-ignored
275 (minibuffer-with-setup-hook
277 (context-coloring-mode)
278 (context-coloring-test-assert-not-message
279 "Context coloring is unavailable here"
286 (context-coloring-test-define-derived-mode define-dispatch-error)
288 (context-coloring-test-deftest define-dispatch-error
290 (context-coloring-test-assert-error
292 (context-coloring-define-dispatch
293 'define-dispatch-no-modes))
294 "No mode or predicate defined for dispatch")
295 (context-coloring-test-assert-error
297 (context-coloring-define-dispatch
298 'define-dispatch-no-strategy
299 :modes '(context-coloring-test-define-dispatch-error-mode)))
300 "No colorizer defined for dispatch")))
302 (context-coloring-test-define-derived-mode disable-mode)
304 (context-coloring-test-deftest disable-mode
307 (context-coloring-define-dispatch
309 :modes '(context-coloring-test-disable-mode-mode)
313 (context-coloring-test-disable-mode-mode)
314 (context-coloring-mode)
315 (context-coloring-mode -1)
316 (when (not torn-down)
317 (ert-fail "Expected teardown function to have been called, but it wasn't.")))))
319 (defun context-coloring-test-assert-maximum-face (expected)
320 "Assert that `context-coloring-maximum-face' is EXPECTED."
321 (when (not (= context-coloring-maximum-face expected))
322 (ert-fail (format "Expected maximum face to be %s, but it was %s"
323 expected context-coloring-maximum-face))))
325 (deftheme context-coloring-test-custom-theme)
327 (context-coloring-test-define-derived-mode custom-theme)
329 (context-coloring-test-deftest custom-theme
331 (custom-theme-set-faces
332 'context-coloring-test-custom-theme
333 '(context-coloring-level-0-face ((t :foreground "#aaaaaa")))
334 '(context-coloring-level-1-face ((t :foreground "#bbbbbb"))))
336 '(context-coloring-level-0-face ((t :foreground "#aaaaaa"))))
337 (enable-theme 'context-coloring-test-custom-theme)
338 (context-coloring-define-dispatch
340 :modes '(context-coloring-test-custom-theme-mode)
342 (context-coloring-test-custom-theme-mode)
343 (context-coloring-colorize)
344 (context-coloring-test-assert-maximum-face 1)
345 ;; This theme should now be ignored in favor of the `user' theme.
346 (custom-theme-reset-faces
347 'context-coloring-test-custom-theme
348 '(context-coloring-level-0-face nil)
349 '(context-coloring-level-1-face nil))
350 (context-coloring-colorize)
351 ;; Maximum face for `user'.
352 (context-coloring-test-assert-maximum-face 0)
353 ;; Now `user' should be ignored too.
355 '(context-coloring-level-0-face nil))
356 (context-coloring-colorize)
357 ;; Expect the package's defaults.
358 (context-coloring-test-assert-maximum-face
359 context-coloring-default-maximum-face))
362 '(context-coloring-level-0-face nil))
363 (disable-theme 'context-coloring-test-custom-theme)))
368 (defun context-coloring-test-assert-position-level (position level)
369 "Assert that POSITION has LEVEL."
370 (let ((face (get-text-property position 'face))
373 (let* ((face-string (symbol-name face))
374 (matches (string-match
375 context-coloring-level-face-regexp
378 (setq actual-level (string-to-number
379 (substring face-string
382 (= level actual-level)))))
383 (ert-fail (format (concat "Expected level at position %s, "
384 "which is \"%s\", to be %s; "
387 (buffer-substring-no-properties position (1+ position)) level
390 (defun context-coloring-test-assert-position-face (position face-regexp)
391 "Assert that the face at POSITION satisfies FACE-REGEXP."
392 (let ((face (get-text-property position 'face)))
394 ;; Pass a non-string to do an `equal' check (against a symbol or nil).
395 (unless (stringp face-regexp)
396 (not (equal face-regexp face)))
397 ;; Otherwise do the matching.
398 (when (stringp face-regexp)
399 (not (string-match-p face-regexp (symbol-name face)))))
400 (ert-fail (format (concat "Expected face at position %s, "
401 "which is \"%s\", to be %s; "
404 (buffer-substring-no-properties position (1+ position)) face-regexp
407 (defun context-coloring-test-assert-position-comment (position)
408 "Assert that the face at POSITION is a comment."
409 (context-coloring-test-assert-position-face
410 position "\\`font-lock-comment\\(-delimiter\\)?-face\\'"))
412 (defun context-coloring-test-assert-position-constant-comment (position)
413 "Assert that the face at POSITION is a constant comment."
414 (context-coloring-test-assert-position-face position '(font-lock-constant-face
415 font-lock-comment-face)))
417 (defun context-coloring-test-assert-position-string (position)
418 "Assert that the face at POSITION is a string."
419 (context-coloring-test-assert-position-face position 'font-lock-string-face))
421 (defun context-coloring-test-assert-position-nil (position)
422 "Assert that the face at POSITION is nil."
423 (context-coloring-test-assert-position-face position nil))
425 (defun context-coloring-test-assert-coloring (map)
426 "Assert that the current buffer's coloring will match MAP.
428 MAP's newlines should correspond to the current fixture.
430 The following characters appearing in MAP assert coloring for
431 corresponding points in the fixture:
433 0-9: Level equals number.
434 C: Face is constant comment.
439 Any other characters are discarded. Characters \"x\" and any
440 other non-letters are guaranteed to always be discarded."
441 ;; Omit the superfluous, formatting-related leading newline. Can't use
442 ;; `save-excursion' here because if an assertion fails it will cause future
443 ;; tests to get messed up.
444 (goto-char (point-min))
445 (let* ((map (substring map 1))
449 (while (< index (length map))
450 (setq char-string (substring map index (1+ index)))
451 (setq char (string-to-char char-string))
460 (context-coloring-test-assert-position-level
461 (point) (string-to-number char-string))
463 ;; 'C' = Constant comment
465 (context-coloring-test-assert-position-constant-comment (point))
469 (context-coloring-test-assert-position-comment (point))
473 (context-coloring-test-assert-position-nil (point))
477 (context-coloring-test-assert-position-string (point))
481 (setq index (1+ index)))))
483 (context-coloring-test-deftest-javascript function-scopes
485 (context-coloring-test-assert-coloring "
486 000 0 0 11111111 11 110
488 111 1 1 22222222 22 221
492 (context-coloring-test-deftest-javascript global
494 (context-coloring-test-assert-coloring "
496 111 1 1 00000001xxx11
499 (context-coloring-test-deftest-javascript block-scopes
501 (context-coloring-test-assert-coloring "
509 (setq context-coloring-javascript-block-scopes t))
511 (setq context-coloring-javascript-block-scopes nil)))
513 (context-coloring-test-deftest-javascript catch
515 (context-coloring-test-assert-coloring "
525 (context-coloring-test-deftest-javascript key-names
527 (context-coloring-test-assert-coloring "
535 (context-coloring-test-deftest-javascript property-lookup
537 (context-coloring-test-assert-coloring "
544 (context-coloring-test-deftest-javascript key-values
546 (context-coloring-test-assert-coloring "
556 (context-coloring-test-deftest-javascript syntactic-comments-and-strings
558 (context-coloring-test-assert-coloring "
563 :fixture "comments-and-strings.js")
565 (context-coloring-test-deftest-javascript syntactic-comments
567 (context-coloring-test-assert-coloring "
572 :fixture "comments-and-strings.js"
574 (setq context-coloring-syntactic-strings nil))
576 (setq context-coloring-syntactic-strings t)))
578 (context-coloring-test-deftest-javascript syntactic-strings
580 (context-coloring-test-assert-coloring "
585 :fixture "comments-and-strings.js"
587 (setq context-coloring-syntactic-comments nil))
589 (setq context-coloring-syntactic-comments t)))
591 (context-coloring-test-deftest-javascript unterminated-comment
592 ;; As long as `add-text-properties' doesn't signal an error, this test passes.
595 (context-coloring-test-deftest-emacs-lisp defun
597 (context-coloring-test-assert-coloring "
598 111111 000 1111 111 111111111 1111
599 11 111 111 111 000011
607 (context-coloring-test-deftest-emacs-lisp defadvice
609 (context-coloring-test-assert-coloring "
610 1111111111 0 1111111 111111 11111 111 111111111
614 (context-coloring-test-deftest-emacs-lisp lambda
616 (context-coloring-test-assert-coloring "
617 00000000 1111111 1111
618 11111111 11 2222222 2222
619 222 22 12 2221 111 0 00")))
621 (context-coloring-test-deftest-emacs-lisp quote
623 (context-coloring-test-assert-coloring "
624 (xxxxx 0000000 00 00000)
625 (xxx () (xxxxxxxxx (,0000)))
630 111111 1 1111111111 11 111 1 111 1 00001 10000 11 00001 1 10000
638 (xxxxxx () 111111 11111)")))
640 (context-coloring-test-deftest-emacs-lisp splice
642 (context-coloring-test-assert-coloring "
644 111111 00001 100001)")))
646 (context-coloring-test-deftest-emacs-lisp comment
648 ;; Just check that the comment isn't parsed syntactically.
649 (context-coloring-test-assert-coloring "
651 (xx (x xxxxx-xxxx xx) cccccccccc
652 11 00000-0000 11))) cccccccccc")))
654 (context-coloring-test-deftest-emacs-lisp string
656 (context-coloring-test-assert-coloring "
658 (xxxxxx x x sss 1 0 sssss 0 1 sssssss11")))
660 (context-coloring-test-deftest-emacs-lisp ignored
662 (context-coloring-test-assert-coloring "
664 (x x 1 11 11 111 111 11 11 11 1 111 (1 1 1)))")))
666 (context-coloring-test-deftest-emacs-lisp sexp
668 (context-coloring-test-assert-coloring "
674 (context-coloring-test-deftest-emacs-lisp let
676 (context-coloring-test-assert-coloring "
689 (context-coloring-test-deftest-emacs-lisp let*
691 (context-coloring-test-assert-coloring "
701 2222 1 1 2 2 2 000022
702 1111 1 1 1 0 0 000011")))
704 (context-coloring-test-deftest-emacs-lisp cond
706 (context-coloring-test-assert-coloring "
716 (context-coloring-test-deftest-emacs-lisp condition-case
718 (context-coloring-test-assert-coloring "
722 1111111 111111 111 000011
724 (111111111-1111-111111-11111 111
730 (context-coloring-test-deftest-emacs-lisp dolist
732 (context-coloring-test-assert-coloring "
734 2222222 2222 1111 2222222
735 3333333 33 33 222 1111 2222223321")))
737 (defun context-coloring-test-insert-unread-space ()
738 "Simulate the insertion of a space as if by a user."
739 (setq unread-command-events (cons '(t . 32)
740 unread-command-events)))
742 (defun context-coloring-test-remove-faces ()
743 "Remove all faces in the current buffer."
744 (remove-text-properties (point-min) (point-max) '(face nil)))
746 (context-coloring-test-deftest-emacs-lisp iteration
748 (let ((context-coloring-elisp-sexps-per-pause 2))
749 (context-coloring-colorize)
750 (context-coloring-test-assert-coloring "
753 (context-coloring-test-remove-faces)
754 (context-coloring-test-insert-unread-space)
755 (context-coloring-colorize)
756 ;; Coloring is interrupted after the first "sexp" (the comment in this
758 (context-coloring-test-assert-coloring "
762 (context-coloring-test-deftest-emacs-lisp changed
764 (context-coloring-test-remove-faces)
766 (goto-char (point-min))
767 (forward-line (1- 3))
769 ;; Mock `pos-visible-in-window-p' because in batch mode `get-buffer-window'
770 ;; returns nil. Emacs must not have a window in that environment.
771 (cl-letf (((symbol-function 'pos-visible-in-window-p)
775 ;; First and third calls start from center. Second and
776 ;; fourth calls are made immediately after moving past
777 ;; the first defun in either direction "off screen".
783 (setq calls (1+ calls)))))))
784 (context-coloring-colorize))
785 (context-coloring-test-assert-coloring "
790 nnnnn n nnn nnnnnnnn")))
792 (context-coloring-test-deftest-emacs-lisp unbalanced-parenthesis
794 (context-coloring-test-assert-coloring "
798 (context-coloring-test-deftest-eval-expression let
800 (minibuffer-with-setup-hook
802 ;; Perform the test in a hook as it's the only way I know of examining
803 ;; the minibuffer's contents. The contents are implicitly submitted,
804 ;; so we have to ignore the errors in the arbitrary test subject code.
805 (insert "(ignore-errors (let (a) (message a free)))")
806 (context-coloring-colorize)
807 (context-coloring-test-assert-coloring "
808 xxxx: 0000000-000000 1111 111 11111111 1 0000110"))
809 ;; Simulate user input because `call-interactively' is blocking and
810 ;; doesn't seem to run the hook.
813 [?\C-u] ;; Don't output the result of the arbitrary test subject code.
816 (provide 'context-coloring-test)
818 ;;; context-coloring-test.el ends here