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 (defun context-coloring-test-js2-mode ()
115 "Enable js2-mode and parse synchronously."
119 (context-coloring-test-define-deftest javascript
120 :mode #'context-coloring-test-js2-mode
122 :enable-context-coloring-mode t
123 :before-each (lambda ()
124 (setq js2-mode-show-parse-errors nil)
125 (setq js2-mode-show-strict-warnings nil)))
127 (context-coloring-test-define-deftest emacs-lisp
128 :mode #'emacs-lisp-mode
130 :enable-context-coloring-mode t)
132 (context-coloring-test-define-deftest eval-expression
133 :mode #'fundamental-mode
137 ;;; Assertion functions
139 (defun context-coloring-test-get-last-message ()
140 "Get the last message in the current messages bufffer."
141 (let ((messages (split-string
142 (buffer-substring-no-properties
146 (car (nthcdr (- (length messages) 2) messages))))
148 (defun context-coloring-test-assert-message (expected buffer)
149 "Assert that message EXPECTED is at the end of BUFFER."
150 (when (null (get-buffer buffer))
154 "Expected buffer `%s' to have message \"%s\", "
155 "but the buffer did not have any messages.")
157 (with-current-buffer buffer
158 (let ((message (context-coloring-test-get-last-message)))
159 (when (not (equal message expected))
163 "Expected buffer `%s' to have message \"%s\", "
164 "but instead it was \"%s\"")
168 (defun context-coloring-test-assert-not-message (expected buffer)
169 "Assert that message EXPECTED is not at the end of BUFFER."
170 (when (get-buffer buffer)
171 (with-current-buffer buffer
172 (let ((message (context-coloring-test-get-last-message)))
173 (when (equal message expected)
177 "Expected buffer `%s' not to have message \"%s\", "
179 buffer expected)))))))
181 (defun context-coloring-test-assert-error (body error-message)
182 "Assert that BODY signals ERROR-MESSAGE."
183 (let ((error-signaled-p nil))
188 (setq error-signaled-p t)
189 (when (not (string-equal (cadr err) error-message))
190 (ert-fail (format (concat "Expected the error \"%s\" to be thrown, "
191 "but instead it was \"%s\".")
194 (when (not error-signaled-p)
195 (ert-fail "Expected an error to be thrown, but there wasn't."))))
198 ;;; Miscellaneous tests
200 (defmacro context-coloring-test-define-derived-mode (name)
201 "Define a derived mode exclusively for any test with NAME."
202 (let ((name (intern (format "context-coloring-test-%s-mode" name))))
203 `(define-derived-mode ,name fundamental-mode "Testing")))
205 (defvar context-coloring-test-caused-p nil
206 "If non-nil, coloring was caused.")
208 (defmacro context-coloring-test-assert-causes-coloring (&rest body)
209 "Assert that BODY causes coloring."
211 ;; Gross, but I want this to pass on 24.3.
212 (ad-add-advice #'context-coloring-colorize
213 '(assert-causes-coloring
216 (setq context-coloring-test-caused-p t))))
219 (ad-activate #'context-coloring-colorize)
221 (when (not context-coloring-test-caused-p)
222 (ert-fail "Expected to have colorized, but it didn't."))))
224 (defun context-coloring-test-cleanup-assert-causes-coloring ()
225 "Undo `context-coloring-test-assert-causes-coloring'."
226 (ad-unadvise #'context-coloring-colorize)
227 (setq context-coloring-test-caused-p nil))
229 (context-coloring-test-define-derived-mode mode-startup)
231 (context-coloring-test-deftest mode-startup
233 (context-coloring-define-dispatch
235 :modes '(context-coloring-test-mode-startup-mode)
237 (context-coloring-test-mode-startup-mode)
238 (context-coloring-test-assert-causes-coloring
239 (context-coloring-mode)))
241 (context-coloring-test-cleanup-assert-causes-coloring)))
243 (context-coloring-test-define-derived-mode change-detection)
245 (context-coloring-test-deftest change-detection
247 (context-coloring-define-dispatch
249 :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-test-change-detection-mode)
254 (context-coloring-mode)
255 (context-coloring-test-assert-causes-coloring
257 ;; Simply cannot figure out how to trigger an idle timer; would much rather
258 ;; test that. But (current-idle-time) always returns nil in these tests.
259 (context-coloring-maybe-colorize-with-buffer (current-buffer))))
261 (context-coloring-test-cleanup-assert-causes-coloring)))
263 (context-coloring-test-deftest unsupported-mode
265 (context-coloring-mode)
266 (context-coloring-test-assert-message
267 "Context coloring is unavailable here"
270 (context-coloring-test-deftest unavailable-message-ignored
272 (minibuffer-with-setup-hook
274 (context-coloring-mode)
275 (context-coloring-test-assert-not-message
276 "Context coloring is unavailable here"
283 (context-coloring-test-define-derived-mode define-dispatch-error)
285 (context-coloring-test-deftest define-dispatch-error
287 (context-coloring-test-assert-error
289 (context-coloring-define-dispatch
290 'define-dispatch-no-modes))
291 "No mode or predicate defined for dispatch")
292 (context-coloring-test-assert-error
294 (context-coloring-define-dispatch
295 'define-dispatch-no-strategy
296 :modes '(context-coloring-test-define-dispatch-error-mode)))
297 "No colorizer defined for dispatch")))
299 (context-coloring-test-define-derived-mode disable-mode)
301 (context-coloring-test-deftest disable-mode
304 (context-coloring-define-dispatch
306 :modes '(context-coloring-test-disable-mode-mode)
310 (context-coloring-test-disable-mode-mode)
311 (context-coloring-mode)
312 (context-coloring-mode -1)
313 (when (not torn-down)
314 (ert-fail "Expected teardown function to have been called, but it wasn't.")))))
316 (defun context-coloring-test-assert-maximum-face (expected)
317 "Assert that `context-coloring-maximum-face' is EXPECTED."
318 (when (not (= context-coloring-maximum-face expected))
319 (ert-fail (format "Expected maximum face to be %s, but it was %s"
320 expected context-coloring-maximum-face))))
322 (deftheme context-coloring-test-custom-theme)
324 (context-coloring-test-define-derived-mode custom-theme)
326 (context-coloring-test-deftest custom-theme
328 (custom-theme-set-faces
329 'context-coloring-test-custom-theme
330 '(context-coloring-level-0-face ((t :foreground "#aaaaaa")))
331 '(context-coloring-level-1-face ((t :foreground "#bbbbbb"))))
333 '(context-coloring-level-0-face ((t :foreground "#aaaaaa"))))
334 (enable-theme 'context-coloring-test-custom-theme)
335 (context-coloring-define-dispatch
337 :modes '(context-coloring-test-custom-theme-mode)
339 (context-coloring-test-custom-theme-mode)
340 (context-coloring-colorize)
341 (context-coloring-test-assert-maximum-face 1)
342 ;; This theme should now be ignored in favor of the `user' theme.
343 (custom-theme-reset-faces
344 'context-coloring-test-custom-theme
345 '(context-coloring-level-0-face nil)
346 '(context-coloring-level-1-face nil))
347 (context-coloring-colorize)
348 ;; Maximum face for `user'.
349 (context-coloring-test-assert-maximum-face 0)
350 ;; Now `user' should be ignored too.
352 '(context-coloring-level-0-face nil))
353 (context-coloring-colorize)
354 ;; Expect the package's defaults.
355 (context-coloring-test-assert-maximum-face
356 context-coloring-default-maximum-face))
359 '(context-coloring-level-0-face nil))
360 (disable-theme 'context-coloring-test-custom-theme)))
365 (defun context-coloring-test-face-to-level (face)
366 "Convert FACE symbol to its corresponding level, or nil."
368 (let* ((face-string (symbol-name face))
369 (matches (string-match
370 context-coloring-level-face-regexp
373 (string-to-number (match-string 1 face-string))))))
375 (defun context-coloring-test-assert-position-level (position level)
376 "Assert that POSITION has LEVEL."
377 (let* ((face (get-text-property position 'face))
378 (actual-level (context-coloring-test-face-to-level face)))
379 (when (not (= level actual-level))
380 (ert-fail (format (concat "Expected level at position %s, "
381 "which is \"%s\", to be %s; "
384 (buffer-substring-no-properties position (1+ position)) level
387 (defun context-coloring-test-assert-position-face (position face-regexp)
388 "Assert that the face at POSITION satisfies FACE-REGEXP."
389 (let ((face (get-text-property position 'face)))
391 ;; Pass a non-string to do an `equal' check (against a symbol or nil).
392 (unless (stringp face-regexp)
393 (not (equal face-regexp face)))
394 ;; Otherwise do the matching.
395 (when (stringp face-regexp)
396 (not (string-match-p face-regexp (symbol-name face)))))
397 (ert-fail (format (concat "Expected face at position %s, "
398 "which is \"%s\", to be %s; "
401 (buffer-substring-no-properties position (1+ position)) face-regexp
404 (defun context-coloring-test-assert-position-comment (position)
405 "Assert that the face at POSITION is a comment."
406 (context-coloring-test-assert-position-face
407 position "\\`font-lock-comment\\(-delimiter\\)?-face\\'"))
409 (defun context-coloring-test-assert-position-constant-comment (position)
410 "Assert that the face at POSITION is a constant comment."
411 (context-coloring-test-assert-position-face position '(font-lock-constant-face
412 font-lock-comment-face)))
414 (defun context-coloring-test-assert-position-string (position)
415 "Assert that the face at POSITION is a string."
416 (context-coloring-test-assert-position-face position 'font-lock-string-face))
418 (defun context-coloring-test-assert-position-nil (position)
419 "Assert that the face at POSITION is nil."
420 (context-coloring-test-assert-position-face position nil))
422 (defun context-coloring-test-assert-coloring (map)
423 "Assert that the current buffer's coloring will match MAP.
425 MAP's newlines should correspond to the current fixture.
427 The following characters appearing in MAP assert coloring for
428 corresponding points in the fixture:
430 0-9: Level equals number.
431 C: Face is constant comment.
436 Any other characters are discarded. Characters \"x\" and any
437 other non-letters are guaranteed to always be discarded."
438 ;; Omit the superfluous, formatting-related leading newline. Can't use
439 ;; `save-excursion' here because if an assertion fails it will cause future
440 ;; tests to get messed up.
441 (goto-char (point-min))
442 (let* ((map (substring map 1))
446 (while (< index (length map))
447 (setq char-string (substring map index (1+ index)))
448 (setq char (string-to-char char-string))
457 (context-coloring-test-assert-position-level
458 (point) (string-to-number char-string))
460 ;; 'C' = Constant comment
462 (context-coloring-test-assert-position-constant-comment (point))
466 (context-coloring-test-assert-position-comment (point))
470 (context-coloring-test-assert-position-nil (point))
474 (context-coloring-test-assert-position-string (point))
478 (setq index (1+ index)))))
480 (context-coloring-test-deftest-javascript function-scopes
482 (context-coloring-test-assert-coloring "
483 000 0 0 11111111 11 110
485 111 1 1 22222222 22 221
489 (context-coloring-test-deftest-javascript global
491 (context-coloring-test-assert-coloring "
496 (context-coloring-test-deftest-javascript block-scopes
498 (context-coloring-test-assert-coloring "
516 (setq context-coloring-javascript-block-scopes t))
518 (setq context-coloring-javascript-block-scopes nil)))
520 (context-coloring-test-deftest-javascript catch
522 (context-coloring-test-assert-coloring "
532 (context-coloring-test-deftest-javascript key-names
534 (context-coloring-test-assert-coloring "
542 (context-coloring-test-deftest-javascript property-lookup
544 (context-coloring-test-assert-coloring "
551 (context-coloring-test-deftest-javascript key-values
553 (context-coloring-test-assert-coloring "
563 (context-coloring-test-deftest-javascript syntactic-comments-and-strings
565 (context-coloring-test-assert-coloring "
570 :fixture "comments-and-strings.js")
572 (context-coloring-test-deftest-javascript syntactic-comments
574 (context-coloring-test-assert-coloring "
579 :fixture "comments-and-strings.js"
581 (setq context-coloring-syntactic-strings nil))
583 (setq context-coloring-syntactic-strings t)))
585 (context-coloring-test-deftest-javascript syntactic-strings
587 (context-coloring-test-assert-coloring "
592 :fixture "comments-and-strings.js"
594 (setq context-coloring-syntactic-comments nil))
596 (setq context-coloring-syntactic-comments t)))
598 (context-coloring-test-deftest-javascript unterminated-comment
599 ;; As long as `add-text-properties' doesn't signal an error, this test passes.
602 (defun context-coloring-test-assert-javascript-elevated-level ()
603 "Assert that the \"initial-level.js\" file has elevated scope."
604 (context-coloring-test-assert-coloring "
606 111 1 1 0000001xxx11"))
608 (defun context-coloring-test-assert-javascript-global-level ()
609 "Assert that the \"initial-level.js\" file has global scope."
610 (context-coloring-test-assert-coloring "
612 000 0 0 0000000xxx00"))
614 (context-coloring-test-deftest-javascript initial-level
616 (context-coloring-test-assert-javascript-elevated-level))
617 :fixture "initial-level.js"
619 (setq context-coloring-initial-level 1))
621 (setq context-coloring-initial-level 0)))
623 (defun context-coloring-test-setup-top-level-scope (string)
624 "Make STRING the first line and colorize again."
625 (goto-char (point-min))
628 ;; Reparsing triggers recoloring.
631 (context-coloring-test-deftest-javascript top-level-scope
633 (let ((positive-indicators
634 (list "#!/usr/bin/env node"
635 "/*jslint node: true */"
636 "// jshint node: true"
637 "/*eslint-env node */"
643 (list "// Blah blah jshint blah."
646 "var require; require('a')")))
647 (dolist (indicator positive-indicators)
648 (context-coloring-test-setup-top-level-scope indicator)
649 (context-coloring-test-assert-javascript-elevated-level))
650 (dolist (indicator negative-indicators)
651 (context-coloring-test-setup-top-level-scope indicator)
652 (context-coloring-test-assert-javascript-global-level))))
653 :fixture "initial-level.js")
655 (context-coloring-test-deftest-emacs-lisp defun
657 (context-coloring-test-assert-coloring "
658 111111 000 1111 111 111111111 1111
659 11 111 111 111 000011
667 (context-coloring-test-deftest-emacs-lisp defadvice
669 (context-coloring-test-assert-coloring "
670 1111111111 0 1111111 111111 11111 111 111111111
674 (context-coloring-test-deftest-emacs-lisp lambda
676 (context-coloring-test-assert-coloring "
677 00000000 1111111 1111
678 11111111 11 2222222 2222
679 222 22 12 2221 111 0 00")))
681 (context-coloring-test-deftest-emacs-lisp quote
683 (context-coloring-test-assert-coloring "
684 (xxxxx 0000000 00 00000)
685 (xxx () (xxxxxxxxx (,0000)))
690 111111 1 1111111111 11 111 1 111 1 00001 10000 11 00001 1 10000
698 (xxxxxx () 111111 11111)")))
700 (context-coloring-test-deftest-emacs-lisp splice
702 (context-coloring-test-assert-coloring "
704 111111 00001 100001)")))
706 (context-coloring-test-deftest-emacs-lisp comment
708 ;; Just check that the comment isn't parsed syntactically.
709 (context-coloring-test-assert-coloring "
711 (xx (x xxxxx-xxxx xx) cccccccccc
712 11 00000-0000 11))) cccccccccc")))
714 (context-coloring-test-deftest-emacs-lisp string
716 (context-coloring-test-assert-coloring "
718 (xxxxxx x x sss 1 0 sssss 0 1 sssssss11")))
720 (context-coloring-test-deftest-emacs-lisp ignored
722 (context-coloring-test-assert-coloring "
724 (x x 1 11 11 111 111 11 11 11 1 111 (1 1 1)))")))
726 (context-coloring-test-deftest-emacs-lisp sexp
728 (context-coloring-test-assert-coloring "
734 (context-coloring-test-deftest-emacs-lisp let
736 (context-coloring-test-assert-coloring "
749 (context-coloring-test-deftest-emacs-lisp empty-varlist
751 (context-coloring-test-assert-coloring "
759 (context-coloring-test-deftest-emacs-lisp varlist-spacing
761 (context-coloring-test-assert-coloring "
771 (context-coloring-test-deftest-emacs-lisp let*
773 (context-coloring-test-assert-coloring "
783 2222 1 1 2 2 2 000022
784 1111 1 1 1 0 0 000011"))
785 :fixture "let-star.el")
787 (context-coloring-test-deftest-emacs-lisp macroexp-let2
789 (context-coloring-test-assert-coloring "
791 222222222-2222 00000000-00000000-0 2 111
794 (11111111-1111 00000000-00000000-0)
797 (context-coloring-test-deftest-emacs-lisp cond
799 (context-coloring-test-assert-coloring "
809 (context-coloring-test-deftest-emacs-lisp condition-case
811 (context-coloring-test-assert-coloring "
815 1111111 111111 111 000011
817 (111111111-1111-111111-11111 111
823 (context-coloring-test-deftest-emacs-lisp dolist
825 (context-coloring-test-assert-coloring "
827 2222222 2222 1111 2222222
828 3333333 33 33 222 1111 2222223321")))
830 (defun context-coloring-test-insert-unread-space ()
831 "Simulate the insertion of a space as if by a user."
832 (setq unread-command-events (cons '(t . 32)
833 unread-command-events)))
835 (defun context-coloring-test-remove-faces ()
836 "Remove all faces in the current buffer."
837 (remove-text-properties (point-min) (point-max) '(face nil)))
839 (context-coloring-test-deftest-emacs-lisp iteration
841 (let ((context-coloring-elisp-sexps-per-pause 2))
842 (context-coloring-colorize)
843 (context-coloring-test-assert-coloring "
846 (context-coloring-test-remove-faces)
847 (context-coloring-test-insert-unread-space)
848 (context-coloring-colorize)
849 ;; Coloring is interrupted after the first "sexp" (the comment in this
851 (context-coloring-test-assert-coloring "
855 (context-coloring-test-deftest-emacs-lisp changed
857 (context-coloring-test-remove-faces)
859 (goto-char (point-min))
860 (forward-line (1- 3))
862 ;; Mock `pos-visible-in-window-p' because in batch mode `get-buffer-window'
863 ;; returns nil. Emacs must not have a window in that environment.
864 (cl-letf (((symbol-function 'pos-visible-in-window-p)
868 ;; First and third calls start from center. Second and
869 ;; fourth calls are made immediately after moving past
870 ;; the first defun in either direction "off screen".
876 (setq calls (1+ calls)))))))
877 (context-coloring-colorize))
878 (context-coloring-test-assert-coloring "
883 nnnnn n nnn nnnnnnnn")))
885 (context-coloring-test-deftest-emacs-lisp unbalanced-parenthesis
887 (context-coloring-test-assert-coloring "
891 (context-coloring-test-deftest-eval-expression let
893 (minibuffer-with-setup-hook
895 ;; Perform the test in a hook as it's the only way I know of examining
896 ;; the minibuffer's contents. The contents are implicitly submitted,
897 ;; so we have to ignore the errors in the arbitrary test subject code.
898 (insert "(ignore-errors (let (a) (message a free)))")
899 (context-coloring-colorize)
900 (context-coloring-test-assert-coloring "
901 xxxx: 0000000-000000 1111 111 11111111 1 0000110"))
902 ;; Simulate user input because `call-interactively' is blocking and
903 ;; doesn't seem to run the hook.
906 [?\C-u] ;; Don't output the result of the arbitrary test subject code.
909 (provide 'context-coloring-test)
911 ;;; context-coloring-test.el ends here