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'.
28 (require 'context-coloring)
33 ;;; Test running utilities
35 (defconst context-coloring-test-path
36 (file-name-directory (or load-file-name buffer-file-name))
37 "This file's directory.")
39 (defun context-coloring-test-read-file (path)
40 "Read a file's contents from PATH into a string."
42 (insert-file-contents (expand-file-name path context-coloring-test-path))
45 (defun context-coloring-test-setup ()
46 "Prepare before all tests."
47 (setq context-coloring-syntactic-comments nil)
48 (setq context-coloring-syntactic-strings nil))
50 (defun context-coloring-test-cleanup ()
51 "Cleanup after all tests."
52 (setq context-coloring-comments-and-strings nil)
53 (setq context-coloring-js-block-scopes nil)
54 (setq context-coloring-colorize-hook nil)
55 (setq context-coloring-check-scopifier-version-hook nil)
56 (setq context-coloring-maximum-face 7)
57 (setq context-coloring-original-maximum-face
58 context-coloring-maximum-face))
60 (defmacro context-coloring-test-with-fixture (fixture &rest body)
61 "With the relative FIXTURE, evaluate BODY in a temporary
66 (context-coloring-test-setup)
67 (insert (context-coloring-test-read-file ,fixture))
69 (context-coloring-test-cleanup))))
71 (defun context-coloring-test-with-temp-buffer-async (callback)
72 "Create a temporary buffer, and evaluate CALLBACK there. A
73 teardown callback is passed to CALLBACK for it to invoke when it
75 (let ((previous-buffer (current-buffer))
76 (temp-buffer (generate-new-buffer " *temp*")))
77 (set-buffer temp-buffer)
81 (and (buffer-name temp-buffer)
82 (kill-buffer temp-buffer))
83 (set-buffer previous-buffer)))))
85 (defun context-coloring-test-with-fixture-async
86 (fixture callback &optional setup)
87 "With the relative FIXTURE, evaluate CALLBACK in a temporary
88 buffer. A teardown callback is passed to CALLBACK for it to
89 invoke when it is done. An optional SETUP callback can run
90 arbitrary code before the mode is invoked."
91 (context-coloring-test-with-temp-buffer-async
92 (lambda (done-with-temp-buffer)
93 (context-coloring-test-setup)
94 (when setup (funcall setup))
95 (insert (context-coloring-test-read-file fixture))
99 (context-coloring-test-cleanup)
100 (funcall done-with-temp-buffer))))))
103 ;;; Test defining utilities
105 (defun context-coloring-test-js-mode (fixture callback &optional setup)
106 "Use FIXTURE as the subject matter for test logic in CALLBACK.
107 Optionally, provide setup code to run before the mode is
108 instantiated in SETUP."
109 (context-coloring-test-with-fixture-async
111 (lambda (done-with-test)
113 (context-coloring-mode)
114 (context-coloring-colorize
116 (funcall callback done-with-test))))
119 (defmacro context-coloring-test-js2-mode (fixture setup &rest body)
120 "Use FIXTURE as the subject matter for test logic in BODY."
121 `(context-coloring-test-with-fixture
124 (setq js2-mode-show-parse-errors nil)
125 (setq js2-mode-show-strict-warnings nil)
127 (when ,setup (funcall ,setup))
128 (context-coloring-mode)
131 (cl-defmacro context-coloring-test-deftest-js-mode (name &key fixture-name)
132 "Define an asynchronous test for `js-mode' with the name NAME
133 in the typical format."
134 (declare (indent defun))
135 (let ((test-name (intern (format "context-coloring-test-js-mode-%s" name)))
136 (fixture (format "./fixtures/%s.js" (or fixture-name name)))
137 (function-name (intern-soft
138 (format "context-coloring-test-js-%s" name)))
139 (setup-function-name (intern-soft
141 "context-coloring-test-js-%s-setup" name))))
142 `(ert-deftest-async ,test-name (done)
143 (context-coloring-test-js-mode
150 ',setup-function-name))))
152 (cl-defmacro context-coloring-test-deftest-js2-mode (name &key fixture-name)
153 "Define a test for `js2-mode' with the name NAME in the typical
155 (declare (indent defun))
156 (let ((test-name (intern (format "context-coloring-test-js2-mode-%s" name)))
157 (fixture (format "./fixtures/%s.js" (or fixture-name name)))
158 (function-name (intern-soft
159 (format "context-coloring-test-js-%s" name)))
160 (setup-function-name (intern-soft
162 "context-coloring-test-js-%s-setup" name))))
163 `(ert-deftest ,test-name ()
164 (context-coloring-test-js2-mode
166 ',setup-function-name
169 (cl-defmacro context-coloring-test-deftest-emacs-lisp-mode (name
172 "Define a test for `emacs-lisp-mode' with name and fixture as
173 NAME, with BODY containing the assertions, and SETUP defining the
175 (declare (indent defun))
176 (let ((test-name (intern (format "context-coloring-emacs-lisp-mode-%s" name)))
177 (fixture (format "./fixtures/%s.el" name)))
178 `(ert-deftest ,test-name ()
179 (context-coloring-test-with-fixture
182 (when ,setup (funcall ,setup))
183 (context-coloring-mode)
187 ;;; Assertion functions
189 (defun context-coloring-test-assert-position-level (position level)
190 "Assert that POSITION has LEVEL."
191 (let ((face (get-text-property position 'face))
194 (let* ((face-string (symbol-name face))
195 (matches (string-match
196 context-coloring-level-face-regexp
199 (setq actual-level (string-to-number
200 (substring face-string
203 (= level actual-level)))))
204 (ert-fail (format (concat "Expected level at position %s, "
205 "which is \"%s\", to be %s; "
208 (buffer-substring-no-properties position (1+ position)) level
211 (defun context-coloring-test-assert-position-face (position face-regexp)
212 "Assert that the face at POSITION satisfies FACE-REGEXP."
213 (let ((face (get-text-property position 'face)))
215 ;; Pass a non-string to do an `equal' check (against a symbol or nil).
216 (unless (stringp face-regexp)
217 (not (equal face-regexp face)))
218 ;; Otherwise do the matching.
219 (when (stringp face-regexp)
220 (not (string-match-p face-regexp (symbol-name face)))))
221 (ert-fail (format (concat "Expected face at position %s, "
222 "which is \"%s\", to be %s; "
225 (buffer-substring-no-properties position (1+ position)) face-regexp
228 (defun context-coloring-test-assert-position-comment (position)
229 (context-coloring-test-assert-position-face
230 position "\\`font-lock-comment\\(-delimiter\\)?-face\\'"))
232 (defun context-coloring-test-assert-position-constant-comment (position)
233 (context-coloring-test-assert-position-face position '(font-lock-constant-face
234 font-lock-comment-face)))
236 (defun context-coloring-test-assert-position-string (position)
237 (context-coloring-test-assert-position-face position 'font-lock-string-face))
239 (defun context-coloring-test-assert-coloring (map)
240 "Assert that the current buffer's coloring matches MAP."
241 ;; Omit the superfluous, formatting-related leading newline. Can't use
242 ;; `save-excursion' here because if an assertion fails it will cause future
243 ;; tests to get messed up.
244 (goto-char (point-min))
245 (let* ((map (substring map 1))
249 (while (< index (length map))
250 (setq char-string (substring map index (1+ index)))
251 (setq char (string-to-char char-string))
260 (context-coloring-test-assert-position-level
261 (point) (string-to-number char-string))
265 (context-coloring-test-assert-position-comment (point))
267 ;; 'c' = Constant comment
269 (context-coloring-test-assert-position-constant-comment (point))
273 (context-coloring-test-assert-position-string (point))
277 (setq index (1+ index)))))
279 (defmacro context-coloring-test-assert-region (&rest body)
280 "Assert something about the face of points in a region.
281 Provides the free variables `i', `length', `point', `face' and
282 `actual-level' to the code in BODY."
284 (length (- end start)))
286 (let* ((point (+ i start))
287 (face (get-text-property point 'face)))
291 (defun context-coloring-test-assert-region-level (start end level)
292 "Assert that all points in the range [START, END) are of level
294 (context-coloring-test-assert-region
296 (when (not (when face
297 (let* ((face-string (symbol-name face))
298 (matches (string-match
299 context-coloring-level-face-regexp
302 (setq actual-level (string-to-number
303 (substring face-string
306 (= level actual-level)))))
307 (ert-fail (format (concat "Expected level in region [%s, %s), "
308 "which is \"%s\", to be %s; "
309 "but at point %s, it was %s")
311 (buffer-substring-no-properties start end) level
312 point actual-level))))))
314 (defun context-coloring-test-assert-region-face (start end expected-face)
315 "Assert that all points in the range [START, END) have the face
317 (context-coloring-test-assert-region
318 (when (not (eq face expected-face))
319 (ert-fail (format (concat "Expected face in region [%s, %s), "
320 "which is \"%s\", to be %s; "
321 "but at point %s, it was %s")
323 (buffer-substring-no-properties start end) expected-face
326 (defun context-coloring-test-assert-region-comment-delimiter (start end)
327 "Assert that all points in the range [START, END) have
328 `font-lock-comment-delimiter-face'."
329 (context-coloring-test-assert-region-face
330 start end 'font-lock-comment-delimiter-face))
332 (defun context-coloring-test-assert-region-comment (start end)
333 "Assert that all points in the range [START, END) have
334 `font-lock-comment-face'."
335 (context-coloring-test-assert-region-face
336 start end 'font-lock-comment-face))
338 (defun context-coloring-test-assert-region-string (start end)
339 "Assert that all points in the range [START, END) have
340 `font-lock-string-face'."
341 (context-coloring-test-assert-region-face
342 start end 'font-lock-string-face))
344 (defun context-coloring-test-assert-message (expected buffer)
345 "Assert that message EXPECTED exists in BUFFER."
346 (when (null (get-buffer buffer))
350 "Expected buffer `%s' to have message \"%s\", "
351 "but the buffer did not have any messages.")
353 (with-current-buffer buffer
354 (let ((messages (split-string
355 (buffer-substring-no-properties
359 (let ((message (car (nthcdr (- (length messages) 2) messages))))
360 (when (not (equal message expected))
364 "Expected buffer `%s' to have message \"%s\", "
365 "but instead it was \"%s\"")
369 (defun context-coloring-test-assert-no-message (buffer)
370 "Assert that BUFFER has no message."
371 (when (get-buffer buffer)
372 (ert-fail (format (concat "Expected buffer `%s' to have no messages, "
375 (with-current-buffer buffer
378 (defun context-coloring-test-kill-buffer (buffer)
379 "Kill BUFFER if it exists."
380 (when (get-buffer buffer) (kill-buffer buffer)))
382 (defun context-coloring-test-assert-face (level foreground &optional negate)
383 "Assert that a face for LEVEL exists and that its `:foreground'
384 is FOREGROUND, or the inverse if NEGATE is non-nil."
385 (let* ((face (context-coloring-level-face level))
387 (when (not (or negate
389 (ert-fail (format (concat "Expected face for level `%s' to exist; "
392 (setq actual-foreground (face-attribute face :foreground))
393 (when (funcall (if negate 'identity 'not)
394 (string-equal foreground actual-foreground))
395 (ert-fail (format (concat "Expected face for level `%s' "
396 "%sto have foreground `%s'; "
399 (if negate "not " "") foreground
401 "did" (format "was `%s'" actual-foreground)))))))
403 (defun context-coloring-test-assert-not-face (&rest arguments)
404 "Assert that LEVEL does not have a face with `:foreground'
405 FOREGROUND. Apply ARGUMENTS to
406 `context-coloring-test-assert-face', see that function."
407 (apply 'context-coloring-test-assert-face
408 (append arguments '(t))))
410 (defun context-coloring-test-assert-error (body error-message)
411 "Assert that BODY signals ERROR-MESSAGE."
412 (let ((error-signaled-p nil))
417 (setq error-signaled-p t)
418 (when (not (string-equal (cadr err) error-message))
419 (ert-fail (format (concat "Expected the error \"%s\" to be thrown, "
420 "but instead it was \"%s\".")
423 (when (not error-signaled-p)
424 (ert-fail "Expected an error to be thrown, but there wasn't."))))
426 (defun context-coloring-test-assert-trimmed (result expected)
427 (when (not (string-equal result expected))
428 (ert-fail "Expected string to be trimmed, but it wasn't.")))
433 (ert-deftest context-coloring-test-trim ()
434 (context-coloring-test-assert-trimmed (context-coloring-trim "") "")
435 (context-coloring-test-assert-trimmed (context-coloring-trim " ") "")
436 (context-coloring-test-assert-trimmed (context-coloring-trim "a") "a")
437 (context-coloring-test-assert-trimmed (context-coloring-trim " a") "a")
438 (context-coloring-test-assert-trimmed (context-coloring-trim "a ") "a")
439 (context-coloring-test-assert-trimmed (context-coloring-trim " a ") "a"))
441 (ert-deftest-async context-coloring-test-async-mode-startup (done)
442 (context-coloring-test-with-fixture-async
447 'context-coloring-colorize-hook
449 ;; If this runs we are implicitly successful; this test only confirms
450 ;; that colorization occurs on mode startup.
453 (context-coloring-mode))))
456 context-coloring-change-detection-mode
459 "Prevent `context-coloring-test-change-detection' from
460 having any unintentional side-effects on mode support.")
462 ;; Simply cannot figure out how to trigger an idle timer; would much rather test
463 ;; that. But (current-idle-time) always returns nil in these tests.
464 (ert-deftest-async context-coloring-test-change-detection (done)
465 (context-coloring-define-dispatch
467 :modes '(context-coloring-change-detection-mode)
469 :command "node test/binaries/noop")
470 (context-coloring-test-with-fixture-async
473 (context-coloring-change-detection-mode)
475 'context-coloring-colorize-hook
477 (setq context-coloring-colorize-hook nil)
479 'context-coloring-colorize-hook
484 (set-window-buffer (selected-window) (current-buffer))
485 (context-coloring-maybe-colorize (current-buffer))))
486 (context-coloring-mode))))
488 (ert-deftest context-coloring-test-check-version ()
489 (when (not (context-coloring-check-version "2.1.3" "3.0.1"))
490 (ert-fail "Expected version 3.0.1 to satisfy 2.1.3, but it didn't."))
491 (when (context-coloring-check-version "3.0.1" "2.1.3")
492 (ert-fail "Expected version 2.1.3 not to satisfy 3.0.1, but it did.")))
494 (ert-deftest context-coloring-test-unsupported-mode ()
495 (context-coloring-test-with-fixture
497 (context-coloring-mode)
498 (context-coloring-test-assert-message
499 "Context coloring is not available for this major mode"
503 context-coloring-test-define-dispatch-error-mode
506 "Prevent `context-coloring-test-define-dispatch-error' from
507 having any unintentional side-effects on mode support.")
509 (ert-deftest context-coloring-test-define-dispatch-error ()
510 (context-coloring-test-assert-error
512 (context-coloring-define-dispatch
513 'define-dispatch-no-modes))
514 "No mode defined for dispatch")
515 (context-coloring-test-assert-error
517 (context-coloring-define-dispatch
518 'define-dispatch-no-strategy
519 :modes '(context-coloring-test-define-dispatch-error-mode)))
520 "No colorizer, scopifier or command defined for dispatch"))
523 context-coloring-test-define-dispatch-scopifier-mode
526 "Prevent `context-coloring-test-define-dispatch-scopifier' from
527 having any unintentional side-effects on mode support.")
529 (ert-deftest context-coloring-test-define-dispatch-scopifier ()
530 (context-coloring-define-dispatch
531 'define-dispatch-scopifier
532 :modes '(context-coloring-test-define-dispatch-scopifier-mode)
533 :scopifier (lambda () (vector)))
535 (context-coloring-test-define-dispatch-scopifier-mode)
536 (context-coloring-mode)
537 (context-coloring-colorize)))
540 context-coloring-test-missing-executable-mode
543 "Prevent `context-coloring-test-define-dispatch-scopifier' from
544 having any unintentional side-effects on mode support.")
546 (ert-deftest context-coloring-test-missing-executable ()
547 (context-coloring-define-dispatch
549 :modes '(context-coloring-test-missing-executable-mode)
551 :executable "__should_not_exist__")
553 (context-coloring-test-missing-executable-mode)
554 (context-coloring-mode)))
557 context-coloring-test-unsupported-version-mode
560 "Prevent `context-coloring-test-unsupported-version' from
561 having any unintentional side-effects on mode support.")
563 (ert-deftest-async context-coloring-test-unsupported-version (done)
564 (context-coloring-define-dispatch
566 :modes '(context-coloring-test-unsupported-version-mode)
568 :command "node test/binaries/outta-date"
570 (context-coloring-test-with-fixture-async
573 (context-coloring-test-unsupported-version-mode)
575 'context-coloring-check-scopifier-version-hook
579 ;; Normally the executable would be something like "outta-date"
580 ;; rather than "node".
581 (context-coloring-test-assert-message
582 "Update to the minimum version of \"node\" (v2.1.3)"
586 (context-coloring-mode))))
589 context-coloring-test-disable-mode-mode
592 "Prevent `context-coloring-test-disable-mode' from having any
593 unintentional side-effects on mode support.")
595 (ert-deftest-async context-coloring-test-disable-mode (done)
597 (context-coloring-define-dispatch
599 :modes '(context-coloring-test-disable-mode-mode)
601 :command "node test/binaries/noop"
604 (context-coloring-test-with-fixture-async
609 (context-coloring-test-disable-mode-mode)
610 (context-coloring-mode)
611 (context-coloring-mode -1)
612 (when (not torn-down)
613 (ert-fail "Expected teardown function to have been called, but it wasn't.")))
617 (defvar context-coloring-test-theme-index 0
618 "Unique index for unique theme names.")
620 (defun context-coloring-test-get-next-theme ()
621 "Return a unique symbol for a throwaway theme."
623 (intern (format "context-coloring-test-theme-%s"
624 context-coloring-test-theme-index))
625 (setq context-coloring-test-theme-index
626 (+ context-coloring-test-theme-index 1))))
628 (defun context-coloring-test-assert-theme-originally-set-p
629 (settings &optional negate)
630 "Assert that `context-coloring-theme-originally-set-p' returns
631 t for a theme with SETTINGS, or the inverse if NEGATE is
633 (let ((theme (context-coloring-test-get-next-theme)))
634 (put theme 'theme-settings settings)
635 (when (funcall (if negate 'identity 'not)
636 (context-coloring-theme-originally-set-p theme))
637 (ert-fail (format (concat "Expected theme `%s' with settings `%s' "
638 "%sto be considered to have defined a level, "
641 (if negate "not " "")
642 (if negate "was" "wasn't"))))))
644 (defun context-coloring-test-assert-not-theme-originally-set-p (&rest arguments)
645 "Assert that `context-coloring-theme-originally-set-p' does not
646 return t for a theme with SETTINGS. Apply ARGUMENTS to
647 `context-coloring-test-assert-theme-originally-set-p', see that
649 (apply 'context-coloring-test-assert-theme-originally-set-p
650 (append arguments '(t))))
652 (ert-deftest context-coloring-test-theme-originally-set-p ()
653 (context-coloring-test-assert-theme-originally-set-p
654 '((theme-face context-coloring-level-0-face)))
655 (context-coloring-test-assert-theme-originally-set-p
657 (theme-face context-coloring-level-0-face)))
658 (context-coloring-test-assert-theme-originally-set-p
659 '((theme-face context-coloring-level-0-face)
661 (context-coloring-test-assert-not-theme-originally-set-p
662 '((theme-face face)))
665 (defun context-coloring-test-assert-theme-settings-highest-level
666 (settings expected-level)
667 "Assert that a theme with SETTINGS has the highest level
669 (let ((theme (context-coloring-test-get-next-theme)))
670 (put theme 'theme-settings settings)
671 (context-coloring-test-assert-theme-highest-level theme expected-level)))
673 (defun context-coloring-test-assert-theme-highest-level
674 (theme expected-level &optional negate)
675 "Assert that THEME has the highest level EXPECTED-LEVEL, or the
676 inverse if NEGATE is non-nil."
677 (let ((highest-level (context-coloring-theme-highest-level theme)))
678 (when (funcall (if negate 'identity 'not) (eq highest-level expected-level))
679 (ert-fail (format (concat "Expected theme with settings `%s' "
680 "%sto have a highest level of `%s', "
682 (get theme 'theme-settings)
683 (if negate "not " "") expected-level
684 (if negate "did" (format "was %s" highest-level)))))))
686 (defun context-coloring-test-assert-theme-not-highest-level (&rest arguments)
687 "Assert that THEME's highest level is not EXPECTED-LEVEL.
689 `context-coloring-test-assert-theme-highest-level', see that
691 (apply 'context-coloring-test-assert-theme-highest-level
692 (append arguments '(t))))
694 (ert-deftest context-coloring-test-theme-highest-level ()
695 (context-coloring-test-assert-theme-settings-highest-level
698 (context-coloring-test-assert-theme-settings-highest-level
699 '((theme-face context-coloring-level-0-face))
701 (context-coloring-test-assert-theme-settings-highest-level
702 '((theme-face context-coloring-level-1-face))
704 (context-coloring-test-assert-theme-settings-highest-level
705 '((theme-face context-coloring-level-1-face)
706 (theme-face context-coloring-level-0-face))
708 (context-coloring-test-assert-theme-settings-highest-level
709 '((theme-face context-coloring-level-0-face)
710 (theme-face context-coloring-level-1-face))
714 (defmacro context-coloring-test-deftest-define-theme (name &rest body)
715 "Define a test with name NAME and an automatically-generated
716 theme symbol available as a free variable `theme'. Side-effects
717 from enabling themes are reversed after BODY is executed and the
719 (declare (indent defun))
720 (let ((deftest-name (intern
721 (format "context-coloring-test-define-theme-%s" name))))
722 `(ert-deftest ,deftest-name ()
723 (context-coloring-test-kill-buffer "*Warnings*")
724 (context-coloring-test-setup)
725 (let ((theme (context-coloring-test-get-next-theme)))
730 (disable-theme theme)
731 (context-coloring-test-cleanup))))))
733 (defun context-coloring-test-deftheme (theme)
734 "Dynamically define theme THEME."
735 (eval (macroexpand `(deftheme ,theme))))
737 (context-coloring-test-deftest-define-theme additive
738 (context-coloring-test-deftheme theme)
739 (context-coloring-define-theme
743 (context-coloring-test-assert-no-message "*Warnings*")
745 (context-coloring-test-assert-no-message "*Warnings*")
746 (context-coloring-test-assert-face 0 "#aaaaaa")
747 (context-coloring-test-assert-face 1 "#bbbbbb"))
749 (defun context-coloring-test-assert-defined-warning (theme)
750 "Assert that a warning about colors already being defined for
751 theme THEME is signaled."
752 (context-coloring-test-assert-message
753 (format (concat "Warning (emacs): Context coloring colors for theme "
754 "`%s' are already defined")
758 (context-coloring-test-deftest-define-theme unintentional-override
759 (context-coloring-test-deftheme theme)
760 (custom-theme-set-faces
762 '(context-coloring-level-0-face ((t (:foreground "#aaaaaa"))))
763 '(context-coloring-level-1-face ((t (:foreground "#bbbbbb")))))
764 (context-coloring-define-theme
768 (context-coloring-test-assert-defined-warning theme)
769 (context-coloring-test-kill-buffer "*Warnings*")
771 (context-coloring-test-assert-defined-warning theme)
772 (context-coloring-test-assert-face 0 "#cccccc")
773 (context-coloring-test-assert-face 1 "#dddddd"))
775 (context-coloring-test-deftest-define-theme intentional-override
776 (context-coloring-test-deftheme theme)
777 (custom-theme-set-faces
779 '(context-coloring-level-0-face ((t (:foreground "#aaaaaa"))))
780 '(context-coloring-level-1-face ((t (:foreground "#bbbbbb")))))
781 (context-coloring-define-theme
786 (context-coloring-test-assert-no-message "*Warnings*")
788 (context-coloring-test-assert-no-message "*Warnings*")
789 (context-coloring-test-assert-face 0 "#cccccc")
790 (context-coloring-test-assert-face 1 "#dddddd"))
792 (context-coloring-test-deftest-define-theme pre-recede
793 (context-coloring-define-theme
798 (context-coloring-test-deftheme theme)
799 (custom-theme-set-faces
801 '(context-coloring-level-0-face ((t (:foreground "#cccccc"))))
802 '(context-coloring-level-1-face ((t (:foreground "#dddddd")))))
804 (context-coloring-test-assert-no-message "*Warnings*")
805 (context-coloring-test-assert-face 0 "#cccccc")
806 (context-coloring-test-assert-face 1 "#dddddd"))
808 (context-coloring-test-deftest-define-theme pre-recede-delayed-application
809 (context-coloring-define-theme
814 (context-coloring-test-deftheme theme)
816 (context-coloring-test-assert-no-message "*Warnings*")
817 (context-coloring-test-assert-face 0 "#aaaaaa")
818 (context-coloring-test-assert-face 1 "#bbbbbb"))
820 (context-coloring-test-deftest-define-theme post-recede
821 (context-coloring-test-deftheme theme)
822 (custom-theme-set-faces
824 '(context-coloring-level-0-face ((t (:foreground "#aaaaaa"))))
825 '(context-coloring-level-1-face ((t (:foreground "#bbbbbb")))))
826 (context-coloring-define-theme
831 (context-coloring-test-assert-no-message "*Warnings*")
832 (context-coloring-test-assert-face 0 "#aaaaaa")
833 (context-coloring-test-assert-face 1 "#bbbbbb")
835 (context-coloring-test-assert-no-message "*Warnings*")
836 (context-coloring-test-assert-face 0 "#aaaaaa")
837 (context-coloring-test-assert-face 1 "#bbbbbb"))
839 (context-coloring-test-deftest-define-theme recede-not-defined
840 (context-coloring-test-deftheme theme)
841 (custom-theme-set-faces
843 '(foo-face ((t (:foreground "#ffffff")))))
844 (context-coloring-define-theme
849 (context-coloring-test-assert-no-message "*Warnings*")
850 (context-coloring-test-assert-face 0 "#aaaaaa")
851 (context-coloring-test-assert-face 1 "#bbbbbb")
853 (context-coloring-test-assert-no-message "*Warnings*")
854 (context-coloring-test-assert-face 0 "#aaaaaa")
855 (context-coloring-test-assert-face 1 "#bbbbbb"))
857 (context-coloring-test-deftest-define-theme unintentional-obstinance
858 (context-coloring-define-theme
862 (context-coloring-test-deftheme theme)
863 (custom-theme-set-faces
865 '(context-coloring-level-0-face ((t (:foreground "#cccccc"))))
866 '(context-coloring-level-1-face ((t (:foreground "#dddddd")))))
868 (context-coloring-test-assert-defined-warning theme)
869 (context-coloring-test-assert-face 0 "#aaaaaa")
870 (context-coloring-test-assert-face 1 "#bbbbbb"))
872 (context-coloring-test-deftest-define-theme intentional-obstinance
873 (context-coloring-define-theme
878 (context-coloring-test-deftheme theme)
879 (custom-theme-set-faces
881 '(context-coloring-level-0-face ((t (:foreground "#cccccc"))))
882 '(context-coloring-level-1-face ((t (:foreground "#dddddd")))))
884 (context-coloring-test-assert-no-message "*Warnings*")
885 (context-coloring-test-assert-face 0 "#aaaaaa")
886 (context-coloring-test-assert-face 1 "#bbbbbb"))
888 (defun context-coloring-test-assert-maximum-face (maximum &optional negate)
889 "Assert that `context-coloring-maximum-face' is MAXIMUM, or the
890 inverse if NEGATE is non-nil."
891 (when (funcall (if negate 'identity 'not)
892 (eq context-coloring-maximum-face maximum))
893 (ert-fail (format (concat "Expected `context-coloring-maximum-face' "
896 (if negate "not " "") maximum
899 (format "was `%s'" context-coloring-maximum-face))))))
901 (defun context-coloring-test-assert-not-maximum-face (&rest arguments)
902 "Assert that `context-coloring-maximum-face' is not MAXIMUM.
903 Apply ARGUMENTS to `context-coloring-test-assert-maximum-face',
905 (apply 'context-coloring-test-assert-maximum-face
906 (append arguments '(t))))
908 (context-coloring-test-deftest-define-theme disable-cascade
909 (let ((maximum-face-value 9999))
910 (setq context-coloring-maximum-face maximum-face-value)
911 (context-coloring-test-deftheme theme)
912 (context-coloring-define-theme
916 (let ((second-theme (context-coloring-test-get-next-theme)))
917 (context-coloring-test-deftheme second-theme)
918 (context-coloring-define-theme
923 (let ((third-theme (context-coloring-test-get-next-theme)))
924 (context-coloring-test-deftheme third-theme)
925 (context-coloring-define-theme
932 (enable-theme second-theme)
933 (enable-theme third-theme)
934 (disable-theme third-theme)
935 (context-coloring-test-assert-face 0 "#cccccc")
936 (context-coloring-test-assert-face 1 "#dddddd")
937 (context-coloring-test-assert-face 2 "#eeeeee")
938 (context-coloring-test-assert-maximum-face 2))
939 (disable-theme second-theme)
940 (context-coloring-test-assert-face 0 "#aaaaaa")
941 (context-coloring-test-assert-face 1 "#bbbbbb")
942 (context-coloring-test-assert-maximum-face 1))
943 (disable-theme theme)
944 (context-coloring-test-assert-not-face 0 "#aaaaaa")
945 (context-coloring-test-assert-not-face 1 "#bbbbbb")
946 (context-coloring-test-assert-maximum-face
947 maximum-face-value)))
949 (defun context-coloring-test-js-function-scopes ()
950 "Test fixtures/functions-scopes.js."
951 (context-coloring-test-assert-region-level 1 9 0)
952 (context-coloring-test-assert-region-level 9 23 1)
953 (context-coloring-test-assert-region-level 23 25 0)
954 (context-coloring-test-assert-region-level 25 34 1)
955 (context-coloring-test-assert-region-level 34 35 0)
956 (context-coloring-test-assert-region-level 35 52 1)
957 (context-coloring-test-assert-region-level 52 66 2)
958 (context-coloring-test-assert-region-level 66 72 1)
959 (context-coloring-test-assert-region-level 72 81 2)
960 (context-coloring-test-assert-region-level 81 82 1)
961 (context-coloring-test-assert-region-level 82 87 2)
962 (context-coloring-test-assert-region-level 87 89 1))
964 (context-coloring-test-deftest-js-mode function-scopes)
965 (context-coloring-test-deftest-js2-mode function-scopes)
967 (defun context-coloring-test-js-global ()
968 "Test fixtures/global.js."
969 (context-coloring-test-assert-region-level 20 28 1)
970 (context-coloring-test-assert-region-level 28 35 0)
971 (context-coloring-test-assert-region-level 35 41 1))
973 (context-coloring-test-deftest-js-mode global)
974 (context-coloring-test-deftest-js2-mode global)
976 (defun context-coloring-test-js-block-scopes ()
977 "Test fixtures/block-scopes.js."
978 (context-coloring-test-assert-region-level 20 64 1)
979 (setq context-coloring-js-block-scopes t)
980 (context-coloring-colorize)
981 (context-coloring-test-assert-region-level 20 27 1)
982 (context-coloring-test-assert-region-level 27 41 2)
983 (context-coloring-test-assert-region-level 41 42 1)
984 (context-coloring-test-assert-region-level 42 64 2))
986 (context-coloring-test-deftest-js2-mode block-scopes)
988 (defun context-coloring-test-js-catch ()
989 "Test fixtures/js-catch.js."
990 (context-coloring-test-assert-region-level 20 27 1)
991 (context-coloring-test-assert-region-level 27 51 2)
992 (context-coloring-test-assert-region-level 51 52 1)
993 (context-coloring-test-assert-region-level 52 73 2)
994 (context-coloring-test-assert-region-level 73 101 3)
995 (context-coloring-test-assert-region-level 101 102 1)
996 (context-coloring-test-assert-region-level 102 117 3)
997 (context-coloring-test-assert-region-level 117 123 2))
999 (context-coloring-test-deftest-js-mode catch)
1000 (context-coloring-test-deftest-js2-mode catch)
1002 (defun context-coloring-test-js-key-names ()
1003 "Test fixtures/key-names.js."
1004 (context-coloring-test-assert-region-level 20 63 1))
1006 (context-coloring-test-deftest-js-mode key-names)
1007 (context-coloring-test-deftest-js2-mode key-names)
1009 (defun context-coloring-test-js-property-lookup ()
1010 "Test fixtures/property-lookup.js."
1011 (context-coloring-test-assert-region-level 20 26 0)
1012 (context-coloring-test-assert-region-level 26 38 1)
1013 (context-coloring-test-assert-region-level 38 44 0)
1014 (context-coloring-test-assert-region-level 44 52 1)
1015 (context-coloring-test-assert-region-level 57 63 0)
1016 (context-coloring-test-assert-region-level 63 74 1))
1018 (context-coloring-test-deftest-js-mode property-lookup)
1019 (context-coloring-test-deftest-js2-mode property-lookup)
1021 (defun context-coloring-test-js-key-values ()
1022 "Test fixtures/key-values.js."
1023 (context-coloring-test-assert-region-level 78 79 1))
1025 (context-coloring-test-deftest-js-mode key-values)
1026 (context-coloring-test-deftest-js2-mode key-values)
1028 (defun context-coloring-test-js-syntactic-comments-and-strings ()
1029 "Test comments and strings."
1030 (context-coloring-test-assert-region-level 1 8 0)
1031 (context-coloring-test-assert-region-comment-delimiter 9 12)
1032 (context-coloring-test-assert-region-comment 12 16)
1033 (context-coloring-test-assert-region-comment-delimiter 17 20)
1034 (context-coloring-test-assert-region-comment 20 27)
1035 (context-coloring-test-assert-region-string 28 40)
1036 (context-coloring-test-assert-region-level 40 41 0))
1038 (defun context-coloring-test-js-syntactic-comments-and-strings-setup ()
1039 (setq context-coloring-syntactic-comments t)
1040 (setq context-coloring-syntactic-strings t))
1042 (context-coloring-test-deftest-js-mode syntactic-comments-and-strings
1043 :fixture-name comments-and-strings)
1044 (context-coloring-test-deftest-js2-mode syntactic-comments-and-strings
1045 :fixture-name comments-and-strings)
1047 (defalias 'context-coloring-test-js-comments-and-strings
1048 'context-coloring-test-js-syntactic-comments-and-strings
1049 "Test comments and strings. Deprecated.")
1051 (defun context-coloring-test-js-comments-and-strings-setup ()
1052 "Setup comments and strings. Deprecated."
1053 (setq context-coloring-comments-and-strings t))
1055 (context-coloring-test-deftest-js-mode comments-and-strings)
1056 (context-coloring-test-deftest-js2-mode comments-and-strings)
1058 (defun context-coloring-test-js-syntactic-comments ()
1059 "Test syntactic comments."
1060 (context-coloring-test-assert-region-level 1 8 0)
1061 (context-coloring-test-assert-region-comment-delimiter 9 12)
1062 (context-coloring-test-assert-region-comment 12 16)
1063 (context-coloring-test-assert-region-comment-delimiter 17 20)
1064 (context-coloring-test-assert-region-comment 20 27)
1065 (context-coloring-test-assert-region-level 28 41 0))
1067 (defun context-coloring-test-js-syntactic-comments-setup ()
1068 "Setup syntactic comments."
1069 (setq context-coloring-syntactic-comments t))
1071 (context-coloring-test-deftest-js-mode syntactic-comments
1072 :fixture-name comments-and-strings)
1073 (context-coloring-test-deftest-js2-mode syntactic-comments
1074 :fixture-name comments-and-strings)
1076 (defun context-coloring-test-js-syntactic-strings ()
1077 "Test syntactic strings."
1078 (context-coloring-test-assert-region-level 1 28 0)
1079 (context-coloring-test-assert-region-string 28 40)
1080 (context-coloring-test-assert-region-level 40 41 0))
1082 (defun context-coloring-test-js-syntactic-strings-setup ()
1083 "Setup syntactic strings."
1084 (setq context-coloring-syntactic-strings t))
1086 (context-coloring-test-deftest-js-mode syntactic-strings
1087 :fixture-name comments-and-strings)
1088 (context-coloring-test-deftest-js2-mode syntactic-strings
1089 :fixture-name comments-and-strings)
1091 ;; As long as `add-text-properties' doesn't signal an error, this test passes.
1092 (defun context-coloring-test-js-unterminated-comment ()
1093 "Test unterminated multiline comments.")
1095 (context-coloring-test-deftest-js2-mode unterminated-comment)
1097 (context-coloring-test-deftest-emacs-lisp-mode defun
1099 (context-coloring-test-assert-coloring "
1100 111111 000 1111 111 111111111 1111
1101 11 111 111 111 000011
1108 (context-coloring-test-deftest-emacs-lisp-mode lambda
1110 (context-coloring-test-assert-coloring "
1111 00000000 1111111 1111
1112 11111111 11 2222222 2222
1113 222 22 12 2221 111 0 00")))
1115 (context-coloring-test-deftest-emacs-lisp-mode quote
1117 (context-coloring-test-assert-coloring "
1121 111111 1 1111111111 11 111 1 111 1 00001 10000 11 00001 1 100001111")))
1123 (context-coloring-test-deftest-emacs-lisp-mode comment
1125 ;; Just check that the comment isn't parsed syntactically.
1126 (context-coloring-test-assert-coloring "
1128 (xx (x xxxxx-xxxx xx) ;;;;;;;;;;
1129 11 00000-0000 11))) ;;;;;;;;;;"))
1131 (setq context-coloring-syntactic-comments t)))
1133 (context-coloring-test-deftest-emacs-lisp-mode string
1135 (context-coloring-test-assert-coloring "
1137 (xxxxxx x x sss 1 0 sssss 0 1 sssssss11"))
1139 (setq context-coloring-syntactic-strings t)))
1141 (context-coloring-test-deftest-emacs-lisp-mode ignored
1143 (context-coloring-test-assert-coloring "
1145 (x x 1 11 11 111 11 1 111 (1 1 1)))")))
1147 (context-coloring-test-deftest-emacs-lisp-mode let
1149 (context-coloring-test-assert-coloring "
1157 1111 1 1 1 000011")))
1159 (context-coloring-test-deftest-emacs-lisp-mode let*
1161 (context-coloring-test-assert-coloring "
1165 1111 1 1 1 0 0 00001
1171 2222 1 1 2 2 2 000022
1172 1111 1 1 1 0 0 000011")))
1174 (context-coloring-test-deftest-emacs-lisp-mode depth
1176 (let ((context-coloring-emacs-lisp-iterations-per-pause 1))
1177 (context-coloring-colorize)
1178 (context-coloring-test-assert-coloring "
1182 (setq context-coloring-syntactic-comments t)
1183 (setq context-coloring-syntactic-strings t)))
1185 (provide 'context-coloring-test)
1187 ;;; context-coloring-test.el ends here