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 (defun context-coloring-test-before-all ()
46 "Prepare before all tests."
47 (setq context-coloring-syntactic-comments nil)
48 (setq context-coloring-syntactic-strings nil)
49 ;; TODO: Should only be for js2-mode tests.
50 (setq js2-mode-show-parse-errors nil)
51 (setq js2-mode-show-strict-warnings nil))
53 (defun context-coloring-test-after-all ()
54 "Cleanup after all tests."
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-before-all)
68 (insert (context-coloring-test-read-file ,fixture))
70 (context-coloring-test-after-all))))
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 (fixture callback)
87 "With the relative FIXTURE, evaluate CALLBACK in a temporary
88 buffer. A teardown callback is passed to CALLBACK for it to
89 invoke when it is done."
90 (context-coloring-test-with-temp-buffer-async
91 (lambda (done-with-temp-buffer)
92 (context-coloring-test-before-all)
93 (insert (context-coloring-test-read-file fixture))
97 (context-coloring-test-after-all)
98 (funcall done-with-temp-buffer))))))
101 ;;; Test defining utilities
103 (cl-defmacro context-coloring-test-define-deftest (name
107 "Define a deftest defmacro for tests prefixed with NAME. MODE
108 is called to set up the test's environment. EXTENSION denotes
109 the suffix for tests' fixture files."
110 (declare (indent defun))
111 (let ((macro-name (intern (format "context-coloring-test-deftest-%s" name))))
112 `(cl-defmacro ,macro-name (name
117 ,(format "Define a test for `%s' suffixed with NAME.
118 Function BODY makes assertions. The default fixture has a
119 filename matching NAME (plus the filetype extension, \"%s\"),
120 unless FIXTURE is specified to override it. Functions BEFORE
121 and AFTER run before and after the test, even if an error is
124 BODY is run after `context-coloring-mode' is activated, or after
125 initial colorization if colorization should occur."
126 (cadr mode) extension)
127 (declare (indent defun))
128 ;; Commas in nested backquotes are not evaluated. Binding the mode here
129 ;; is probably the cleanest workaround.
131 (test-name (intern (format ,(format "%s-%%s" name) name)))
133 (fixture (format "./fixtures/%s" fixture))
134 (t (format ,(format "./fixtures/%%s.%s" extension) name)))))
137 `(`(ert-deftest-async ,test-name (done)
138 (context-coloring-test-with-fixture-async
140 (lambda (done-with-fixture)
142 (when ,before (funcall ,before))
143 (context-coloring-mode)
144 ;; TODO: Rigid expectations, should be looser.
145 (context-coloring-colorize
150 (when ,after (funcall ,after))
151 (funcall done-with-fixture))
152 (funcall done))))))))
154 `(`(ert-deftest ,test-name ()
155 (context-coloring-test-with-fixture
158 (when ,before (funcall ,before))
159 (context-coloring-mode)
163 (when ,after (funcall ,after))))))))))))
165 (context-coloring-test-define-deftest js
170 (context-coloring-test-define-deftest js2
174 (defmacro context-coloring-test-deftest-js-js2 (&rest args)
175 "Simultaneously define the same test for js and js2."
176 (declare (indent defun))
178 (context-coloring-test-deftest-js ,@args)
179 (context-coloring-test-deftest-js2 ,@args)))
181 (context-coloring-test-define-deftest emacs-lisp
182 :mode 'emacs-lisp-mode
186 ;;; Assertion functions
188 (defun context-coloring-test-get-last-message ()
189 (let ((messages (split-string
190 (buffer-substring-no-properties
194 (car (nthcdr (- (length messages) 2) messages))))
196 (defun context-coloring-test-assert-message (expected buffer)
197 "Assert that message EXPECTED is at the end of BUFFER."
198 (when (null (get-buffer buffer))
202 "Expected buffer `%s' to have message \"%s\", "
203 "but the buffer did not have any messages.")
205 (with-current-buffer buffer
206 (let ((message (context-coloring-test-get-last-message)))
207 (when (not (equal message expected))
211 "Expected buffer `%s' to have message \"%s\", "
212 "but instead it was \"%s\"")
216 (defun context-coloring-test-assert-not-message (expected buffer)
217 "Assert that message EXPECTED is not at the end of BUFFER."
218 (when (get-buffer buffer)
219 (with-current-buffer buffer
220 (let ((message (context-coloring-test-get-last-message)))
221 (when (equal message expected)
225 "Expected buffer `%s' not to have message \"%s\", "
227 buffer expected)))))))
229 (defun context-coloring-test-assert-no-message (buffer)
230 "Assert that BUFFER has no message."
231 (when (get-buffer buffer)
232 (ert-fail (format (concat "Expected buffer `%s' to have no messages, "
235 (with-current-buffer buffer
238 (defun context-coloring-test-assert-error (body error-message)
239 "Assert that BODY signals ERROR-MESSAGE."
240 (let ((error-signaled-p nil))
245 (setq error-signaled-p t)
246 (when (not (string-equal (cadr err) error-message))
247 (ert-fail (format (concat "Expected the error \"%s\" to be thrown, "
248 "but instead it was \"%s\".")
251 (when (not error-signaled-p)
252 (ert-fail "Expected an error to be thrown, but there wasn't."))))
255 ;;; Miscellaneous tests
257 (defun context-coloring-test-assert-trimmed (result expected)
258 (when (not (string-equal result expected))
259 (ert-fail "Expected string to be trimmed, but it wasn't.")))
261 (ert-deftest context-coloring-test-trim ()
262 (context-coloring-test-assert-trimmed (context-coloring-trim "") "")
263 (context-coloring-test-assert-trimmed (context-coloring-trim " ") "")
264 (context-coloring-test-assert-trimmed (context-coloring-trim "a") "a")
265 (context-coloring-test-assert-trimmed (context-coloring-trim " a") "a")
266 (context-coloring-test-assert-trimmed (context-coloring-trim "a ") "a")
267 (context-coloring-test-assert-trimmed (context-coloring-trim " a ") "a"))
269 (ert-deftest-async context-coloring-test-async-mode-startup (done)
270 (context-coloring-test-with-fixture-async
275 'context-coloring-colorize-hook
277 ;; If this runs we are implicitly successful; this test only confirms
278 ;; that colorization occurs on mode startup.
281 (context-coloring-mode))))
283 (defmacro context-coloring-test-define-derived-mode (name)
284 (let ((name (intern (format "context-coloring-test-%s-mode" name))))
285 `(define-derived-mode ,name fundamental-mode "Testing")))
287 (context-coloring-test-define-derived-mode change-detection)
289 ;; Simply cannot figure out how to trigger an idle timer; would much rather test
290 ;; that. But (current-idle-time) always returns nil in these tests.
291 (ert-deftest-async context-coloring-test-change-detection (done)
292 (context-coloring-define-dispatch
294 :modes '(context-coloring-test-change-detection-mode)
296 :command "node test/binaries/noop")
297 (context-coloring-test-with-fixture-async
300 (context-coloring-test-change-detection-mode)
302 'context-coloring-colorize-hook
304 (setq context-coloring-colorize-hook nil)
306 'context-coloring-colorize-hook
311 (set-window-buffer (selected-window) (current-buffer))
312 (context-coloring-maybe-colorize (current-buffer))))
313 (context-coloring-mode))))
315 (ert-deftest context-coloring-test-check-version ()
316 (when (not (context-coloring-check-version "2.1.3" "3.0.1"))
317 (ert-fail "Expected version 3.0.1 to satisfy 2.1.3, but it didn't."))
318 (when (context-coloring-check-version "3.0.1" "2.1.3")
319 (ert-fail "Expected version 2.1.3 not to satisfy 3.0.1, but it did.")))
321 (ert-deftest context-coloring-test-unsupported-mode ()
322 (context-coloring-test-with-fixture
324 (context-coloring-mode)
325 (context-coloring-test-assert-message
326 "Context coloring is not available for this major mode"
329 (ert-deftest context-coloring-test-derived-mode ()
330 (context-coloring-test-with-fixture
332 (lisp-interaction-mode)
333 (context-coloring-mode)
334 (context-coloring-test-assert-not-message
335 "Context coloring is not available for this major mode"
338 (context-coloring-test-define-derived-mode define-dispatch-error)
340 (ert-deftest context-coloring-test-define-dispatch-error ()
341 (context-coloring-test-assert-error
343 (context-coloring-define-dispatch
344 'define-dispatch-no-modes))
345 "No mode defined for dispatch")
346 (context-coloring-test-assert-error
348 (context-coloring-define-dispatch
349 'define-dispatch-no-strategy
350 :modes '(context-coloring-test-define-dispatch-error-mode)))
351 "No colorizer, scopifier or command defined for dispatch"))
353 (context-coloring-test-define-derived-mode define-dispatch-scopifier)
355 (ert-deftest context-coloring-test-define-dispatch-scopifier ()
356 (context-coloring-define-dispatch
357 'define-dispatch-scopifier
358 :modes '(context-coloring-test-define-dispatch-scopifier-mode)
359 :scopifier (lambda () (vector)))
361 (context-coloring-test-define-dispatch-scopifier-mode)
362 (context-coloring-mode)
363 (context-coloring-colorize)))
365 (context-coloring-test-define-derived-mode missing-executable)
367 (ert-deftest context-coloring-test-missing-executable ()
368 (context-coloring-define-dispatch
370 :modes '(context-coloring-test-missing-executable-mode)
372 :executable "__should_not_exist__")
374 (context-coloring-test-missing-executable-mode)
375 (context-coloring-mode)))
377 (context-coloring-test-define-derived-mode unsupported-version)
379 (ert-deftest-async context-coloring-test-unsupported-version (done)
380 (context-coloring-define-dispatch
382 :modes '(context-coloring-test-unsupported-version-mode)
384 :command "node test/binaries/outta-date"
386 (context-coloring-test-with-fixture-async
389 (context-coloring-test-unsupported-version-mode)
391 'context-coloring-check-scopifier-version-hook
395 ;; Normally the executable would be something like "outta-date"
396 ;; rather than "node".
397 (context-coloring-test-assert-message
398 "Update to the minimum version of \"node\" (v2.1.3)"
402 (context-coloring-mode))))
404 (context-coloring-test-define-derived-mode disable-mode)
406 (ert-deftest-async context-coloring-test-disable-mode (done)
408 (context-coloring-define-dispatch
410 :modes '(context-coloring-test-disable-mode-mode)
412 :command "node test/binaries/noop"
415 (context-coloring-test-with-fixture-async
420 (context-coloring-test-disable-mode-mode)
421 (context-coloring-mode)
422 (context-coloring-mode -1)
423 (when (not torn-down)
424 (ert-fail "Expected teardown function to have been called, but it wasn't.")))
431 (defvar context-coloring-test-theme-index 0
432 "Unique index for unique theme names.")
434 (defun context-coloring-test-get-next-theme ()
435 "Return a unique symbol for a throwaway theme."
437 (intern (format "context-coloring-test-theme-%s"
438 context-coloring-test-theme-index))
439 (setq context-coloring-test-theme-index
440 (+ context-coloring-test-theme-index 1))))
442 (defun context-coloring-test-assert-face (level foreground &optional negate)
443 "Assert that a face for LEVEL exists and that its `:foreground'
444 is FOREGROUND, or the inverse if NEGATE is non-nil."
445 (let* ((face (context-coloring-level-face level))
447 (when (not (or negate
449 (ert-fail (format (concat "Expected face for level `%s' to exist; "
452 (setq actual-foreground (face-attribute face :foreground))
453 (when (funcall (if negate 'identity 'not)
454 (string-equal foreground actual-foreground))
455 (ert-fail (format (concat "Expected face for level `%s' "
456 "%sto have foreground `%s'; "
459 (if negate "not " "") foreground
461 "did" (format "was `%s'" actual-foreground)))))))
463 (defun context-coloring-test-assert-not-face (&rest arguments)
464 "Assert that LEVEL does not have a face with `:foreground'
465 FOREGROUND. Apply ARGUMENTS to
466 `context-coloring-test-assert-face', see that function."
467 (apply 'context-coloring-test-assert-face
468 (append arguments '(t))))
470 (defun context-coloring-test-assert-theme-originally-set-p
471 (settings &optional negate)
472 "Assert that `context-coloring-theme-originally-set-p' returns
473 t for a theme with SETTINGS, or the inverse if NEGATE is
475 (let ((theme (context-coloring-test-get-next-theme)))
476 (put theme 'theme-settings settings)
477 (when (funcall (if negate 'identity 'not)
478 (context-coloring-theme-originally-set-p theme))
479 (ert-fail (format (concat "Expected theme `%s' with settings `%s' "
480 "%sto be considered to have defined a level, "
483 (if negate "not " "")
484 (if negate "was" "wasn't"))))))
486 (defun context-coloring-test-assert-not-theme-originally-set-p (&rest arguments)
487 "Assert that `context-coloring-theme-originally-set-p' does not
488 return t for a theme with SETTINGS. Apply ARGUMENTS to
489 `context-coloring-test-assert-theme-originally-set-p', see that
491 (apply 'context-coloring-test-assert-theme-originally-set-p
492 (append arguments '(t))))
494 (ert-deftest context-coloring-test-theme-originally-set-p ()
495 (context-coloring-test-assert-theme-originally-set-p
496 '((theme-face context-coloring-level-0-face)))
497 (context-coloring-test-assert-theme-originally-set-p
499 (theme-face context-coloring-level-0-face)))
500 (context-coloring-test-assert-theme-originally-set-p
501 '((theme-face context-coloring-level-0-face)
503 (context-coloring-test-assert-not-theme-originally-set-p
504 '((theme-face face))))
506 (defun context-coloring-test-assert-theme-settings-highest-level
507 (settings expected-level)
508 "Assert that a theme with SETTINGS has the highest level
510 (let ((theme (context-coloring-test-get-next-theme)))
511 (put theme 'theme-settings settings)
512 (context-coloring-test-assert-theme-highest-level theme expected-level)))
514 (defun context-coloring-test-assert-theme-highest-level
515 (theme expected-level &optional negate)
516 "Assert that THEME has the highest level EXPECTED-LEVEL, or the
517 inverse if NEGATE is non-nil."
518 (let ((highest-level (context-coloring-theme-highest-level theme)))
519 (when (funcall (if negate 'identity 'not) (eq highest-level expected-level))
520 (ert-fail (format (concat "Expected theme with settings `%s' "
521 "%sto have a highest level of `%s', "
523 (get theme 'theme-settings)
524 (if negate "not " "") expected-level
525 (if negate "did" (format "was %s" highest-level)))))))
527 (defun context-coloring-test-assert-theme-not-highest-level (&rest arguments)
528 "Assert that THEME's highest level is not EXPECTED-LEVEL.
530 `context-coloring-test-assert-theme-highest-level', see that
532 (apply 'context-coloring-test-assert-theme-highest-level
533 (append arguments '(t))))
535 (ert-deftest context-coloring-test-theme-highest-level ()
536 (context-coloring-test-assert-theme-settings-highest-level
539 (context-coloring-test-assert-theme-settings-highest-level
540 '((theme-face context-coloring-level-0-face))
542 (context-coloring-test-assert-theme-settings-highest-level
543 '((theme-face context-coloring-level-1-face))
545 (context-coloring-test-assert-theme-settings-highest-level
546 '((theme-face context-coloring-level-1-face)
547 (theme-face context-coloring-level-0-face))
549 (context-coloring-test-assert-theme-settings-highest-level
550 '((theme-face context-coloring-level-0-face)
551 (theme-face context-coloring-level-1-face))
554 (defun context-coloring-test-kill-buffer (buffer)
555 "Kill BUFFER if it exists."
556 (when (get-buffer buffer) (kill-buffer buffer)))
558 (defmacro context-coloring-test-deftest-define-theme (name &rest body)
559 "Define a test with name NAME and an automatically-generated
560 theme symbol available as a free variable `theme'. Side-effects
561 from enabling themes are reversed after BODY is executed and the
563 (declare (indent defun))
564 (let ((deftest-name (intern
565 (format "context-coloring-test-define-theme-%s" name))))
566 `(ert-deftest ,deftest-name ()
567 (context-coloring-test-kill-buffer "*Warnings*")
568 (context-coloring-test-before-all)
569 (let ((theme (context-coloring-test-get-next-theme)))
574 (disable-theme theme)
575 (context-coloring-test-after-all))))))
577 (defun context-coloring-test-deftheme (theme)
578 "Dynamically define theme THEME."
579 (eval (macroexpand `(deftheme ,theme))))
581 (context-coloring-test-deftest-define-theme additive
582 (context-coloring-test-deftheme theme)
583 (context-coloring-define-theme
587 (context-coloring-test-assert-no-message "*Warnings*")
589 (context-coloring-test-assert-no-message "*Warnings*")
590 (context-coloring-test-assert-face 0 "#aaaaaa")
591 (context-coloring-test-assert-face 1 "#bbbbbb"))
593 (defun context-coloring-test-assert-defined-warning (theme)
594 "Assert that a warning about colors already being defined for
595 theme THEME is signaled."
596 (context-coloring-test-assert-message
597 (format (concat "Warning (emacs): Context coloring colors for theme "
598 "`%s' are already defined")
602 (context-coloring-test-deftest-define-theme unintentional-override
603 (context-coloring-test-deftheme theme)
604 (custom-theme-set-faces
606 '(context-coloring-level-0-face ((t (:foreground "#aaaaaa"))))
607 '(context-coloring-level-1-face ((t (:foreground "#bbbbbb")))))
608 (context-coloring-define-theme
612 (context-coloring-test-assert-defined-warning theme)
613 (context-coloring-test-kill-buffer "*Warnings*")
615 (context-coloring-test-assert-defined-warning theme)
616 (context-coloring-test-assert-face 0 "#cccccc")
617 (context-coloring-test-assert-face 1 "#dddddd"))
619 (context-coloring-test-deftest-define-theme intentional-override
620 (context-coloring-test-deftheme theme)
621 (custom-theme-set-faces
623 '(context-coloring-level-0-face ((t (:foreground "#aaaaaa"))))
624 '(context-coloring-level-1-face ((t (:foreground "#bbbbbb")))))
625 (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 "#cccccc")
634 (context-coloring-test-assert-face 1 "#dddddd"))
636 (context-coloring-test-deftest-define-theme pre-recede
637 (context-coloring-define-theme
642 (context-coloring-test-deftheme theme)
643 (custom-theme-set-faces
645 '(context-coloring-level-0-face ((t (:foreground "#cccccc"))))
646 '(context-coloring-level-1-face ((t (:foreground "#dddddd")))))
648 (context-coloring-test-assert-no-message "*Warnings*")
649 (context-coloring-test-assert-face 0 "#cccccc")
650 (context-coloring-test-assert-face 1 "#dddddd"))
652 (context-coloring-test-deftest-define-theme pre-recede-delayed-application
653 (context-coloring-define-theme
658 (context-coloring-test-deftheme theme)
660 (context-coloring-test-assert-no-message "*Warnings*")
661 (context-coloring-test-assert-face 0 "#aaaaaa")
662 (context-coloring-test-assert-face 1 "#bbbbbb"))
664 (context-coloring-test-deftest-define-theme post-recede
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*")
676 (context-coloring-test-assert-face 0 "#aaaaaa")
677 (context-coloring-test-assert-face 1 "#bbbbbb")
679 (context-coloring-test-assert-no-message "*Warnings*")
680 (context-coloring-test-assert-face 0 "#aaaaaa")
681 (context-coloring-test-assert-face 1 "#bbbbbb"))
683 (context-coloring-test-deftest-define-theme recede-not-defined
684 (context-coloring-test-deftheme theme)
685 (custom-theme-set-faces
687 '(foo-face ((t (:foreground "#ffffff")))))
688 (context-coloring-define-theme
693 (context-coloring-test-assert-no-message "*Warnings*")
694 (context-coloring-test-assert-face 0 "#aaaaaa")
695 (context-coloring-test-assert-face 1 "#bbbbbb")
697 (context-coloring-test-assert-no-message "*Warnings*")
698 (context-coloring-test-assert-face 0 "#aaaaaa")
699 (context-coloring-test-assert-face 1 "#bbbbbb"))
701 (context-coloring-test-deftest-define-theme unintentional-obstinance
702 (context-coloring-define-theme
706 (context-coloring-test-deftheme theme)
707 (custom-theme-set-faces
709 '(context-coloring-level-0-face ((t (:foreground "#cccccc"))))
710 '(context-coloring-level-1-face ((t (:foreground "#dddddd")))))
712 (context-coloring-test-assert-defined-warning theme)
713 (context-coloring-test-assert-face 0 "#aaaaaa")
714 (context-coloring-test-assert-face 1 "#bbbbbb"))
716 (context-coloring-test-deftest-define-theme intentional-obstinance
717 (context-coloring-define-theme
722 (context-coloring-test-deftheme theme)
723 (custom-theme-set-faces
725 '(context-coloring-level-0-face ((t (:foreground "#cccccc"))))
726 '(context-coloring-level-1-face ((t (:foreground "#dddddd")))))
728 (context-coloring-test-assert-no-message "*Warnings*")
729 (context-coloring-test-assert-face 0 "#aaaaaa")
730 (context-coloring-test-assert-face 1 "#bbbbbb"))
732 (defun context-coloring-test-assert-maximum-face (maximum &optional negate)
733 "Assert that `context-coloring-maximum-face' is MAXIMUM, or the
734 inverse if NEGATE is non-nil."
735 (when (funcall (if negate 'identity 'not)
736 (eq context-coloring-maximum-face maximum))
737 (ert-fail (format (concat "Expected `context-coloring-maximum-face' "
740 (if negate "not " "") maximum
743 (format "was `%s'" context-coloring-maximum-face))))))
745 (defun context-coloring-test-assert-not-maximum-face (&rest arguments)
746 "Assert that `context-coloring-maximum-face' is not MAXIMUM.
747 Apply ARGUMENTS to `context-coloring-test-assert-maximum-face',
749 (apply 'context-coloring-test-assert-maximum-face
750 (append arguments '(t))))
752 (context-coloring-test-deftest-define-theme disable-cascade
753 (let ((maximum-face-value 9999))
754 (setq context-coloring-maximum-face maximum-face-value)
755 (context-coloring-test-deftheme theme)
756 (context-coloring-define-theme
760 (let ((second-theme (context-coloring-test-get-next-theme)))
761 (context-coloring-test-deftheme second-theme)
762 (context-coloring-define-theme
767 (let ((third-theme (context-coloring-test-get-next-theme)))
768 (context-coloring-test-deftheme third-theme)
769 (context-coloring-define-theme
776 (enable-theme second-theme)
777 (enable-theme third-theme)
778 (disable-theme third-theme)
779 (context-coloring-test-assert-face 0 "#cccccc")
780 (context-coloring-test-assert-face 1 "#dddddd")
781 (context-coloring-test-assert-face 2 "#eeeeee")
782 (context-coloring-test-assert-maximum-face 2))
783 (disable-theme second-theme)
784 (context-coloring-test-assert-face 0 "#aaaaaa")
785 (context-coloring-test-assert-face 1 "#bbbbbb")
786 (context-coloring-test-assert-maximum-face 1))
787 (disable-theme theme)
788 (context-coloring-test-assert-not-face 0 "#aaaaaa")
789 (context-coloring-test-assert-not-face 1 "#bbbbbb")
790 (context-coloring-test-assert-maximum-face
791 maximum-face-value)))
796 (defun context-coloring-test-assert-position-level (position level)
797 "Assert that POSITION has LEVEL."
798 (let ((face (get-text-property position 'face))
801 (let* ((face-string (symbol-name face))
802 (matches (string-match
803 context-coloring-level-face-regexp
806 (setq actual-level (string-to-number
807 (substring face-string
810 (= level actual-level)))))
811 (ert-fail (format (concat "Expected level at position %s, "
812 "which is \"%s\", to be %s; "
815 (buffer-substring-no-properties position (1+ position)) level
818 (defun context-coloring-test-assert-position-face (position face-regexp)
819 "Assert that the face at POSITION satisfies FACE-REGEXP."
820 (let ((face (get-text-property position 'face)))
822 ;; Pass a non-string to do an `equal' check (against a symbol or nil).
823 (unless (stringp face-regexp)
824 (not (equal face-regexp face)))
825 ;; Otherwise do the matching.
826 (when (stringp face-regexp)
827 (not (string-match-p face-regexp (symbol-name face)))))
828 (ert-fail (format (concat "Expected face at position %s, "
829 "which is \"%s\", to be %s; "
832 (buffer-substring-no-properties position (1+ position)) face-regexp
835 (defun context-coloring-test-assert-position-comment (position)
836 (context-coloring-test-assert-position-face
837 position "\\`font-lock-comment\\(-delimiter\\)?-face\\'"))
839 (defun context-coloring-test-assert-position-constant-comment (position)
840 (context-coloring-test-assert-position-face position '(font-lock-constant-face
841 font-lock-comment-face)))
843 (defun context-coloring-test-assert-position-string (position)
844 (context-coloring-test-assert-position-face position 'font-lock-string-face))
846 (defun context-coloring-test-assert-position-nil (position)
847 (context-coloring-test-assert-position-face position nil))
849 (defun context-coloring-test-assert-coloring (map)
850 "Assert that the current buffer's coloring matches MAP."
851 ;; Omit the superfluous, formatting-related leading newline. Can't use
852 ;; `save-excursion' here because if an assertion fails it will cause future
853 ;; tests to get messed up.
854 (goto-char (point-min))
855 (let* ((map (substring map 1))
859 (while (< index (length map))
860 (setq char-string (substring map index (1+ index)))
861 (setq char (string-to-char char-string))
870 (context-coloring-test-assert-position-level
871 (point) (string-to-number char-string))
873 ;; 'C' = Constant comment
875 (context-coloring-test-assert-position-constant-comment (point))
879 (context-coloring-test-assert-position-comment (point))
883 (context-coloring-test-assert-position-nil (point))
887 (context-coloring-test-assert-position-string (point))
891 (setq index (1+ index)))))
893 (context-coloring-test-deftest-js-js2 function-scopes
895 (context-coloring-test-assert-coloring "
896 000 0 0 11111111 11 110
898 111 1 1 22222222 22 221
902 (context-coloring-test-deftest-js-js2 global
904 (context-coloring-test-assert-coloring "
906 111 1 1 00000001xxx11
909 (context-coloring-test-deftest-js2 block-scopes
911 (context-coloring-test-assert-coloring "
919 (setq context-coloring-js-block-scopes t))
921 (setq context-coloring-js-block-scopes nil)))
923 (context-coloring-test-deftest-js-js2 catch
925 (context-coloring-test-assert-coloring "
935 (context-coloring-test-deftest-js-js2 key-names
937 (context-coloring-test-assert-coloring "
945 (context-coloring-test-deftest-js-js2 property-lookup
947 (context-coloring-test-assert-coloring "
954 (context-coloring-test-deftest-js-js2 key-values
956 (context-coloring-test-assert-coloring "
966 (context-coloring-test-deftest-js-js2 syntactic-comments-and-strings
968 (context-coloring-test-assert-coloring "
973 :fixture "comments-and-strings.js"
975 (setq context-coloring-syntactic-comments t)
976 (setq context-coloring-syntactic-strings t)))
978 (context-coloring-test-deftest-js-js2 syntactic-comments
980 (context-coloring-test-assert-coloring "
985 :fixture "comments-and-strings.js"
987 (setq context-coloring-syntactic-comments t)))
989 (context-coloring-test-deftest-js-js2 syntactic-strings
991 (context-coloring-test-assert-coloring "
996 :fixture "comments-and-strings.js"
998 (setq context-coloring-syntactic-strings t)))
1000 (context-coloring-test-deftest-js2 unterminated-comment
1001 ;; As long as `add-text-properties' doesn't signal an error, this test passes.
1004 (context-coloring-test-deftest-emacs-lisp defun
1006 (context-coloring-test-assert-coloring "
1007 111111 000 1111 111 111111111 1111
1008 11 111 111 111 000011
1015 (context-coloring-test-deftest-emacs-lisp lambda
1017 (context-coloring-test-assert-coloring "
1018 00000000 1111111 1111
1019 11111111 11 2222222 2222
1020 222 22 12 2221 111 0 00")))
1022 (context-coloring-test-deftest-emacs-lisp quote
1024 (context-coloring-test-assert-coloring "
1028 111111 1 1111111111 11 111 1 111 1 00001 10000 11 00001 1 100001111")))
1030 (context-coloring-test-deftest-emacs-lisp comment
1032 ;; Just check that the comment isn't parsed syntactically.
1033 (context-coloring-test-assert-coloring "
1035 (xx (x xxxxx-xxxx xx) cccccccccc
1036 11 00000-0000 11))) cccccccccc"))
1038 (setq context-coloring-syntactic-comments t)))
1040 (context-coloring-test-deftest-emacs-lisp string
1042 (context-coloring-test-assert-coloring "
1044 (xxxxxx x x sss 1 0 sssss 0 1 sssssss11"))
1046 (setq context-coloring-syntactic-strings t)))
1048 (context-coloring-test-deftest-emacs-lisp ignored
1050 (context-coloring-test-assert-coloring "
1052 (x x 1 11 11 111 11 1 111 (1 1 1)))")))
1054 (context-coloring-test-deftest-emacs-lisp let
1056 (context-coloring-test-assert-coloring "
1064 1111 1 1 1 000011")))
1066 (context-coloring-test-deftest-emacs-lisp let*
1068 (context-coloring-test-assert-coloring "
1072 1111 1 1 1 0 0 00001
1078 2222 1 1 2 2 2 000022
1079 1111 1 1 1 0 0 000011")))
1081 (defun context-coloring-test-insert-unread-space ()
1082 (setq unread-command-events (cons '(t . 32)
1083 unread-command-events)))
1085 (defun context-coloring-test-remove-faces ()
1086 (remove-text-properties (point-min) (point-max) '(face nil)))
1088 (context-coloring-test-deftest-emacs-lisp iteration
1090 (let ((context-coloring-emacs-lisp-iterations-per-pause 1))
1091 (context-coloring-colorize)
1092 (context-coloring-test-assert-coloring "
1095 (context-coloring-test-remove-faces)
1096 (context-coloring-test-insert-unread-space)
1097 (context-coloring-colorize)
1098 ;; The first iteration will color the first part of the comment, but
1099 ;; that's it. Then it will be interrupted.
1100 (context-coloring-test-assert-coloring "
1104 (setq context-coloring-syntactic-comments t)
1105 (setq context-coloring-syntactic-strings t)))
1107 (provide 'context-coloring-test)
1109 ;;; context-coloring-test.el ends here