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-comments-and-strings nil))
49 (defun context-coloring-test-cleanup ()
50 "Cleanup after all tests."
51 (setq context-coloring-comments-and-strings t)
52 (setq context-coloring-syntactic-comments nil)
53 (setq context-coloring-syntactic-strings nil)
54 (setq context-coloring-js-block-scopes nil)
55 (setq context-coloring-colorize-hook nil)
56 (setq context-coloring-check-scopifier-version-hook nil)
57 (setq context-coloring-maximum-face 7)
58 (setq context-coloring-original-maximum-face
59 context-coloring-maximum-face))
61 (defmacro context-coloring-test-with-fixture (fixture &rest body)
62 "With the relative FIXTURE, evaluate BODY in a temporary
67 (context-coloring-test-setup)
68 (insert (context-coloring-test-read-file ,fixture))
70 (context-coloring-test-cleanup))))
72 (defun context-coloring-test-with-temp-buffer-async (callback)
73 "Create a temporary buffer, and evaluate CALLBACK there. A
74 teardown callback is passed to CALLBACK for it to invoke when it
76 (let ((previous-buffer (current-buffer))
77 (temp-buffer (generate-new-buffer " *temp*")))
78 (set-buffer temp-buffer)
82 (and (buffer-name temp-buffer)
83 (kill-buffer temp-buffer))
84 (set-buffer previous-buffer)))))
86 (defun context-coloring-test-with-fixture-async
87 (fixture callback &optional setup)
88 "With the relative FIXTURE, evaluate CALLBACK in a temporary
89 buffer. A teardown callback is passed to CALLBACK for it to
90 invoke when it is done. An optional SETUP callback can run
91 arbitrary code before the mode is invoked."
92 (context-coloring-test-with-temp-buffer-async
93 (lambda (done-with-temp-buffer)
94 (context-coloring-test-setup)
95 (when setup (funcall setup))
96 (insert (context-coloring-test-read-file fixture))
100 (context-coloring-test-cleanup)
101 (funcall done-with-temp-buffer))))))
104 ;;; Test defining utilities
106 (defun context-coloring-test-js-mode (fixture callback &optional setup)
107 "Use FIXTURE as the subject matter for test logic in CALLBACK.
108 Optionally, provide setup code to run before the mode is
109 instantiated in SETUP."
110 (context-coloring-test-with-fixture-async
112 (lambda (done-with-test)
114 (context-coloring-mode)
115 (context-coloring-colorize
117 (funcall callback done-with-test))))
120 (defmacro context-coloring-test-js2-mode (fixture setup &rest body)
121 "Use FIXTURE as the subject matter for test logic in BODY."
122 `(context-coloring-test-with-fixture
125 (setq js2-mode-show-parse-errors nil)
126 (setq js2-mode-show-strict-warnings nil)
128 (when ,setup (funcall ,setup))
129 (context-coloring-mode)
132 (cl-defmacro context-coloring-test-deftest-js-mode (name &key fixture-name)
133 "Define an asynchronous test for `js-mode' with the name NAME
134 in the typical format."
135 (declare (indent defun))
136 (let ((test-name (intern (format "context-coloring-test-js-mode-%s" name)))
137 (fixture (format "./fixtures/%s.js" (or fixture-name name)))
138 (function-name (intern-soft
139 (format "context-coloring-test-js-%s" name)))
140 (setup-function-name (intern-soft
142 "context-coloring-test-js-%s-setup" name))))
143 `(ert-deftest-async ,test-name (done)
144 (context-coloring-test-js-mode
151 ',setup-function-name))))
153 (cl-defmacro context-coloring-test-deftest-js2-mode (name &key fixture-name)
154 "Define a test for `js2-mode' with the name NAME in the typical
156 (declare (indent defun))
157 (let ((test-name (intern (format "context-coloring-test-js2-mode-%s" name)))
158 (fixture (format "./fixtures/%s.js" (or fixture-name name)))
159 (function-name (intern-soft
160 (format "context-coloring-test-js-%s" name)))
161 (setup-function-name (intern-soft
163 "context-coloring-test-js-%s-setup" name))))
164 `(ert-deftest ,test-name ()
165 (context-coloring-test-js2-mode
167 ',setup-function-name
171 ;;; Assertion functions
173 (defmacro context-coloring-test-assert-region (&rest body)
174 "Assert something about the face of points in a region.
175 Provides the free variables `i', `length', `point', `face' and
176 `actual-level' to the code in BODY."
178 (length (- end start)))
180 (let* ((point (+ i start))
181 (face (get-text-property point 'face)))
185 (defun context-coloring-test-assert-region-level (start end level)
186 "Assert that all points in the range [START, END) are of level
188 (context-coloring-test-assert-region
190 (when (not (when face
191 (let* ((face-string (symbol-name face))
192 (matches (string-match
193 context-coloring-level-face-regexp
196 (setq actual-level (string-to-number
197 (substring face-string
200 (= level actual-level)))))
201 (ert-fail (format (concat "Expected level in region [%s, %s), "
202 "which is \"%s\", to be %s; "
203 "but at point %s, it was %s")
205 (buffer-substring-no-properties start end) level
206 point actual-level))))))
208 (defun context-coloring-test-assert-region-face (start end expected-face)
209 "Assert that all points in the range [START, END) have the face
211 (context-coloring-test-assert-region
212 (when (not (eq face expected-face))
213 (ert-fail (format (concat "Expected face in region [%s, %s), "
214 "which is \"%s\", to be %s; "
215 "but at point %s, it was %s")
217 (buffer-substring-no-properties start end) expected-face
220 (defun context-coloring-test-assert-region-comment-delimiter (start end)
221 "Assert that all points in the range [START, END) have
222 `font-lock-comment-delimiter-face'."
223 (context-coloring-test-assert-region-face
224 start end 'font-lock-comment-delimiter-face))
226 (defun context-coloring-test-assert-region-comment (start end)
227 "Assert that all points in the range [START, END) have
228 `font-lock-comment-face'."
229 (context-coloring-test-assert-region-face
230 start end 'font-lock-comment-face))
232 (defun context-coloring-test-assert-region-string (start end)
233 "Assert that all points in the range [START, END) have
234 `font-lock-string-face'."
235 (context-coloring-test-assert-region-face
236 start end 'font-lock-string-face))
238 (defun context-coloring-test-assert-message (expected buffer)
239 "Assert that message EXPECTED exists in BUFFER."
240 (when (null (get-buffer buffer))
244 "Expected buffer `%s' to have message \"%s\", "
245 "but the buffer did not have any messages.")
247 (with-current-buffer buffer
248 (let ((messages (split-string
249 (buffer-substring-no-properties
253 (let ((message (car (nthcdr (- (length messages) 2) messages))))
254 (when (not (equal message expected))
258 "Expected buffer `%s' to have message \"%s\", "
259 "but instead it was \"%s\"")
263 (defun context-coloring-test-assert-no-message (buffer)
264 "Assert that BUFFER has no message."
265 (when (get-buffer buffer)
266 (ert-fail (format (concat "Expected buffer `%s' to have no messages, "
269 (with-current-buffer buffer
272 (defun context-coloring-test-kill-buffer (buffer)
273 "Kill BUFFER if it exists."
274 (when (get-buffer buffer) (kill-buffer buffer)))
276 (defun context-coloring-test-assert-face (level foreground &optional negate)
277 "Assert that a face for LEVEL exists and that its `:foreground'
278 is FOREGROUND, or the inverse if NEGATE is non-nil."
279 (let* ((face (context-coloring-level-face level))
281 (when (not (or negate
283 (ert-fail (format (concat "Expected face for level `%s' to exist; "
286 (setq actual-foreground (face-attribute face :foreground))
287 (when (funcall (if negate 'identity 'not)
288 (string-equal foreground actual-foreground))
289 (ert-fail (format (concat "Expected face for level `%s' "
290 "%sto have foreground `%s'; "
293 (if negate "not " "") foreground
295 "did" (format "was `%s'" actual-foreground)))))))
297 (defun context-coloring-test-assert-not-face (&rest arguments)
298 "Assert that LEVEL does not have a face with `:foreground'
299 FOREGROUND. Apply ARGUMENTS to
300 `context-coloring-test-assert-face', see that function."
301 (apply 'context-coloring-test-assert-face
302 (append arguments '(t))))
304 (defun context-coloring-test-assert-error (body error-message)
305 "Assert that BODY signals ERROR-MESSAGE."
306 (let ((error-signaled-p nil))
311 (setq error-signaled-p t)
312 (when (not (string-equal (cadr err) error-message))
313 (ert-fail (format (concat "Expected the error \"%s\" to be thrown, "
314 "but instead it was \"%s\".")
317 (when (not error-signaled-p)
318 (ert-fail "Expected an error to be thrown, but there wasn't."))))
320 (defun context-coloring-test-assert-trimmed (result expected)
321 (when (not (string-equal result expected))
322 (ert-fail "Expected string to be trimmed, but it wasn't.")))
327 (ert-deftest context-coloring-test-trim ()
328 (context-coloring-test-assert-trimmed (context-coloring-trim "") "")
329 (context-coloring-test-assert-trimmed (context-coloring-trim " ") "")
330 (context-coloring-test-assert-trimmed (context-coloring-trim "a") "a")
331 (context-coloring-test-assert-trimmed (context-coloring-trim " a") "a")
332 (context-coloring-test-assert-trimmed (context-coloring-trim "a ") "a")
333 (context-coloring-test-assert-trimmed (context-coloring-trim " a ") "a"))
335 (ert-deftest-async context-coloring-test-async-mode-startup (done)
336 (context-coloring-test-with-fixture-async
341 'context-coloring-colorize-hook
343 ;; If this runs we are implicitly successful; this test only confirms
344 ;; that colorization occurs on mode startup.
347 (context-coloring-mode))))
350 context-coloring-change-detection-mode
353 "Prevent `context-coloring-test-change-detection' from
354 having any unintentional side-effects on mode support.")
356 ;; Simply cannot figure out how to trigger an idle timer; would much rather test
357 ;; that. But (current-idle-time) always returns nil in these tests.
358 (ert-deftest-async context-coloring-test-change-detection (done)
359 (context-coloring-define-dispatch
361 :modes '(context-coloring-change-detection-mode)
363 :command "node test/binaries/noop")
364 (context-coloring-test-with-fixture-async
367 (context-coloring-change-detection-mode)
369 'context-coloring-colorize-hook
371 (setq context-coloring-colorize-hook nil)
373 'context-coloring-colorize-hook
378 (set-window-buffer (selected-window) (current-buffer))
379 (context-coloring-maybe-colorize)))
380 (context-coloring-mode))))
382 (ert-deftest context-coloring-test-check-version ()
383 (when (not (context-coloring-check-version "2.1.3" "3.0.1"))
384 (ert-fail "Expected version 3.0.1 to satisfy 2.1.3, but it didn't."))
385 (when (context-coloring-check-version "3.0.1" "2.1.3")
386 (ert-fail "Expected version 2.1.3 not to satisfy 3.0.1, but it did.")))
388 (ert-deftest context-coloring-test-unsupported-mode ()
389 (context-coloring-test-with-fixture
391 (context-coloring-mode)
392 (context-coloring-test-assert-message
393 "Context coloring is not available for this major mode"
397 context-coloring-test-define-dispatch-error-mode
400 "Prevent `context-coloring-test-define-dispatch-error' from
401 having any unintentional side-effects on mode support.")
403 (ert-deftest context-coloring-test-define-dispatch-error ()
404 (context-coloring-test-assert-error
406 (context-coloring-define-dispatch
407 'define-dispatch-no-modes))
408 "No mode defined for dispatch")
409 (context-coloring-test-assert-error
411 (context-coloring-define-dispatch
412 'define-dispatch-no-strategy
413 :modes '(context-coloring-test-define-dispatch-error-mode)))
414 "No colorizer, scopifier or command defined for dispatch"))
417 context-coloring-test-define-dispatch-scopifier-mode
420 "Prevent `context-coloring-test-define-dispatch-scopifier' from
421 having any unintentional side-effects on mode support.")
423 (ert-deftest context-coloring-test-define-dispatch-scopifier ()
424 (context-coloring-define-dispatch
425 'define-dispatch-scopifier
426 :modes '(context-coloring-test-define-dispatch-scopifier-mode)
427 :scopifier (lambda () (vector)))
429 (context-coloring-test-define-dispatch-scopifier-mode)
430 (context-coloring-mode)
431 (context-coloring-colorize)))
434 context-coloring-test-missing-executable-mode
437 "Prevent `context-coloring-test-define-dispatch-scopifier' from
438 having any unintentional side-effects on mode support.")
440 (ert-deftest context-coloring-test-missing-executable ()
441 (context-coloring-define-dispatch
443 :modes '(context-coloring-test-missing-executable-mode)
445 :executable "__should_not_exist__")
447 (context-coloring-test-missing-executable-mode)
448 (context-coloring-mode)))
451 context-coloring-test-unsupported-version-mode
454 "Prevent `context-coloring-test-unsupported-version' from
455 having any unintentional side-effects on mode support.")
457 (ert-deftest-async context-coloring-test-unsupported-version (done)
458 (context-coloring-define-dispatch
460 :modes '(context-coloring-test-unsupported-version-mode)
462 :command "node test/binaries/outta-date"
464 (context-coloring-test-with-fixture-async
467 (context-coloring-test-unsupported-version-mode)
469 'context-coloring-check-scopifier-version-hook
473 ;; Normally the executable would be something like "outta-date"
474 ;; rather than "node".
475 (context-coloring-test-assert-message
476 "Update to the minimum version of \"node\" (v2.1.3)"
480 (context-coloring-mode))))
483 context-coloring-test-disable-mode-mode
486 "Prevent `context-coloring-test-disable-mode' from having any
487 unintentional side-effects on mode support.")
489 (ert-deftest-async context-coloring-test-disable-mode (done)
491 (context-coloring-define-dispatch
493 :modes '(context-coloring-test-disable-mode-mode)
495 :command "node test/binaries/noop"
498 (context-coloring-test-with-fixture-async
503 (context-coloring-test-disable-mode-mode)
504 (context-coloring-mode)
505 (context-coloring-mode -1)
506 (when (not torn-down)
507 (ert-fail "Expected teardown function to have been called, but it wasn't.")))
511 (defvar context-coloring-test-theme-index 0
512 "Unique index for unique theme names.")
514 (defun context-coloring-test-get-next-theme ()
515 "Return a unique symbol for a throwaway theme."
517 (intern (format "context-coloring-test-theme-%s"
518 context-coloring-test-theme-index))
519 (setq context-coloring-test-theme-index
520 (+ context-coloring-test-theme-index 1))))
522 (defun context-coloring-test-assert-theme-originally-set-p
523 (settings &optional negate)
524 "Assert that `context-coloring-theme-originally-set-p' returns
525 t for a theme with SETTINGS, or the inverse if NEGATE is
527 (let ((theme (context-coloring-test-get-next-theme)))
528 (put theme 'theme-settings settings)
529 (when (funcall (if negate 'identity 'not)
530 (context-coloring-theme-originally-set-p theme))
531 (ert-fail (format (concat "Expected theme `%s' with settings `%s' "
532 "%sto be considered to have defined a level, "
535 (if negate "not " "")
536 (if negate "was" "wasn't"))))))
538 (defun context-coloring-test-assert-not-theme-originally-set-p (&rest arguments)
539 "Assert that `context-coloring-theme-originally-set-p' does not
540 return t for a theme with SETTINGS. Apply ARGUMENTS to
541 `context-coloring-test-assert-theme-originally-set-p', see that
543 (apply 'context-coloring-test-assert-theme-originally-set-p
544 (append arguments '(t))))
546 (ert-deftest context-coloring-test-theme-originally-set-p ()
547 (context-coloring-test-assert-theme-originally-set-p
548 '((theme-face context-coloring-level-0-face)))
549 (context-coloring-test-assert-theme-originally-set-p
551 (theme-face context-coloring-level-0-face)))
552 (context-coloring-test-assert-theme-originally-set-p
553 '((theme-face context-coloring-level-0-face)
555 (context-coloring-test-assert-not-theme-originally-set-p
556 '((theme-face face)))
559 (defun context-coloring-test-assert-theme-settings-highest-level
560 (settings expected-level)
561 "Assert that a theme with SETTINGS has the highest level
563 (let ((theme (context-coloring-test-get-next-theme)))
564 (put theme 'theme-settings settings)
565 (context-coloring-test-assert-theme-highest-level theme expected-level)))
567 (defun context-coloring-test-assert-theme-highest-level
568 (theme expected-level &optional negate)
569 "Assert that THEME has the highest level EXPECTED-LEVEL, or the
570 inverse if NEGATE is non-nil."
571 (let ((highest-level (context-coloring-theme-highest-level theme)))
572 (when (funcall (if negate 'identity 'not) (eq highest-level expected-level))
573 (ert-fail (format (concat "Expected theme with settings `%s' "
574 "%sto have a highest level of `%s', "
576 (get theme 'theme-settings)
577 (if negate "not " "") expected-level
578 (if negate "did" (format "was %s" highest-level)))))))
580 (defun context-coloring-test-assert-theme-not-highest-level (&rest arguments)
581 "Assert that THEME's highest level is not EXPECTED-LEVEL.
583 `context-coloring-test-assert-theme-highest-level', see that
585 (apply 'context-coloring-test-assert-theme-highest-level
586 (append arguments '(t))))
588 (ert-deftest context-coloring-test-theme-highest-level ()
589 (context-coloring-test-assert-theme-settings-highest-level
592 (context-coloring-test-assert-theme-settings-highest-level
593 '((theme-face context-coloring-level-0-face))
595 (context-coloring-test-assert-theme-settings-highest-level
596 '((theme-face context-coloring-level-1-face))
598 (context-coloring-test-assert-theme-settings-highest-level
599 '((theme-face context-coloring-level-1-face)
600 (theme-face context-coloring-level-0-face))
602 (context-coloring-test-assert-theme-settings-highest-level
603 '((theme-face context-coloring-level-0-face)
604 (theme-face context-coloring-level-1-face))
608 (defmacro context-coloring-test-deftest-define-theme (name &rest body)
609 "Define a test with name NAME and an automatically-generated
610 theme symbol available as a free variable `theme'. Side-effects
611 from enabling themes are reversed after BODY is executed and the
613 (declare (indent defun))
614 (let ((deftest-name (intern
615 (format "context-coloring-test-define-theme-%s" name))))
616 `(ert-deftest ,deftest-name ()
617 (context-coloring-test-kill-buffer "*Warnings*")
618 (context-coloring-test-setup)
619 (let ((theme (context-coloring-test-get-next-theme)))
624 (disable-theme theme)
625 (context-coloring-test-cleanup))))))
627 (defun context-coloring-test-deftheme (theme)
628 "Dynamically define theme THEME."
629 (eval (macroexpand `(deftheme ,theme))))
631 (context-coloring-test-deftest-define-theme additive
632 (context-coloring-test-deftheme theme)
633 (context-coloring-define-theme
637 (context-coloring-test-assert-no-message "*Warnings*")
639 (context-coloring-test-assert-no-message "*Warnings*")
640 (context-coloring-test-assert-face 0 "#aaaaaa")
641 (context-coloring-test-assert-face 1 "#bbbbbb"))
643 (defun context-coloring-test-assert-defined-warning (theme)
644 "Assert that a warning about colors already being defined for
645 theme THEME is signaled."
646 (context-coloring-test-assert-message
647 (format (concat "Warning (emacs): Context coloring colors for theme "
648 "`%s' are already defined")
652 (context-coloring-test-deftest-define-theme unintentional-override
653 (context-coloring-test-deftheme theme)
654 (custom-theme-set-faces
656 '(context-coloring-level-0-face ((t (:foreground "#aaaaaa"))))
657 '(context-coloring-level-1-face ((t (:foreground "#bbbbbb")))))
658 (context-coloring-define-theme
662 (context-coloring-test-assert-defined-warning theme)
663 (context-coloring-test-kill-buffer "*Warnings*")
665 (context-coloring-test-assert-defined-warning theme)
666 (context-coloring-test-assert-face 0 "#cccccc")
667 (context-coloring-test-assert-face 1 "#dddddd"))
669 (context-coloring-test-deftest-define-theme intentional-override
670 (context-coloring-test-deftheme theme)
671 (custom-theme-set-faces
673 '(context-coloring-level-0-face ((t (:foreground "#aaaaaa"))))
674 '(context-coloring-level-1-face ((t (:foreground "#bbbbbb")))))
675 (context-coloring-define-theme
680 (context-coloring-test-assert-no-message "*Warnings*")
682 (context-coloring-test-assert-no-message "*Warnings*")
683 (context-coloring-test-assert-face 0 "#cccccc")
684 (context-coloring-test-assert-face 1 "#dddddd"))
686 (context-coloring-test-deftest-define-theme pre-recede
687 (context-coloring-define-theme
692 (context-coloring-test-deftheme theme)
693 (custom-theme-set-faces
695 '(context-coloring-level-0-face ((t (:foreground "#cccccc"))))
696 '(context-coloring-level-1-face ((t (:foreground "#dddddd")))))
698 (context-coloring-test-assert-no-message "*Warnings*")
699 (context-coloring-test-assert-face 0 "#cccccc")
700 (context-coloring-test-assert-face 1 "#dddddd"))
702 (context-coloring-test-deftest-define-theme pre-recede-delayed-application
703 (context-coloring-define-theme
708 (context-coloring-test-deftheme theme)
710 (context-coloring-test-assert-no-message "*Warnings*")
711 (context-coloring-test-assert-face 0 "#aaaaaa")
712 (context-coloring-test-assert-face 1 "#bbbbbb"))
714 (context-coloring-test-deftest-define-theme post-recede
715 (context-coloring-test-deftheme theme)
716 (custom-theme-set-faces
718 '(context-coloring-level-0-face ((t (:foreground "#aaaaaa"))))
719 '(context-coloring-level-1-face ((t (:foreground "#bbbbbb")))))
720 (context-coloring-define-theme
725 (context-coloring-test-assert-no-message "*Warnings*")
726 (context-coloring-test-assert-face 0 "#aaaaaa")
727 (context-coloring-test-assert-face 1 "#bbbbbb")
729 (context-coloring-test-assert-no-message "*Warnings*")
730 (context-coloring-test-assert-face 0 "#aaaaaa")
731 (context-coloring-test-assert-face 1 "#bbbbbb"))
733 (context-coloring-test-deftest-define-theme recede-not-defined
734 (context-coloring-test-deftheme theme)
735 (custom-theme-set-faces
737 '(foo-face ((t (:foreground "#ffffff")))))
738 (context-coloring-define-theme
743 (context-coloring-test-assert-no-message "*Warnings*")
744 (context-coloring-test-assert-face 0 "#aaaaaa")
745 (context-coloring-test-assert-face 1 "#bbbbbb")
747 (context-coloring-test-assert-no-message "*Warnings*")
748 (context-coloring-test-assert-face 0 "#aaaaaa")
749 (context-coloring-test-assert-face 1 "#bbbbbb"))
751 (context-coloring-test-deftest-define-theme unintentional-obstinance
752 (context-coloring-define-theme
756 (context-coloring-test-deftheme theme)
757 (custom-theme-set-faces
759 '(context-coloring-level-0-face ((t (:foreground "#cccccc"))))
760 '(context-coloring-level-1-face ((t (:foreground "#dddddd")))))
762 (context-coloring-test-assert-defined-warning theme)
763 (context-coloring-test-assert-face 0 "#aaaaaa")
764 (context-coloring-test-assert-face 1 "#bbbbbb"))
766 (context-coloring-test-deftest-define-theme intentional-obstinance
767 (context-coloring-define-theme
772 (context-coloring-test-deftheme theme)
773 (custom-theme-set-faces
775 '(context-coloring-level-0-face ((t (:foreground "#cccccc"))))
776 '(context-coloring-level-1-face ((t (:foreground "#dddddd")))))
778 (context-coloring-test-assert-no-message "*Warnings*")
779 (context-coloring-test-assert-face 0 "#aaaaaa")
780 (context-coloring-test-assert-face 1 "#bbbbbb"))
782 (defun context-coloring-test-assert-maximum-face (maximum &optional negate)
783 "Assert that `context-coloring-maximum-face' is MAXIMUM, or the
784 inverse if NEGATE is non-nil."
785 (when (funcall (if negate 'identity 'not)
786 (eq context-coloring-maximum-face maximum))
787 (ert-fail (format (concat "Expected `context-coloring-maximum-face' "
790 (if negate "not " "") maximum
793 (format "was `%s'" context-coloring-maximum-face))))))
795 (defun context-coloring-test-assert-not-maximum-face (&rest arguments)
796 "Assert that `context-coloring-maximum-face' is not MAXIMUM.
797 Apply ARGUMENTS to `context-coloring-test-assert-maximum-face',
799 (apply 'context-coloring-test-assert-maximum-face
800 (append arguments '(t))))
802 (context-coloring-test-deftest-define-theme disable-cascade
803 (let ((maximum-face-value 9999))
804 (setq context-coloring-maximum-face maximum-face-value)
805 (context-coloring-test-deftheme theme)
806 (context-coloring-define-theme
810 (let ((second-theme (context-coloring-test-get-next-theme)))
811 (context-coloring-test-deftheme second-theme)
812 (context-coloring-define-theme
817 (let ((third-theme (context-coloring-test-get-next-theme)))
818 (context-coloring-test-deftheme third-theme)
819 (context-coloring-define-theme
826 (enable-theme second-theme)
827 (enable-theme third-theme)
828 (disable-theme third-theme)
829 (context-coloring-test-assert-face 0 "#cccccc")
830 (context-coloring-test-assert-face 1 "#dddddd")
831 (context-coloring-test-assert-face 2 "#eeeeee")
832 (context-coloring-test-assert-maximum-face 2))
833 (disable-theme second-theme)
834 (context-coloring-test-assert-face 0 "#aaaaaa")
835 (context-coloring-test-assert-face 1 "#bbbbbb")
836 (context-coloring-test-assert-maximum-face 1))
837 (disable-theme theme)
838 (context-coloring-test-assert-not-face 0 "#aaaaaa")
839 (context-coloring-test-assert-not-face 1 "#bbbbbb")
840 (context-coloring-test-assert-maximum-face
841 maximum-face-value)))
843 (defun context-coloring-test-js-function-scopes ()
844 "Test fixtures/functions-scopes.js."
845 (context-coloring-test-assert-region-level 1 9 0)
846 (context-coloring-test-assert-region-level 9 23 1)
847 (context-coloring-test-assert-region-level 23 25 0)
848 (context-coloring-test-assert-region-level 25 34 1)
849 (context-coloring-test-assert-region-level 34 35 0)
850 (context-coloring-test-assert-region-level 35 52 1)
851 (context-coloring-test-assert-region-level 52 66 2)
852 (context-coloring-test-assert-region-level 66 72 1)
853 (context-coloring-test-assert-region-level 72 81 2)
854 (context-coloring-test-assert-region-level 81 82 1)
855 (context-coloring-test-assert-region-level 82 87 2)
856 (context-coloring-test-assert-region-level 87 89 1))
858 (context-coloring-test-deftest-js-mode function-scopes)
859 (context-coloring-test-deftest-js2-mode function-scopes)
861 (defun context-coloring-test-js-global ()
862 "Test fixtures/global.js."
863 (context-coloring-test-assert-region-level 20 28 1)
864 (context-coloring-test-assert-region-level 28 35 0)
865 (context-coloring-test-assert-region-level 35 41 1))
867 (context-coloring-test-deftest-js-mode global)
868 (context-coloring-test-deftest-js2-mode global)
870 (defun context-coloring-test-js-block-scopes ()
871 "Test fixtures/block-scopes.js."
872 (context-coloring-test-assert-region-level 20 64 1)
873 (setq context-coloring-js-block-scopes t)
874 (context-coloring-colorize)
875 (context-coloring-test-assert-region-level 20 27 1)
876 (context-coloring-test-assert-region-level 27 41 2)
877 (context-coloring-test-assert-region-level 41 42 1)
878 (context-coloring-test-assert-region-level 42 64 2))
880 (context-coloring-test-deftest-js2-mode block-scopes)
882 (defun context-coloring-test-js-catch ()
883 "Test fixtures/js-catch.js."
884 (context-coloring-test-assert-region-level 20 27 1)
885 (context-coloring-test-assert-region-level 27 51 2)
886 (context-coloring-test-assert-region-level 51 52 1)
887 (context-coloring-test-assert-region-level 52 73 2)
888 (context-coloring-test-assert-region-level 73 101 3)
889 (context-coloring-test-assert-region-level 101 102 1)
890 (context-coloring-test-assert-region-level 102 117 3)
891 (context-coloring-test-assert-region-level 117 123 2))
893 (context-coloring-test-deftest-js-mode catch)
894 (context-coloring-test-deftest-js2-mode catch)
896 (defun context-coloring-test-js-key-names ()
897 "Test fixtures/key-names.js."
898 (context-coloring-test-assert-region-level 20 63 1))
900 (context-coloring-test-deftest-js-mode key-names)
901 (context-coloring-test-deftest-js2-mode key-names)
903 (defun context-coloring-test-js-property-lookup ()
904 "Test fixtures/property-lookup.js."
905 (context-coloring-test-assert-region-level 20 26 0)
906 (context-coloring-test-assert-region-level 26 38 1)
907 (context-coloring-test-assert-region-level 38 44 0)
908 (context-coloring-test-assert-region-level 44 52 1)
909 (context-coloring-test-assert-region-level 57 63 0)
910 (context-coloring-test-assert-region-level 63 74 1))
912 (context-coloring-test-deftest-js-mode property-lookup)
913 (context-coloring-test-deftest-js2-mode property-lookup)
915 (defun context-coloring-test-js-key-values ()
916 "Test fixtures/key-values.js."
917 (context-coloring-test-assert-region-level 78 79 1))
919 (context-coloring-test-deftest-js-mode key-values)
920 (context-coloring-test-deftest-js2-mode key-values)
922 (defun context-coloring-test-js-syntactic-comments-and-strings ()
923 "Test comments and strings."
924 (context-coloring-test-assert-region-level 1 8 0)
925 (context-coloring-test-assert-region-comment-delimiter 9 12)
926 (context-coloring-test-assert-region-comment 12 16)
927 (context-coloring-test-assert-region-comment-delimiter 17 20)
928 (context-coloring-test-assert-region-comment 20 27)
929 (context-coloring-test-assert-region-string 28 40)
930 (context-coloring-test-assert-region-level 40 41 0))
932 (defun context-coloring-test-js-syntactic-comments-and-strings-setup ()
933 (setq context-coloring-syntactic-comments t)
934 (setq context-coloring-syntactic-strings t))
936 (context-coloring-test-deftest-js-mode syntactic-comments-and-strings
937 :fixture-name comments-and-strings)
938 (context-coloring-test-deftest-js2-mode syntactic-comments-and-strings
939 :fixture-name comments-and-strings)
941 (defalias 'context-coloring-test-js-comments-and-strings
942 'context-coloring-test-js-syntactic-comments-and-strings
943 "Test comments and strings. Deprecated.")
945 (defun context-coloring-test-js-comments-and-strings-setup ()
946 "Setup comments and strings. Deprecated."
947 (setq context-coloring-comments-and-strings t))
949 (context-coloring-test-deftest-js-mode comments-and-strings)
950 (context-coloring-test-deftest-js2-mode comments-and-strings)
952 (defun context-coloring-test-js-syntactic-comments ()
953 "Test syntactic comments."
954 (context-coloring-test-assert-region-level 1 8 0)
955 (context-coloring-test-assert-region-comment-delimiter 9 12)
956 (context-coloring-test-assert-region-comment 12 16)
957 (context-coloring-test-assert-region-comment-delimiter 17 20)
958 (context-coloring-test-assert-region-comment 20 27)
959 (context-coloring-test-assert-region-level 28 41 0))
961 (defun context-coloring-test-js-syntactic-comments-setup ()
962 "Setup syntactic comments."
963 (setq context-coloring-syntactic-comments t))
965 (context-coloring-test-deftest-js-mode syntactic-comments
966 :fixture-name comments-and-strings)
967 (context-coloring-test-deftest-js2-mode syntactic-comments
968 :fixture-name comments-and-strings)
970 (defun context-coloring-test-js-syntactic-strings ()
971 "Test syntactic strings."
972 (context-coloring-test-assert-region-level 1 28 0)
973 (context-coloring-test-assert-region-string 28 40)
974 (context-coloring-test-assert-region-level 40 41 0))
976 (defun context-coloring-test-js-syntactic-strings-setup ()
977 "Setup syntactic strings."
978 (setq context-coloring-syntactic-strings t))
980 (context-coloring-test-deftest-js-mode syntactic-strings
981 :fixture-name comments-and-strings)
982 (context-coloring-test-deftest-js2-mode syntactic-strings
983 :fixture-name comments-and-strings)
985 ;; As long as `add-text-properties' doesn't signal an error, this test passes.
986 (defun context-coloring-test-js-unterminated-comment ()
987 "Test unterminated multiline comments.")
989 (context-coloring-test-deftest-js2-mode unterminated-comment)
991 (provide 'context-coloring-test)
993 ;;; context-coloring-test.el ends here