]> code.delx.au - gnu-emacs-elpa/blob - test/context-coloring-test.el
Add constant comment test.
[gnu-emacs-elpa] / test / context-coloring-test.el
1 ;;; 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 ;; Use with `make test'.
25
26 ;;; Code:
27
28 (require 'context-coloring)
29 (require 'ert-async)
30 (require 'js2-mode)
31
32
33 ;;; Test running utilities
34
35 (defconst context-coloring-test-path
36 (file-name-directory (or load-file-name buffer-file-name))
37 "This file's directory.")
38
39 (defun context-coloring-test-read-file (path)
40 "Read a file's contents from PATH into a string."
41 (with-temp-buffer
42 (insert-file-contents (expand-file-name path context-coloring-test-path))
43 (buffer-string)))
44
45 (defun context-coloring-test-setup ()
46 "Prepare before all tests."
47 (setq context-coloring-syntactic-comments nil)
48 (setq context-coloring-syntactic-strings nil))
49
50 (defun context-coloring-test-cleanup ()
51 "Cleanup after all tests."
52 (setq context-coloring-comments-and-strings nil)
53 (setq context-coloring-js-block-scopes nil)
54 (setq context-coloring-colorize-hook nil)
55 (setq context-coloring-check-scopifier-version-hook nil)
56 (setq context-coloring-maximum-face 7)
57 (setq context-coloring-original-maximum-face
58 context-coloring-maximum-face))
59
60 (defmacro context-coloring-test-with-fixture (fixture &rest body)
61 "With the relative FIXTURE, evaluate BODY in a temporary
62 buffer."
63 `(with-temp-buffer
64 (unwind-protect
65 (progn
66 (context-coloring-test-setup)
67 (insert (context-coloring-test-read-file ,fixture))
68 ,@body)
69 (context-coloring-test-cleanup))))
70
71 (defun context-coloring-test-with-temp-buffer-async (callback)
72 "Create a temporary buffer, and evaluate CALLBACK there. A
73 teardown callback is passed to CALLBACK for it to invoke when it
74 is done."
75 (let ((previous-buffer (current-buffer))
76 (temp-buffer (generate-new-buffer " *temp*")))
77 (set-buffer temp-buffer)
78 (funcall
79 callback
80 (lambda ()
81 (and (buffer-name temp-buffer)
82 (kill-buffer temp-buffer))
83 (set-buffer previous-buffer)))))
84
85 (defun context-coloring-test-with-fixture-async
86 (fixture callback &optional setup)
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. An optional SETUP callback can run
90 arbitrary code before the mode is invoked."
91 (context-coloring-test-with-temp-buffer-async
92 (lambda (done-with-temp-buffer)
93 (context-coloring-test-setup)
94 (when setup (funcall setup))
95 (insert (context-coloring-test-read-file fixture))
96 (funcall
97 callback
98 (lambda ()
99 (context-coloring-test-cleanup)
100 (funcall done-with-temp-buffer))))))
101
102
103 ;;; Test defining utilities
104
105 (defun context-coloring-test-js-mode (fixture callback &optional setup)
106 "Use FIXTURE as the subject matter for test logic in CALLBACK.
107 Optionally, provide setup code to run before the mode is
108 instantiated in SETUP."
109 (context-coloring-test-with-fixture-async
110 fixture
111 (lambda (done-with-test)
112 (js-mode)
113 (context-coloring-mode)
114 (context-coloring-colorize
115 (lambda ()
116 (funcall callback done-with-test))))
117 setup))
118
119 (defmacro context-coloring-test-js2-mode (fixture setup &rest body)
120 "Use FIXTURE as the subject matter for test logic in BODY."
121 `(context-coloring-test-with-fixture
122 ,fixture
123 (require 'js2-mode)
124 (setq js2-mode-show-parse-errors nil)
125 (setq js2-mode-show-strict-warnings nil)
126 (js2-mode)
127 (when ,setup (funcall ,setup))
128 (context-coloring-mode)
129 ,@body))
130
131 (cl-defmacro context-coloring-test-deftest-js-mode (name &key fixture-name)
132 "Define an asynchronous test for `js-mode' with the name NAME
133 in the typical format."
134 (declare (indent defun))
135 (let ((test-name (intern (format "context-coloring-test-js-mode-%s" name)))
136 (fixture (format "./fixtures/%s.js" (or fixture-name name)))
137 (function-name (intern-soft
138 (format "context-coloring-test-js-%s" name)))
139 (setup-function-name (intern-soft
140 (format
141 "context-coloring-test-js-%s-setup" name))))
142 `(ert-deftest-async ,test-name (done)
143 (context-coloring-test-js-mode
144 ,fixture
145 (lambda (teardown)
146 (unwind-protect
147 (,function-name)
148 (funcall teardown))
149 (funcall done))
150 ',setup-function-name))))
151
152 (cl-defmacro context-coloring-test-deftest-js2-mode (name &key fixture-name)
153 "Define a test for `js2-mode' with the name NAME in the typical
154 format."
155 (declare (indent defun))
156 (let ((test-name (intern (format "context-coloring-test-js2-mode-%s" name)))
157 (fixture (format "./fixtures/%s.js" (or fixture-name name)))
158 (function-name (intern-soft
159 (format "context-coloring-test-js-%s" name)))
160 (setup-function-name (intern-soft
161 (format
162 "context-coloring-test-js-%s-setup" name))))
163 `(ert-deftest ,test-name ()
164 (context-coloring-test-js2-mode
165 ,fixture
166 ',setup-function-name
167 (,function-name)))))
168
169 (cl-defmacro context-coloring-test-deftest-emacs-lisp-mode (name
170 body
171 &key setup)
172 "Define a test for `emacs-lisp-mode' with name and fixture as
173 NAME, with BODY containing the assertions, and SETUP defining the
174 environment."
175 (declare (indent defun))
176 (let ((test-name (intern (format "context-coloring-emacs-lisp-mode-%s" name)))
177 (fixture (format "./fixtures/%s.el" name)))
178 `(ert-deftest ,test-name ()
179 (context-coloring-test-with-fixture
180 ,fixture
181 (emacs-lisp-mode)
182 (when ,setup (funcall ,setup))
183 (context-coloring-mode)
184 (funcall ,body)))))
185
186
187 ;;; Assertion functions
188
189 (defun context-coloring-test-assert-position-level (position level)
190 "Assert that POSITION has LEVEL."
191 (let ((face (get-text-property position 'face))
192 actual-level)
193 (when (not (and 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 at position %s, "
205 "which is \"%s\", to be %s; "
206 "but it was %s")
207 position
208 (buffer-substring-no-properties position (1+ position)) level
209 actual-level)))))
210
211 (defun context-coloring-test-assert-position-face (position face-regexp)
212 "Assert that the face at POSITION satisfies FACE-REGEXP."
213 (let ((face (get-text-property position 'face)))
214 (when (or
215 ;; Pass a non-string to do an `equal' check (against a symbol or nil).
216 (unless (stringp face-regexp)
217 (not (equal face-regexp face)))
218 ;; Otherwise do the matching.
219 (when (stringp face-regexp)
220 (not (string-match-p face-regexp (symbol-name face)))))
221 (ert-fail (format (concat "Expected face at position %s, "
222 "which is \"%s\", to be %s; "
223 "but it was %s")
224 position
225 (buffer-substring-no-properties position (1+ position)) face-regexp
226 face)))))
227
228 (defun context-coloring-test-assert-position-comment (position)
229 (context-coloring-test-assert-position-face
230 position "\\`font-lock-comment\\(-delimiter\\)?-face\\'"))
231
232 (defun context-coloring-test-assert-position-constant-comment (position)
233 (context-coloring-test-assert-position-face position '(font-lock-constant-face
234 font-lock-comment-face)))
235
236 (defun context-coloring-test-assert-position-string (position)
237 (context-coloring-test-assert-position-face position 'font-lock-string-face))
238
239 (defun context-coloring-test-assert-coloring (map)
240 "Assert that the current buffer's coloring matches MAP."
241 ;; Omit the superfluous, formatting-related leading newline. Can't use
242 ;; `save-excursion' here because if an assertion fails it will cause future
243 ;; tests to get messed up.
244 (goto-char (point-min))
245 (let* ((map (substring map 1))
246 (index 0)
247 char-string
248 char)
249 (while (< index (length map))
250 (setq char-string (substring map index (1+ index)))
251 (setq char (string-to-char char-string))
252 (cond
253 ;; Newline
254 ((= char 10)
255 (next-logical-line)
256 (beginning-of-line))
257 ;; Number
258 ((and (>= char 48)
259 (<= char 57))
260 (context-coloring-test-assert-position-level
261 (point) (string-to-number char-string))
262 (forward-char))
263 ;; ';' = Comment
264 ((= char 59)
265 (context-coloring-test-assert-position-comment (point))
266 (forward-char))
267 ;; 'c' = Constant comment
268 ((= char 99)
269 (context-coloring-test-assert-position-constant-comment (point))
270 (forward-char))
271 ;; 's' = String
272 ((= char 115)
273 (context-coloring-test-assert-position-string (point))
274 (forward-char))
275 (t
276 (forward-char)))
277 (setq index (1+ index)))))
278
279 (defmacro context-coloring-test-assert-region (&rest body)
280 "Assert something about the face of points in a region.
281 Provides the free variables `i', `length', `point', `face' and
282 `actual-level' to the code in BODY."
283 `(let ((i 0)
284 (length (- end start)))
285 (while (< i length)
286 (let* ((point (+ i start))
287 (face (get-text-property point 'face)))
288 ,@body)
289 (setq i (+ i 1)))))
290
291 (defun context-coloring-test-assert-region-level (start end level)
292 "Assert that all points in the range [START, END) are of level
293 LEVEL."
294 (context-coloring-test-assert-region
295 (let (actual-level)
296 (when (not (when face
297 (let* ((face-string (symbol-name face))
298 (matches (string-match
299 context-coloring-level-face-regexp
300 face-string)))
301 (when matches
302 (setq actual-level (string-to-number
303 (substring face-string
304 (match-beginning 1)
305 (match-end 1))))
306 (= level actual-level)))))
307 (ert-fail (format (concat "Expected level in region [%s, %s), "
308 "which is \"%s\", to be %s; "
309 "but at point %s, it was %s")
310 start end
311 (buffer-substring-no-properties start end) level
312 point actual-level))))))
313
314 (defun context-coloring-test-assert-region-face (start end expected-face)
315 "Assert that all points in the range [START, END) have the face
316 EXPECTED-FACE."
317 (context-coloring-test-assert-region
318 (when (not (eq face expected-face))
319 (ert-fail (format (concat "Expected face in region [%s, %s), "
320 "which is \"%s\", to be %s; "
321 "but at point %s, it was %s")
322 start end
323 (buffer-substring-no-properties start end) expected-face
324 point face)))))
325
326 (defun context-coloring-test-assert-region-comment-delimiter (start end)
327 "Assert that all points in the range [START, END) have
328 `font-lock-comment-delimiter-face'."
329 (context-coloring-test-assert-region-face
330 start end 'font-lock-comment-delimiter-face))
331
332 (defun context-coloring-test-assert-region-comment (start end)
333 "Assert that all points in the range [START, END) have
334 `font-lock-comment-face'."
335 (context-coloring-test-assert-region-face
336 start end 'font-lock-comment-face))
337
338 (defun context-coloring-test-assert-region-string (start end)
339 "Assert that all points in the range [START, END) have
340 `font-lock-string-face'."
341 (context-coloring-test-assert-region-face
342 start end 'font-lock-string-face))
343
344 (defun context-coloring-test-assert-message (expected buffer)
345 "Assert that message EXPECTED exists in BUFFER."
346 (when (null (get-buffer buffer))
347 (ert-fail
348 (format
349 (concat
350 "Expected buffer `%s' to have message \"%s\", "
351 "but the buffer did not have any messages.")
352 buffer expected)))
353 (with-current-buffer buffer
354 (let ((messages (split-string
355 (buffer-substring-no-properties
356 (point-min)
357 (point-max))
358 "\n")))
359 (let ((message (car (nthcdr (- (length messages) 2) messages))))
360 (when (not (equal message expected))
361 (ert-fail
362 (format
363 (concat
364 "Expected buffer `%s' to have message \"%s\", "
365 "but instead it was \"%s\"")
366 buffer expected
367 message)))))))
368
369 (defun context-coloring-test-assert-no-message (buffer)
370 "Assert that BUFFER has no message."
371 (when (get-buffer buffer)
372 (ert-fail (format (concat "Expected buffer `%s' to have no messages, "
373 "but it did: `%s'")
374 buffer
375 (with-current-buffer buffer
376 (buffer-string))))))
377
378 (defun context-coloring-test-kill-buffer (buffer)
379 "Kill BUFFER if it exists."
380 (when (get-buffer buffer) (kill-buffer buffer)))
381
382 (defun context-coloring-test-assert-face (level foreground &optional negate)
383 "Assert that a face for LEVEL exists and that its `:foreground'
384 is FOREGROUND, or the inverse if NEGATE is non-nil."
385 (let* ((face (context-coloring-level-face level))
386 actual-foreground)
387 (when (not (or negate
388 face))
389 (ert-fail (format (concat "Expected face for level `%s' to exist; "
390 "but it didn't")
391 level)))
392 (setq actual-foreground (face-attribute face :foreground))
393 (when (funcall (if negate 'identity 'not)
394 (string-equal foreground actual-foreground))
395 (ert-fail (format (concat "Expected face for level `%s' "
396 "%sto have foreground `%s'; "
397 "but it %s.")
398 level
399 (if negate "not " "") foreground
400 (if negate
401 "did" (format "was `%s'" actual-foreground)))))))
402
403 (defun context-coloring-test-assert-not-face (&rest arguments)
404 "Assert that LEVEL does not have a face with `:foreground'
405 FOREGROUND. Apply ARGUMENTS to
406 `context-coloring-test-assert-face', see that function."
407 (apply 'context-coloring-test-assert-face
408 (append arguments '(t))))
409
410 (defun context-coloring-test-assert-error (body error-message)
411 "Assert that BODY signals ERROR-MESSAGE."
412 (let ((error-signaled-p nil))
413 (condition-case err
414 (progn
415 (funcall body))
416 (error
417 (setq error-signaled-p t)
418 (when (not (string-equal (cadr err) error-message))
419 (ert-fail (format (concat "Expected the error \"%s\" to be thrown, "
420 "but instead it was \"%s\".")
421 error-message
422 (cadr err))))))
423 (when (not error-signaled-p)
424 (ert-fail "Expected an error to be thrown, but there wasn't."))))
425
426 (defun context-coloring-test-assert-trimmed (result expected)
427 (when (not (string-equal result expected))
428 (ert-fail "Expected string to be trimmed, but it wasn't.")))
429
430
431 ;;; The tests
432
433 (ert-deftest context-coloring-test-trim ()
434 (context-coloring-test-assert-trimmed (context-coloring-trim "") "")
435 (context-coloring-test-assert-trimmed (context-coloring-trim " ") "")
436 (context-coloring-test-assert-trimmed (context-coloring-trim "a") "a")
437 (context-coloring-test-assert-trimmed (context-coloring-trim " a") "a")
438 (context-coloring-test-assert-trimmed (context-coloring-trim "a ") "a")
439 (context-coloring-test-assert-trimmed (context-coloring-trim " a ") "a"))
440
441 (ert-deftest-async context-coloring-test-async-mode-startup (done)
442 (context-coloring-test-with-fixture-async
443 "./fixtures/empty"
444 (lambda (teardown)
445 (js-mode)
446 (add-hook
447 'context-coloring-colorize-hook
448 (lambda ()
449 ;; If this runs we are implicitly successful; this test only confirms
450 ;; that colorization occurs on mode startup.
451 (funcall teardown)
452 (funcall done)))
453 (context-coloring-mode))))
454
455 (define-derived-mode
456 context-coloring-change-detection-mode
457 fundamental-mode
458 "Testing"
459 "Prevent `context-coloring-test-change-detection' from
460 having any unintentional side-effects on mode support.")
461
462 ;; Simply cannot figure out how to trigger an idle timer; would much rather test
463 ;; that. But (current-idle-time) always returns nil in these tests.
464 (ert-deftest-async context-coloring-test-change-detection (done)
465 (context-coloring-define-dispatch
466 'idle-change
467 :modes '(context-coloring-change-detection-mode)
468 :executable "node"
469 :command "node test/binaries/noop")
470 (context-coloring-test-with-fixture-async
471 "./fixtures/empty"
472 (lambda (teardown)
473 (context-coloring-change-detection-mode)
474 (add-hook
475 'context-coloring-colorize-hook
476 (lambda ()
477 (setq context-coloring-colorize-hook nil)
478 (add-hook
479 'context-coloring-colorize-hook
480 (lambda ()
481 (funcall teardown)
482 (funcall done)))
483 (insert " ")
484 (set-window-buffer (selected-window) (current-buffer))
485 (context-coloring-maybe-colorize (current-buffer))))
486 (context-coloring-mode))))
487
488 (ert-deftest context-coloring-test-check-version ()
489 (when (not (context-coloring-check-version "2.1.3" "3.0.1"))
490 (ert-fail "Expected version 3.0.1 to satisfy 2.1.3, but it didn't."))
491 (when (context-coloring-check-version "3.0.1" "2.1.3")
492 (ert-fail "Expected version 2.1.3 not to satisfy 3.0.1, but it did.")))
493
494 (ert-deftest context-coloring-test-unsupported-mode ()
495 (context-coloring-test-with-fixture
496 "./fixtures/empty"
497 (context-coloring-mode)
498 (context-coloring-test-assert-message
499 "Context coloring is not available for this major mode"
500 "*Messages*")))
501
502 (define-derived-mode
503 context-coloring-test-define-dispatch-error-mode
504 fundamental-mode
505 "Testing"
506 "Prevent `context-coloring-test-define-dispatch-error' from
507 having any unintentional side-effects on mode support.")
508
509 (ert-deftest context-coloring-test-define-dispatch-error ()
510 (context-coloring-test-assert-error
511 (lambda ()
512 (context-coloring-define-dispatch
513 'define-dispatch-no-modes))
514 "No mode defined for dispatch")
515 (context-coloring-test-assert-error
516 (lambda ()
517 (context-coloring-define-dispatch
518 'define-dispatch-no-strategy
519 :modes '(context-coloring-test-define-dispatch-error-mode)))
520 "No colorizer, scopifier or command defined for dispatch"))
521
522 (define-derived-mode
523 context-coloring-test-define-dispatch-scopifier-mode
524 fundamental-mode
525 "Testing"
526 "Prevent `context-coloring-test-define-dispatch-scopifier' from
527 having any unintentional side-effects on mode support.")
528
529 (ert-deftest context-coloring-test-define-dispatch-scopifier ()
530 (context-coloring-define-dispatch
531 'define-dispatch-scopifier
532 :modes '(context-coloring-test-define-dispatch-scopifier-mode)
533 :scopifier (lambda () (vector)))
534 (with-temp-buffer
535 (context-coloring-test-define-dispatch-scopifier-mode)
536 (context-coloring-mode)
537 (context-coloring-colorize)))
538
539 (define-derived-mode
540 context-coloring-test-missing-executable-mode
541 fundamental-mode
542 "Testing"
543 "Prevent `context-coloring-test-define-dispatch-scopifier' from
544 having any unintentional side-effects on mode support.")
545
546 (ert-deftest context-coloring-test-missing-executable ()
547 (context-coloring-define-dispatch
548 'scopifier
549 :modes '(context-coloring-test-missing-executable-mode)
550 :command ""
551 :executable "__should_not_exist__")
552 (with-temp-buffer
553 (context-coloring-test-missing-executable-mode)
554 (context-coloring-mode)))
555
556 (define-derived-mode
557 context-coloring-test-unsupported-version-mode
558 fundamental-mode
559 "Testing"
560 "Prevent `context-coloring-test-unsupported-version' from
561 having any unintentional side-effects on mode support.")
562
563 (ert-deftest-async context-coloring-test-unsupported-version (done)
564 (context-coloring-define-dispatch
565 'outta-date
566 :modes '(context-coloring-test-unsupported-version-mode)
567 :executable "node"
568 :command "node test/binaries/outta-date"
569 :version "v2.1.3")
570 (context-coloring-test-with-fixture-async
571 "./fixtures/empty"
572 (lambda (teardown)
573 (context-coloring-test-unsupported-version-mode)
574 (add-hook
575 'context-coloring-check-scopifier-version-hook
576 (lambda ()
577 (unwind-protect
578 (progn
579 ;; Normally the executable would be something like "outta-date"
580 ;; rather than "node".
581 (context-coloring-test-assert-message
582 "Update to the minimum version of \"node\" (v2.1.3)"
583 "*Messages*"))
584 (funcall teardown))
585 (funcall done)))
586 (context-coloring-mode))))
587
588 (define-derived-mode
589 context-coloring-test-disable-mode-mode
590 fundamental-mode
591 "Testing"
592 "Prevent `context-coloring-test-disable-mode' from having any
593 unintentional side-effects on mode support.")
594
595 (ert-deftest-async context-coloring-test-disable-mode (done)
596 (let (torn-down)
597 (context-coloring-define-dispatch
598 'disable-mode
599 :modes '(context-coloring-test-disable-mode-mode)
600 :executable "node"
601 :command "node test/binaries/noop"
602 :teardown (lambda ()
603 (setq torn-down t)))
604 (context-coloring-test-with-fixture-async
605 "./fixtures/empty"
606 (lambda (teardown)
607 (unwind-protect
608 (progn
609 (context-coloring-test-disable-mode-mode)
610 (context-coloring-mode)
611 (context-coloring-mode -1)
612 (when (not torn-down)
613 (ert-fail "Expected teardown function to have been called, but it wasn't.")))
614 (funcall teardown))
615 (funcall done)))))
616
617 (defvar context-coloring-test-theme-index 0
618 "Unique index for unique theme names.")
619
620 (defun context-coloring-test-get-next-theme ()
621 "Return a unique symbol for a throwaway theme."
622 (prog1
623 (intern (format "context-coloring-test-theme-%s"
624 context-coloring-test-theme-index))
625 (setq context-coloring-test-theme-index
626 (+ context-coloring-test-theme-index 1))))
627
628 (defun context-coloring-test-assert-theme-originally-set-p
629 (settings &optional negate)
630 "Assert that `context-coloring-theme-originally-set-p' returns
631 t for a theme with SETTINGS, or the inverse if NEGATE is
632 non-nil."
633 (let ((theme (context-coloring-test-get-next-theme)))
634 (put theme 'theme-settings settings)
635 (when (funcall (if negate 'identity 'not)
636 (context-coloring-theme-originally-set-p theme))
637 (ert-fail (format (concat "Expected theme `%s' with settings `%s' "
638 "%sto be considered to have defined a level, "
639 "but it %s.")
640 theme settings
641 (if negate "not " "")
642 (if negate "was" "wasn't"))))))
643
644 (defun context-coloring-test-assert-not-theme-originally-set-p (&rest arguments)
645 "Assert that `context-coloring-theme-originally-set-p' does not
646 return t for a theme with SETTINGS. Apply ARGUMENTS to
647 `context-coloring-test-assert-theme-originally-set-p', see that
648 function."
649 (apply 'context-coloring-test-assert-theme-originally-set-p
650 (append arguments '(t))))
651
652 (ert-deftest context-coloring-test-theme-originally-set-p ()
653 (context-coloring-test-assert-theme-originally-set-p
654 '((theme-face context-coloring-level-0-face)))
655 (context-coloring-test-assert-theme-originally-set-p
656 '((theme-face face)
657 (theme-face context-coloring-level-0-face)))
658 (context-coloring-test-assert-theme-originally-set-p
659 '((theme-face context-coloring-level-0-face)
660 (theme-face face)))
661 (context-coloring-test-assert-not-theme-originally-set-p
662 '((theme-face face)))
663 )
664
665 (defun context-coloring-test-assert-theme-settings-highest-level
666 (settings expected-level)
667 "Assert that a theme with SETTINGS has the highest level
668 EXPECTED-LEVEL."
669 (let ((theme (context-coloring-test-get-next-theme)))
670 (put theme 'theme-settings settings)
671 (context-coloring-test-assert-theme-highest-level theme expected-level)))
672
673 (defun context-coloring-test-assert-theme-highest-level
674 (theme expected-level &optional negate)
675 "Assert that THEME has the highest level EXPECTED-LEVEL, or the
676 inverse if NEGATE is non-nil."
677 (let ((highest-level (context-coloring-theme-highest-level theme)))
678 (when (funcall (if negate 'identity 'not) (eq highest-level expected-level))
679 (ert-fail (format (concat "Expected theme with settings `%s' "
680 "%sto have a highest level of `%s', "
681 "but it %s.")
682 (get theme 'theme-settings)
683 (if negate "not " "") expected-level
684 (if negate "did" (format "was %s" highest-level)))))))
685
686 (defun context-coloring-test-assert-theme-not-highest-level (&rest arguments)
687 "Assert that THEME's highest level is not EXPECTED-LEVEL.
688 Apply ARGUMENTS to
689 `context-coloring-test-assert-theme-highest-level', see that
690 function."
691 (apply 'context-coloring-test-assert-theme-highest-level
692 (append arguments '(t))))
693
694 (ert-deftest context-coloring-test-theme-highest-level ()
695 (context-coloring-test-assert-theme-settings-highest-level
696 '((theme-face foo))
697 -1)
698 (context-coloring-test-assert-theme-settings-highest-level
699 '((theme-face context-coloring-level-0-face))
700 0)
701 (context-coloring-test-assert-theme-settings-highest-level
702 '((theme-face context-coloring-level-1-face))
703 1)
704 (context-coloring-test-assert-theme-settings-highest-level
705 '((theme-face context-coloring-level-1-face)
706 (theme-face context-coloring-level-0-face))
707 1)
708 (context-coloring-test-assert-theme-settings-highest-level
709 '((theme-face context-coloring-level-0-face)
710 (theme-face context-coloring-level-1-face))
711 1)
712 )
713
714 (defmacro context-coloring-test-deftest-define-theme (name &rest body)
715 "Define a test with name NAME and an automatically-generated
716 theme symbol available as a free variable `theme'. Side-effects
717 from enabling themes are reversed after BODY is executed and the
718 test completes."
719 (declare (indent defun))
720 (let ((deftest-name (intern
721 (format "context-coloring-test-define-theme-%s" name))))
722 `(ert-deftest ,deftest-name ()
723 (context-coloring-test-kill-buffer "*Warnings*")
724 (context-coloring-test-setup)
725 (let ((theme (context-coloring-test-get-next-theme)))
726 (unwind-protect
727 (progn
728 ,@body)
729 ;; Always cleanup.
730 (disable-theme theme)
731 (context-coloring-test-cleanup))))))
732
733 (defun context-coloring-test-deftheme (theme)
734 "Dynamically define theme THEME."
735 (eval (macroexpand `(deftheme ,theme))))
736
737 (context-coloring-test-deftest-define-theme additive
738 (context-coloring-test-deftheme theme)
739 (context-coloring-define-theme
740 theme
741 :colors '("#aaaaaa"
742 "#bbbbbb"))
743 (context-coloring-test-assert-no-message "*Warnings*")
744 (enable-theme theme)
745 (context-coloring-test-assert-no-message "*Warnings*")
746 (context-coloring-test-assert-face 0 "#aaaaaa")
747 (context-coloring-test-assert-face 1 "#bbbbbb"))
748
749 (defun context-coloring-test-assert-defined-warning (theme)
750 "Assert that a warning about colors already being defined for
751 theme THEME is signaled."
752 (context-coloring-test-assert-message
753 (format (concat "Warning (emacs): Context coloring colors for theme "
754 "`%s' are already defined")
755 theme)
756 "*Warnings*"))
757
758 (context-coloring-test-deftest-define-theme unintentional-override
759 (context-coloring-test-deftheme theme)
760 (custom-theme-set-faces
761 theme
762 '(context-coloring-level-0-face ((t (:foreground "#aaaaaa"))))
763 '(context-coloring-level-1-face ((t (:foreground "#bbbbbb")))))
764 (context-coloring-define-theme
765 theme
766 :colors '("#cccccc"
767 "#dddddd"))
768 (context-coloring-test-assert-defined-warning theme)
769 (context-coloring-test-kill-buffer "*Warnings*")
770 (enable-theme theme)
771 (context-coloring-test-assert-defined-warning theme)
772 (context-coloring-test-assert-face 0 "#cccccc")
773 (context-coloring-test-assert-face 1 "#dddddd"))
774
775 (context-coloring-test-deftest-define-theme intentional-override
776 (context-coloring-test-deftheme theme)
777 (custom-theme-set-faces
778 theme
779 '(context-coloring-level-0-face ((t (:foreground "#aaaaaa"))))
780 '(context-coloring-level-1-face ((t (:foreground "#bbbbbb")))))
781 (context-coloring-define-theme
782 theme
783 :override t
784 :colors '("#cccccc"
785 "#dddddd"))
786 (context-coloring-test-assert-no-message "*Warnings*")
787 (enable-theme theme)
788 (context-coloring-test-assert-no-message "*Warnings*")
789 (context-coloring-test-assert-face 0 "#cccccc")
790 (context-coloring-test-assert-face 1 "#dddddd"))
791
792 (context-coloring-test-deftest-define-theme pre-recede
793 (context-coloring-define-theme
794 theme
795 :recede t
796 :colors '("#aaaaaa"
797 "#bbbbbb"))
798 (context-coloring-test-deftheme theme)
799 (custom-theme-set-faces
800 theme
801 '(context-coloring-level-0-face ((t (:foreground "#cccccc"))))
802 '(context-coloring-level-1-face ((t (:foreground "#dddddd")))))
803 (enable-theme theme)
804 (context-coloring-test-assert-no-message "*Warnings*")
805 (context-coloring-test-assert-face 0 "#cccccc")
806 (context-coloring-test-assert-face 1 "#dddddd"))
807
808 (context-coloring-test-deftest-define-theme pre-recede-delayed-application
809 (context-coloring-define-theme
810 theme
811 :recede t
812 :colors '("#aaaaaa"
813 "#bbbbbb"))
814 (context-coloring-test-deftheme theme)
815 (enable-theme theme)
816 (context-coloring-test-assert-no-message "*Warnings*")
817 (context-coloring-test-assert-face 0 "#aaaaaa")
818 (context-coloring-test-assert-face 1 "#bbbbbb"))
819
820 (context-coloring-test-deftest-define-theme post-recede
821 (context-coloring-test-deftheme theme)
822 (custom-theme-set-faces
823 theme
824 '(context-coloring-level-0-face ((t (:foreground "#aaaaaa"))))
825 '(context-coloring-level-1-face ((t (:foreground "#bbbbbb")))))
826 (context-coloring-define-theme
827 theme
828 :recede t
829 :colors '("#cccccc"
830 "#dddddd"))
831 (context-coloring-test-assert-no-message "*Warnings*")
832 (context-coloring-test-assert-face 0 "#aaaaaa")
833 (context-coloring-test-assert-face 1 "#bbbbbb")
834 (enable-theme theme)
835 (context-coloring-test-assert-no-message "*Warnings*")
836 (context-coloring-test-assert-face 0 "#aaaaaa")
837 (context-coloring-test-assert-face 1 "#bbbbbb"))
838
839 (context-coloring-test-deftest-define-theme recede-not-defined
840 (context-coloring-test-deftheme theme)
841 (custom-theme-set-faces
842 theme
843 '(foo-face ((t (:foreground "#ffffff")))))
844 (context-coloring-define-theme
845 theme
846 :recede t
847 :colors '("#aaaaaa"
848 "#bbbbbb"))
849 (context-coloring-test-assert-no-message "*Warnings*")
850 (context-coloring-test-assert-face 0 "#aaaaaa")
851 (context-coloring-test-assert-face 1 "#bbbbbb")
852 (enable-theme theme)
853 (context-coloring-test-assert-no-message "*Warnings*")
854 (context-coloring-test-assert-face 0 "#aaaaaa")
855 (context-coloring-test-assert-face 1 "#bbbbbb"))
856
857 (context-coloring-test-deftest-define-theme unintentional-obstinance
858 (context-coloring-define-theme
859 theme
860 :colors '("#aaaaaa"
861 "#bbbbbb"))
862 (context-coloring-test-deftheme theme)
863 (custom-theme-set-faces
864 theme
865 '(context-coloring-level-0-face ((t (:foreground "#cccccc"))))
866 '(context-coloring-level-1-face ((t (:foreground "#dddddd")))))
867 (enable-theme theme)
868 (context-coloring-test-assert-defined-warning theme)
869 (context-coloring-test-assert-face 0 "#aaaaaa")
870 (context-coloring-test-assert-face 1 "#bbbbbb"))
871
872 (context-coloring-test-deftest-define-theme intentional-obstinance
873 (context-coloring-define-theme
874 theme
875 :override t
876 :colors '("#aaaaaa"
877 "#bbbbbb"))
878 (context-coloring-test-deftheme theme)
879 (custom-theme-set-faces
880 theme
881 '(context-coloring-level-0-face ((t (:foreground "#cccccc"))))
882 '(context-coloring-level-1-face ((t (:foreground "#dddddd")))))
883 (enable-theme theme)
884 (context-coloring-test-assert-no-message "*Warnings*")
885 (context-coloring-test-assert-face 0 "#aaaaaa")
886 (context-coloring-test-assert-face 1 "#bbbbbb"))
887
888 (defun context-coloring-test-assert-maximum-face (maximum &optional negate)
889 "Assert that `context-coloring-maximum-face' is MAXIMUM, or the
890 inverse if NEGATE is non-nil."
891 (when (funcall (if negate 'identity 'not)
892 (eq context-coloring-maximum-face maximum))
893 (ert-fail (format (concat "Expected `context-coloring-maximum-face' "
894 "%sto be `%s', "
895 "but it %s.")
896 (if negate "not " "") maximum
897 (if negate
898 "was"
899 (format "was `%s'" context-coloring-maximum-face))))))
900
901 (defun context-coloring-test-assert-not-maximum-face (&rest arguments)
902 "Assert that `context-coloring-maximum-face' is not MAXIMUM.
903 Apply ARGUMENTS to `context-coloring-test-assert-maximum-face',
904 see that function."
905 (apply 'context-coloring-test-assert-maximum-face
906 (append arguments '(t))))
907
908 (context-coloring-test-deftest-define-theme disable-cascade
909 (let ((maximum-face-value 9999))
910 (setq context-coloring-maximum-face maximum-face-value)
911 (context-coloring-test-deftheme theme)
912 (context-coloring-define-theme
913 theme
914 :colors '("#aaaaaa"
915 "#bbbbbb"))
916 (let ((second-theme (context-coloring-test-get-next-theme)))
917 (context-coloring-test-deftheme second-theme)
918 (context-coloring-define-theme
919 second-theme
920 :colors '("#cccccc"
921 "#dddddd"
922 "#eeeeee"))
923 (let ((third-theme (context-coloring-test-get-next-theme)))
924 (context-coloring-test-deftheme third-theme)
925 (context-coloring-define-theme
926 third-theme
927 :colors '("#111111"
928 "#222222"
929 "#333333"
930 "#444444"))
931 (enable-theme theme)
932 (enable-theme second-theme)
933 (enable-theme third-theme)
934 (disable-theme third-theme)
935 (context-coloring-test-assert-face 0 "#cccccc")
936 (context-coloring-test-assert-face 1 "#dddddd")
937 (context-coloring-test-assert-face 2 "#eeeeee")
938 (context-coloring-test-assert-maximum-face 2))
939 (disable-theme second-theme)
940 (context-coloring-test-assert-face 0 "#aaaaaa")
941 (context-coloring-test-assert-face 1 "#bbbbbb")
942 (context-coloring-test-assert-maximum-face 1))
943 (disable-theme theme)
944 (context-coloring-test-assert-not-face 0 "#aaaaaa")
945 (context-coloring-test-assert-not-face 1 "#bbbbbb")
946 (context-coloring-test-assert-maximum-face
947 maximum-face-value)))
948
949 (defun context-coloring-test-js-function-scopes ()
950 "Test fixtures/functions-scopes.js."
951 (context-coloring-test-assert-region-level 1 9 0)
952 (context-coloring-test-assert-region-level 9 23 1)
953 (context-coloring-test-assert-region-level 23 25 0)
954 (context-coloring-test-assert-region-level 25 34 1)
955 (context-coloring-test-assert-region-level 34 35 0)
956 (context-coloring-test-assert-region-level 35 52 1)
957 (context-coloring-test-assert-region-level 52 66 2)
958 (context-coloring-test-assert-region-level 66 72 1)
959 (context-coloring-test-assert-region-level 72 81 2)
960 (context-coloring-test-assert-region-level 81 82 1)
961 (context-coloring-test-assert-region-level 82 87 2)
962 (context-coloring-test-assert-region-level 87 89 1))
963
964 (context-coloring-test-deftest-js-mode function-scopes)
965 (context-coloring-test-deftest-js2-mode function-scopes)
966
967 (defun context-coloring-test-js-global ()
968 "Test fixtures/global.js."
969 (context-coloring-test-assert-region-level 20 28 1)
970 (context-coloring-test-assert-region-level 28 35 0)
971 (context-coloring-test-assert-region-level 35 41 1))
972
973 (context-coloring-test-deftest-js-mode global)
974 (context-coloring-test-deftest-js2-mode global)
975
976 (defun context-coloring-test-js-block-scopes ()
977 "Test fixtures/block-scopes.js."
978 (context-coloring-test-assert-region-level 20 64 1)
979 (setq context-coloring-js-block-scopes t)
980 (context-coloring-colorize)
981 (context-coloring-test-assert-region-level 20 27 1)
982 (context-coloring-test-assert-region-level 27 41 2)
983 (context-coloring-test-assert-region-level 41 42 1)
984 (context-coloring-test-assert-region-level 42 64 2))
985
986 (context-coloring-test-deftest-js2-mode block-scopes)
987
988 (defun context-coloring-test-js-catch ()
989 "Test fixtures/js-catch.js."
990 (context-coloring-test-assert-region-level 20 27 1)
991 (context-coloring-test-assert-region-level 27 51 2)
992 (context-coloring-test-assert-region-level 51 52 1)
993 (context-coloring-test-assert-region-level 52 73 2)
994 (context-coloring-test-assert-region-level 73 101 3)
995 (context-coloring-test-assert-region-level 101 102 1)
996 (context-coloring-test-assert-region-level 102 117 3)
997 (context-coloring-test-assert-region-level 117 123 2))
998
999 (context-coloring-test-deftest-js-mode catch)
1000 (context-coloring-test-deftest-js2-mode catch)
1001
1002 (defun context-coloring-test-js-key-names ()
1003 "Test fixtures/key-names.js."
1004 (context-coloring-test-assert-region-level 20 63 1))
1005
1006 (context-coloring-test-deftest-js-mode key-names)
1007 (context-coloring-test-deftest-js2-mode key-names)
1008
1009 (defun context-coloring-test-js-property-lookup ()
1010 "Test fixtures/property-lookup.js."
1011 (context-coloring-test-assert-region-level 20 26 0)
1012 (context-coloring-test-assert-region-level 26 38 1)
1013 (context-coloring-test-assert-region-level 38 44 0)
1014 (context-coloring-test-assert-region-level 44 52 1)
1015 (context-coloring-test-assert-region-level 57 63 0)
1016 (context-coloring-test-assert-region-level 63 74 1))
1017
1018 (context-coloring-test-deftest-js-mode property-lookup)
1019 (context-coloring-test-deftest-js2-mode property-lookup)
1020
1021 (defun context-coloring-test-js-key-values ()
1022 "Test fixtures/key-values.js."
1023 (context-coloring-test-assert-region-level 78 79 1))
1024
1025 (context-coloring-test-deftest-js-mode key-values)
1026 (context-coloring-test-deftest-js2-mode key-values)
1027
1028 (defun context-coloring-test-js-syntactic-comments-and-strings ()
1029 "Test comments and strings."
1030 (context-coloring-test-assert-region-level 1 8 0)
1031 (context-coloring-test-assert-region-comment-delimiter 9 12)
1032 (context-coloring-test-assert-region-comment 12 16)
1033 (context-coloring-test-assert-region-comment-delimiter 17 20)
1034 (context-coloring-test-assert-region-comment 20 27)
1035 (context-coloring-test-assert-region-string 28 40)
1036 (context-coloring-test-assert-region-level 40 41 0))
1037
1038 (defun context-coloring-test-js-syntactic-comments-and-strings-setup ()
1039 (setq context-coloring-syntactic-comments t)
1040 (setq context-coloring-syntactic-strings t))
1041
1042 (context-coloring-test-deftest-js-mode syntactic-comments-and-strings
1043 :fixture-name comments-and-strings)
1044 (context-coloring-test-deftest-js2-mode syntactic-comments-and-strings
1045 :fixture-name comments-and-strings)
1046
1047 (defalias 'context-coloring-test-js-comments-and-strings
1048 'context-coloring-test-js-syntactic-comments-and-strings
1049 "Test comments and strings. Deprecated.")
1050
1051 (defun context-coloring-test-js-comments-and-strings-setup ()
1052 "Setup comments and strings. Deprecated."
1053 (setq context-coloring-comments-and-strings t))
1054
1055 (context-coloring-test-deftest-js-mode comments-and-strings)
1056 (context-coloring-test-deftest-js2-mode comments-and-strings)
1057
1058 (defun context-coloring-test-js-syntactic-comments ()
1059 "Test syntactic comments."
1060 (context-coloring-test-assert-region-level 1 8 0)
1061 (context-coloring-test-assert-region-comment-delimiter 9 12)
1062 (context-coloring-test-assert-region-comment 12 16)
1063 (context-coloring-test-assert-region-comment-delimiter 17 20)
1064 (context-coloring-test-assert-region-comment 20 27)
1065 (context-coloring-test-assert-region-level 28 41 0))
1066
1067 (defun context-coloring-test-js-syntactic-comments-setup ()
1068 "Setup syntactic comments."
1069 (setq context-coloring-syntactic-comments t))
1070
1071 (context-coloring-test-deftest-js-mode syntactic-comments
1072 :fixture-name comments-and-strings)
1073 (context-coloring-test-deftest-js2-mode syntactic-comments
1074 :fixture-name comments-and-strings)
1075
1076 (defun context-coloring-test-js-syntactic-strings ()
1077 "Test syntactic strings."
1078 (context-coloring-test-assert-region-level 1 28 0)
1079 (context-coloring-test-assert-region-string 28 40)
1080 (context-coloring-test-assert-region-level 40 41 0))
1081
1082 (defun context-coloring-test-js-syntactic-strings-setup ()
1083 "Setup syntactic strings."
1084 (setq context-coloring-syntactic-strings t))
1085
1086 (context-coloring-test-deftest-js-mode syntactic-strings
1087 :fixture-name comments-and-strings)
1088 (context-coloring-test-deftest-js2-mode syntactic-strings
1089 :fixture-name comments-and-strings)
1090
1091 ;; As long as `add-text-properties' doesn't signal an error, this test passes.
1092 (defun context-coloring-test-js-unterminated-comment ()
1093 "Test unterminated multiline comments.")
1094
1095 (context-coloring-test-deftest-js2-mode unterminated-comment)
1096
1097 (context-coloring-test-deftest-emacs-lisp-mode defun
1098 (lambda ()
1099 (context-coloring-test-assert-coloring "
1100 111111 000 1111 111 111111111 1111
1101 11 111 111 111 000011
1102
1103 0000 0 0 00
1104
1105 111111 01
1106 111111 111")))
1107
1108 (context-coloring-test-deftest-emacs-lisp-mode lambda
1109 (lambda ()
1110 (context-coloring-test-assert-coloring "
1111 00000000 1111111 1111
1112 11111111 11 2222222 2222
1113 222 22 12 2221 111 0 00")))
1114
1115 (context-coloring-test-deftest-emacs-lisp-mode quote
1116 (lambda ()
1117 (context-coloring-test-assert-coloring "
1118 (xxxxx x (x)
1119 (xx (xx x 111
1120 111111 1 111 111
1121 111111 1 1111111111 11 111 1 111 1 00001 10000 11 00001 1 100001111")))
1122
1123 (context-coloring-test-deftest-emacs-lisp-mode comment
1124 (lambda ()
1125 ;; Just check that the comment isn't parsed syntactically.
1126 (context-coloring-test-assert-coloring "
1127 (xxxxx x ()
1128 (xx (x xxxxx-xxxx xx) ;;;;;;;;;;
1129 11 00000-0000 11))) ;;;;;;;;;;"))
1130 :setup (lambda ()
1131 (setq context-coloring-syntactic-comments t)))
1132
1133 (context-coloring-test-deftest-emacs-lisp-mode string
1134 (lambda ()
1135 (context-coloring-test-assert-coloring "
1136 (xxxxx x (x)
1137 (xxxxxx x x sss 1 0 sssss 0 1 sssssss11"))
1138 :setup (lambda ()
1139 (setq context-coloring-syntactic-strings t)))
1140
1141 (context-coloring-test-deftest-emacs-lisp-mode ignored
1142 (lambda ()
1143 (context-coloring-test-assert-coloring "
1144 (xxxxx x ()
1145 (x x 1 11 11 111 11 1 111 (1 1 1)))")))
1146
1147 (context-coloring-test-deftest-emacs-lisp-mode let
1148 (lambda ()
1149 (context-coloring-test-assert-coloring "
1150 1111 11
1151 11 01
1152 11 00001
1153 11 2222 22
1154 22 02
1155 22 000022
1156 2222 2 2 2 00002211
1157 1111 1 1 1 000011")))
1158
1159 (context-coloring-test-deftest-emacs-lisp-mode let*
1160 (lambda ()
1161 (context-coloring-test-assert-coloring "
1162 11111 11
1163 11 11
1164 11 000011
1165 1111 1 1 1 0 0 00001
1166 22222 22
1167 22 12
1168 22 00002
1169 22 02
1170 22 222
1171 2222 1 1 2 2 2 000022
1172 1111 1 1 1 0 0 000011")))
1173
1174 (context-coloring-test-deftest-emacs-lisp-mode depth
1175 (lambda ()
1176 (let ((context-coloring-emacs-lisp-iterations-per-pause 1))
1177 (context-coloring-colorize)
1178 (context-coloring-test-assert-coloring "
1179 ;; `cc' `cc'
1180 (xxxxx x ())")))
1181 :setup (lambda ()
1182 (setq context-coloring-syntactic-comments t)
1183 (setq context-coloring-syntactic-strings t)))
1184
1185 (provide 'context-coloring-test)
1186
1187 ;;; context-coloring-test.el ends here