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 "Get the last message in the current messages bufffer."
252 (let ((messages (split-string
253 (buffer-substring-no-properties
257 (car (nthcdr (- (length messages) 2) messages))))
259 (defun context-coloring-test-assert-message (expected buffer)
260 "Assert that message EXPECTED is at the end of BUFFER."
261 (when (null (get-buffer buffer))
265 "Expected buffer `%s' to have message \"%s\", "
266 "but the buffer did not have any messages.")
268 (with-current-buffer buffer
269 (let ((message (context-coloring-test-get-last-message)))
270 (when (not (equal message expected))
274 "Expected buffer `%s' to have message \"%s\", "
275 "but instead it was \"%s\"")
279 (defun context-coloring-test-assert-not-message (expected buffer)
280 "Assert that message EXPECTED is not at the end of BUFFER."
281 (when (get-buffer buffer)
282 (with-current-buffer buffer
283 (let ((message (context-coloring-test-get-last-message)))
284 (when (equal message expected)
288 "Expected buffer `%s' not to have message \"%s\", "
290 buffer expected)))))))
292 (defun context-coloring-test-assert-no-message (buffer)
293 "Assert that BUFFER has no message."
294 (when (get-buffer buffer)
295 (ert-fail (format (concat "Expected buffer `%s' to have no messages, "
298 (with-current-buffer buffer
301 (defun context-coloring-test-assert-error (body error-message)
302 "Assert that BODY signals ERROR-MESSAGE."
303 (let ((error-signaled-p nil))
308 (setq error-signaled-p t)
309 (when (not (string-equal (cadr err) error-message))
310 (ert-fail (format (concat "Expected the error \"%s\" to be thrown, "
311 "but instead it was \"%s\".")
314 (when (not error-signaled-p)
315 (ert-fail "Expected an error to be thrown, but there wasn't."))))
318 ;;; Miscellaneous tests
320 (defun context-coloring-test-assert-trimmed (result expected)
321 "Assert that RESULT is trimmed like EXPECTED."
322 (when (not (string-equal result expected))
323 (ert-fail "Expected string to be trimmed, but it wasn't.")))
325 (context-coloring-test-deftest trim
327 (context-coloring-test-assert-trimmed (context-coloring-trim "") "")
328 (context-coloring-test-assert-trimmed (context-coloring-trim " ") "")
329 (context-coloring-test-assert-trimmed (context-coloring-trim "a") "a")
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")))
334 (context-coloring-test-deftest-async mode-startup
338 'context-coloring-colorize-hook
340 ;; If this runs we are implicitly successful; this test only confirms
341 ;; that colorization occurs on mode startup.
343 (context-coloring-mode))
345 ;; TODO: This won't run if there is a timeout. Will probably have to
346 ;; roll our own `ert-deftest-async'.
347 (setq context-coloring-colorize-hook nil)))
349 (defmacro context-coloring-test-define-derived-mode (name)
350 "Define a derived mode exclusively for tests."
351 (let ((name (intern (format "context-coloring-test-%s-mode" name))))
352 `(define-derived-mode ,name fundamental-mode "Testing")))
354 (context-coloring-test-define-derived-mode change-detection)
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 (context-coloring-test-deftest-async change-detection
360 (context-coloring-define-dispatch
362 :modes '(context-coloring-test-change-detection-mode)
364 :command "node test/binaries/noop")
365 (context-coloring-test-change-detection-mode)
367 'context-coloring-colorize-hook
369 (setq context-coloring-colorize-hook nil)
371 'context-coloring-colorize-hook
375 (set-window-buffer (selected-window) (current-buffer))
376 (context-coloring-maybe-colorize (current-buffer))))
377 (context-coloring-mode))
379 (setq context-coloring-colorize-hook nil)))
381 (context-coloring-test-deftest 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 (context-coloring-test-deftest unsupported-mode
390 (context-coloring-mode)
391 (context-coloring-test-assert-message
392 "Context coloring is not available for this major mode"
395 (context-coloring-test-deftest derived-mode
397 (lisp-interaction-mode)
398 (context-coloring-mode)
399 (context-coloring-test-assert-not-message
400 "Context coloring is not available for this major mode"
403 (context-coloring-test-define-derived-mode define-dispatch-error)
405 (context-coloring-test-deftest define-dispatch-error
407 (context-coloring-test-assert-error
409 (context-coloring-define-dispatch
410 'define-dispatch-no-modes))
411 "No mode defined for dispatch")
412 (context-coloring-test-assert-error
414 (context-coloring-define-dispatch
415 'define-dispatch-no-strategy
416 :modes '(context-coloring-test-define-dispatch-error-mode)))
417 "No colorizer or command defined for dispatch")))
419 (context-coloring-test-define-derived-mode missing-executable)
421 (context-coloring-test-deftest missing-executable
423 (context-coloring-define-dispatch
425 :modes '(context-coloring-test-missing-executable-mode)
427 :executable "__should_not_exist__")
428 (context-coloring-test-missing-executable-mode)
429 (context-coloring-mode)))
431 (context-coloring-test-define-derived-mode unsupported-version)
433 (context-coloring-test-deftest-async unsupported-version
435 (context-coloring-define-dispatch
437 :modes '(context-coloring-test-unsupported-version-mode)
439 :command "node test/binaries/outta-date"
441 (context-coloring-test-unsupported-version-mode)
443 'context-coloring-check-scopifier-version-hook
447 ;; Normally the executable would be something like "outta-date"
448 ;; rather than "node".
449 (context-coloring-test-assert-message
450 "Update to the minimum version of \"node\" (v2.1.3)"
453 (context-coloring-mode))
455 (setq context-coloring-check-scopifier-version-hook nil)))
457 (context-coloring-test-define-derived-mode disable-mode)
459 (context-coloring-test-deftest-async disable-mode
462 (context-coloring-define-dispatch
464 :modes '(context-coloring-test-disable-mode-mode)
466 :command "node test/binaries/noop"
471 (context-coloring-test-disable-mode-mode)
472 (context-coloring-mode)
473 (context-coloring-mode -1)
474 (when (not torn-down)
475 (ert-fail "Expected teardown function to have been called, but it wasn't.")))
481 (defvar context-coloring-test-theme-index 0
482 "Unique index for unique theme names.")
484 (defun context-coloring-test-get-next-theme ()
485 "Return a unique symbol for a throwaway theme."
487 (intern (format "context-coloring-test-theme-%s"
488 context-coloring-test-theme-index))
489 (setq context-coloring-test-theme-index
490 (+ context-coloring-test-theme-index 1))))
492 (defun context-coloring-test-assert-face (level foreground &optional negate)
493 "Assert that a face for LEVEL exists and that its `:foreground'
494 is FOREGROUND, or the inverse if NEGATE is non-nil."
495 (let* ((face (context-coloring-level-face level))
497 (when (not (or negate
499 (ert-fail (format (concat "Expected face for level `%s' to exist; "
502 (setq actual-foreground (face-attribute face :foreground))
503 (when (funcall (if negate 'identity 'not)
504 (string-equal foreground actual-foreground))
505 (ert-fail (format (concat "Expected face for level `%s' "
506 "%sto have foreground `%s'; "
509 (if negate "not " "") foreground
511 "did" (format "was `%s'" actual-foreground)))))))
513 (defun context-coloring-test-assert-not-face (&rest arguments)
514 "Assert that LEVEL does not have a face with `:foreground'
515 FOREGROUND. Apply ARGUMENTS to
516 `context-coloring-test-assert-face', see that function."
517 (apply 'context-coloring-test-assert-face
518 (append arguments '(t))))
520 (defun context-coloring-test-assert-theme-originally-set-p
521 (settings &optional negate)
522 "Assert that `context-coloring-theme-originally-set-p' returns
523 t for a theme with SETTINGS, or the inverse if NEGATE is
525 (let ((theme (context-coloring-test-get-next-theme)))
526 (put theme 'theme-settings settings)
527 (when (funcall (if negate 'identity 'not)
528 (context-coloring-theme-originally-set-p theme))
529 (ert-fail (format (concat "Expected theme `%s' with settings `%s' "
530 "%sto be considered to have defined a level, "
533 (if negate "not " "")
534 (if negate "was" "wasn't"))))))
536 (defun context-coloring-test-assert-not-theme-originally-set-p (&rest arguments)
537 "Assert that `context-coloring-theme-originally-set-p' does not
538 return t for a theme with SETTINGS. Apply ARGUMENTS to
539 `context-coloring-test-assert-theme-originally-set-p', see that
541 (apply 'context-coloring-test-assert-theme-originally-set-p
542 (append arguments '(t))))
544 (context-coloring-test-deftest theme-originally-set-p
546 (context-coloring-test-assert-theme-originally-set-p
547 '((theme-face context-coloring-level-0-face)))
548 (context-coloring-test-assert-theme-originally-set-p
550 (theme-face context-coloring-level-0-face)))
551 (context-coloring-test-assert-theme-originally-set-p
552 '((theme-face context-coloring-level-0-face)
554 (context-coloring-test-assert-not-theme-originally-set-p
555 '((theme-face face)))))
557 (defun context-coloring-test-assert-theme-settings-highest-level
558 (settings expected-level)
559 "Assert that a theme with SETTINGS has the highest level
561 (let ((theme (context-coloring-test-get-next-theme)))
562 (put theme 'theme-settings settings)
563 (context-coloring-test-assert-theme-highest-level theme expected-level)))
565 (defun context-coloring-test-assert-theme-highest-level
566 (theme expected-level &optional negate)
567 "Assert that THEME has the highest level EXPECTED-LEVEL, or the
568 inverse if NEGATE is non-nil."
569 (let ((highest-level (context-coloring-theme-highest-level theme)))
570 (when (funcall (if negate 'identity 'not) (eq highest-level expected-level))
571 (ert-fail (format (concat "Expected theme with settings `%s' "
572 "%sto have a highest level of `%s', "
574 (get theme 'theme-settings)
575 (if negate "not " "") expected-level
576 (if negate "did" (format "was %s" highest-level)))))))
578 (defun context-coloring-test-assert-theme-not-highest-level (&rest arguments)
579 "Assert that THEME's highest level is not EXPECTED-LEVEL.
581 `context-coloring-test-assert-theme-highest-level', see that
583 (apply 'context-coloring-test-assert-theme-highest-level
584 (append arguments '(t))))
586 (context-coloring-test-deftest theme-highest-level
588 (context-coloring-test-assert-theme-settings-highest-level
591 (context-coloring-test-assert-theme-settings-highest-level
592 '((theme-face context-coloring-level-0-face))
594 (context-coloring-test-assert-theme-settings-highest-level
595 '((theme-face context-coloring-level-1-face))
597 (context-coloring-test-assert-theme-settings-highest-level
598 '((theme-face context-coloring-level-1-face)
599 (theme-face context-coloring-level-0-face))
601 (context-coloring-test-assert-theme-settings-highest-level
602 '((theme-face context-coloring-level-0-face)
603 (theme-face context-coloring-level-1-face))
606 (defun context-coloring-test-kill-buffer (buffer)
607 "Kill BUFFER if it exists."
608 (when (get-buffer buffer) (kill-buffer buffer)))
610 (defun context-coloring-test-deftheme (theme)
611 "Dynamically define theme THEME."
612 (eval (macroexpand `(deftheme ,theme))))
614 (context-coloring-test-deftest-define-theme additive
616 (context-coloring-test-deftheme theme)
617 (context-coloring-define-theme
621 (context-coloring-test-assert-no-message "*Warnings*")
623 (context-coloring-test-assert-no-message "*Warnings*")
624 (context-coloring-test-assert-face 0 "#aaaaaa")
625 (context-coloring-test-assert-face 1 "#bbbbbb")))
627 (defun context-coloring-test-assert-defined-warning (theme)
628 "Assert that a warning about colors already being defined for
629 theme THEME is signaled."
630 (context-coloring-test-assert-message
631 (format (concat "Warning (emacs): Context coloring colors for theme "
632 "`%s' are already defined")
636 (context-coloring-test-deftest-define-theme unintentional-override
638 (context-coloring-test-deftheme theme)
639 (custom-theme-set-faces
641 '(context-coloring-level-0-face ((t (:foreground "#aaaaaa"))))
642 '(context-coloring-level-1-face ((t (:foreground "#bbbbbb")))))
643 (context-coloring-define-theme
647 (context-coloring-test-assert-defined-warning theme)
648 (context-coloring-test-kill-buffer "*Warnings*")
650 (context-coloring-test-assert-defined-warning theme)
651 (context-coloring-test-assert-face 0 "#cccccc")
652 (context-coloring-test-assert-face 1 "#dddddd")))
654 (context-coloring-test-deftest-define-theme intentional-override
656 (context-coloring-test-deftheme theme)
657 (custom-theme-set-faces
659 '(context-coloring-level-0-face ((t (:foreground "#aaaaaa"))))
660 '(context-coloring-level-1-face ((t (:foreground "#bbbbbb")))))
661 (context-coloring-define-theme
666 (context-coloring-test-assert-no-message "*Warnings*")
668 (context-coloring-test-assert-no-message "*Warnings*")
669 (context-coloring-test-assert-face 0 "#cccccc")
670 (context-coloring-test-assert-face 1 "#dddddd")))
672 (context-coloring-test-deftest-define-theme pre-recede
674 (context-coloring-define-theme
679 (context-coloring-test-deftheme theme)
680 (custom-theme-set-faces
682 '(context-coloring-level-0-face ((t (:foreground "#cccccc"))))
683 '(context-coloring-level-1-face ((t (:foreground "#dddddd")))))
685 (context-coloring-test-assert-no-message "*Warnings*")
686 (context-coloring-test-assert-face 0 "#cccccc")
687 (context-coloring-test-assert-face 1 "#dddddd")))
689 (context-coloring-test-deftest-define-theme pre-recede-delayed-application
691 (context-coloring-define-theme
696 (context-coloring-test-deftheme theme)
698 (context-coloring-test-assert-no-message "*Warnings*")
699 (context-coloring-test-assert-face 0 "#aaaaaa")
700 (context-coloring-test-assert-face 1 "#bbbbbb")))
702 (context-coloring-test-deftest-define-theme post-recede
704 (context-coloring-test-deftheme theme)
705 (custom-theme-set-faces
707 '(context-coloring-level-0-face ((t (:foreground "#aaaaaa"))))
708 '(context-coloring-level-1-face ((t (:foreground "#bbbbbb")))))
709 (context-coloring-define-theme
714 (context-coloring-test-assert-no-message "*Warnings*")
715 (context-coloring-test-assert-face 0 "#aaaaaa")
716 (context-coloring-test-assert-face 1 "#bbbbbb")
718 (context-coloring-test-assert-no-message "*Warnings*")
719 (context-coloring-test-assert-face 0 "#aaaaaa")
720 (context-coloring-test-assert-face 1 "#bbbbbb")))
722 (context-coloring-test-deftest-define-theme recede-not-defined
724 (context-coloring-test-deftheme theme)
725 (custom-theme-set-faces
727 '(foo-face ((t (:foreground "#ffffff")))))
728 (context-coloring-define-theme
733 (context-coloring-test-assert-no-message "*Warnings*")
734 (context-coloring-test-assert-face 0 "#aaaaaa")
735 (context-coloring-test-assert-face 1 "#bbbbbb")
737 (context-coloring-test-assert-no-message "*Warnings*")
738 (context-coloring-test-assert-face 0 "#aaaaaa")
739 (context-coloring-test-assert-face 1 "#bbbbbb")))
741 (context-coloring-test-deftest-define-theme unintentional-obstinance
743 (context-coloring-define-theme
747 (context-coloring-test-deftheme theme)
748 (custom-theme-set-faces
750 '(context-coloring-level-0-face ((t (:foreground "#cccccc"))))
751 '(context-coloring-level-1-face ((t (:foreground "#dddddd")))))
753 (context-coloring-test-assert-defined-warning theme)
754 (context-coloring-test-assert-face 0 "#aaaaaa")
755 (context-coloring-test-assert-face 1 "#bbbbbb")))
757 (context-coloring-test-deftest-define-theme intentional-obstinance
759 (context-coloring-define-theme
764 (context-coloring-test-deftheme theme)
765 (custom-theme-set-faces
767 '(context-coloring-level-0-face ((t (:foreground "#cccccc"))))
768 '(context-coloring-level-1-face ((t (:foreground "#dddddd")))))
770 (context-coloring-test-assert-no-message "*Warnings*")
771 (context-coloring-test-assert-face 0 "#aaaaaa")
772 (context-coloring-test-assert-face 1 "#bbbbbb")))
774 (defun context-coloring-test-assert-maximum-face (maximum &optional negate)
775 "Assert that `context-coloring-maximum-face' is MAXIMUM, or the
776 inverse if NEGATE is non-nil."
777 (when (funcall (if negate 'identity 'not)
778 (eq context-coloring-maximum-face maximum))
779 (ert-fail (format (concat "Expected `context-coloring-maximum-face' "
782 (if negate "not " "") maximum
785 (format "was `%s'" context-coloring-maximum-face))))))
787 (defun context-coloring-test-assert-not-maximum-face (&rest arguments)
788 "Assert that `context-coloring-maximum-face' is not MAXIMUM.
789 Apply ARGUMENTS to `context-coloring-test-assert-maximum-face',
791 (apply 'context-coloring-test-assert-maximum-face
792 (append arguments '(t))))
794 (context-coloring-test-deftest-define-theme disable-cascade
796 (let ((maximum-face-value 9999))
797 (setq context-coloring-maximum-face maximum-face-value)
798 (context-coloring-test-deftheme theme)
799 (context-coloring-define-theme
803 (let ((second-theme (context-coloring-test-get-next-theme)))
804 (context-coloring-test-deftheme second-theme)
805 (context-coloring-define-theme
810 (let ((third-theme (context-coloring-test-get-next-theme)))
811 (context-coloring-test-deftheme third-theme)
812 (context-coloring-define-theme
819 (enable-theme second-theme)
820 (enable-theme third-theme)
821 (disable-theme third-theme)
822 (context-coloring-test-assert-face 0 "#cccccc")
823 (context-coloring-test-assert-face 1 "#dddddd")
824 (context-coloring-test-assert-face 2 "#eeeeee")
825 (context-coloring-test-assert-maximum-face 2))
826 (disable-theme second-theme)
827 (context-coloring-test-assert-face 0 "#aaaaaa")
828 (context-coloring-test-assert-face 1 "#bbbbbb")
829 (context-coloring-test-assert-maximum-face 1))
830 (disable-theme theme)
831 (context-coloring-test-assert-not-face 0 "#aaaaaa")
832 (context-coloring-test-assert-not-face 1 "#bbbbbb")
833 (context-coloring-test-assert-maximum-face
834 maximum-face-value))))
839 (defun context-coloring-test-assert-position-level (position level)
840 "Assert that POSITION has LEVEL."
841 (let ((face (get-text-property position 'face))
844 (let* ((face-string (symbol-name face))
845 (matches (string-match
846 context-coloring-level-face-regexp
849 (setq actual-level (string-to-number
850 (substring face-string
853 (= level actual-level)))))
854 (ert-fail (format (concat "Expected level at position %s, "
855 "which is \"%s\", to be %s; "
858 (buffer-substring-no-properties position (1+ position)) level
861 (defun context-coloring-test-assert-position-face (position face-regexp)
862 "Assert that the face at POSITION satisfies FACE-REGEXP."
863 (let ((face (get-text-property position 'face)))
865 ;; Pass a non-string to do an `equal' check (against a symbol or nil).
866 (unless (stringp face-regexp)
867 (not (equal face-regexp face)))
868 ;; Otherwise do the matching.
869 (when (stringp face-regexp)
870 (not (string-match-p face-regexp (symbol-name face)))))
871 (ert-fail (format (concat "Expected face at position %s, "
872 "which is \"%s\", to be %s; "
875 (buffer-substring-no-properties position (1+ position)) face-regexp
878 (defun context-coloring-test-assert-position-comment (position)
879 "Assert that the face at POSITION is a comment."
880 (context-coloring-test-assert-position-face
881 position "\\`font-lock-comment\\(-delimiter\\)?-face\\'"))
883 (defun context-coloring-test-assert-position-constant-comment (position)
884 "Assert that the face at POSITION is a constant comment."
885 (context-coloring-test-assert-position-face position '(font-lock-constant-face
886 font-lock-comment-face)))
888 (defun context-coloring-test-assert-position-string (position)
889 "Assert that the face at POSITION is a string."
890 (context-coloring-test-assert-position-face position 'font-lock-string-face))
892 (defun context-coloring-test-assert-position-nil (position)
893 "Assert that the face at POSITION is nil."
894 (context-coloring-test-assert-position-face position nil))
896 (defun context-coloring-test-assert-coloring (map)
897 "Assert that the current buffer's coloring matches MAP.
899 MAP's newlines should correspond to the current fixture.
901 The following characters appearing in MAP assert coloring for
902 corresponding points in the fixture:
904 0-9: Level equals number.
905 C: Face is constant comment.
910 Any other characters are discarded. Characters \"x\" and any
911 other non-letters are guaranteed to always be discarded."
912 ;; Omit the superfluous, formatting-related leading newline. Can't use
913 ;; `save-excursion' here because if an assertion fails it will cause future
914 ;; tests to get messed up.
915 (goto-char (point-min))
916 (let* ((map (substring map 1))
920 (while (< index (length map))
921 (setq char-string (substring map index (1+ index)))
922 (setq char (string-to-char char-string))
931 (context-coloring-test-assert-position-level
932 (point) (string-to-number char-string))
934 ;; 'C' = Constant comment
936 (context-coloring-test-assert-position-constant-comment (point))
940 (context-coloring-test-assert-position-comment (point))
944 (context-coloring-test-assert-position-nil (point))
948 (context-coloring-test-assert-position-string (point))
952 (setq index (1+ index)))))
954 (context-coloring-test-deftest-js-js2 function-scopes
956 (context-coloring-test-assert-coloring "
957 000 0 0 11111111 11 110
959 111 1 1 22222222 22 221
963 (context-coloring-test-deftest-js-js2 global
965 (context-coloring-test-assert-coloring "
967 111 1 1 00000001xxx11
970 (context-coloring-test-deftest-js2 block-scopes
972 (context-coloring-test-assert-coloring "
980 (setq context-coloring-js-block-scopes t))
982 (setq context-coloring-js-block-scopes nil)))
984 (context-coloring-test-deftest-js-js2 catch
986 (context-coloring-test-assert-coloring "
996 (context-coloring-test-deftest-js-js2 key-names
998 (context-coloring-test-assert-coloring "
1006 (context-coloring-test-deftest-js-js2 property-lookup
1008 (context-coloring-test-assert-coloring "
1015 (context-coloring-test-deftest-js-js2 key-values
1017 (context-coloring-test-assert-coloring "
1027 (context-coloring-test-deftest-js-js2 syntactic-comments-and-strings
1029 (context-coloring-test-assert-coloring "
1034 :fixture "comments-and-strings.js")
1036 (context-coloring-test-deftest-js-js2 syntactic-comments
1038 (context-coloring-test-assert-coloring "
1043 :fixture "comments-and-strings.js"
1045 (setq context-coloring-syntactic-strings nil))
1047 (setq context-coloring-syntactic-strings t)))
1049 (context-coloring-test-deftest-js-js2 syntactic-strings
1051 (context-coloring-test-assert-coloring "
1056 :fixture "comments-and-strings.js"
1058 (setq context-coloring-syntactic-comments nil))
1060 (setq context-coloring-syntactic-comments t)))
1062 (context-coloring-test-deftest-js2 unterminated-comment
1063 ;; As long as `add-text-properties' doesn't signal an error, this test passes.
1066 (context-coloring-test-deftest-emacs-lisp defun
1068 (context-coloring-test-assert-coloring "
1069 111111 000 1111 111 111111111 1111
1070 11 111 111 111 000011
1077 (context-coloring-test-deftest-emacs-lisp lambda
1079 (context-coloring-test-assert-coloring "
1080 00000000 1111111 1111
1081 11111111 11 2222222 2222
1082 222 22 12 2221 111 0 00")))
1084 (context-coloring-test-deftest-emacs-lisp quote
1086 (context-coloring-test-assert-coloring "
1090 111111 1 1111111111 11 111 1 111 1 00001 10000 11 00001 1 10000
1098 (xxxxxx () 111111 11111)")))
1100 (context-coloring-test-deftest-emacs-lisp splice
1102 (context-coloring-test-assert-coloring "
1104 111111 00001 100001)")))
1106 (context-coloring-test-deftest-emacs-lisp comment
1108 ;; Just check that the comment isn't parsed syntactically.
1109 (context-coloring-test-assert-coloring "
1111 (xx (x xxxxx-xxxx xx) cccccccccc
1112 11 00000-0000 11))) cccccccccc")))
1114 (context-coloring-test-deftest-emacs-lisp string
1116 (context-coloring-test-assert-coloring "
1118 (xxxxxx x x sss 1 0 sssss 0 1 sssssss11")))
1120 (context-coloring-test-deftest-emacs-lisp ignored
1122 (context-coloring-test-assert-coloring "
1124 (x x 1 11 11 111 111 11 1 111 (1 1 1)))")))
1126 (context-coloring-test-deftest-emacs-lisp let
1128 (context-coloring-test-assert-coloring "
1138 1111 1 1 1 000011")))
1140 (context-coloring-test-deftest-emacs-lisp let*
1142 (context-coloring-test-assert-coloring "
1146 1111 1 1 1 0 0 00001
1152 2222 1 1 2 2 2 000022
1153 1111 1 1 1 0 0 000011")))
1155 (defun context-coloring-test-insert-unread-space ()
1156 "Simulate the insertion of a space as if by a user."
1157 (setq unread-command-events (cons '(t . 32)
1158 unread-command-events)))
1160 (defun context-coloring-test-remove-faces ()
1161 "Remove all faces in the current buffer."
1162 (remove-text-properties (point-min) (point-max) '(face nil)))
1164 (context-coloring-test-deftest-emacs-lisp iteration
1166 (let ((context-coloring-elisp-sexps-per-pause 2))
1167 (context-coloring-colorize)
1168 (context-coloring-test-assert-coloring "
1171 (context-coloring-test-remove-faces)
1172 (context-coloring-test-insert-unread-space)
1173 (context-coloring-colorize)
1174 ;; Coloring is interrupted after the first "sexp" (the comment in this
1176 (context-coloring-test-assert-coloring "
1180 (provide 'context-coloring-test)
1182 ;;; context-coloring-test.el ends here