]> code.delx.au - gnu-emacs-elpa/blob - test/context-coloring-test.el
Fix original-maximum-face restoration bug.
[gnu-emacs-elpa] / test / context-coloring-test.el
1 ;;; test/context-coloring-test.el --- Tests for context coloring. -*- lexical-binding: t; -*-
2
3 ;; Copyright (C) 2014-2015 Free Software Foundation, Inc.
4
5 ;; This file is part of GNU Emacs.
6
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.
11
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.
16
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/>.
19
20 ;;; Commentary:
21
22 ;; Tests for context-coloring.
23
24 ;; Tests for both synchronous (elisp) and asynchronous (shell command) coloring
25 ;; are available. Basic plugin functionality is also tested.
26
27 ;; To run, execute `make test' from the project root.
28
29 ;;; Code:
30
31 (require 'context-coloring)
32 (require 'ert-async)
33 (require 'js2-mode)
34
35
36 ;;; Test running utilities
37
38 (defconst context-coloring-test-path
39 (file-name-directory (or load-file-name buffer-file-name))
40 "This file's directory.")
41
42 (defun context-coloring-test-read-file (path)
43 "Read a file's contents from PATH into a string."
44 (with-temp-buffer
45 (insert-file-contents (expand-file-name path context-coloring-test-path))
46 (buffer-string)))
47
48 (defun context-coloring-test-setup ()
49 "Prepare before all tests."
50 (setq context-coloring-comments-and-strings nil))
51
52 (defun context-coloring-test-cleanup ()
53 "Cleanup after all tests."
54 (setq context-coloring-comments-and-strings t)
55 (setq context-coloring-syntactic-comments nil)
56 (setq context-coloring-syntactic-strings nil)
57 (setq context-coloring-js-block-scopes nil)
58 (setq context-coloring-colorize-hook nil)
59 (setq context-coloring-check-scopifier-version-hook nil)
60 (setq context-coloring-maximum-face 7)
61 (setq context-coloring-original-maximum-face
62 context-coloring-maximum-face))
63
64 (defmacro context-coloring-test-with-fixture (fixture &rest body)
65 "With the relative FIXTURE, evaluate BODY in a temporary
66 buffer."
67 `(with-temp-buffer
68 (unwind-protect
69 (progn
70 (context-coloring-test-setup)
71 (insert (context-coloring-test-read-file ,fixture))
72 ,@body)
73 (context-coloring-test-cleanup))))
74
75 (defun context-coloring-test-with-temp-buffer-async (callback)
76 "Create a temporary buffer, and evaluate CALLBACK there. A
77 teardown callback is passed to CALLBACK for it to invoke when it
78 is done."
79 (let ((previous-buffer (current-buffer))
80 (temp-buffer (generate-new-buffer " *temp*")))
81 (set-buffer temp-buffer)
82 (funcall
83 callback
84 (lambda ()
85 (and (buffer-name temp-buffer)
86 (kill-buffer temp-buffer))
87 (set-buffer previous-buffer)))))
88
89 (defun context-coloring-test-with-fixture-async
90 (fixture callback &optional setup)
91 "With the relative FIXTURE, evaluate CALLBACK in a temporary
92 buffer. A teardown callback is passed to CALLBACK for it to
93 invoke when it is done. An optional SETUP callback can run
94 arbitrary code before the mode is invoked."
95 (context-coloring-test-with-temp-buffer-async
96 (lambda (done-with-temp-buffer)
97 (context-coloring-test-setup)
98 (when setup (funcall setup))
99 (insert (context-coloring-test-read-file fixture))
100 (funcall
101 callback
102 (lambda ()
103 (context-coloring-test-cleanup)
104 (funcall done-with-temp-buffer))))))
105
106
107 ;;; Test defining utilities
108
109 (defun context-coloring-test-js-mode (fixture callback &optional setup)
110 "Use FIXTURE as the subject matter for test logic in CALLBACK.
111 Optionally, provide setup code to run before the mode is
112 instantiated in SETUP."
113 (context-coloring-test-with-fixture-async
114 fixture
115 (lambda (done-with-test)
116 (js-mode)
117 (context-coloring-mode)
118 (context-coloring-colorize
119 (lambda ()
120 (funcall callback done-with-test))))
121 setup))
122
123 (defmacro context-coloring-test-js2-mode (fixture setup &rest body)
124 "Use FIXTURE as the subject matter for test logic in BODY."
125 `(context-coloring-test-with-fixture
126 ,fixture
127 (require 'js2-mode)
128 (setq js2-mode-show-parse-errors nil)
129 (setq js2-mode-show-strict-warnings nil)
130 (js2-mode)
131 (when ,setup (funcall ,setup))
132 (context-coloring-mode)
133 ,@body))
134
135 (cl-defmacro context-coloring-test-deftest-js-mode (name &key fixture-name)
136 "Define an asynchronous test for `js-mode' with the name NAME
137 in the typical format."
138 (declare (indent defun))
139 (let ((test-name (intern (format "context-coloring-test-js-mode-%s" name)))
140 (fixture (format "./fixtures/%s.js" (or fixture-name name)))
141 (function-name (intern-soft
142 (format "context-coloring-test-js-%s" name)))
143 (setup-function-name (intern-soft
144 (format
145 "context-coloring-test-js-%s-setup" name))))
146 `(ert-deftest-async ,test-name (done)
147 (context-coloring-test-js-mode
148 ,fixture
149 (lambda (teardown)
150 (unwind-protect
151 (,function-name)
152 (funcall teardown))
153 (funcall done))
154 ',setup-function-name))))
155
156 (cl-defmacro context-coloring-test-deftest-js2-mode (name &key fixture-name)
157 "Define a test for `js2-mode' with the name NAME in the typical
158 format."
159 (declare (indent defun))
160 (let ((test-name (intern (format "context-coloring-test-js2-mode-%s" name)))
161 (fixture (format "./fixtures/%s.js" (or fixture-name name)))
162 (function-name (intern-soft
163 (format "context-coloring-test-js-%s" name)))
164 (setup-function-name (intern-soft
165 (format
166 "context-coloring-test-js-%s-setup" name))))
167 `(ert-deftest ,test-name ()
168 (context-coloring-test-js2-mode
169 ,fixture
170 ',setup-function-name
171 (,function-name)))))
172
173
174 ;;; Assertion functions
175
176 (defmacro context-coloring-test-assert-region (&rest body)
177 "Assert something about the face of points in a region.
178 Provides the free variables `i', `length', `point', `face' and
179 `actual-level' to the code in BODY."
180 `(let ((i 0)
181 (length (- end start)))
182 (while (< i length)
183 (let* ((point (+ i start))
184 (face (get-text-property point 'face)))
185 ,@body)
186 (setq i (+ i 1)))))
187
188 (defun context-coloring-test-assert-region-level (start end level)
189 "Assert that all points in the range [START, END) are of level
190 LEVEL."
191 (context-coloring-test-assert-region
192 (let (actual-level)
193 (when (not (when face
194 (let* ((face-string (symbol-name face))
195 (matches (string-match
196 context-coloring-level-face-regexp
197 face-string)))
198 (when matches
199 (setq actual-level (string-to-number
200 (substring face-string
201 (match-beginning 1)
202 (match-end 1))))
203 (= level actual-level)))))
204 (ert-fail (format (concat "Expected level in region [%s, %s), "
205 "which is \"%s\", to be %s; "
206 "but at point %s, it was %s")
207 start end
208 (buffer-substring-no-properties start end) level
209 point actual-level))))))
210
211 (defun context-coloring-test-assert-region-face (start end expected-face)
212 "Assert that all points in the range [START, END) have the face
213 EXPECTED-FACE."
214 (context-coloring-test-assert-region
215 (when (not (eq face expected-face))
216 (ert-fail (format (concat "Expected face in region [%s, %s), "
217 "which is \"%s\", to be %s; "
218 "but at point %s, it was %s")
219 start end
220 (buffer-substring-no-properties start end) expected-face
221 point face)))))
222
223 (defun context-coloring-test-assert-region-comment-delimiter (start end)
224 "Assert that all points in the range [START, END) have
225 `font-lock-comment-delimiter-face'."
226 (context-coloring-test-assert-region-face
227 start end 'font-lock-comment-delimiter-face))
228
229 (defun context-coloring-test-assert-region-comment (start end)
230 "Assert that all points in the range [START, END) have
231 `font-lock-comment-face'."
232 (context-coloring-test-assert-region-face
233 start end 'font-lock-comment-face))
234
235 (defun context-coloring-test-assert-region-string (start end)
236 "Assert that all points in the range [START, END) have
237 `font-lock-string-face'."
238 (context-coloring-test-assert-region-face
239 start end 'font-lock-string-face))
240
241 (defun context-coloring-test-assert-message (expected buffer)
242 "Assert that message EXPECTED exists in BUFFER."
243 (when (null (get-buffer buffer))
244 (ert-fail
245 (format
246 (concat
247 "Expected buffer `%s' to have message \"%s\", "
248 "but the buffer did not have any messages.")
249 buffer expected)))
250 (with-current-buffer buffer
251 (let ((messages (split-string
252 (buffer-substring-no-properties
253 (point-min)
254 (point-max))
255 "\n")))
256 (let ((message (car (nthcdr (- (length messages) 2) messages))))
257 (when (not (equal message expected))
258 (ert-fail
259 (format
260 (concat
261 "Expected buffer `%s' to have message \"%s\", "
262 "but instead it was \"%s\"")
263 buffer expected
264 message)))))))
265
266 (defun context-coloring-test-assert-no-message (buffer)
267 "Assert that BUFFER has no message."
268 (when (get-buffer buffer)
269 (ert-fail (format (concat "Expected buffer `%s' to have no messages, "
270 "but it did: `%s'")
271 buffer
272 (with-current-buffer buffer
273 (buffer-string))))))
274
275 (defun context-coloring-test-kill-buffer (buffer)
276 "Kill BUFFER if it exists."
277 (when (get-buffer buffer) (kill-buffer buffer)))
278
279 (defun context-coloring-test-assert-face (level foreground &optional negate)
280 "Assert that a face for LEVEL exists and that its `:foreground'
281 is FOREGROUND, or the inverse if NEGATE is non-nil."
282 (let* ((face (context-coloring-level-face level))
283 actual-foreground)
284 (when (not (or negate
285 face))
286 (ert-fail (format (concat "Expected face for level `%s' to exist; "
287 "but it didn't")
288 level)))
289 (setq actual-foreground (face-attribute face :foreground))
290 (when (funcall (if negate 'identity 'not)
291 (string-equal foreground actual-foreground))
292 (ert-fail (format (concat "Expected face for level `%s' "
293 "%sto have foreground `%s'; "
294 "but it %s.")
295 level
296 (if negate "not " "") foreground
297 (if negate
298 "did" (format "was `%s'" actual-foreground)))))))
299
300 (defun context-coloring-test-assert-not-face (&rest arguments)
301 "Assert that LEVEL does not have a face with `:foreground'
302 FOREGROUND. Apply ARGUMENTS to
303 `context-coloring-test-assert-face', see that function."
304 (apply 'context-coloring-test-assert-face
305 (append arguments '(t))))
306
307
308 ;;; The tests
309
310 (ert-deftest-async context-coloring-test-async-mode-startup (done)
311 (context-coloring-test-with-fixture-async
312 "./fixtures/function-scopes.js"
313 (lambda (teardown)
314 (js-mode)
315 (add-hook
316 'context-coloring-colorize-hook
317 (lambda ()
318 ;; If this runs we are implicitly successful; this test only confirms
319 ;; that colorization occurs on mode startup.
320 (funcall teardown)
321 (funcall done)))
322 (context-coloring-mode))))
323
324 (ert-deftest context-coloring-test-check-version ()
325 (when (not (context-coloring-check-version "2.1.3" "3.0.1"))
326 (ert-fail "Expected version 3.0.1 to satisfy 2.1.3, but it didn't."))
327 (when (context-coloring-check-version "3.0.1" "2.1.3")
328 (ert-fail "Expected version 2.1.3 not to satisfy 3.0.1, but it did.")))
329
330 (ert-deftest context-coloring-test-unsupported-mode ()
331 (context-coloring-test-with-fixture
332 "./fixtures/function-scopes.js"
333 (context-coloring-mode)
334 (context-coloring-test-assert-message
335 "Context coloring is not available for this major mode"
336 "*Messages*")))
337
338 (define-derived-mode
339 context-coloring-test-unsupported-version-mode
340 fundamental-mode
341 "Testing"
342 "Prevent `context-coloring-test-unsupported-version' from
343 having any unintentional side-effects on mode support.")
344
345 (ert-deftest-async context-coloring-test-unsupported-version (done)
346 (context-coloring-define-dispatch
347 'outta-date
348 :modes '(context-coloring-test-unsupported-version-mode)
349 :executable "node"
350 :command "node test/binaries/outta-date"
351 :version "v2.1.3")
352 (context-coloring-test-with-fixture-async
353 "./fixtures/function-scopes.js"
354 (lambda (teardown)
355 (context-coloring-test-unsupported-version-mode)
356 (add-hook
357 'context-coloring-check-scopifier-version-hook
358 (lambda ()
359 (unwind-protect
360 (progn
361 ;; Normally the executable would be something like "outta-date"
362 ;; rather than "node".
363 (context-coloring-test-assert-message
364 "Update to the minimum version of \"node\" (v2.1.3)"
365 "*Messages*"))
366 (funcall teardown))
367 (funcall done)))
368 (context-coloring-mode))))
369
370 (define-derived-mode
371 context-coloring-test-disable-mode-mode
372 fundamental-mode
373 "Testing"
374 "Prevent `context-coloring-test-disable-mode' from having any
375 unintentional side-effects on mode support.")
376
377 (ert-deftest-async context-coloring-test-disable-mode (done)
378 (let (torn-down)
379 (context-coloring-define-dispatch
380 'disable-mode
381 :modes '(context-coloring-test-disable-mode-mode)
382 :executable "node"
383 :command "node test/binaries/noop"
384 :teardown (lambda ()
385 (setq torn-down t)))
386 (context-coloring-test-with-fixture-async
387 "./fixtures/function-scopes.js"
388 (lambda (teardown)
389 (unwind-protect
390 (progn
391 (context-coloring-test-disable-mode-mode)
392 (context-coloring-mode)
393 (context-coloring-mode -1)
394 (when (not torn-down)
395 (ert-fail "Expected teardown function to have been called, but it wasn't.")))
396 (funcall teardown))
397 (funcall done)))))
398
399 (defvar context-coloring-test-theme-index 0
400 "Unique index for unique theme names.")
401
402 (defun context-coloring-test-get-next-theme ()
403 "Return a unique symbol for a throwaway theme."
404 (prog1
405 (intern (format "context-coloring-test-theme-%s"
406 context-coloring-test-theme-index))
407 (setq context-coloring-test-theme-index
408 (+ context-coloring-test-theme-index 1))))
409
410 (defun context-coloring-test-assert-theme-originally-set-p
411 (settings &optional negate)
412 "Assert that `context-coloring-theme-originally-set-p' returns
413 t for a theme with SETTINGS, or the inverse if NEGATE is
414 non-nil."
415 (let ((theme (context-coloring-test-get-next-theme)))
416 (put theme 'theme-settings settings)
417 (when (funcall (if negate 'identity 'not)
418 (context-coloring-theme-originally-set-p theme))
419 (ert-fail (format (concat "Expected theme `%s' with settings `%s' "
420 "%sto be considered to have defined a level, "
421 "but it %s.")
422 theme settings
423 (if negate "not " "")
424 (if negate "was" "wasn't"))))))
425
426 (defun context-coloring-test-assert-not-theme-originally-set-p (&rest arguments)
427 "Assert that `context-coloring-theme-originally-set-p' does not
428 return t for a theme with SETTINGS. Apply ARGUMENTS to
429 `context-coloring-test-assert-theme-originally-set-p', see that
430 function."
431 (apply 'context-coloring-test-assert-theme-originally-set-p
432 (append arguments '(t))))
433
434 (ert-deftest context-coloring-test-theme-originally-set-p ()
435 (context-coloring-test-assert-theme-originally-set-p
436 '((theme-face context-coloring-level-0-face)))
437 (context-coloring-test-assert-theme-originally-set-p
438 '((theme-face face)
439 (theme-face context-coloring-level-0-face)))
440 (context-coloring-test-assert-theme-originally-set-p
441 '((theme-face context-coloring-level-0-face)
442 (theme-face face)))
443 (context-coloring-test-assert-not-theme-originally-set-p
444 '((theme-face face)))
445 )
446
447 (defun context-coloring-test-assert-theme-settings-highest-level
448 (settings expected-level)
449 "Assert that a theme with SETTINGS has the highest level
450 EXPECTED-LEVEL."
451 (let ((theme (context-coloring-test-get-next-theme)))
452 (put theme 'theme-settings settings)
453 (context-coloring-test-assert-theme-highest-level theme expected-level)))
454
455 (defun context-coloring-test-assert-theme-highest-level
456 (theme expected-level &optional negate)
457 "Assert that THEME has the highest level EXPECTED-LEVEL, or the
458 inverse if NEGATE is non-nil."
459 (let ((highest-level (context-coloring-theme-highest-level theme)))
460 (when (funcall (if negate 'identity 'not) (eq highest-level expected-level))
461 (ert-fail (format (concat "Expected theme with settings `%s' "
462 "%sto have a highest level of `%s', "
463 "but it %s.")
464 (get theme 'theme-settings)
465 (if negate "not " "") expected-level
466 (if negate "did" (format "was %s" highest-level)))))))
467
468 (defun context-coloring-test-assert-theme-not-highest-level (&rest arguments)
469 "Assert that THEME's highest level is not EXPECTED-LEVEL.
470 Apply ARGUMENTS to
471 `context-coloring-test-assert-theme-highest-level', see that
472 function."
473 (apply 'context-coloring-test-assert-theme-highest-level
474 (append arguments '(t))))
475
476 (ert-deftest context-coloring-test-theme-highest-level ()
477 (context-coloring-test-assert-theme-settings-highest-level
478 '((theme-face foo))
479 -1)
480 (context-coloring-test-assert-theme-settings-highest-level
481 '((theme-face context-coloring-level-0-face))
482 0)
483 (context-coloring-test-assert-theme-settings-highest-level
484 '((theme-face context-coloring-level-1-face))
485 1)
486 (context-coloring-test-assert-theme-settings-highest-level
487 '((theme-face context-coloring-level-1-face)
488 (theme-face context-coloring-level-0-face))
489 1)
490 (context-coloring-test-assert-theme-settings-highest-level
491 '((theme-face context-coloring-level-0-face)
492 (theme-face context-coloring-level-1-face))
493 1)
494 )
495
496 (defmacro context-coloring-test-deftest-define-theme (name &rest body)
497 "Define a test with name NAME and an automatically-generated
498 theme symbol available as a free variable `theme'. Side-effects
499 from enabling themes are reversed after BODY is executed and the
500 test completes."
501 (declare (indent defun))
502 (let ((deftest-name (intern
503 (format "context-coloring-test-define-theme-%s" name))))
504 `(ert-deftest ,deftest-name ()
505 (context-coloring-test-kill-buffer "*Warnings*")
506 (context-coloring-test-setup)
507 (let ((theme (context-coloring-test-get-next-theme)))
508 (unwind-protect
509 (progn
510 ,@body)
511 ;; Always cleanup.
512 (disable-theme theme)
513 (context-coloring-test-cleanup))))))
514
515 (defun context-coloring-test-deftheme (theme)
516 "Dynamically define theme THEME."
517 (eval (macroexpand `(deftheme ,theme))))
518
519 (context-coloring-test-deftest-define-theme additive
520 (context-coloring-test-deftheme theme)
521 (context-coloring-define-theme
522 theme
523 :colors '("#aaaaaa"
524 "#bbbbbb"))
525 (context-coloring-test-assert-no-message "*Warnings*")
526 (enable-theme theme)
527 (context-coloring-test-assert-no-message "*Warnings*")
528 (context-coloring-test-assert-face 0 "#aaaaaa")
529 (context-coloring-test-assert-face 1 "#bbbbbb"))
530
531 (defun context-coloring-test-assert-defined-warning (theme)
532 "Assert that a warning about colors already being defined for
533 theme THEME is signaled."
534 (context-coloring-test-assert-message
535 (format (concat "Warning (emacs): Context coloring colors for theme "
536 "`%s' are already defined")
537 theme)
538 "*Warnings*"))
539
540 (context-coloring-test-deftest-define-theme unintentional-override
541 (context-coloring-test-deftheme theme)
542 (custom-theme-set-faces
543 theme
544 '(context-coloring-level-0-face ((t (:foreground "#aaaaaa"))))
545 '(context-coloring-level-1-face ((t (:foreground "#bbbbbb")))))
546 (context-coloring-define-theme
547 theme
548 :colors '("#cccccc"
549 "#dddddd"))
550 (context-coloring-test-assert-defined-warning theme)
551 (context-coloring-test-kill-buffer "*Warnings*")
552 (enable-theme theme)
553 (context-coloring-test-assert-defined-warning theme)
554 (context-coloring-test-assert-face 0 "#cccccc")
555 (context-coloring-test-assert-face 1 "#dddddd"))
556
557 (context-coloring-test-deftest-define-theme intentional-override
558 (context-coloring-test-deftheme theme)
559 (custom-theme-set-faces
560 theme
561 '(context-coloring-level-0-face ((t (:foreground "#aaaaaa"))))
562 '(context-coloring-level-1-face ((t (:foreground "#bbbbbb")))))
563 (context-coloring-define-theme
564 theme
565 :override t
566 :colors '("#cccccc"
567 "#dddddd"))
568 (context-coloring-test-assert-no-message "*Warnings*")
569 (enable-theme theme)
570 (context-coloring-test-assert-no-message "*Warnings*")
571 (context-coloring-test-assert-face 0 "#cccccc")
572 (context-coloring-test-assert-face 1 "#dddddd"))
573
574 (context-coloring-test-deftest-define-theme pre-recede
575 (context-coloring-define-theme
576 theme
577 :recede t
578 :colors '("#aaaaaa"
579 "#bbbbbb"))
580 (context-coloring-test-deftheme theme)
581 (custom-theme-set-faces
582 theme
583 '(context-coloring-level-0-face ((t (:foreground "#cccccc"))))
584 '(context-coloring-level-1-face ((t (:foreground "#dddddd")))))
585 (enable-theme theme)
586 (context-coloring-test-assert-no-message "*Warnings*")
587 (context-coloring-test-assert-face 0 "#cccccc")
588 (context-coloring-test-assert-face 1 "#dddddd"))
589
590 (context-coloring-test-deftest-define-theme post-recede
591 (context-coloring-test-deftheme theme)
592 (custom-theme-set-faces
593 theme
594 '(context-coloring-level-0-face ((t (:foreground "#aaaaaa"))))
595 '(context-coloring-level-1-face ((t (:foreground "#bbbbbb")))))
596 (context-coloring-define-theme
597 theme
598 :recede t
599 :colors '("#cccccc"
600 "#dddddd"))
601 (context-coloring-test-assert-no-message "*Warnings*")
602 (context-coloring-test-assert-face 0 "#aaaaaa")
603 (context-coloring-test-assert-face 1 "#bbbbbb")
604 (enable-theme theme)
605 (context-coloring-test-assert-no-message "*Warnings*")
606 (context-coloring-test-assert-face 0 "#aaaaaa")
607 (context-coloring-test-assert-face 1 "#bbbbbb"))
608
609 (context-coloring-test-deftest-define-theme recede-not-defined
610 (context-coloring-test-deftheme theme)
611 (custom-theme-set-faces
612 theme
613 '(foo-face ((t (:foreground "#ffffff")))))
614 (context-coloring-define-theme
615 theme
616 :recede t
617 :colors '("#aaaaaa"
618 "#bbbbbb"))
619 (context-coloring-test-assert-no-message "*Warnings*")
620 (context-coloring-test-assert-face 0 "#aaaaaa")
621 (context-coloring-test-assert-face 1 "#bbbbbb")
622 (enable-theme theme)
623 (context-coloring-test-assert-no-message "*Warnings*")
624 (context-coloring-test-assert-face 0 "#aaaaaa")
625 (context-coloring-test-assert-face 1 "#bbbbbb"))
626
627 (context-coloring-test-deftest-define-theme unintentional-obstinance
628 (context-coloring-define-theme
629 theme
630 :colors '("#aaaaaa"
631 "#bbbbbb"))
632 (context-coloring-test-deftheme theme)
633 (custom-theme-set-faces
634 theme
635 '(context-coloring-level-0-face ((t (:foreground "#cccccc"))))
636 '(context-coloring-level-1-face ((t (:foreground "#dddddd")))))
637 (enable-theme theme)
638 (context-coloring-test-assert-defined-warning theme)
639 (context-coloring-test-assert-face 0 "#aaaaaa")
640 (context-coloring-test-assert-face 1 "#bbbbbb"))
641
642 (context-coloring-test-deftest-define-theme intentional-obstinance
643 (context-coloring-define-theme
644 theme
645 :override t
646 :colors '("#aaaaaa"
647 "#bbbbbb"))
648 (context-coloring-test-deftheme theme)
649 (custom-theme-set-faces
650 theme
651 '(context-coloring-level-0-face ((t (:foreground "#cccccc"))))
652 '(context-coloring-level-1-face ((t (:foreground "#dddddd")))))
653 (enable-theme theme)
654 (context-coloring-test-assert-no-message "*Warnings*")
655 (context-coloring-test-assert-face 0 "#aaaaaa")
656 (context-coloring-test-assert-face 1 "#bbbbbb"))
657
658 (defun context-coloring-test-assert-maximum-face (maximum &optional negate)
659 "Assert that `context-coloring-maximum-face' is MAXIMUM, or the
660 inverse if NEGATE is non-nil."
661 (when (funcall (if negate 'identity 'not)
662 (eq context-coloring-maximum-face maximum))
663 (ert-fail (format (concat "Expected `context-coloring-maximum-face' "
664 "%sto be `%s', "
665 "but it %s.")
666 (if negate "not " "") maximum
667 (if negate
668 "was"
669 (format "was `%s'" context-coloring-maximum-face))))))
670
671 (defun context-coloring-test-assert-not-maximum-face (&rest arguments)
672 "Assert that `context-coloring-maximum-face' is not MAXIMUM.
673 Apply ARGUMENTS to `context-coloring-test-assert-maximum-face',
674 see that function."
675 (apply 'context-coloring-test-assert-maximum-face
676 (append arguments '(t))))
677
678 (context-coloring-test-deftest-define-theme disable-cascade
679 (let ((maximum-face-value 9999))
680 (setq context-coloring-maximum-face maximum-face-value)
681 (context-coloring-test-deftheme theme)
682 (context-coloring-define-theme
683 theme
684 :colors '("#aaaaaa"
685 "#bbbbbb"))
686 (let ((second-theme (context-coloring-test-get-next-theme)))
687 (context-coloring-test-deftheme second-theme)
688 (context-coloring-define-theme
689 second-theme
690 :colors '("#cccccc"
691 "#dddddd"
692 "#eeeeee"))
693 (let ((third-theme (context-coloring-test-get-next-theme)))
694 (context-coloring-test-deftheme third-theme)
695 (context-coloring-define-theme
696 third-theme
697 :colors '("#111111"
698 "#222222"
699 "#333333"
700 "#444444"))
701 (enable-theme theme)
702 (enable-theme second-theme)
703 (enable-theme third-theme)
704 (disable-theme third-theme)
705 (context-coloring-test-assert-face 0 "#cccccc")
706 (context-coloring-test-assert-face 1 "#dddddd")
707 (context-coloring-test-assert-face 2 "#eeeeee")
708 (context-coloring-test-assert-maximum-face 2))
709 (disable-theme second-theme)
710 (context-coloring-test-assert-face 0 "#aaaaaa")
711 (context-coloring-test-assert-face 1 "#bbbbbb")
712 (context-coloring-test-assert-maximum-face 1))
713 (disable-theme theme)
714 (context-coloring-test-assert-not-face 0 "#aaaaaa")
715 (context-coloring-test-assert-not-face 1 "#bbbbbb")
716 (context-coloring-test-assert-maximum-face
717 maximum-face-value)))
718
719 (defun context-coloring-test-js-function-scopes ()
720 "Test fixtures/functions-scopes.js."
721 (context-coloring-test-assert-region-level 1 9 0)
722 (context-coloring-test-assert-region-level 9 23 1)
723 (context-coloring-test-assert-region-level 23 25 0)
724 (context-coloring-test-assert-region-level 25 34 1)
725 (context-coloring-test-assert-region-level 34 35 0)
726 (context-coloring-test-assert-region-level 35 52 1)
727 (context-coloring-test-assert-region-level 52 66 2)
728 (context-coloring-test-assert-region-level 66 72 1)
729 (context-coloring-test-assert-region-level 72 81 2)
730 (context-coloring-test-assert-region-level 81 82 1)
731 (context-coloring-test-assert-region-level 82 87 2)
732 (context-coloring-test-assert-region-level 87 89 1))
733
734 (context-coloring-test-deftest-js-mode function-scopes)
735 (context-coloring-test-deftest-js2-mode function-scopes)
736
737 (defun context-coloring-test-js-global ()
738 "Test fixtures/global.js."
739 (context-coloring-test-assert-region-level 20 28 1)
740 (context-coloring-test-assert-region-level 28 35 0)
741 (context-coloring-test-assert-region-level 35 41 1))
742
743 (context-coloring-test-deftest-js-mode global)
744 (context-coloring-test-deftest-js2-mode global)
745
746 (defun context-coloring-test-js-block-scopes ()
747 "Test fixtures/block-scopes.js."
748 (context-coloring-test-assert-region-level 20 64 1)
749 (setq context-coloring-js-block-scopes t)
750 (context-coloring-colorize)
751 (context-coloring-test-assert-region-level 20 27 1)
752 (context-coloring-test-assert-region-level 27 41 2)
753 (context-coloring-test-assert-region-level 41 42 1)
754 (context-coloring-test-assert-region-level 42 64 2))
755
756 (context-coloring-test-deftest-js2-mode block-scopes)
757
758 (defun context-coloring-test-js-catch ()
759 "Test fixtures/js-catch.js."
760 (context-coloring-test-assert-region-level 20 27 1)
761 (context-coloring-test-assert-region-level 27 51 2)
762 (context-coloring-test-assert-region-level 51 52 1)
763 (context-coloring-test-assert-region-level 52 73 2)
764 (context-coloring-test-assert-region-level 73 101 3)
765 (context-coloring-test-assert-region-level 101 102 1)
766 (context-coloring-test-assert-region-level 102 117 3)
767 (context-coloring-test-assert-region-level 117 123 2))
768
769 (context-coloring-test-deftest-js-mode catch)
770 (context-coloring-test-deftest-js2-mode catch)
771
772 (defun context-coloring-test-js-key-names ()
773 "Test fixtures/key-names.js."
774 (context-coloring-test-assert-region-level 20 63 1))
775
776 (context-coloring-test-deftest-js-mode key-names)
777 (context-coloring-test-deftest-js2-mode key-names)
778
779 (defun context-coloring-test-js-property-lookup ()
780 "Test fixtures/property-lookup.js."
781 (context-coloring-test-assert-region-level 20 26 0)
782 (context-coloring-test-assert-region-level 26 38 1)
783 (context-coloring-test-assert-region-level 38 44 0)
784 (context-coloring-test-assert-region-level 44 52 1)
785 (context-coloring-test-assert-region-level 57 63 0)
786 (context-coloring-test-assert-region-level 63 74 1))
787
788 (context-coloring-test-deftest-js-mode property-lookup)
789 (context-coloring-test-deftest-js2-mode property-lookup)
790
791 (defun context-coloring-test-js-key-values ()
792 "Test fixtures/key-values.js."
793 (context-coloring-test-assert-region-level 78 79 1))
794
795 (context-coloring-test-deftest-js-mode key-values)
796 (context-coloring-test-deftest-js2-mode key-values)
797
798 (defun context-coloring-test-js-syntactic-comments-and-strings ()
799 "Test comments and strings."
800 (context-coloring-test-assert-region-level 1 8 0)
801 (context-coloring-test-assert-region-comment-delimiter 9 12)
802 (context-coloring-test-assert-region-comment 12 16)
803 (context-coloring-test-assert-region-comment-delimiter 17 20)
804 (context-coloring-test-assert-region-comment 20 27)
805 (context-coloring-test-assert-region-string 28 40)
806 (context-coloring-test-assert-region-level 40 41 0))
807
808 (defun context-coloring-test-js-syntactic-comments-and-strings-setup ()
809 (setq context-coloring-syntactic-comments t)
810 (setq context-coloring-syntactic-strings t))
811
812 (context-coloring-test-deftest-js-mode syntactic-comments-and-strings
813 :fixture-name comments-and-strings)
814 (context-coloring-test-deftest-js2-mode syntactic-comments-and-strings
815 :fixture-name comments-and-strings)
816
817 (defalias 'context-coloring-test-js-comments-and-strings
818 'context-coloring-test-js-syntactic-comments-and-strings
819 "Test comments and strings. Deprecated.")
820
821 (defun context-coloring-test-js-comments-and-strings-setup ()
822 "Setup comments and strings. Deprecated."
823 (setq context-coloring-comments-and-strings t))
824
825 (context-coloring-test-deftest-js-mode comments-and-strings)
826 (context-coloring-test-deftest-js2-mode comments-and-strings)
827
828 (defun context-coloring-test-js-syntactic-comments ()
829 "Test syntactic comments."
830 (context-coloring-test-assert-region-level 1 8 0)
831 (context-coloring-test-assert-region-comment-delimiter 9 12)
832 (context-coloring-test-assert-region-comment 12 16)
833 (context-coloring-test-assert-region-comment-delimiter 17 20)
834 (context-coloring-test-assert-region-comment 20 27)
835 (context-coloring-test-assert-region-level 28 41 0))
836
837 (defun context-coloring-test-js-syntactic-comments-setup ()
838 "Setup syntactic comments."
839 (setq context-coloring-syntactic-comments t))
840
841 (context-coloring-test-deftest-js-mode syntactic-comments
842 :fixture-name comments-and-strings)
843 (context-coloring-test-deftest-js2-mode syntactic-comments
844 :fixture-name comments-and-strings)
845
846 (defun context-coloring-test-js-syntactic-strings ()
847 "Test syntactic strings."
848 (context-coloring-test-assert-region-level 1 28 0)
849 (context-coloring-test-assert-region-string 28 40)
850 (context-coloring-test-assert-region-level 40 41 0))
851
852 (defun context-coloring-test-js-syntactic-strings-setup ()
853 "Setup syntactic strings."
854 (setq context-coloring-syntactic-strings t))
855
856 (context-coloring-test-deftest-js-mode syntactic-strings
857 :fixture-name comments-and-strings)
858 (context-coloring-test-deftest-js2-mode syntactic-strings
859 :fixture-name comments-and-strings)
860
861 (provide 'context-coloring-test)
862
863 ;;; context-coloring-test.el ends here