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 "Return the file's contents from PATH as a string."
42 (insert-file-contents (expand-file-name path context-coloring-test-path))
45 (defmacro context-coloring-test-with-fixture (fixture &rest body)
46 "With the relative FIXTURE, evaluate BODY in a temporary
50 (insert (context-coloring-test-read-file ,fixture))
53 (defun context-coloring-test-with-temp-buffer-async (callback)
54 "Create a temporary buffer, and evaluate CALLBACK there. A
55 teardown callback is passed to CALLBACK for it to invoke when it
57 (let ((previous-buffer (current-buffer))
58 (temp-buffer (generate-new-buffer " *temp*")))
59 (set-buffer temp-buffer)
63 (and (buffer-name temp-buffer)
64 (kill-buffer temp-buffer))
65 (set-buffer previous-buffer)))))
67 (defun context-coloring-test-with-fixture-async (fixture callback)
68 "With the relative FIXTURE, evaluate CALLBACK in a temporary
69 buffer. A teardown callback is passed to CALLBACK for it to
70 invoke when it is done."
71 (context-coloring-test-with-temp-buffer-async
72 (lambda (done-with-temp-buffer)
73 (insert (context-coloring-test-read-file fixture))
77 (funcall done-with-temp-buffer))))))
80 ;;; Test defining utilities
82 (cl-defmacro context-coloring-test-define-deftest (name
87 &key post-colorization
88 &key enable-context-coloring-mode
92 "Define a deftest defmacro for tests prefixed with NAME. MODE
93 is called to set up tests' environments. EXTENSION denotes the
94 suffix for tests' fixture files. If NO-FIXTURE is non-nil, don't
95 use a fixture. If ASYNC is non-nil, pass a callback to the
96 defined tests' bodies for them to call when they are done. If
97 POST-COLORIZATION is non-nil, the tests run after
98 `context-coloring-colorize' finishes asynchronously. If
99 ENABLE-CONTEXT-COLORING-MODE is non-nil, `context-coloring-mode'
100 is activated before tests. GET-ARGS provides arguments to apply
101 to BEFORE-EACH, AFTER-EACH, and each tests' body, before and
102 after functions. Functions BEFORE-EACH and AFTER-EACH run before
103 the major mode is activated before each test, and after each
104 test, even if an error is signaled."
105 (declare (indent defun))
106 (let ((macro-name (intern (format "context-coloring-test-deftest%s"
108 ;; No name means no dash.
110 (t (format "-%s" name)))))))
111 `(cl-defmacro ,macro-name (name
116 ,(format "Define a test for `%s' suffixed with NAME.
118 Function BODY makes assertions.
121 Functions BEFORE and AFTER run before and after the test, even if
122 an error is signaled.
124 BODY is run after `context-coloring-mode' is activated, or after
125 initial colorization if colorization should occur."
129 There is no fixture, unless FIXTURE is specified.")
132 The default fixture has a filename matching NAME (plus the
133 filetype extension, \"%s\"), unless FIXTURE is specified to
136 (declare (indent defun))
137 ;; Commas in nested backquotes are not evaluated. Binding the variables
138 ;; here is probably the cleanest workaround.
142 (t '(lambda () (list)))))
143 (args (make-symbol "args"))
144 (before-each ',before-each)
145 (after-each ',after-each)
146 (test-name (intern (format ,(format "%s-%%s"
151 (fixture (format "./fixtures/%s" fixture))
152 (,no-fixture "./fixtures/empty")
153 (t (format ,(format "./fixtures/%%s.%s" extension) name)))))
155 ((or async post-colorization)
156 `((let ((post-colorization ,post-colorization))
157 `(ert-deftest-async ,test-name (done)
158 (let ((,args (funcall ,get-args)))
159 (context-coloring-test-with-fixture-async
161 (lambda (done-with-fixture)
162 (when ,before-each (apply ,before-each ,args))
164 (when ,before (apply ,before ,args))
167 (context-coloring-colorize
172 (when ,after (apply ,after ,args))
173 (when ,after-each (apply ,after-each ,args))
174 (funcall done-with-fixture))
177 ;; Leave error handling up to the user.
180 (when ,after (apply ,after ,args))
181 (when ,after-each (apply ,after-each ,args))
182 (funcall done-with-fixture)
186 `((let ((enable-context-coloring-mode ,enable-context-coloring-mode))
187 `(ert-deftest ,test-name ()
188 (let ((,args (funcall ,get-args)))
189 (context-coloring-test-with-fixture
191 (when ,before-each (apply ,before-each ,args))
193 (when ,before (apply ,before ,args))
194 (when ,enable-context-coloring-mode (context-coloring-mode))
198 (when ,after (apply ,after ,args))
199 (when ,after-each (apply ,after-each ,args))))))))))))))
201 (context-coloring-test-define-deftest nil
202 :mode 'fundamental-mode
205 (context-coloring-test-define-deftest async
206 :mode 'fundamental-mode
210 (context-coloring-test-define-deftest js
213 :post-colorization t)
215 (context-coloring-test-define-deftest js2
218 :enable-context-coloring-mode t
219 :before-each (lambda ()
220 (setq js2-mode-show-parse-errors nil)
221 (setq js2-mode-show-strict-warnings nil)))
223 (defmacro context-coloring-test-deftest-js-js2 (&rest args)
224 "Simultaneously define the same test for js and js2."
225 (declare (indent defun))
227 (context-coloring-test-deftest-js ,@args)
228 (context-coloring-test-deftest-js2 ,@args)))
230 (context-coloring-test-define-deftest emacs-lisp
231 :mode 'emacs-lisp-mode
233 :enable-context-coloring-mode t)
235 (context-coloring-test-define-deftest define-theme
236 :mode 'fundamental-mode
239 (list (context-coloring-test-get-next-theme)))
240 :after-each (lambda (theme)
241 (setq context-coloring-maximum-face 7)
242 (setq context-coloring-original-maximum-face
243 context-coloring-maximum-face)
244 (disable-theme theme)
245 (context-coloring-test-kill-buffer "*Warnings*")))
248 ;;; Assertion functions
250 (defun context-coloring-test-get-last-message ()
251 (let ((messages (split-string
252 (buffer-substring-no-properties
256 (car (nthcdr (- (length messages) 2) messages))))
258 (defun context-coloring-test-assert-message (expected buffer)
259 "Assert that message EXPECTED is at the end of BUFFER."
260 (when (null (get-buffer buffer))
264 "Expected buffer `%s' to have message \"%s\", "
265 "but the buffer did not have any messages.")
267 (with-current-buffer buffer
268 (let ((message (context-coloring-test-get-last-message)))
269 (when (not (equal message expected))
273 "Expected buffer `%s' to have message \"%s\", "
274 "but instead it was \"%s\"")
278 (defun context-coloring-test-assert-not-message (expected buffer)
279 "Assert that message EXPECTED is not at the end of BUFFER."
280 (when (get-buffer buffer)
281 (with-current-buffer buffer
282 (let ((message (context-coloring-test-get-last-message)))
283 (when (equal message expected)
287 "Expected buffer `%s' not to have message \"%s\", "
289 buffer expected)))))))
291 (defun context-coloring-test-assert-no-message (buffer)
292 "Assert that BUFFER has no message."
293 (when (get-buffer buffer)
294 (ert-fail (format (concat "Expected buffer `%s' to have no messages, "
297 (with-current-buffer buffer
300 (defun context-coloring-test-assert-error (body error-message)
301 "Assert that BODY signals ERROR-MESSAGE."
302 (let ((error-signaled-p nil))
307 (setq error-signaled-p t)
308 (when (not (string-equal (cadr err) error-message))
309 (ert-fail (format (concat "Expected the error \"%s\" to be thrown, "
310 "but instead it was \"%s\".")
313 (when (not error-signaled-p)
314 (ert-fail "Expected an error to be thrown, but there wasn't."))))
317 ;;; Miscellaneous tests
319 (defun context-coloring-test-assert-trimmed (result expected)
320 (when (not (string-equal result expected))
321 (ert-fail "Expected string to be trimmed, but it wasn't.")))
323 (context-coloring-test-deftest trim
325 (context-coloring-test-assert-trimmed (context-coloring-trim "") "")
326 (context-coloring-test-assert-trimmed (context-coloring-trim " ") "")
327 (context-coloring-test-assert-trimmed (context-coloring-trim "a") "a")
328 (context-coloring-test-assert-trimmed (context-coloring-trim " a") "a")
329 (context-coloring-test-assert-trimmed (context-coloring-trim "a ") "a")
330 (context-coloring-test-assert-trimmed (context-coloring-trim " a ") "a")))
332 (context-coloring-test-deftest-async mode-startup
336 'context-coloring-colorize-hook
338 ;; If this runs we are implicitly successful; this test only confirms
339 ;; that colorization occurs on mode startup.
341 (context-coloring-mode))
343 ;; TODO: This won't run if there is a timeout. Will probably have to
344 ;; roll our own `ert-deftest-async'.
345 (setq context-coloring-colorize-hook nil)))
347 (defmacro context-coloring-test-define-derived-mode (name)
348 (let ((name (intern (format "context-coloring-test-%s-mode" name))))
349 `(define-derived-mode ,name fundamental-mode "Testing")))
351 (context-coloring-test-define-derived-mode change-detection)
353 ;; Simply cannot figure out how to trigger an idle timer; would much rather test
354 ;; that. But (current-idle-time) always returns nil in these tests.
355 (context-coloring-test-deftest-async change-detection
357 (context-coloring-define-dispatch
359 :modes '(context-coloring-test-change-detection-mode)
361 :command "node test/binaries/noop")
362 (context-coloring-test-change-detection-mode)
364 'context-coloring-colorize-hook
366 (setq context-coloring-colorize-hook nil)
368 'context-coloring-colorize-hook
372 (set-window-buffer (selected-window) (current-buffer))
373 (context-coloring-maybe-colorize (current-buffer))))
374 (context-coloring-mode))
376 (setq context-coloring-colorize-hook nil)))
378 (context-coloring-test-deftest check-version
380 (when (not (context-coloring-check-version "2.1.3" "3.0.1"))
381 (ert-fail "Expected version 3.0.1 to satisfy 2.1.3, but it didn't."))
382 (when (context-coloring-check-version "3.0.1" "2.1.3")
383 (ert-fail "Expected version 2.1.3 not to satisfy 3.0.1, but it did."))))
385 (context-coloring-test-deftest unsupported-mode
387 (context-coloring-mode)
388 (context-coloring-test-assert-message
389 "Context coloring is not available for this major mode"
392 (context-coloring-test-deftest derived-mode
394 (lisp-interaction-mode)
395 (context-coloring-mode)
396 (context-coloring-test-assert-not-message
397 "Context coloring is not available for this major mode"
400 (context-coloring-test-define-derived-mode define-dispatch-error)
402 (context-coloring-test-deftest 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")))
416 (context-coloring-test-define-derived-mode define-dispatch-scopifier)
418 (context-coloring-test-deftest define-dispatch-scopifier
420 (context-coloring-define-dispatch
421 'define-dispatch-scopifier
422 :modes '(context-coloring-test-define-dispatch-scopifier-mode)
423 :scopifier (lambda () (vector)))
424 (context-coloring-test-define-dispatch-scopifier-mode)
425 (context-coloring-mode)
426 (context-coloring-colorize)))
428 (context-coloring-test-define-derived-mode missing-executable)
430 (context-coloring-test-deftest missing-executable
432 (context-coloring-define-dispatch
434 :modes '(context-coloring-test-missing-executable-mode)
436 :executable "__should_not_exist__")
437 (context-coloring-test-missing-executable-mode)
438 (context-coloring-mode)))
440 (context-coloring-test-define-derived-mode unsupported-version)
442 (context-coloring-test-deftest-async unsupported-version
444 (context-coloring-define-dispatch
446 :modes '(context-coloring-test-unsupported-version-mode)
448 :command "node test/binaries/outta-date"
450 (context-coloring-test-unsupported-version-mode)
452 'context-coloring-check-scopifier-version-hook
456 ;; Normally the executable would be something like "outta-date"
457 ;; rather than "node".
458 (context-coloring-test-assert-message
459 "Update to the minimum version of \"node\" (v2.1.3)"
462 (context-coloring-mode))
464 (setq context-coloring-check-scopifier-version-hook nil)))
466 (context-coloring-test-define-derived-mode disable-mode)
468 (context-coloring-test-deftest-async disable-mode
471 (context-coloring-define-dispatch
473 :modes '(context-coloring-test-disable-mode-mode)
475 :command "node test/binaries/noop"
480 (context-coloring-test-disable-mode-mode)
481 (context-coloring-mode)
482 (context-coloring-mode -1)
483 (when (not torn-down)
484 (ert-fail "Expected teardown function to have been called, but it wasn't.")))
490 (defvar context-coloring-test-theme-index 0
491 "Unique index for unique theme names.")
493 (defun context-coloring-test-get-next-theme ()
494 "Return a unique symbol for a throwaway theme."
496 (intern (format "context-coloring-test-theme-%s"
497 context-coloring-test-theme-index))
498 (setq context-coloring-test-theme-index
499 (+ context-coloring-test-theme-index 1))))
501 (defun context-coloring-test-assert-face (level foreground &optional negate)
502 "Assert that a face for LEVEL exists and that its `:foreground'
503 is FOREGROUND, or the inverse if NEGATE is non-nil."
504 (let* ((face (context-coloring-level-face level))
506 (when (not (or negate
508 (ert-fail (format (concat "Expected face for level `%s' to exist; "
511 (setq actual-foreground (face-attribute face :foreground))
512 (when (funcall (if negate 'identity 'not)
513 (string-equal foreground actual-foreground))
514 (ert-fail (format (concat "Expected face for level `%s' "
515 "%sto have foreground `%s'; "
518 (if negate "not " "") foreground
520 "did" (format "was `%s'" actual-foreground)))))))
522 (defun context-coloring-test-assert-not-face (&rest arguments)
523 "Assert that LEVEL does not have a face with `:foreground'
524 FOREGROUND. Apply ARGUMENTS to
525 `context-coloring-test-assert-face', see that function."
526 (apply 'context-coloring-test-assert-face
527 (append arguments '(t))))
529 (defun context-coloring-test-assert-theme-originally-set-p
530 (settings &optional negate)
531 "Assert that `context-coloring-theme-originally-set-p' returns
532 t for a theme with SETTINGS, or the inverse if NEGATE is
534 (let ((theme (context-coloring-test-get-next-theme)))
535 (put theme 'theme-settings settings)
536 (when (funcall (if negate 'identity 'not)
537 (context-coloring-theme-originally-set-p theme))
538 (ert-fail (format (concat "Expected theme `%s' with settings `%s' "
539 "%sto be considered to have defined a level, "
542 (if negate "not " "")
543 (if negate "was" "wasn't"))))))
545 (defun context-coloring-test-assert-not-theme-originally-set-p (&rest arguments)
546 "Assert that `context-coloring-theme-originally-set-p' does not
547 return t for a theme with SETTINGS. Apply ARGUMENTS to
548 `context-coloring-test-assert-theme-originally-set-p', see that
550 (apply 'context-coloring-test-assert-theme-originally-set-p
551 (append arguments '(t))))
553 (context-coloring-test-deftest theme-originally-set-p
555 (context-coloring-test-assert-theme-originally-set-p
556 '((theme-face context-coloring-level-0-face)))
557 (context-coloring-test-assert-theme-originally-set-p
559 (theme-face context-coloring-level-0-face)))
560 (context-coloring-test-assert-theme-originally-set-p
561 '((theme-face context-coloring-level-0-face)
563 (context-coloring-test-assert-not-theme-originally-set-p
564 '((theme-face face)))))
566 (defun context-coloring-test-assert-theme-settings-highest-level
567 (settings expected-level)
568 "Assert that a theme with SETTINGS has the highest level
570 (let ((theme (context-coloring-test-get-next-theme)))
571 (put theme 'theme-settings settings)
572 (context-coloring-test-assert-theme-highest-level theme expected-level)))
574 (defun context-coloring-test-assert-theme-highest-level
575 (theme expected-level &optional negate)
576 "Assert that THEME has the highest level EXPECTED-LEVEL, or the
577 inverse if NEGATE is non-nil."
578 (let ((highest-level (context-coloring-theme-highest-level theme)))
579 (when (funcall (if negate 'identity 'not) (eq highest-level expected-level))
580 (ert-fail (format (concat "Expected theme with settings `%s' "
581 "%sto have a highest level of `%s', "
583 (get theme 'theme-settings)
584 (if negate "not " "") expected-level
585 (if negate "did" (format "was %s" highest-level)))))))
587 (defun context-coloring-test-assert-theme-not-highest-level (&rest arguments)
588 "Assert that THEME's highest level is not EXPECTED-LEVEL.
590 `context-coloring-test-assert-theme-highest-level', see that
592 (apply 'context-coloring-test-assert-theme-highest-level
593 (append arguments '(t))))
595 (context-coloring-test-deftest theme-highest-level
597 (context-coloring-test-assert-theme-settings-highest-level
600 (context-coloring-test-assert-theme-settings-highest-level
601 '((theme-face context-coloring-level-0-face))
603 (context-coloring-test-assert-theme-settings-highest-level
604 '((theme-face context-coloring-level-1-face))
606 (context-coloring-test-assert-theme-settings-highest-level
607 '((theme-face context-coloring-level-1-face)
608 (theme-face context-coloring-level-0-face))
610 (context-coloring-test-assert-theme-settings-highest-level
611 '((theme-face context-coloring-level-0-face)
612 (theme-face context-coloring-level-1-face))
615 (defun context-coloring-test-kill-buffer (buffer)
616 "Kill BUFFER if it exists."
617 (when (get-buffer buffer) (kill-buffer buffer)))
619 (defun context-coloring-test-deftheme (theme)
620 "Dynamically define theme THEME."
621 (eval (macroexpand `(deftheme ,theme))))
623 (context-coloring-test-deftest-define-theme additive
625 (context-coloring-test-deftheme theme)
626 (context-coloring-define-theme
630 (context-coloring-test-assert-no-message "*Warnings*")
632 (context-coloring-test-assert-no-message "*Warnings*")
633 (context-coloring-test-assert-face 0 "#aaaaaa")
634 (context-coloring-test-assert-face 1 "#bbbbbb")))
636 (defun context-coloring-test-assert-defined-warning (theme)
637 "Assert that a warning about colors already being defined for
638 theme THEME is signaled."
639 (context-coloring-test-assert-message
640 (format (concat "Warning (emacs): Context coloring colors for theme "
641 "`%s' are already defined")
645 (context-coloring-test-deftest-define-theme unintentional-override
647 (context-coloring-test-deftheme theme)
648 (custom-theme-set-faces
650 '(context-coloring-level-0-face ((t (:foreground "#aaaaaa"))))
651 '(context-coloring-level-1-face ((t (:foreground "#bbbbbb")))))
652 (context-coloring-define-theme
656 (context-coloring-test-assert-defined-warning theme)
657 (context-coloring-test-kill-buffer "*Warnings*")
659 (context-coloring-test-assert-defined-warning theme)
660 (context-coloring-test-assert-face 0 "#cccccc")
661 (context-coloring-test-assert-face 1 "#dddddd")))
663 (context-coloring-test-deftest-define-theme intentional-override
665 (context-coloring-test-deftheme theme)
666 (custom-theme-set-faces
668 '(context-coloring-level-0-face ((t (:foreground "#aaaaaa"))))
669 '(context-coloring-level-1-face ((t (:foreground "#bbbbbb")))))
670 (context-coloring-define-theme
675 (context-coloring-test-assert-no-message "*Warnings*")
677 (context-coloring-test-assert-no-message "*Warnings*")
678 (context-coloring-test-assert-face 0 "#cccccc")
679 (context-coloring-test-assert-face 1 "#dddddd")))
681 (context-coloring-test-deftest-define-theme pre-recede
683 (context-coloring-define-theme
688 (context-coloring-test-deftheme theme)
689 (custom-theme-set-faces
691 '(context-coloring-level-0-face ((t (:foreground "#cccccc"))))
692 '(context-coloring-level-1-face ((t (:foreground "#dddddd")))))
694 (context-coloring-test-assert-no-message "*Warnings*")
695 (context-coloring-test-assert-face 0 "#cccccc")
696 (context-coloring-test-assert-face 1 "#dddddd")))
698 (context-coloring-test-deftest-define-theme pre-recede-delayed-application
700 (context-coloring-define-theme
705 (context-coloring-test-deftheme theme)
707 (context-coloring-test-assert-no-message "*Warnings*")
708 (context-coloring-test-assert-face 0 "#aaaaaa")
709 (context-coloring-test-assert-face 1 "#bbbbbb")))
711 (context-coloring-test-deftest-define-theme post-recede
713 (context-coloring-test-deftheme theme)
714 (custom-theme-set-faces
716 '(context-coloring-level-0-face ((t (:foreground "#aaaaaa"))))
717 '(context-coloring-level-1-face ((t (:foreground "#bbbbbb")))))
718 (context-coloring-define-theme
723 (context-coloring-test-assert-no-message "*Warnings*")
724 (context-coloring-test-assert-face 0 "#aaaaaa")
725 (context-coloring-test-assert-face 1 "#bbbbbb")
727 (context-coloring-test-assert-no-message "*Warnings*")
728 (context-coloring-test-assert-face 0 "#aaaaaa")
729 (context-coloring-test-assert-face 1 "#bbbbbb")))
731 (context-coloring-test-deftest-define-theme recede-not-defined
733 (context-coloring-test-deftheme theme)
734 (custom-theme-set-faces
736 '(foo-face ((t (:foreground "#ffffff")))))
737 (context-coloring-define-theme
742 (context-coloring-test-assert-no-message "*Warnings*")
743 (context-coloring-test-assert-face 0 "#aaaaaa")
744 (context-coloring-test-assert-face 1 "#bbbbbb")
746 (context-coloring-test-assert-no-message "*Warnings*")
747 (context-coloring-test-assert-face 0 "#aaaaaa")
748 (context-coloring-test-assert-face 1 "#bbbbbb")))
750 (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
768 (context-coloring-define-theme
773 (context-coloring-test-deftheme theme)
774 (custom-theme-set-faces
776 '(context-coloring-level-0-face ((t (:foreground "#cccccc"))))
777 '(context-coloring-level-1-face ((t (:foreground "#dddddd")))))
779 (context-coloring-test-assert-no-message "*Warnings*")
780 (context-coloring-test-assert-face 0 "#aaaaaa")
781 (context-coloring-test-assert-face 1 "#bbbbbb")))
783 (defun context-coloring-test-assert-maximum-face (maximum &optional negate)
784 "Assert that `context-coloring-maximum-face' is MAXIMUM, or the
785 inverse if NEGATE is non-nil."
786 (when (funcall (if negate 'identity 'not)
787 (eq context-coloring-maximum-face maximum))
788 (ert-fail (format (concat "Expected `context-coloring-maximum-face' "
791 (if negate "not " "") maximum
794 (format "was `%s'" context-coloring-maximum-face))))))
796 (defun context-coloring-test-assert-not-maximum-face (&rest arguments)
797 "Assert that `context-coloring-maximum-face' is not MAXIMUM.
798 Apply ARGUMENTS to `context-coloring-test-assert-maximum-face',
800 (apply 'context-coloring-test-assert-maximum-face
801 (append arguments '(t))))
803 (context-coloring-test-deftest-define-theme disable-cascade
805 (let ((maximum-face-value 9999))
806 (setq context-coloring-maximum-face maximum-face-value)
807 (context-coloring-test-deftheme theme)
808 (context-coloring-define-theme
812 (let ((second-theme (context-coloring-test-get-next-theme)))
813 (context-coloring-test-deftheme second-theme)
814 (context-coloring-define-theme
819 (let ((third-theme (context-coloring-test-get-next-theme)))
820 (context-coloring-test-deftheme third-theme)
821 (context-coloring-define-theme
828 (enable-theme second-theme)
829 (enable-theme third-theme)
830 (disable-theme third-theme)
831 (context-coloring-test-assert-face 0 "#cccccc")
832 (context-coloring-test-assert-face 1 "#dddddd")
833 (context-coloring-test-assert-face 2 "#eeeeee")
834 (context-coloring-test-assert-maximum-face 2))
835 (disable-theme second-theme)
836 (context-coloring-test-assert-face 0 "#aaaaaa")
837 (context-coloring-test-assert-face 1 "#bbbbbb")
838 (context-coloring-test-assert-maximum-face 1))
839 (disable-theme theme)
840 (context-coloring-test-assert-not-face 0 "#aaaaaa")
841 (context-coloring-test-assert-not-face 1 "#bbbbbb")
842 (context-coloring-test-assert-maximum-face
843 maximum-face-value))))
848 (defun context-coloring-test-assert-position-level (position level)
849 "Assert that POSITION has LEVEL."
850 (let ((face (get-text-property position 'face))
853 (let* ((face-string (symbol-name face))
854 (matches (string-match
855 context-coloring-level-face-regexp
858 (setq actual-level (string-to-number
859 (substring face-string
862 (= level actual-level)))))
863 (ert-fail (format (concat "Expected level at position %s, "
864 "which is \"%s\", to be %s; "
867 (buffer-substring-no-properties position (1+ position)) level
870 (defun context-coloring-test-assert-position-face (position face-regexp)
871 "Assert that the face at POSITION satisfies FACE-REGEXP."
872 (let ((face (get-text-property position 'face)))
874 ;; Pass a non-string to do an `equal' check (against a symbol or nil).
875 (unless (stringp face-regexp)
876 (not (equal face-regexp face)))
877 ;; Otherwise do the matching.
878 (when (stringp face-regexp)
879 (not (string-match-p face-regexp (symbol-name face)))))
880 (ert-fail (format (concat "Expected face at position %s, "
881 "which is \"%s\", to be %s; "
884 (buffer-substring-no-properties position (1+ position)) face-regexp
887 (defun context-coloring-test-assert-position-comment (position)
888 (context-coloring-test-assert-position-face
889 position "\\`font-lock-comment\\(-delimiter\\)?-face\\'"))
891 (defun context-coloring-test-assert-position-constant-comment (position)
892 (context-coloring-test-assert-position-face position '(font-lock-constant-face
893 font-lock-comment-face)))
895 (defun context-coloring-test-assert-position-string (position)
896 (context-coloring-test-assert-position-face position 'font-lock-string-face))
898 (defun context-coloring-test-assert-position-nil (position)
899 (context-coloring-test-assert-position-face position nil))
901 (defun context-coloring-test-assert-coloring (map)
902 "Assert that the current buffer's coloring matches MAP."
903 ;; Omit the superfluous, formatting-related leading newline. Can't use
904 ;; `save-excursion' here because if an assertion fails it will cause future
905 ;; tests to get messed up.
906 (goto-char (point-min))
907 (let* ((map (substring map 1))
911 (while (< index (length map))
912 (setq char-string (substring map index (1+ index)))
913 (setq char (string-to-char char-string))
922 (context-coloring-test-assert-position-level
923 (point) (string-to-number char-string))
925 ;; 'C' = Constant comment
927 (context-coloring-test-assert-position-constant-comment (point))
931 (context-coloring-test-assert-position-comment (point))
935 (context-coloring-test-assert-position-nil (point))
939 (context-coloring-test-assert-position-string (point))
943 (setq index (1+ index)))))
945 (context-coloring-test-deftest-js-js2 function-scopes
947 (context-coloring-test-assert-coloring "
948 000 0 0 11111111 11 110
950 111 1 1 22222222 22 221
954 (context-coloring-test-deftest-js-js2 global
956 (context-coloring-test-assert-coloring "
958 111 1 1 00000001xxx11
961 (context-coloring-test-deftest-js2 block-scopes
963 (context-coloring-test-assert-coloring "
971 (setq context-coloring-js-block-scopes t))
973 (setq context-coloring-js-block-scopes nil)))
975 (context-coloring-test-deftest-js-js2 catch
977 (context-coloring-test-assert-coloring "
987 (context-coloring-test-deftest-js-js2 key-names
989 (context-coloring-test-assert-coloring "
997 (context-coloring-test-deftest-js-js2 property-lookup
999 (context-coloring-test-assert-coloring "
1006 (context-coloring-test-deftest-js-js2 key-values
1008 (context-coloring-test-assert-coloring "
1018 (context-coloring-test-deftest-js-js2 syntactic-comments-and-strings
1020 (context-coloring-test-assert-coloring "
1025 :fixture "comments-and-strings.js")
1027 (context-coloring-test-deftest-js-js2 syntactic-comments
1029 (context-coloring-test-assert-coloring "
1034 :fixture "comments-and-strings.js"
1036 (setq context-coloring-syntactic-strings nil))
1038 (setq context-coloring-syntactic-strings t)))
1040 (context-coloring-test-deftest-js-js2 syntactic-strings
1042 (context-coloring-test-assert-coloring "
1047 :fixture "comments-and-strings.js"
1049 (setq context-coloring-syntactic-comments nil))
1051 (setq context-coloring-syntactic-comments t)))
1053 (context-coloring-test-deftest-js2 unterminated-comment
1054 ;; As long as `add-text-properties' doesn't signal an error, this test passes.
1057 (context-coloring-test-deftest-emacs-lisp defun
1059 (context-coloring-test-assert-coloring "
1060 111111 000 1111 111 111111111 1111
1061 11 111 111 111 000011
1068 (context-coloring-test-deftest-emacs-lisp lambda
1070 (context-coloring-test-assert-coloring "
1071 00000000 1111111 1111
1072 11111111 11 2222222 2222
1073 222 22 12 2221 111 0 00")))
1075 (context-coloring-test-deftest-emacs-lisp quote
1077 (context-coloring-test-assert-coloring "
1081 111111 1 1111111111 11 111 1 111 1 00001 10000 11 00001 1 100001111")))
1083 (context-coloring-test-deftest-emacs-lisp comment
1085 ;; Just check that the comment isn't parsed syntactically.
1086 (context-coloring-test-assert-coloring "
1088 (xx (x xxxxx-xxxx xx) cccccccccc
1089 11 00000-0000 11))) cccccccccc"))
1091 (setq context-coloring-syntactic-comments t)))
1093 (context-coloring-test-deftest-emacs-lisp string
1095 (context-coloring-test-assert-coloring "
1097 (xxxxxx x x sss 1 0 sssss 0 1 sssssss11"))
1099 (setq context-coloring-syntactic-strings t)))
1101 (context-coloring-test-deftest-emacs-lisp ignored
1103 (context-coloring-test-assert-coloring "
1105 (x x 1 11 11 111 11 1 111 (1 1 1)))")))
1107 (context-coloring-test-deftest-emacs-lisp let
1109 (context-coloring-test-assert-coloring "
1117 1111 1 1 1 000011")))
1119 (context-coloring-test-deftest-emacs-lisp let*
1121 (context-coloring-test-assert-coloring "
1125 1111 1 1 1 0 0 00001
1131 2222 1 1 2 2 2 000022
1132 1111 1 1 1 0 0 000011")))
1134 (defun context-coloring-test-insert-unread-space ()
1135 (setq unread-command-events (cons '(t . 32)
1136 unread-command-events)))
1138 (defun context-coloring-test-remove-faces ()
1139 (remove-text-properties (point-min) (point-max) '(face nil)))
1141 (context-coloring-test-deftest-emacs-lisp iteration
1143 (let ((context-coloring-emacs-lisp-iterations-per-pause 1))
1144 (context-coloring-colorize)
1145 (context-coloring-test-assert-coloring "
1148 (context-coloring-test-remove-faces)
1149 (context-coloring-test-insert-unread-space)
1150 (context-coloring-colorize)
1151 ;; The first iteration will color the first part of the comment, but
1152 ;; that's it. Then it will be interrupted.
1153 (context-coloring-test-assert-coloring "
1157 (setq context-coloring-syntactic-comments t)
1158 (setq context-coloring-syntactic-strings t)))
1160 (provide 'context-coloring-test)
1162 ;;; context-coloring-test.el ends here