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-position-nil (position)
240 (context-coloring-test-assert-position-face position nil))
242 (defun context-coloring-test-assert-coloring (map)
243 "Assert that the current buffer's coloring matches MAP."
244 ;; Omit the superfluous, formatting-related leading newline. Can't use
245 ;; `save-excursion' here because if an assertion fails it will cause future
246 ;; tests to get messed up.
247 (goto-char (point-min))
248 (let* ((map (substring map 1))
252 (while (< index (length map))
253 (setq char-string (substring map index (1+ index)))
254 (setq char (string-to-char char-string))
263 (context-coloring-test-assert-position-level
264 (point) (string-to-number char-string))
268 (context-coloring-test-assert-position-comment (point))
270 ;; 'c' = Constant comment
272 (context-coloring-test-assert-position-constant-comment (point))
276 (context-coloring-test-assert-position-nil (point))
280 (context-coloring-test-assert-position-string (point))
284 (setq index (1+ index)))))
286 (defmacro context-coloring-test-assert-region (&rest body)
287 "Assert something about the face of points in a region.
288 Provides the free variables `i', `length', `point', `face' and
289 `actual-level' to the code in BODY."
291 (length (- end start)))
293 (let* ((point (+ i start))
294 (face (get-text-property point 'face)))
298 (defun context-coloring-test-assert-region-level (start end level)
299 "Assert that all points in the range [START, END) are of level
301 (context-coloring-test-assert-region
303 (when (not (when face
304 (let* ((face-string (symbol-name face))
305 (matches (string-match
306 context-coloring-level-face-regexp
309 (setq actual-level (string-to-number
310 (substring face-string
313 (= level actual-level)))))
314 (ert-fail (format (concat "Expected level in region [%s, %s), "
315 "which is \"%s\", to be %s; "
316 "but at point %s, it was %s")
318 (buffer-substring-no-properties start end) level
319 point actual-level))))))
321 (defun context-coloring-test-assert-region-face (start end expected-face)
322 "Assert that all points in the range [START, END) have the face
324 (context-coloring-test-assert-region
325 (when (not (eq face expected-face))
326 (ert-fail (format (concat "Expected face in region [%s, %s), "
327 "which is \"%s\", to be %s; "
328 "but at point %s, it was %s")
330 (buffer-substring-no-properties start end) expected-face
333 (defun context-coloring-test-assert-region-comment-delimiter (start end)
334 "Assert that all points in the range [START, END) have
335 `font-lock-comment-delimiter-face'."
336 (context-coloring-test-assert-region-face
337 start end 'font-lock-comment-delimiter-face))
339 (defun context-coloring-test-assert-region-comment (start end)
340 "Assert that all points in the range [START, END) have
341 `font-lock-comment-face'."
342 (context-coloring-test-assert-region-face
343 start end 'font-lock-comment-face))
345 (defun context-coloring-test-assert-region-string (start end)
346 "Assert that all points in the range [START, END) have
347 `font-lock-string-face'."
348 (context-coloring-test-assert-region-face
349 start end 'font-lock-string-face))
351 (defun context-coloring-test-get-last-message ()
352 (let ((messages (split-string
353 (buffer-substring-no-properties
357 (car (nthcdr (- (length messages) 2) messages))))
359 (defun context-coloring-test-assert-message (expected buffer)
360 "Assert that message EXPECTED is at the end of BUFFER."
361 (when (null (get-buffer buffer))
365 "Expected buffer `%s' to have message \"%s\", "
366 "but the buffer did not have any messages.")
368 (with-current-buffer buffer
369 (let ((message (context-coloring-test-get-last-message)))
370 (when (not (equal message expected))
374 "Expected buffer `%s' to have message \"%s\", "
375 "but instead it was \"%s\"")
379 (defun context-coloring-test-assert-not-message (expected buffer)
380 "Assert that message EXPECTED is not at the end of BUFFER."
381 (when (get-buffer buffer)
382 (with-current-buffer buffer
383 (let ((message (context-coloring-test-get-last-message)))
384 (when (equal message expected)
388 "Expected buffer `%s' not to have message \"%s\", "
390 buffer expected)))))))
392 (defun context-coloring-test-assert-no-message (buffer)
393 "Assert that BUFFER has no message."
394 (when (get-buffer buffer)
395 (ert-fail (format (concat "Expected buffer `%s' to have no messages, "
398 (with-current-buffer buffer
401 (defun context-coloring-test-kill-buffer (buffer)
402 "Kill BUFFER if it exists."
403 (when (get-buffer buffer) (kill-buffer buffer)))
405 (defun context-coloring-test-assert-face (level foreground &optional negate)
406 "Assert that a face for LEVEL exists and that its `:foreground'
407 is FOREGROUND, or the inverse if NEGATE is non-nil."
408 (let* ((face (context-coloring-level-face level))
410 (when (not (or negate
412 (ert-fail (format (concat "Expected face for level `%s' to exist; "
415 (setq actual-foreground (face-attribute face :foreground))
416 (when (funcall (if negate 'identity 'not)
417 (string-equal foreground actual-foreground))
418 (ert-fail (format (concat "Expected face for level `%s' "
419 "%sto have foreground `%s'; "
422 (if negate "not " "") foreground
424 "did" (format "was `%s'" actual-foreground)))))))
426 (defun context-coloring-test-assert-not-face (&rest arguments)
427 "Assert that LEVEL does not have a face with `:foreground'
428 FOREGROUND. Apply ARGUMENTS to
429 `context-coloring-test-assert-face', see that function."
430 (apply 'context-coloring-test-assert-face
431 (append arguments '(t))))
433 (defun context-coloring-test-assert-error (body error-message)
434 "Assert that BODY signals ERROR-MESSAGE."
435 (let ((error-signaled-p nil))
440 (setq error-signaled-p t)
441 (when (not (string-equal (cadr err) error-message))
442 (ert-fail (format (concat "Expected the error \"%s\" to be thrown, "
443 "but instead it was \"%s\".")
446 (when (not error-signaled-p)
447 (ert-fail "Expected an error to be thrown, but there wasn't."))))
449 (defun context-coloring-test-assert-trimmed (result expected)
450 (when (not (string-equal result expected))
451 (ert-fail "Expected string to be trimmed, but it wasn't.")))
456 (ert-deftest context-coloring-test-trim ()
457 (context-coloring-test-assert-trimmed (context-coloring-trim "") "")
458 (context-coloring-test-assert-trimmed (context-coloring-trim " ") "")
459 (context-coloring-test-assert-trimmed (context-coloring-trim "a") "a")
460 (context-coloring-test-assert-trimmed (context-coloring-trim " a") "a")
461 (context-coloring-test-assert-trimmed (context-coloring-trim "a ") "a")
462 (context-coloring-test-assert-trimmed (context-coloring-trim " a ") "a"))
464 (ert-deftest-async context-coloring-test-async-mode-startup (done)
465 (context-coloring-test-with-fixture-async
470 'context-coloring-colorize-hook
472 ;; If this runs we are implicitly successful; this test only confirms
473 ;; that colorization occurs on mode startup.
476 (context-coloring-mode))))
479 context-coloring-change-detection-mode
482 "Prevent `context-coloring-test-change-detection' from
483 having any unintentional side-effects on mode support.")
485 ;; Simply cannot figure out how to trigger an idle timer; would much rather test
486 ;; that. But (current-idle-time) always returns nil in these tests.
487 (ert-deftest-async context-coloring-test-change-detection (done)
488 (context-coloring-define-dispatch
490 :modes '(context-coloring-change-detection-mode)
492 :command "node test/binaries/noop")
493 (context-coloring-test-with-fixture-async
496 (context-coloring-change-detection-mode)
498 'context-coloring-colorize-hook
500 (setq context-coloring-colorize-hook nil)
502 'context-coloring-colorize-hook
507 (set-window-buffer (selected-window) (current-buffer))
508 (context-coloring-maybe-colorize (current-buffer))))
509 (context-coloring-mode))))
511 (ert-deftest context-coloring-test-check-version ()
512 (when (not (context-coloring-check-version "2.1.3" "3.0.1"))
513 (ert-fail "Expected version 3.0.1 to satisfy 2.1.3, but it didn't."))
514 (when (context-coloring-check-version "3.0.1" "2.1.3")
515 (ert-fail "Expected version 2.1.3 not to satisfy 3.0.1, but it did.")))
517 (ert-deftest context-coloring-test-unsupported-mode ()
518 (context-coloring-test-with-fixture
520 (context-coloring-mode)
521 (context-coloring-test-assert-message
522 "Context coloring is not available for this major mode"
525 (ert-deftest context-coloring-test-derived-mode ()
526 (context-coloring-test-with-fixture
528 (lisp-interaction-mode)
529 (context-coloring-mode)
530 (context-coloring-test-assert-not-message
531 "Context coloring is not available for this major mode"
535 context-coloring-test-define-dispatch-error-mode
538 "Prevent `context-coloring-test-define-dispatch-error' from
539 having any unintentional side-effects on mode support.")
541 (ert-deftest context-coloring-test-define-dispatch-error ()
542 (context-coloring-test-assert-error
544 (context-coloring-define-dispatch
545 'define-dispatch-no-modes))
546 "No mode defined for dispatch")
547 (context-coloring-test-assert-error
549 (context-coloring-define-dispatch
550 'define-dispatch-no-strategy
551 :modes '(context-coloring-test-define-dispatch-error-mode)))
552 "No colorizer, scopifier or command defined for dispatch"))
555 context-coloring-test-define-dispatch-scopifier-mode
558 "Prevent `context-coloring-test-define-dispatch-scopifier' from
559 having any unintentional side-effects on mode support.")
561 (ert-deftest context-coloring-test-define-dispatch-scopifier ()
562 (context-coloring-define-dispatch
563 'define-dispatch-scopifier
564 :modes '(context-coloring-test-define-dispatch-scopifier-mode)
565 :scopifier (lambda () (vector)))
567 (context-coloring-test-define-dispatch-scopifier-mode)
568 (context-coloring-mode)
569 (context-coloring-colorize)))
572 context-coloring-test-missing-executable-mode
575 "Prevent `context-coloring-test-define-dispatch-scopifier' from
576 having any unintentional side-effects on mode support.")
578 (ert-deftest context-coloring-test-missing-executable ()
579 (context-coloring-define-dispatch
581 :modes '(context-coloring-test-missing-executable-mode)
583 :executable "__should_not_exist__")
585 (context-coloring-test-missing-executable-mode)
586 (context-coloring-mode)))
589 context-coloring-test-unsupported-version-mode
592 "Prevent `context-coloring-test-unsupported-version' from
593 having any unintentional side-effects on mode support.")
595 (ert-deftest-async context-coloring-test-unsupported-version (done)
596 (context-coloring-define-dispatch
598 :modes '(context-coloring-test-unsupported-version-mode)
600 :command "node test/binaries/outta-date"
602 (context-coloring-test-with-fixture-async
605 (context-coloring-test-unsupported-version-mode)
607 'context-coloring-check-scopifier-version-hook
611 ;; Normally the executable would be something like "outta-date"
612 ;; rather than "node".
613 (context-coloring-test-assert-message
614 "Update to the minimum version of \"node\" (v2.1.3)"
618 (context-coloring-mode))))
621 context-coloring-test-disable-mode-mode
624 "Prevent `context-coloring-test-disable-mode' from having any
625 unintentional side-effects on mode support.")
627 (ert-deftest-async context-coloring-test-disable-mode (done)
629 (context-coloring-define-dispatch
631 :modes '(context-coloring-test-disable-mode-mode)
633 :command "node test/binaries/noop"
636 (context-coloring-test-with-fixture-async
641 (context-coloring-test-disable-mode-mode)
642 (context-coloring-mode)
643 (context-coloring-mode -1)
644 (when (not torn-down)
645 (ert-fail "Expected teardown function to have been called, but it wasn't.")))
649 (defvar context-coloring-test-theme-index 0
650 "Unique index for unique theme names.")
652 (defun context-coloring-test-get-next-theme ()
653 "Return a unique symbol for a throwaway theme."
655 (intern (format "context-coloring-test-theme-%s"
656 context-coloring-test-theme-index))
657 (setq context-coloring-test-theme-index
658 (+ context-coloring-test-theme-index 1))))
660 (defun context-coloring-test-assert-theme-originally-set-p
661 (settings &optional negate)
662 "Assert that `context-coloring-theme-originally-set-p' returns
663 t for a theme with SETTINGS, or the inverse if NEGATE is
665 (let ((theme (context-coloring-test-get-next-theme)))
666 (put theme 'theme-settings settings)
667 (when (funcall (if negate 'identity 'not)
668 (context-coloring-theme-originally-set-p theme))
669 (ert-fail (format (concat "Expected theme `%s' with settings `%s' "
670 "%sto be considered to have defined a level, "
673 (if negate "not " "")
674 (if negate "was" "wasn't"))))))
676 (defun context-coloring-test-assert-not-theme-originally-set-p (&rest arguments)
677 "Assert that `context-coloring-theme-originally-set-p' does not
678 return t for a theme with SETTINGS. Apply ARGUMENTS to
679 `context-coloring-test-assert-theme-originally-set-p', see that
681 (apply 'context-coloring-test-assert-theme-originally-set-p
682 (append arguments '(t))))
684 (ert-deftest context-coloring-test-theme-originally-set-p ()
685 (context-coloring-test-assert-theme-originally-set-p
686 '((theme-face context-coloring-level-0-face)))
687 (context-coloring-test-assert-theme-originally-set-p
689 (theme-face context-coloring-level-0-face)))
690 (context-coloring-test-assert-theme-originally-set-p
691 '((theme-face context-coloring-level-0-face)
693 (context-coloring-test-assert-not-theme-originally-set-p
694 '((theme-face face)))
697 (defun context-coloring-test-assert-theme-settings-highest-level
698 (settings expected-level)
699 "Assert that a theme with SETTINGS has the highest level
701 (let ((theme (context-coloring-test-get-next-theme)))
702 (put theme 'theme-settings settings)
703 (context-coloring-test-assert-theme-highest-level theme expected-level)))
705 (defun context-coloring-test-assert-theme-highest-level
706 (theme expected-level &optional negate)
707 "Assert that THEME has the highest level EXPECTED-LEVEL, or the
708 inverse if NEGATE is non-nil."
709 (let ((highest-level (context-coloring-theme-highest-level theme)))
710 (when (funcall (if negate 'identity 'not) (eq highest-level expected-level))
711 (ert-fail (format (concat "Expected theme with settings `%s' "
712 "%sto have a highest level of `%s', "
714 (get theme 'theme-settings)
715 (if negate "not " "") expected-level
716 (if negate "did" (format "was %s" highest-level)))))))
718 (defun context-coloring-test-assert-theme-not-highest-level (&rest arguments)
719 "Assert that THEME's highest level is not EXPECTED-LEVEL.
721 `context-coloring-test-assert-theme-highest-level', see that
723 (apply 'context-coloring-test-assert-theme-highest-level
724 (append arguments '(t))))
726 (ert-deftest context-coloring-test-theme-highest-level ()
727 (context-coloring-test-assert-theme-settings-highest-level
730 (context-coloring-test-assert-theme-settings-highest-level
731 '((theme-face context-coloring-level-0-face))
733 (context-coloring-test-assert-theme-settings-highest-level
734 '((theme-face context-coloring-level-1-face))
736 (context-coloring-test-assert-theme-settings-highest-level
737 '((theme-face context-coloring-level-1-face)
738 (theme-face context-coloring-level-0-face))
740 (context-coloring-test-assert-theme-settings-highest-level
741 '((theme-face context-coloring-level-0-face)
742 (theme-face context-coloring-level-1-face))
746 (defmacro context-coloring-test-deftest-define-theme (name &rest body)
747 "Define a test with name NAME and an automatically-generated
748 theme symbol available as a free variable `theme'. Side-effects
749 from enabling themes are reversed after BODY is executed and the
751 (declare (indent defun))
752 (let ((deftest-name (intern
753 (format "context-coloring-test-define-theme-%s" name))))
754 `(ert-deftest ,deftest-name ()
755 (context-coloring-test-kill-buffer "*Warnings*")
756 (context-coloring-test-setup)
757 (let ((theme (context-coloring-test-get-next-theme)))
762 (disable-theme theme)
763 (context-coloring-test-cleanup))))))
765 (defun context-coloring-test-deftheme (theme)
766 "Dynamically define theme THEME."
767 (eval (macroexpand `(deftheme ,theme))))
769 (context-coloring-test-deftest-define-theme additive
770 (context-coloring-test-deftheme theme)
771 (context-coloring-define-theme
775 (context-coloring-test-assert-no-message "*Warnings*")
777 (context-coloring-test-assert-no-message "*Warnings*")
778 (context-coloring-test-assert-face 0 "#aaaaaa")
779 (context-coloring-test-assert-face 1 "#bbbbbb"))
781 (defun context-coloring-test-assert-defined-warning (theme)
782 "Assert that a warning about colors already being defined for
783 theme THEME is signaled."
784 (context-coloring-test-assert-message
785 (format (concat "Warning (emacs): Context coloring colors for theme "
786 "`%s' are already defined")
790 (context-coloring-test-deftest-define-theme unintentional-override
791 (context-coloring-test-deftheme theme)
792 (custom-theme-set-faces
794 '(context-coloring-level-0-face ((t (:foreground "#aaaaaa"))))
795 '(context-coloring-level-1-face ((t (:foreground "#bbbbbb")))))
796 (context-coloring-define-theme
800 (context-coloring-test-assert-defined-warning theme)
801 (context-coloring-test-kill-buffer "*Warnings*")
803 (context-coloring-test-assert-defined-warning theme)
804 (context-coloring-test-assert-face 0 "#cccccc")
805 (context-coloring-test-assert-face 1 "#dddddd"))
807 (context-coloring-test-deftest-define-theme intentional-override
808 (context-coloring-test-deftheme theme)
809 (custom-theme-set-faces
811 '(context-coloring-level-0-face ((t (:foreground "#aaaaaa"))))
812 '(context-coloring-level-1-face ((t (:foreground "#bbbbbb")))))
813 (context-coloring-define-theme
818 (context-coloring-test-assert-no-message "*Warnings*")
820 (context-coloring-test-assert-no-message "*Warnings*")
821 (context-coloring-test-assert-face 0 "#cccccc")
822 (context-coloring-test-assert-face 1 "#dddddd"))
824 (context-coloring-test-deftest-define-theme pre-recede
825 (context-coloring-define-theme
830 (context-coloring-test-deftheme theme)
831 (custom-theme-set-faces
833 '(context-coloring-level-0-face ((t (:foreground "#cccccc"))))
834 '(context-coloring-level-1-face ((t (:foreground "#dddddd")))))
836 (context-coloring-test-assert-no-message "*Warnings*")
837 (context-coloring-test-assert-face 0 "#cccccc")
838 (context-coloring-test-assert-face 1 "#dddddd"))
840 (context-coloring-test-deftest-define-theme pre-recede-delayed-application
841 (context-coloring-define-theme
846 (context-coloring-test-deftheme theme)
848 (context-coloring-test-assert-no-message "*Warnings*")
849 (context-coloring-test-assert-face 0 "#aaaaaa")
850 (context-coloring-test-assert-face 1 "#bbbbbb"))
852 (context-coloring-test-deftest-define-theme post-recede
853 (context-coloring-test-deftheme theme)
854 (custom-theme-set-faces
856 '(context-coloring-level-0-face ((t (:foreground "#aaaaaa"))))
857 '(context-coloring-level-1-face ((t (:foreground "#bbbbbb")))))
858 (context-coloring-define-theme
863 (context-coloring-test-assert-no-message "*Warnings*")
864 (context-coloring-test-assert-face 0 "#aaaaaa")
865 (context-coloring-test-assert-face 1 "#bbbbbb")
867 (context-coloring-test-assert-no-message "*Warnings*")
868 (context-coloring-test-assert-face 0 "#aaaaaa")
869 (context-coloring-test-assert-face 1 "#bbbbbb"))
871 (context-coloring-test-deftest-define-theme recede-not-defined
872 (context-coloring-test-deftheme theme)
873 (custom-theme-set-faces
875 '(foo-face ((t (:foreground "#ffffff")))))
876 (context-coloring-define-theme
881 (context-coloring-test-assert-no-message "*Warnings*")
882 (context-coloring-test-assert-face 0 "#aaaaaa")
883 (context-coloring-test-assert-face 1 "#bbbbbb")
885 (context-coloring-test-assert-no-message "*Warnings*")
886 (context-coloring-test-assert-face 0 "#aaaaaa")
887 (context-coloring-test-assert-face 1 "#bbbbbb"))
889 (context-coloring-test-deftest-define-theme unintentional-obstinance
890 (context-coloring-define-theme
894 (context-coloring-test-deftheme theme)
895 (custom-theme-set-faces
897 '(context-coloring-level-0-face ((t (:foreground "#cccccc"))))
898 '(context-coloring-level-1-face ((t (:foreground "#dddddd")))))
900 (context-coloring-test-assert-defined-warning theme)
901 (context-coloring-test-assert-face 0 "#aaaaaa")
902 (context-coloring-test-assert-face 1 "#bbbbbb"))
904 (context-coloring-test-deftest-define-theme intentional-obstinance
905 (context-coloring-define-theme
910 (context-coloring-test-deftheme theme)
911 (custom-theme-set-faces
913 '(context-coloring-level-0-face ((t (:foreground "#cccccc"))))
914 '(context-coloring-level-1-face ((t (:foreground "#dddddd")))))
916 (context-coloring-test-assert-no-message "*Warnings*")
917 (context-coloring-test-assert-face 0 "#aaaaaa")
918 (context-coloring-test-assert-face 1 "#bbbbbb"))
920 (defun context-coloring-test-assert-maximum-face (maximum &optional negate)
921 "Assert that `context-coloring-maximum-face' is MAXIMUM, or the
922 inverse if NEGATE is non-nil."
923 (when (funcall (if negate 'identity 'not)
924 (eq context-coloring-maximum-face maximum))
925 (ert-fail (format (concat "Expected `context-coloring-maximum-face' "
928 (if negate "not " "") maximum
931 (format "was `%s'" context-coloring-maximum-face))))))
933 (defun context-coloring-test-assert-not-maximum-face (&rest arguments)
934 "Assert that `context-coloring-maximum-face' is not MAXIMUM.
935 Apply ARGUMENTS to `context-coloring-test-assert-maximum-face',
937 (apply 'context-coloring-test-assert-maximum-face
938 (append arguments '(t))))
940 (context-coloring-test-deftest-define-theme disable-cascade
941 (let ((maximum-face-value 9999))
942 (setq context-coloring-maximum-face maximum-face-value)
943 (context-coloring-test-deftheme theme)
944 (context-coloring-define-theme
948 (let ((second-theme (context-coloring-test-get-next-theme)))
949 (context-coloring-test-deftheme second-theme)
950 (context-coloring-define-theme
955 (let ((third-theme (context-coloring-test-get-next-theme)))
956 (context-coloring-test-deftheme third-theme)
957 (context-coloring-define-theme
964 (enable-theme second-theme)
965 (enable-theme third-theme)
966 (disable-theme third-theme)
967 (context-coloring-test-assert-face 0 "#cccccc")
968 (context-coloring-test-assert-face 1 "#dddddd")
969 (context-coloring-test-assert-face 2 "#eeeeee")
970 (context-coloring-test-assert-maximum-face 2))
971 (disable-theme second-theme)
972 (context-coloring-test-assert-face 0 "#aaaaaa")
973 (context-coloring-test-assert-face 1 "#bbbbbb")
974 (context-coloring-test-assert-maximum-face 1))
975 (disable-theme theme)
976 (context-coloring-test-assert-not-face 0 "#aaaaaa")
977 (context-coloring-test-assert-not-face 1 "#bbbbbb")
978 (context-coloring-test-assert-maximum-face
979 maximum-face-value)))
981 (defun context-coloring-test-js-function-scopes ()
982 "Test fixtures/functions-scopes.js."
983 (context-coloring-test-assert-region-level 1 9 0)
984 (context-coloring-test-assert-region-level 9 23 1)
985 (context-coloring-test-assert-region-level 23 25 0)
986 (context-coloring-test-assert-region-level 25 34 1)
987 (context-coloring-test-assert-region-level 34 35 0)
988 (context-coloring-test-assert-region-level 35 52 1)
989 (context-coloring-test-assert-region-level 52 66 2)
990 (context-coloring-test-assert-region-level 66 72 1)
991 (context-coloring-test-assert-region-level 72 81 2)
992 (context-coloring-test-assert-region-level 81 82 1)
993 (context-coloring-test-assert-region-level 82 87 2)
994 (context-coloring-test-assert-region-level 87 89 1))
996 (context-coloring-test-deftest-js-mode function-scopes)
997 (context-coloring-test-deftest-js2-mode function-scopes)
999 (defun context-coloring-test-js-global ()
1000 "Test fixtures/global.js."
1001 (context-coloring-test-assert-region-level 20 28 1)
1002 (context-coloring-test-assert-region-level 28 35 0)
1003 (context-coloring-test-assert-region-level 35 41 1))
1005 (context-coloring-test-deftest-js-mode global)
1006 (context-coloring-test-deftest-js2-mode global)
1008 (defun context-coloring-test-js-block-scopes ()
1009 "Test fixtures/block-scopes.js."
1010 (context-coloring-test-assert-region-level 20 64 1)
1011 (setq context-coloring-js-block-scopes t)
1012 (context-coloring-colorize)
1013 (context-coloring-test-assert-region-level 20 27 1)
1014 (context-coloring-test-assert-region-level 27 41 2)
1015 (context-coloring-test-assert-region-level 41 42 1)
1016 (context-coloring-test-assert-region-level 42 64 2))
1018 (context-coloring-test-deftest-js2-mode block-scopes)
1020 (defun context-coloring-test-js-catch ()
1021 "Test fixtures/js-catch.js."
1022 (context-coloring-test-assert-region-level 20 27 1)
1023 (context-coloring-test-assert-region-level 27 51 2)
1024 (context-coloring-test-assert-region-level 51 52 1)
1025 (context-coloring-test-assert-region-level 52 73 2)
1026 (context-coloring-test-assert-region-level 73 101 3)
1027 (context-coloring-test-assert-region-level 101 102 1)
1028 (context-coloring-test-assert-region-level 102 117 3)
1029 (context-coloring-test-assert-region-level 117 123 2))
1031 (context-coloring-test-deftest-js-mode catch)
1032 (context-coloring-test-deftest-js2-mode catch)
1034 (defun context-coloring-test-js-key-names ()
1035 "Test fixtures/key-names.js."
1036 (context-coloring-test-assert-region-level 20 63 1))
1038 (context-coloring-test-deftest-js-mode key-names)
1039 (context-coloring-test-deftest-js2-mode key-names)
1041 (defun context-coloring-test-js-property-lookup ()
1042 "Test fixtures/property-lookup.js."
1043 (context-coloring-test-assert-region-level 20 26 0)
1044 (context-coloring-test-assert-region-level 26 38 1)
1045 (context-coloring-test-assert-region-level 38 44 0)
1046 (context-coloring-test-assert-region-level 44 52 1)
1047 (context-coloring-test-assert-region-level 57 63 0)
1048 (context-coloring-test-assert-region-level 63 74 1))
1050 (context-coloring-test-deftest-js-mode property-lookup)
1051 (context-coloring-test-deftest-js2-mode property-lookup)
1053 (defun context-coloring-test-js-key-values ()
1054 "Test fixtures/key-values.js."
1055 (context-coloring-test-assert-region-level 78 79 1))
1057 (context-coloring-test-deftest-js-mode key-values)
1058 (context-coloring-test-deftest-js2-mode key-values)
1060 (defun context-coloring-test-js-syntactic-comments-and-strings ()
1061 "Test comments and strings."
1062 (context-coloring-test-assert-region-level 1 8 0)
1063 (context-coloring-test-assert-region-comment-delimiter 9 12)
1064 (context-coloring-test-assert-region-comment 12 16)
1065 (context-coloring-test-assert-region-comment-delimiter 17 20)
1066 (context-coloring-test-assert-region-comment 20 27)
1067 (context-coloring-test-assert-region-string 28 40)
1068 (context-coloring-test-assert-region-level 40 41 0))
1070 (defun context-coloring-test-js-syntactic-comments-and-strings-setup ()
1071 (setq context-coloring-syntactic-comments t)
1072 (setq context-coloring-syntactic-strings t))
1074 (context-coloring-test-deftest-js-mode syntactic-comments-and-strings
1075 :fixture-name comments-and-strings)
1076 (context-coloring-test-deftest-js2-mode syntactic-comments-and-strings
1077 :fixture-name comments-and-strings)
1079 (defalias 'context-coloring-test-js-comments-and-strings
1080 'context-coloring-test-js-syntactic-comments-and-strings
1081 "Test comments and strings. Deprecated.")
1083 (defun context-coloring-test-js-comments-and-strings-setup ()
1084 "Setup comments and strings. Deprecated."
1085 (setq context-coloring-comments-and-strings t))
1087 (context-coloring-test-deftest-js-mode comments-and-strings)
1088 (context-coloring-test-deftest-js2-mode comments-and-strings)
1090 (defun context-coloring-test-js-syntactic-comments ()
1091 "Test syntactic comments."
1092 (context-coloring-test-assert-region-level 1 8 0)
1093 (context-coloring-test-assert-region-comment-delimiter 9 12)
1094 (context-coloring-test-assert-region-comment 12 16)
1095 (context-coloring-test-assert-region-comment-delimiter 17 20)
1096 (context-coloring-test-assert-region-comment 20 27)
1097 (context-coloring-test-assert-region-level 28 41 0))
1099 (defun context-coloring-test-js-syntactic-comments-setup ()
1100 "Setup syntactic comments."
1101 (setq context-coloring-syntactic-comments t))
1103 (context-coloring-test-deftest-js-mode syntactic-comments
1104 :fixture-name comments-and-strings)
1105 (context-coloring-test-deftest-js2-mode syntactic-comments
1106 :fixture-name comments-and-strings)
1108 (defun context-coloring-test-js-syntactic-strings ()
1109 "Test syntactic strings."
1110 (context-coloring-test-assert-region-level 1 28 0)
1111 (context-coloring-test-assert-region-string 28 40)
1112 (context-coloring-test-assert-region-level 40 41 0))
1114 (defun context-coloring-test-js-syntactic-strings-setup ()
1115 "Setup syntactic strings."
1116 (setq context-coloring-syntactic-strings t))
1118 (context-coloring-test-deftest-js-mode syntactic-strings
1119 :fixture-name comments-and-strings)
1120 (context-coloring-test-deftest-js2-mode syntactic-strings
1121 :fixture-name comments-and-strings)
1123 ;; As long as `add-text-properties' doesn't signal an error, this test passes.
1124 (defun context-coloring-test-js-unterminated-comment ()
1125 "Test unterminated multiline comments.")
1127 (context-coloring-test-deftest-js2-mode unterminated-comment)
1129 (context-coloring-test-deftest-emacs-lisp-mode defun
1131 (context-coloring-test-assert-coloring "
1132 111111 000 1111 111 111111111 1111
1133 11 111 111 111 000011
1140 (context-coloring-test-deftest-emacs-lisp-mode lambda
1142 (context-coloring-test-assert-coloring "
1143 00000000 1111111 1111
1144 11111111 11 2222222 2222
1145 222 22 12 2221 111 0 00")))
1147 (context-coloring-test-deftest-emacs-lisp-mode quote
1149 (context-coloring-test-assert-coloring "
1153 111111 1 1111111111 11 111 1 111 1 00001 10000 11 00001 1 100001111")))
1155 (context-coloring-test-deftest-emacs-lisp-mode comment
1157 ;; Just check that the comment isn't parsed syntactically.
1158 (context-coloring-test-assert-coloring "
1160 (xx (x xxxxx-xxxx xx) ;;;;;;;;;;
1161 11 00000-0000 11))) ;;;;;;;;;;"))
1163 (setq context-coloring-syntactic-comments t)))
1165 (context-coloring-test-deftest-emacs-lisp-mode string
1167 (context-coloring-test-assert-coloring "
1169 (xxxxxx x x sss 1 0 sssss 0 1 sssssss11"))
1171 (setq context-coloring-syntactic-strings t)))
1173 (context-coloring-test-deftest-emacs-lisp-mode ignored
1175 (context-coloring-test-assert-coloring "
1177 (x x 1 11 11 111 11 1 111 (1 1 1)))")))
1179 (context-coloring-test-deftest-emacs-lisp-mode let
1181 (context-coloring-test-assert-coloring "
1189 1111 1 1 1 000011")))
1191 (context-coloring-test-deftest-emacs-lisp-mode let*
1193 (context-coloring-test-assert-coloring "
1197 1111 1 1 1 0 0 00001
1203 2222 1 1 2 2 2 000022
1204 1111 1 1 1 0 0 000011")))
1206 (defun context-coloring-test-insert-unread-space ()
1207 (setq unread-command-events (cons '(t . 32)
1208 unread-command-events)))
1210 (defun context-coloring-test-remove-faces ()
1211 (remove-text-properties (point-min) (point-max) '(face nil)))
1213 (context-coloring-test-deftest-emacs-lisp-mode iteration
1215 (let ((context-coloring-emacs-lisp-iterations-per-pause 1))
1216 (context-coloring-colorize)
1217 (context-coloring-test-assert-coloring "
1220 (context-coloring-test-remove-faces)
1221 (context-coloring-test-insert-unread-space)
1222 (context-coloring-colorize)
1223 ;; The first iteration will color the first part of the comment, but
1224 ;; that's it. Then it will be interrupted.
1225 (context-coloring-test-assert-coloring "
1229 (setq context-coloring-syntactic-comments t)
1230 (setq context-coloring-syntactic-strings t)))
1232 (provide 'context-coloring-test)
1234 ;;; context-coloring-test.el ends here