1 ;;; context-coloring-test.el --- Tests for context coloring -*- lexical-binding: t; no-byte-compile: t; -*-
3 ;; Copyright (C) 2014-2016 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)
30 (require 'context-coloring-javascript)
31 (require 'context-coloring-emacs-lisp)
35 ;;; Test running utilities
37 (defconst context-coloring-test-path
38 (file-name-directory (or load-file-name buffer-file-name))
39 "This file's directory.")
41 (defun context-coloring-test-read-file (path)
42 "Return the file's contents from PATH as a string."
44 (insert-file-contents (expand-file-name path context-coloring-test-path))
47 (defmacro context-coloring-test-with-fixture (fixture &rest body)
48 "With relative FIXTURE, evaluate BODY in a temporary buffer."
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/test/%s" fixture))
95 (,no-fixture "./fixtures/test/empty")
96 (t (format ,(format "./fixtures/test/%%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 (defun context-coloring-test-js2-mode ()
116 "Enable js2-mode and parse synchronously."
120 (context-coloring-test-define-deftest javascript
121 :mode #'context-coloring-test-js2-mode
123 :enable-context-coloring-mode t
124 :before-each (lambda ()
125 (setq js2-mode-show-parse-errors nil)
126 (setq js2-mode-show-strict-warnings nil)))
128 (context-coloring-test-define-deftest emacs-lisp
129 :mode #'emacs-lisp-mode
131 :enable-context-coloring-mode t)
133 (context-coloring-test-define-deftest eval-expression
134 :mode #'fundamental-mode
138 ;;; Assertion functions
140 (defun context-coloring-test-get-last-message ()
141 "Get the last message in the current messages bufffer."
142 (let ((messages (split-string
143 (buffer-substring-no-properties
147 (car (nthcdr (- (length messages) 2) messages))))
149 (defun context-coloring-test-assert-message (expected buffer)
150 "Assert that message EXPECTED is at the end of BUFFER."
151 (when (null (get-buffer buffer))
155 "Expected buffer `%s' to have message \"%s\", "
156 "but the buffer did not have any messages.")
158 (with-current-buffer buffer
159 (let ((message (context-coloring-test-get-last-message)))
160 (when (not (equal message expected))
164 "Expected buffer `%s' to have message \"%s\", "
165 "but instead it was \"%s\"")
169 (defun context-coloring-test-assert-not-message (expected buffer)
170 "Assert that message EXPECTED is not at the end of BUFFER."
171 (when (get-buffer buffer)
172 (with-current-buffer buffer
173 (let ((message (context-coloring-test-get-last-message)))
174 (when (equal message expected)
178 "Expected buffer `%s' not to have message \"%s\", "
180 buffer expected)))))))
182 (defun context-coloring-test-assert-error (body error-message)
183 "Assert that BODY signals ERROR-MESSAGE."
184 (let ((error-signaled-p nil))
189 (setq error-signaled-p t)
190 (when (not (string-equal (cadr err) error-message))
191 (ert-fail (format (concat "Expected the error \"%s\" to be thrown, "
192 "but instead it was \"%s\".")
195 (when (not error-signaled-p)
196 (ert-fail "Expected an error to be thrown, but there wasn't."))))
199 ;;; Miscellaneous tests
201 (defmacro context-coloring-test-define-derived-mode (name)
202 "Define a derived mode exclusively for any test with NAME."
203 (let ((name (intern (format "context-coloring-test-%s-mode" name))))
204 `(define-derived-mode ,name fundamental-mode "Testing")))
206 (defvar context-coloring-test-caused-p nil
207 "If non-nil, coloring was caused.")
209 (defmacro context-coloring-test-assert-causes-coloring (&rest body)
210 "Assert that BODY causes coloring."
212 ;; Gross, but I want this to pass on 24.3.
213 (ad-add-advice #'context-coloring-colorize
214 '(assert-causes-coloring
217 (setq context-coloring-test-caused-p t))))
220 (ad-activate #'context-coloring-colorize)
222 (when (not context-coloring-test-caused-p)
223 (ert-fail "Expected to have colorized, but it didn't."))))
225 (defun context-coloring-test-cleanup-assert-causes-coloring ()
226 "Undo `context-coloring-test-assert-causes-coloring'."
227 (ad-unadvise #'context-coloring-colorize)
228 (setq context-coloring-test-caused-p nil))
230 (context-coloring-test-define-derived-mode mode-startup)
232 (context-coloring-test-deftest mode-startup
236 (list :modes '(context-coloring-test-mode-startup-mode))
237 context-coloring-dispatch-hash-table)
238 (context-coloring-test-mode-startup-mode)
239 (context-coloring-test-assert-causes-coloring
240 (context-coloring-mode)))
242 (context-coloring-test-cleanup-assert-causes-coloring)))
244 (context-coloring-test-define-derived-mode change-detection)
246 (context-coloring-test-deftest change-detection
250 (list :modes '(context-coloring-test-change-detection-mode)
251 :setup #'context-coloring-setup-idle-change-detection
252 :teardown #'context-coloring-teardown-idle-change-detection)
253 context-coloring-dispatch-hash-table)
254 (context-coloring-test-change-detection-mode)
255 (context-coloring-mode)
256 (context-coloring-test-assert-causes-coloring
258 ;; Simply cannot figure out how to trigger an idle timer; would much rather
259 ;; test that. But (current-idle-time) always returns nil in these tests.
260 (context-coloring-maybe-colorize-with-buffer (current-buffer))))
262 (context-coloring-test-cleanup-assert-causes-coloring)))
264 (context-coloring-test-deftest unsupported-mode
266 (context-coloring-mode)
267 (context-coloring-test-assert-message
268 "Context coloring is unavailable here"
271 (context-coloring-test-deftest unavailable-message-ignored
273 (minibuffer-with-setup-hook
275 (context-coloring-mode)
276 (context-coloring-test-assert-not-message
277 "Context coloring is unavailable here"
284 (context-coloring-test-define-derived-mode disable-mode)
286 (context-coloring-test-deftest disable-mode
291 (list :modes '(context-coloring-test-disable-mode-mode)
294 context-coloring-dispatch-hash-table)
295 (context-coloring-test-disable-mode-mode)
296 (context-coloring-mode)
297 (context-coloring-mode -1)
298 (when (not torn-down)
299 (ert-fail "Expected teardown function to have been called, but it wasn't.")))))
301 (defun context-coloring-test-assert-maximum-face (expected)
302 "Assert that `context-coloring-maximum-face' is EXPECTED."
303 (when (not (= context-coloring-maximum-face expected))
304 (ert-fail (format "Expected maximum face to be %s, but it was %s"
305 expected context-coloring-maximum-face))))
307 (deftheme context-coloring-test-custom-theme)
309 (context-coloring-test-define-derived-mode custom-theme)
311 (context-coloring-test-deftest custom-theme
313 (custom-theme-set-faces
314 'context-coloring-test-custom-theme
315 '(context-coloring-level-0-face ((t :foreground "#aaaaaa")))
316 '(context-coloring-level-1-face ((t :foreground "#bbbbbb"))))
318 '(context-coloring-level-0-face ((t :foreground "#aaaaaa"))))
319 (enable-theme 'context-coloring-test-custom-theme)
322 (list :modes '(context-coloring-test-custom-theme-mode))
323 context-coloring-dispatch-hash-table)
324 (context-coloring-test-custom-theme-mode)
325 (context-coloring-colorize)
326 (context-coloring-test-assert-maximum-face 1)
327 ;; This theme should now be ignored in favor of the `user' theme.
328 (custom-theme-reset-faces
329 'context-coloring-test-custom-theme
330 '(context-coloring-level-0-face nil)
331 '(context-coloring-level-1-face nil))
332 (context-coloring-colorize)
333 ;; Maximum face for `user'.
334 (context-coloring-test-assert-maximum-face 0)
335 ;; Now `user' should be ignored too.
337 '(context-coloring-level-0-face nil))
338 (context-coloring-colorize)
339 ;; Expect the package's defaults.
340 (context-coloring-test-assert-maximum-face
341 context-coloring-default-maximum-face))
344 '(context-coloring-level-0-face nil))
345 (disable-theme 'context-coloring-test-custom-theme)))
350 (defun context-coloring-test-face-to-level (face)
351 "Convert FACE symbol to its corresponding level, or nil."
353 (let* ((face-string (symbol-name face))
354 (matches (string-match
355 context-coloring-level-face-regexp
358 (string-to-number (match-string 1 face-string))))))
360 (defun context-coloring-test-assert-position-level (position level)
361 "Assert that POSITION has LEVEL."
362 (let* ((face (get-text-property position 'face))
363 (actual-level (context-coloring-test-face-to-level face)))
364 (when (not (= level actual-level))
365 (ert-fail (format (concat "Expected level at position %s, "
366 "which is \"%s\", to be %s; "
369 (buffer-substring-no-properties position (1+ position)) level
372 (defun context-coloring-test-assert-position-face (position face-regexp)
373 "Assert that the face at POSITION satisfies FACE-REGEXP."
374 (let ((face (get-text-property position 'face)))
376 ;; Pass a non-string to do an `equal' check (against a symbol or nil).
377 (unless (stringp face-regexp)
378 (not (equal face-regexp face)))
379 ;; Otherwise do the matching.
380 (when (stringp face-regexp)
381 (not (string-match-p face-regexp (symbol-name face)))))
382 (ert-fail (format (concat "Expected face at position %s, "
383 "which is \"%s\", to be %s; "
386 (buffer-substring-no-properties position (1+ position)) face-regexp
389 (defun context-coloring-test-assert-position-comment (position)
390 "Assert that the face at POSITION is a comment."
391 (context-coloring-test-assert-position-face
392 position "\\`font-lock-comment\\(-delimiter\\)?-face\\'"))
394 (defun context-coloring-test-assert-position-constant-comment (position)
395 "Assert that the face at POSITION is a constant comment."
396 (context-coloring-test-assert-position-face position '(font-lock-constant-face
397 font-lock-comment-face)))
399 (defun context-coloring-test-assert-position-string (position)
400 "Assert that the face at POSITION is a string."
401 (context-coloring-test-assert-position-face position 'font-lock-string-face))
403 (defun context-coloring-test-assert-position-nil (position)
404 "Assert that the face at POSITION is nil."
405 (context-coloring-test-assert-position-face position nil))
407 (defun context-coloring-test-assert-coloring (map)
408 "Assert that the current buffer's coloring will match MAP.
410 MAP's newlines should correspond to the current fixture.
412 The following characters appearing in MAP assert coloring for
413 corresponding points in the fixture:
415 0-9: Level equals number.
416 C: Face is constant comment.
421 Any other characters are discarded. Characters \"x\" and any
422 other non-letters are guaranteed to always be discarded."
423 ;; Omit the superfluous, formatting-related leading newline. Can't use
424 ;; `save-excursion' here because if an assertion fails it will cause future
425 ;; tests to get messed up.
426 (goto-char (point-min))
427 (let* ((map (substring map 1))
431 (while (< index (length map))
432 (setq char-string (substring map index (1+ index)))
433 (setq char (string-to-char char-string))
442 (context-coloring-test-assert-position-level
443 (point) (string-to-number char-string))
445 ;; 'C' = Constant comment
447 (context-coloring-test-assert-position-constant-comment (point))
451 (context-coloring-test-assert-position-comment (point))
455 (context-coloring-test-assert-position-nil (point))
459 (context-coloring-test-assert-position-string (point))
463 (setq index (1+ index)))))
465 (context-coloring-test-deftest-javascript function-scopes
467 (context-coloring-test-assert-coloring "
468 000 0 0 11111111 11 110
470 111 1 1 22222222 22 221
474 (context-coloring-test-deftest-javascript global
476 (context-coloring-test-assert-coloring "
481 (context-coloring-test-deftest-javascript block-scopes
483 (context-coloring-test-assert-coloring "
501 (setq context-coloring-javascript-block-scopes t))
503 (setq context-coloring-javascript-block-scopes nil)))
505 (context-coloring-test-deftest-javascript catch
507 (context-coloring-test-assert-coloring "
517 (context-coloring-test-deftest-javascript key-names
519 (context-coloring-test-assert-coloring "
527 (context-coloring-test-deftest-javascript property-lookup
529 (context-coloring-test-assert-coloring "
536 (context-coloring-test-deftest-javascript key-values
538 (context-coloring-test-assert-coloring "
548 (context-coloring-test-deftest-javascript syntactic-comments-and-strings
550 (context-coloring-test-assert-coloring "
555 :fixture "comments-and-strings.js")
557 (context-coloring-test-deftest-javascript syntactic-comments
559 (context-coloring-test-assert-coloring "
564 :fixture "comments-and-strings.js"
566 (setq context-coloring-syntactic-strings nil))
568 (setq context-coloring-syntactic-strings t)))
570 (context-coloring-test-deftest-javascript syntactic-strings
572 (context-coloring-test-assert-coloring "
577 :fixture "comments-and-strings.js"
579 (setq context-coloring-syntactic-comments nil))
581 (setq context-coloring-syntactic-comments t)))
583 (context-coloring-test-deftest-javascript unterminated-comment
584 ;; As long as `add-text-properties' doesn't signal an error, this test passes.
587 (defun context-coloring-test-assert-javascript-elevated-level ()
588 "Assert that the \"initial-level.js\" file has elevated scope."
589 (context-coloring-test-assert-coloring "
591 111 1 1 0000001xxx11"))
593 (defun context-coloring-test-assert-javascript-global-level ()
594 "Assert that the \"initial-level.js\" file has global scope."
595 (context-coloring-test-assert-coloring "
597 000 0 0 0000000xxx00"))
599 (context-coloring-test-deftest-javascript initial-level
601 (context-coloring-test-assert-javascript-elevated-level))
602 :fixture "initial-level.js"
604 (setq context-coloring-initial-level 1))
606 (setq context-coloring-initial-level 0)))
608 (defun context-coloring-test-setup-top-level-scope (string)
609 "Make STRING the first line and colorize again."
610 (goto-char (point-min))
613 ;; Reparsing triggers recoloring.
616 (context-coloring-test-deftest-javascript top-level-scope
618 (let ((positive-indicators
619 (list "#!/usr/bin/env node"
620 "/*jslint node: true */"
621 "// jshint node: true"
622 "/*eslint-env node */"
628 (list "// Blah blah jshint blah."
631 "var require; require('a')")))
632 (dolist (indicator positive-indicators)
633 (context-coloring-test-setup-top-level-scope indicator)
634 (context-coloring-test-assert-javascript-elevated-level))
635 (dolist (indicator negative-indicators)
636 (context-coloring-test-setup-top-level-scope indicator)
637 (context-coloring-test-assert-javascript-global-level))))
638 :fixture "initial-level.js")
640 (context-coloring-test-deftest-emacs-lisp defun
642 (context-coloring-test-assert-coloring "
643 111111 000 1111 111 111111111 1111
644 11 111 111 111 000011
652 (context-coloring-test-deftest-emacs-lisp defadvice
654 (context-coloring-test-assert-coloring "
655 1111111111 0 1111111 111111 11111 111 111111111
659 (context-coloring-test-deftest-emacs-lisp lambda
661 (context-coloring-test-assert-coloring "
662 00000000 1111111 1111
663 11111111 11 2222222 2222
664 222 22 12 2221 111 0 00")))
666 (context-coloring-test-deftest-emacs-lisp quote
668 (context-coloring-test-assert-coloring "
669 (xxxxx 0000000 00 00000)
670 (xxx () (xxxxxxxxx (,0000)))
675 111111 1 1111111111 11 111 1 111 1 00001 10000 11 00001 1 10000
683 (xxxxxx () 111111 11111)")))
685 (context-coloring-test-deftest-emacs-lisp splice
687 (context-coloring-test-assert-coloring "
689 111111 00001 100001)")))
691 (context-coloring-test-deftest-emacs-lisp comment
693 ;; Just check that the comment isn't parsed syntactically.
694 (context-coloring-test-assert-coloring "
696 (xx (x xxxxx-xxxx xx) cccccccccc
697 11 00000-0000 11))) cccccccccc")))
699 (context-coloring-test-deftest-emacs-lisp string
701 (context-coloring-test-assert-coloring "
703 (xxxxxx x x sss 1 0 sssss 0 1 sssssss11")))
705 (context-coloring-test-deftest-emacs-lisp ignored
707 (context-coloring-test-assert-coloring "
709 (x x 1 11 11 111 111 11 11 11 1 111 (1 1 1)))")))
711 (context-coloring-test-deftest-emacs-lisp sexp
713 (context-coloring-test-assert-coloring "
719 (context-coloring-test-deftest-emacs-lisp let
721 (context-coloring-test-assert-coloring "
734 (context-coloring-test-deftest-emacs-lisp empty-varlist
736 (context-coloring-test-assert-coloring "
744 (context-coloring-test-deftest-emacs-lisp varlist-spacing
746 (context-coloring-test-assert-coloring "
756 (context-coloring-test-deftest-emacs-lisp let*
758 (context-coloring-test-assert-coloring "
768 2222 1 1 2 2 2 000022
769 1111 1 1 1 0 0 000011"))
770 :fixture "let-star.el")
772 (context-coloring-test-deftest-emacs-lisp macroexp-let2
774 (context-coloring-test-assert-coloring "
776 222222222-2222 00000000-00000000-0 2 111
779 (11111111-1111 00000000-00000000-0)
782 (context-coloring-test-deftest-emacs-lisp cond
784 (context-coloring-test-assert-coloring "
794 (context-coloring-test-deftest-emacs-lisp condition-case
796 (context-coloring-test-assert-coloring "
800 1111111 111111 111 000011
802 (111111111-1111-111111-11111 111
808 (context-coloring-test-deftest-emacs-lisp dolist
810 (context-coloring-test-assert-coloring "
812 2222222 2222 1111 2222222
813 3333333 33 33 222 1111 2222223321")))
815 (defun context-coloring-test-insert-unread-space ()
816 "Simulate the insertion of a space as if by a user."
817 (setq unread-command-events (cons '(t . 32)
818 unread-command-events)))
820 (defun context-coloring-test-remove-faces ()
821 "Remove all faces in the current buffer."
822 (remove-text-properties (point-min) (point-max) '(face nil)))
824 (context-coloring-test-deftest-emacs-lisp iteration
826 (let ((context-coloring-elisp-sexps-per-pause 2))
827 (context-coloring-colorize)
828 (context-coloring-test-assert-coloring "
831 (context-coloring-test-remove-faces)
832 (context-coloring-test-insert-unread-space)
833 (context-coloring-colorize)
834 ;; Coloring is interrupted after the first "sexp" (the comment in this
836 (context-coloring-test-assert-coloring "
840 (context-coloring-test-deftest-emacs-lisp changed
842 (context-coloring-test-remove-faces)
844 (goto-char (point-min))
845 (forward-line (1- 3))
847 ;; Mock `pos-visible-in-window-p' because in batch mode `get-buffer-window'
848 ;; returns nil. Emacs must not have a window in that environment.
849 (cl-letf (((symbol-function 'pos-visible-in-window-p)
853 ;; First and third calls start from center. Second and
854 ;; fourth calls are made immediately after moving past
855 ;; the first defun in either direction "off screen".
861 (setq calls (1+ calls)))))))
862 (context-coloring-colorize))
863 (context-coloring-test-assert-coloring "
868 nnnnn n nnn nnnnnnnn")))
870 (context-coloring-test-deftest-emacs-lisp unbalanced-parenthesis
872 (context-coloring-test-assert-coloring "
876 (context-coloring-test-deftest-eval-expression let
878 (minibuffer-with-setup-hook
880 ;; Perform the test in a hook as it's the only way I know of examining
881 ;; the minibuffer's contents. The contents are implicitly submitted,
882 ;; so we have to ignore the errors in the arbitrary test subject code.
883 (insert "(ignore-errors (let (a) (message a free)))")
884 (context-coloring-mode)
885 (context-coloring-test-assert-coloring "
886 xxxx: 0000000-000000 1111 111 11111111 1 0000110"))
887 ;; Simulate user input because `call-interactively' is blocking and
888 ;; doesn't seem to run the hook.
891 [?\C-u] ;; Don't output the result of the arbitrary test subject code.
894 (provide 'context-coloring-test)
896 ;;; context-coloring-test.el ends here