-;;; test/context-coloring-test.el --- Tests for context coloring. -*- lexical-binding: t; -*-
+;;; context-coloring-test.el --- Tests for context coloring -*- lexical-binding: t; -*-
;; Copyright (C) 2014-2015 Free Software Foundation, Inc.
;;; Commentary:
-;; Tests for context-coloring.
+;; Tests for context coloring.
-;; Tests for both synchronous (elisp) and asynchronous (shell command) coloring
-;; are available. Basic plugin functionality is also tested.
-
-;; To run, execute `make test' from the project root.
+;; Use with `make test'.
;;; Code:
(defun context-coloring-test-setup ()
"Prepare before all tests."
- (setq context-coloring-comments-and-strings nil))
+ (setq context-coloring-syntactic-comments nil)
+ (setq context-coloring-syntactic-strings nil))
(defun context-coloring-test-cleanup ()
"Cleanup after all tests."
- (setq context-coloring-comments-and-strings t)
- (setq context-coloring-syntactic-comments nil)
- (setq context-coloring-syntactic-strings nil)
+ (setq context-coloring-comments-and-strings nil)
(setq context-coloring-js-block-scopes nil)
- (setq context-coloring-check-scopifier-version-hook nil))
+ (setq context-coloring-colorize-hook nil)
+ (setq context-coloring-check-scopifier-version-hook nil)
+ (setq context-coloring-maximum-face 7)
+ (setq context-coloring-original-maximum-face
+ context-coloring-maximum-face))
(defmacro context-coloring-test-with-fixture (fixture &rest body)
"With the relative FIXTURE, evaluate BODY in a temporary
',setup-function-name
(,function-name)))))
+(cl-defmacro context-coloring-test-deftest-emacs-lisp-mode (name
+ body
+ &key setup)
+ "Define a test for `emacs-lisp-mode' with name and fixture as
+NAME, with BODY containing the assertions, and SETUP defining the
+environment."
+ (declare (indent defun))
+ (let ((test-name (intern (format "context-coloring-emacs-lisp-mode-%s" name)))
+ (fixture (format "./fixtures/%s.el" name)))
+ `(ert-deftest ,test-name ()
+ (context-coloring-test-with-fixture
+ ,fixture
+ (emacs-lisp-mode)
+ (when ,setup (funcall ,setup))
+ (context-coloring-mode)
+ (funcall ,body)))))
+
;;; Assertion functions
+(defun context-coloring-test-assert-position-level (position level)
+ "Assert that POSITION has LEVEL."
+ (let ((face (get-text-property position 'face))
+ actual-level)
+ (when (not (and face
+ (let* ((face-string (symbol-name face))
+ (matches (string-match
+ context-coloring-level-face-regexp
+ face-string)))
+ (when matches
+ (setq actual-level (string-to-number
+ (substring face-string
+ (match-beginning 1)
+ (match-end 1))))
+ (= level actual-level)))))
+ (ert-fail (format (concat "Expected level at position %s, "
+ "which is \"%s\", to be %s; "
+ "but it was %s")
+ position
+ (buffer-substring-no-properties position (1+ position)) level
+ actual-level)))))
+
+(defun context-coloring-test-assert-position-face (position face-regexp)
+ "Assert that the face at POSITION satisfies FACE-REGEXP."
+ (let ((face (get-text-property position 'face)))
+ (when (or
+ ;; Pass a non-string to do an `equal' check (against a symbol or nil).
+ (unless (stringp face-regexp)
+ (not (equal face-regexp face)))
+ ;; Otherwise do the matching.
+ (when (stringp face-regexp)
+ (not (string-match-p face-regexp (symbol-name face)))))
+ (ert-fail (format (concat "Expected face at position %s, "
+ "which is \"%s\", to be %s; "
+ "but it was %s")
+ position
+ (buffer-substring-no-properties position (1+ position)) face-regexp
+ face)))))
+
+(defun context-coloring-test-assert-position-comment (position)
+ (context-coloring-test-assert-position-face
+ position "\\`font-lock-comment\\(-delimiter\\)?-face\\'"))
+
+(defun context-coloring-test-assert-position-constant-comment (position)
+ (context-coloring-test-assert-position-face position '(font-lock-constant-face
+ font-lock-comment-face)))
+
+(defun context-coloring-test-assert-position-string (position)
+ (context-coloring-test-assert-position-face position 'font-lock-string-face))
+
+(defun context-coloring-test-assert-position-nil (position)
+ (context-coloring-test-assert-position-face position nil))
+
+(defun context-coloring-test-assert-coloring (map)
+ "Assert that the current buffer's coloring matches MAP."
+ ;; Omit the superfluous, formatting-related leading newline. Can't use
+ ;; `save-excursion' here because if an assertion fails it will cause future
+ ;; tests to get messed up.
+ (goto-char (point-min))
+ (let* ((map (substring map 1))
+ (index 0)
+ char-string
+ char)
+ (while (< index (length map))
+ (setq char-string (substring map index (1+ index)))
+ (setq char (string-to-char char-string))
+ (cond
+ ;; Newline
+ ((= char 10)
+ (next-logical-line)
+ (beginning-of-line))
+ ;; Number
+ ((and (>= char 48)
+ (<= char 57))
+ (context-coloring-test-assert-position-level
+ (point) (string-to-number char-string))
+ (forward-char))
+ ;; ';' = Comment
+ ((= char 59)
+ (context-coloring-test-assert-position-comment (point))
+ (forward-char))
+ ;; 'c' = Constant comment
+ ((= char 99)
+ (context-coloring-test-assert-position-constant-comment (point))
+ (forward-char))
+ ;; 'n' = nil
+ ((= char 110)
+ (context-coloring-test-assert-position-nil (point))
+ (forward-char))
+ ;; 's' = String
+ ((= char 115)
+ (context-coloring-test-assert-position-string (point))
+ (forward-char))
+ (t
+ (forward-char)))
+ (setq index (1+ index)))))
+
(defmacro context-coloring-test-assert-region (&rest body)
"Assert something about the face of points in a region.
Provides the free variables `i', `length', `point', `face' and
(context-coloring-test-assert-region-face
start end 'font-lock-string-face))
+(defun context-coloring-test-get-last-message ()
+ (let ((messages (split-string
+ (buffer-substring-no-properties
+ (point-min)
+ (point-max))
+ "\n")))
+ (car (nthcdr (- (length messages) 2) messages))))
+
(defun context-coloring-test-assert-message (expected buffer)
- "Assert that message EXPECTED exists in BUFFER."
+ "Assert that message EXPECTED is at the end of BUFFER."
(when (null (get-buffer buffer))
(ert-fail
(format
"but the buffer did not have any messages.")
buffer expected)))
(with-current-buffer buffer
- (let ((messages (split-string
- (buffer-substring-no-properties
- (point-min)
- (point-max))
- "\n")))
- (let ((message (car (nthcdr (- (length messages) 2) messages))))
- (when (not (equal message expected))
+ (let ((message (context-coloring-test-get-last-message)))
+ (when (not (equal message expected))
+ (ert-fail
+ (format
+ (concat
+ "Expected buffer `%s' to have message \"%s\", "
+ "but instead it was \"%s\"")
+ buffer expected
+ message))))))
+
+(defun context-coloring-test-assert-not-message (expected buffer)
+ "Assert that message EXPECTED is not at the end of BUFFER."
+ (when (get-buffer buffer)
+ (with-current-buffer buffer
+ (let ((message (context-coloring-test-get-last-message)))
+ (when (equal message expected)
(ert-fail
(format
(concat
- "Expected buffer `%s' to have message \"%s\", "
- "but instead it was \"%s\"")
- buffer expected
- message)))))))
+ "Expected buffer `%s' not to have message \"%s\", "
+ "but it did")
+ buffer expected)))))))
(defun context-coloring-test-assert-no-message (buffer)
"Assert that BUFFER has no message."
(apply 'context-coloring-test-assert-face
(append arguments '(t))))
+(defun context-coloring-test-assert-error (body error-message)
+ "Assert that BODY signals ERROR-MESSAGE."
+ (let ((error-signaled-p nil))
+ (condition-case err
+ (progn
+ (funcall body))
+ (error
+ (setq error-signaled-p t)
+ (when (not (string-equal (cadr err) error-message))
+ (ert-fail (format (concat "Expected the error \"%s\" to be thrown, "
+ "but instead it was \"%s\".")
+ error-message
+ (cadr err))))))
+ (when (not error-signaled-p)
+ (ert-fail "Expected an error to be thrown, but there wasn't."))))
+
+(defun context-coloring-test-assert-trimmed (result expected)
+ (when (not (string-equal result expected))
+ (ert-fail "Expected string to be trimmed, but it wasn't.")))
+
;;; The tests
+(ert-deftest context-coloring-test-trim ()
+ (context-coloring-test-assert-trimmed (context-coloring-trim "") "")
+ (context-coloring-test-assert-trimmed (context-coloring-trim " ") "")
+ (context-coloring-test-assert-trimmed (context-coloring-trim "a") "a")
+ (context-coloring-test-assert-trimmed (context-coloring-trim " a") "a")
+ (context-coloring-test-assert-trimmed (context-coloring-trim "a ") "a")
+ (context-coloring-test-assert-trimmed (context-coloring-trim " a ") "a"))
+
+(ert-deftest-async context-coloring-test-async-mode-startup (done)
+ (context-coloring-test-with-fixture-async
+ "./fixtures/empty"
+ (lambda (teardown)
+ (js-mode)
+ (add-hook
+ 'context-coloring-colorize-hook
+ (lambda ()
+ ;; If this runs we are implicitly successful; this test only confirms
+ ;; that colorization occurs on mode startup.
+ (funcall teardown)
+ (funcall done)))
+ (context-coloring-mode))))
+
+(define-derived-mode
+ context-coloring-change-detection-mode
+ fundamental-mode
+ "Testing"
+ "Prevent `context-coloring-test-change-detection' from
+ having any unintentional side-effects on mode support.")
+
+;; Simply cannot figure out how to trigger an idle timer; would much rather test
+;; that. But (current-idle-time) always returns nil in these tests.
+(ert-deftest-async context-coloring-test-change-detection (done)
+ (context-coloring-define-dispatch
+ 'idle-change
+ :modes '(context-coloring-change-detection-mode)
+ :executable "node"
+ :command "node test/binaries/noop")
+ (context-coloring-test-with-fixture-async
+ "./fixtures/empty"
+ (lambda (teardown)
+ (context-coloring-change-detection-mode)
+ (add-hook
+ 'context-coloring-colorize-hook
+ (lambda ()
+ (setq context-coloring-colorize-hook nil)
+ (add-hook
+ 'context-coloring-colorize-hook
+ (lambda ()
+ (funcall teardown)
+ (funcall done)))
+ (insert " ")
+ (set-window-buffer (selected-window) (current-buffer))
+ (context-coloring-maybe-colorize (current-buffer))))
+ (context-coloring-mode))))
+
+(ert-deftest context-coloring-test-check-version ()
+ (when (not (context-coloring-check-version "2.1.3" "3.0.1"))
+ (ert-fail "Expected version 3.0.1 to satisfy 2.1.3, but it didn't."))
+ (when (context-coloring-check-version "3.0.1" "2.1.3")
+ (ert-fail "Expected version 2.1.3 not to satisfy 3.0.1, but it did.")))
+
(ert-deftest context-coloring-test-unsupported-mode ()
(context-coloring-test-with-fixture
- "./fixtures/function-scopes.js"
+ "./fixtures/empty"
(context-coloring-mode)
(context-coloring-test-assert-message
"Context coloring is not available for this major mode"
"*Messages*")))
+(ert-deftest context-coloring-test-derived-mode ()
+ (context-coloring-test-with-fixture
+ "./fixtures/empty"
+ (lisp-interaction-mode)
+ (context-coloring-mode)
+ (context-coloring-test-assert-not-message
+ "Context coloring is not available for this major mode"
+ "*Messages*")))
+
+(define-derived-mode
+ context-coloring-test-define-dispatch-error-mode
+ fundamental-mode
+ "Testing"
+ "Prevent `context-coloring-test-define-dispatch-error' from
+ having any unintentional side-effects on mode support.")
+
+(ert-deftest context-coloring-test-define-dispatch-error ()
+ (context-coloring-test-assert-error
+ (lambda ()
+ (context-coloring-define-dispatch
+ 'define-dispatch-no-modes))
+ "No mode defined for dispatch")
+ (context-coloring-test-assert-error
+ (lambda ()
+ (context-coloring-define-dispatch
+ 'define-dispatch-no-strategy
+ :modes '(context-coloring-test-define-dispatch-error-mode)))
+ "No colorizer, scopifier or command defined for dispatch"))
+
+(define-derived-mode
+ context-coloring-test-define-dispatch-scopifier-mode
+ fundamental-mode
+ "Testing"
+ "Prevent `context-coloring-test-define-dispatch-scopifier' from
+ having any unintentional side-effects on mode support.")
+
+(ert-deftest context-coloring-test-define-dispatch-scopifier ()
+ (context-coloring-define-dispatch
+ 'define-dispatch-scopifier
+ :modes '(context-coloring-test-define-dispatch-scopifier-mode)
+ :scopifier (lambda () (vector)))
+ (with-temp-buffer
+ (context-coloring-test-define-dispatch-scopifier-mode)
+ (context-coloring-mode)
+ (context-coloring-colorize)))
+
+(define-derived-mode
+ context-coloring-test-missing-executable-mode
+ fundamental-mode
+ "Testing"
+ "Prevent `context-coloring-test-define-dispatch-scopifier' from
+ having any unintentional side-effects on mode support.")
+
+(ert-deftest context-coloring-test-missing-executable ()
+ (context-coloring-define-dispatch
+ 'scopifier
+ :modes '(context-coloring-test-missing-executable-mode)
+ :command ""
+ :executable "__should_not_exist__")
+ (with-temp-buffer
+ (context-coloring-test-missing-executable-mode)
+ (context-coloring-mode)))
+
(define-derived-mode
context-coloring-test-unsupported-version-mode
fundamental-mode
:command "node test/binaries/outta-date"
:version "v2.1.3")
(context-coloring-test-with-fixture-async
- "./fixtures/function-scopes.js"
+ "./fixtures/empty"
(lambda (teardown)
(context-coloring-test-unsupported-version-mode)
(add-hook
(funcall done)))
(context-coloring-mode))))
+(define-derived-mode
+ context-coloring-test-disable-mode-mode
+ fundamental-mode
+ "Testing"
+ "Prevent `context-coloring-test-disable-mode' from having any
+ unintentional side-effects on mode support.")
+
+(ert-deftest-async context-coloring-test-disable-mode (done)
+ (let (torn-down)
+ (context-coloring-define-dispatch
+ 'disable-mode
+ :modes '(context-coloring-test-disable-mode-mode)
+ :executable "node"
+ :command "node test/binaries/noop"
+ :teardown (lambda ()
+ (setq torn-down t)))
+ (context-coloring-test-with-fixture-async
+ "./fixtures/empty"
+ (lambda (teardown)
+ (unwind-protect
+ (progn
+ (context-coloring-test-disable-mode-mode)
+ (context-coloring-mode)
+ (context-coloring-mode -1)
+ (when (not torn-down)
+ (ert-fail "Expected teardown function to have been called, but it wasn't.")))
+ (funcall teardown))
+ (funcall done)))))
+
(defvar context-coloring-test-theme-index 0
"Unique index for unique theme names.")
(format "context-coloring-test-define-theme-%s" name))))
`(ert-deftest ,deftest-name ()
(context-coloring-test-kill-buffer "*Warnings*")
+ (context-coloring-test-setup)
(let ((theme (context-coloring-test-get-next-theme)))
(unwind-protect
(progn
,@body)
;; Always cleanup.
- (disable-theme theme))))))
+ (disable-theme theme)
+ (context-coloring-test-cleanup))))))
(defun context-coloring-test-deftheme (theme)
"Dynamically define theme THEME."
(context-coloring-test-assert-face 0 "#cccccc")
(context-coloring-test-assert-face 1 "#dddddd"))
+(context-coloring-test-deftest-define-theme pre-recede-delayed-application
+ (context-coloring-define-theme
+ theme
+ :recede t
+ :colors '("#aaaaaa"
+ "#bbbbbb"))
+ (context-coloring-test-deftheme theme)
+ (enable-theme theme)
+ (context-coloring-test-assert-no-message "*Warnings*")
+ (context-coloring-test-assert-face 0 "#aaaaaa")
+ (context-coloring-test-assert-face 1 "#bbbbbb"))
+
(context-coloring-test-deftest-define-theme post-recede
(context-coloring-test-deftheme theme)
(custom-theme-set-faces
(append arguments '(t))))
(context-coloring-test-deftest-define-theme disable-cascade
- (context-coloring-test-deftheme theme)
- (context-coloring-define-theme
- theme
- :colors '("#aaaaaa"
- "#bbbbbb"))
- (let ((second-theme (context-coloring-test-get-next-theme)))
- (context-coloring-test-deftheme second-theme)
+ (let ((maximum-face-value 9999))
+ (setq context-coloring-maximum-face maximum-face-value)
+ (context-coloring-test-deftheme theme)
(context-coloring-define-theme
- second-theme
- :colors '("#cccccc"
- "#dddddd"
- "#eeeeee"))
- (let ((third-theme (context-coloring-test-get-next-theme)))
- (context-coloring-test-deftheme third-theme)
+ theme
+ :colors '("#aaaaaa"
+ "#bbbbbb"))
+ (let ((second-theme (context-coloring-test-get-next-theme)))
+ (context-coloring-test-deftheme second-theme)
(context-coloring-define-theme
- third-theme
- :colors '("#111111"
- "#222222"
- "#333333"
- "#444444"))
- (enable-theme theme)
- (enable-theme second-theme)
- (enable-theme third-theme)
- (disable-theme third-theme)
- (context-coloring-test-assert-face 0 "#cccccc")
- (context-coloring-test-assert-face 1 "#dddddd")
- (context-coloring-test-assert-face 2 "#eeeeee")
- (context-coloring-test-assert-maximum-face 2))
- (disable-theme second-theme)
- (context-coloring-test-assert-face 0 "#aaaaaa")
- (context-coloring-test-assert-face 1 "#bbbbbb")
- (context-coloring-test-assert-maximum-face 1))
- (disable-theme theme)
- (context-coloring-test-assert-not-face 0 "#aaaaaa")
- (context-coloring-test-assert-not-face 1 "#bbbbbb")
- (context-coloring-test-assert-not-maximum-face 1))
+ second-theme
+ :colors '("#cccccc"
+ "#dddddd"
+ "#eeeeee"))
+ (let ((third-theme (context-coloring-test-get-next-theme)))
+ (context-coloring-test-deftheme third-theme)
+ (context-coloring-define-theme
+ third-theme
+ :colors '("#111111"
+ "#222222"
+ "#333333"
+ "#444444"))
+ (enable-theme theme)
+ (enable-theme second-theme)
+ (enable-theme third-theme)
+ (disable-theme third-theme)
+ (context-coloring-test-assert-face 0 "#cccccc")
+ (context-coloring-test-assert-face 1 "#dddddd")
+ (context-coloring-test-assert-face 2 "#eeeeee")
+ (context-coloring-test-assert-maximum-face 2))
+ (disable-theme second-theme)
+ (context-coloring-test-assert-face 0 "#aaaaaa")
+ (context-coloring-test-assert-face 1 "#bbbbbb")
+ (context-coloring-test-assert-maximum-face 1))
+ (disable-theme theme)
+ (context-coloring-test-assert-not-face 0 "#aaaaaa")
+ (context-coloring-test-assert-not-face 1 "#bbbbbb")
+ (context-coloring-test-assert-maximum-face
+ maximum-face-value)))
(defun context-coloring-test-js-function-scopes ()
"Test fixtures/functions-scopes.js."
(context-coloring-test-deftest-js2-mode syntactic-strings
:fixture-name comments-and-strings)
+;; As long as `add-text-properties' doesn't signal an error, this test passes.
+(defun context-coloring-test-js-unterminated-comment ()
+ "Test unterminated multiline comments.")
+
+(context-coloring-test-deftest-js2-mode unterminated-comment)
+
+(context-coloring-test-deftest-emacs-lisp-mode defun
+ (lambda ()
+ (context-coloring-test-assert-coloring "
+111111 000 1111 111 111111111 1111
+ 11 111 111 111 000011
+
+0000 0 0 00
+
+111111 01
+111111 111")))
+
+(context-coloring-test-deftest-emacs-lisp-mode lambda
+ (lambda ()
+ (context-coloring-test-assert-coloring "
+00000000 1111111 1111
+ 11111111 11 2222222 2222
+ 222 22 12 2221 111 0 00")))
+
+(context-coloring-test-deftest-emacs-lisp-mode quote
+ (lambda ()
+ (context-coloring-test-assert-coloring "
+(xxxxx x (x)
+ (xx (xx x 111
+ 111111 1 111 111
+ 111111 1 1111111111 11 111 1 111 1 00001 10000 11 00001 1 100001111")))
+
+(context-coloring-test-deftest-emacs-lisp-mode comment
+ (lambda ()
+ ;; Just check that the comment isn't parsed syntactically.
+ (context-coloring-test-assert-coloring "
+(xxxxx x ()
+ (xx (x xxxxx-xxxx xx) ;;;;;;;;;;
+ 11 00000-0000 11))) ;;;;;;;;;;"))
+ :setup (lambda ()
+ (setq context-coloring-syntactic-comments t)))
+
+(context-coloring-test-deftest-emacs-lisp-mode string
+ (lambda ()
+ (context-coloring-test-assert-coloring "
+(xxxxx x (x)
+ (xxxxxx x x sss 1 0 sssss 0 1 sssssss11"))
+ :setup (lambda ()
+ (setq context-coloring-syntactic-strings t)))
+
+(context-coloring-test-deftest-emacs-lisp-mode ignored
+ (lambda ()
+ (context-coloring-test-assert-coloring "
+(xxxxx x ()
+ (x x 1 11 11 111 11 1 111 (1 1 1)))")))
+
+(context-coloring-test-deftest-emacs-lisp-mode let
+ (lambda ()
+ (context-coloring-test-assert-coloring "
+1111 11
+ 11 01
+ 11 00001
+ 11 2222 22
+ 22 02
+ 22 000022
+ 2222 2 2 2 00002211
+ 1111 1 1 1 000011")))
+
+(context-coloring-test-deftest-emacs-lisp-mode let*
+ (lambda ()
+ (context-coloring-test-assert-coloring "
+11111 11
+ 11 11
+ 11 000011
+ 1111 1 1 1 0 0 00001
+ 22222 22
+ 22 12
+ 22 00002
+ 22 02
+ 22 222
+ 2222 1 1 2 2 2 000022
+ 1111 1 1 1 0 0 000011")))
+
+(defun context-coloring-test-insert-unread-space ()
+ (setq unread-command-events (cons '(t . 32)
+ unread-command-events)))
+
+(defun context-coloring-test-remove-faces ()
+ (remove-text-properties (point-min) (point-max) '(face nil)))
+
+(context-coloring-test-deftest-emacs-lisp-mode iteration
+ (lambda ()
+ (let ((context-coloring-emacs-lisp-iterations-per-pause 1))
+ (context-coloring-colorize)
+ (context-coloring-test-assert-coloring "
+;; `cc' `cc'
+(xxxxx x ())")
+ (context-coloring-test-remove-faces)
+ (context-coloring-test-insert-unread-space)
+ (context-coloring-colorize)
+ ;; The first iteration will color the first part of the comment, but
+ ;; that's it. Then it will be interrupted.
+ (context-coloring-test-assert-coloring "
+;; nnnn nnnn
+nnnnnn n nnn")))
+ :setup (lambda ()
+ (setq context-coloring-syntactic-comments t)
+ (setq context-coloring-syntactic-strings t)))
+
(provide 'context-coloring-test)
;;; context-coloring-test.el ends here