]> code.delx.au - gnu-emacs-elpa/blobdiff - packages/context-coloring/test/context-coloring-test.el
Merge commit 'f4e1cbc5386fbf197ff8bcb6a9275f17c9cbe8e4' from context-coloring
[gnu-emacs-elpa] / packages / context-coloring / test / context-coloring-test.el
index fdb0d83cfae0bfd19377cdbd24999727b3549b1b..88a7158077e4738f29818b4d3caa55b64e4a57ec 100644 (file)
 ;; You should have received a copy of the GNU General Public License
 ;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+;;; Commentary:
+
+;; 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.
+
 ;;; Code:
 
 (require 'ert-async)
   "This file's directory.")
 
 (defun context-coloring-test-read-file (path)
-  "Read a file's contents into a string."
+  "Read a file's contents from PATH into a string."
   (with-temp-buffer
     (insert-file-contents (expand-file-name path context-coloring-test-path))
     (buffer-string)))
 
 (defun context-coloring-test-setup ()
-  "Preparation code to run before all tests."
+  "Prepare before all tests."
   (setq context-coloring-comments-and-strings nil))
 
 (defun context-coloring-test-cleanup ()
-  "Cleanup code to run after all tests."
+  "Cleanup after all tests."
   (setq context-coloring-comments-and-strings t)
   (setq context-coloring-after-colorize-hook nil)
-  (setq context-coloring-js-block-scopes nil)
-  (context-coloring-set-colors-default))
+  (setq context-coloring-js-block-scopes nil))
 
 (defmacro context-coloring-test-with-fixture (fixture &rest body)
-  "Evaluate BODY in a temporary buffer with the relative
-FIXTURE."
+  "With the relative FIXTURE, evaluate BODY in a temporary
+buffer."
   `(with-temp-buffer
      (unwind-protect
          (progn
@@ -73,14 +81,14 @@ is done."
 
 (defun context-coloring-test-with-fixture-async
     (fixture callback &optional setup)
-  "Evaluate CALLBACK in a temporary buffer with the relative
-FIXTURE.  A teardown callback is passed to CALLBACK for it to
-invoke when it is done.  An optional SETUP callback can be passed
-to run arbitrary code before the mode is invoked."
+  "With the relative FIXTURE, evaluate CALLBACK in a temporary
+buffer.  A teardown callback is passed to CALLBACK for it to
+invoke when it is done.  An optional SETUP callback can run
+arbitrary code before the mode is invoked."
   (context-coloring-test-with-temp-buffer-async
    (lambda (done-with-temp-buffer)
      (context-coloring-test-setup)
-     (if setup (funcall setup))
+     (when setup (funcall setup))
      (insert (context-coloring-test-read-file fixture))
      (funcall
       callback
@@ -117,8 +125,8 @@ instantiated in SETUP."
     ,@body))
 
 (defmacro context-coloring-test-deftest-js-mode (name)
-  "Define an asynchronous test for `js-mode' in the typical
-format."
+  "Define an asynchronous test for `js-mode' with the name NAME
+in the typical format."
   (let ((test-name (intern (format "context-coloring-test-js-mode-%s" name)))
         (fixture (format "./fixtures/%s.js" name))
         (function-name (intern-soft
@@ -133,7 +141,8 @@ format."
                            (funcall done))))))
 
 (defmacro context-coloring-test-deftest-js2-mode (name)
-    "Define a test for `js2-mode' in the typical format."
+  "Define a test for `js2-mode' with the name NAME in the typical
+format."
   (let ((test-name (intern (format "context-coloring-test-js2-mode-%s" name)))
         (fixture (format "./fixtures/%s.js" name))
         (function-name (intern-soft
@@ -147,9 +156,9 @@ format."
 ;;; Assertion functions
 
 (defmacro context-coloring-test-assert-region (&rest body)
-  "Skeleton for asserting something about the face of points in a
-region.  Provides the free variables `i', `length', `point',
-`face' and `actual-level'."
+  "Assert something about the face of points in a region.
+Provides the free variables `i', `length', `point', `face' and
+`actual-level' to the code in BODY."
   `(let ((i 0)
          (length (- end start)))
      (while (< i length)
@@ -200,19 +209,19 @@ EXPECTED-FACE."
    start end 'font-lock-comment-delimiter-face))
 
 (defun context-coloring-test-assert-region-comment (start end)
-    "Assert that all points in the range [START, END) have
+  "Assert that all points in the range [START, END) have
 `font-lock-comment-face'."
   (context-coloring-test-assert-region-face
    start end 'font-lock-comment-face))
 
 (defun context-coloring-test-assert-region-string (start end)
-    "Assert that all points in the range [START, END) have
+  "Assert that all points in the range [START, END) have
 `font-lock-string-face'."
   (context-coloring-test-assert-region-face
    start end 'font-lock-string-face))
 
 (defun context-coloring-test-assert-message (expected buffer)
-  "Assert that BUFFER has message EXPECTED."
+  "Assert that message EXPECTED exists in BUFFER."
   (when (null (get-buffer buffer))
     (ert-fail
      (format
@@ -247,12 +256,12 @@ EXPECTED-FACE."
 
 (defun context-coloring-test-kill-buffer (buffer)
   "Kill BUFFER if it exists."
-  (if (get-buffer buffer) (kill-buffer buffer)))
+  (when (get-buffer buffer) (kill-buffer buffer)))
 
 (defun context-coloring-test-assert-face (level foreground &optional negate)
   "Assert that a face for LEVEL exists and that its `:foreground'
-is FOREGROUND."
-  (let* ((face (context-coloring-face-symbol level))
+is FOREGROUND, or the inverse if NEGATE is non-nil."
+  (let* ((face (context-coloring-level-face level))
          actual-foreground)
     (when (not (or negate
                    face))
@@ -271,7 +280,8 @@ is FOREGROUND."
 
 (defun context-coloring-test-assert-not-face (&rest arguments)
   "Assert that LEVEL does not have a face with `:foreground'
-FOREGROUND."
+FOREGROUND.  Apply ARGUMENTS to
+`context-coloring-test-assert-face', see that function."
   (apply 'context-coloring-test-assert-face
          (append arguments '(t))))
 
@@ -286,32 +296,6 @@ FOREGROUND."
     "Context coloring is not available for this major mode"
     "*Messages*")))
 
-(ert-deftest context-coloring-test-set-colors ()
-  ;; This test has an irreversible side-effect in that it defines faces beyond
-  ;; 7.  Faces 0 through 7 are reset to their default states, so it might not
-  ;; matter, but be aware anyway.
-  (context-coloring-set-colors
-   "#000000"
-   "#111111"
-   "#222222"
-   "#333333"
-   "#444444"
-   "#555555"
-   "#666666"
-   "#777777"
-   "#888888"
-   "#999999")
-  (context-coloring-test-assert-face 0 "#000000")
-  (context-coloring-test-assert-face 1 "#111111")
-  (context-coloring-test-assert-face 2 "#222222")
-  (context-coloring-test-assert-face 3 "#333333")
-  (context-coloring-test-assert-face 4 "#444444")
-  (context-coloring-test-assert-face 5 "#555555")
-  (context-coloring-test-assert-face 6 "#666666")
-  (context-coloring-test-assert-face 7 "#777777")
-  (context-coloring-test-assert-face 8 "#888888")
-  (context-coloring-test-assert-face 9 "#999999"))
-
 (defvar context-coloring-test-theme-index 0
   "Unique index for unique theme names.")
 
@@ -326,8 +310,8 @@ FOREGROUND."
 (defun context-coloring-test-assert-theme-originally-set-p
     (settings &optional negate)
   "Assert that `context-coloring-theme-originally-set-p' returns
-t for a theme with SETTINGS (or the inverse if NEGATE is
-non-nil)."
+t for a theme with SETTINGSor the inverse if NEGATE is
+non-nil."
   (let ((theme (context-coloring-test-get-next-theme)))
     (put theme 'theme-settings settings)
     (when (funcall (if negate 'identity 'not)
@@ -341,7 +325,9 @@ non-nil)."
 
 (defun context-coloring-test-assert-not-theme-originally-set-p (&rest arguments)
   "Assert that `context-coloring-theme-originally-set-p' does not
-return t for a theme with SETTINGS."
+return t for a theme with SETTINGS.  Apply ARGUMENTS to
+`context-coloring-test-assert-theme-originally-set-p', see that
+function."
   (apply 'context-coloring-test-assert-theme-originally-set-p
          (append arguments '(t))))
 
@@ -368,7 +354,8 @@ EXPECTED-LEVEL."
 
 (defun context-coloring-test-assert-theme-highest-level
     (theme expected-level &optional negate)
-  "Assert that THEME has the highest level EXPECTED-LEVEL."
+  "Assert that THEME has the highest level EXPECTED-LEVEL, or the
+inverse if NEGATE is non-nil."
   (let ((highest-level (context-coloring-theme-highest-level theme)))
     (when (funcall (if negate 'identity 'not) (eq highest-level expected-level))
       (ert-fail (format (concat "Expected theme with settings `%s' "
@@ -379,7 +366,10 @@ EXPECTED-LEVEL."
                         (if negate "did" (format "was %s" highest-level)))))))
 
 (defun context-coloring-test-assert-theme-not-highest-level (&rest arguments)
-  "Assert that THEME's highest level is not EXPECTED-LEVEL."
+  "Assert that THEME's highest level is not EXPECTED-LEVEL.
+Apply ARGUMENTS to
+`context-coloring-test-assert-theme-highest-level', see that
+function."
   (apply 'context-coloring-test-assert-theme-highest-level
          (append arguments '(t))))
 
@@ -404,9 +394,10 @@ EXPECTED-LEVEL."
   )
 
 (defmacro context-coloring-test-deftest-define-theme (name &rest body)
-  "Define a test with an automatically-generated theme symbol
-available as a free variable `theme'.  Side-effects from enabling
-themes are reversed after the test completes."
+  "Define a test with name NAME and an automatically-generated
+theme symbol available as a free variable `theme'.  Side-effects
+from enabling themes are reversed after BODY is executed and the
+test completes."
   (declare (indent defun))
   (let ((deftest-name (intern
                        (format "context-coloring-test-define-theme-%s" name))))
@@ -417,8 +408,7 @@ themes are reversed after the test completes."
              (progn
                ,@body)
            ;; Always cleanup.
-           (disable-theme theme)
-           (context-coloring-set-colors-default))))))
+           (disable-theme theme))))))
 
 (defun context-coloring-test-deftheme (theme)
   "Dynamically define theme THEME."
@@ -563,21 +553,24 @@ theme THEME is signaled."
   (context-coloring-test-assert-face 0 "#aaaaaa")
   (context-coloring-test-assert-face 1 "#bbbbbb"))
 
-(defun context-coloring-test-assert-face-count (count &optional negate)
-  "Assert that `context-coloring-face-count' is COUNT."
+(defun context-coloring-test-assert-maximum-face (maximum &optional negate)
+  "Assert that `context-coloring-maximum-face' is MAXIMUM, or the
+inverse if NEGATE is non-nil."
   (when (funcall (if negate 'identity 'not)
-                 (eq context-coloring-face-count count))
-    (ert-fail (format (concat "Expected `context-coloring-face-count' "
+                 (eq context-coloring-maximum-face maximum))
+    (ert-fail (format (concat "Expected `context-coloring-maximum-face' "
                               "%sto be `%s', "
                               "but it %s.")
-                      (if negate "not " "") count
+                      (if negate "not " "") maximum
                       (if negate
                           "was"
-                        (format "was `%s'" context-coloring-face-count))))))
+                        (format "was `%s'" context-coloring-maximum-face))))))
 
-(defun context-coloring-test-assert-not-face-count (&rest arguments)
-  "Assert that `context-coloring-face-count' is not COUNT."
-  (apply 'context-coloring-test-assert-face-count
+(defun context-coloring-test-assert-not-maximum-face (&rest arguments)
+  "Assert that `context-coloring-maximum-face' is not MAXIMUM.
+Apply ARGUMENTS to `context-coloring-test-assert-maximum-face',
+see that function."
+  (apply 'context-coloring-test-assert-maximum-face
          (append arguments '(t))))
 
 (context-coloring-test-deftest-define-theme disable-cascade
@@ -608,17 +601,18 @@ theme THEME is signaled."
       (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-face-count 3))
+      (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-face-count 2))
+    (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-face-count 2))
+  (context-coloring-test-assert-not-maximum-face 1))
 
 (defun context-coloring-test-js-function-scopes ()
+  "Test fixtures/functions-scopes.js."
   (context-coloring-test-assert-region-level 1 9 0)
   (context-coloring-test-assert-region-level 9 23 1)
   (context-coloring-test-assert-region-level 23 25 0)
@@ -636,6 +630,7 @@ theme THEME is signaled."
 (context-coloring-test-deftest-js2-mode function-scopes)
 
 (defun context-coloring-test-js-global ()
+  "Test fixtures/global.js."
   (context-coloring-test-assert-region-level 20 28 1)
   (context-coloring-test-assert-region-level 28 35 0)
   (context-coloring-test-assert-region-level 35 41 1))
@@ -644,6 +639,7 @@ theme THEME is signaled."
 (context-coloring-test-deftest-js2-mode global)
 
 (defun context-coloring-test-js-block-scopes ()
+  "Test fixtures/block-scopes.js."
   (context-coloring-test-assert-region-level 20 64 1)
    (setq context-coloring-js-block-scopes t)
    (context-coloring-colorize)
@@ -655,6 +651,7 @@ theme THEME is signaled."
 (context-coloring-test-deftest-js2-mode block-scopes)
 
 (defun context-coloring-test-js-catch ()
+  "Test fixtures/js-catch.js."
   (context-coloring-test-assert-region-level 20 27 1)
   (context-coloring-test-assert-region-level 27 51 2)
   (context-coloring-test-assert-region-level 51 52 1)
@@ -668,12 +665,14 @@ theme THEME is signaled."
 (context-coloring-test-deftest-js2-mode catch)
 
 (defun context-coloring-test-js-key-names ()
+  "Test fixtures/key-names.js."
   (context-coloring-test-assert-region-level 20 63 1))
 
 (context-coloring-test-deftest-js-mode key-names)
 (context-coloring-test-deftest-js2-mode key-names)
 
 (defun context-coloring-test-js-property-lookup ()
+  "Test fixtures/property-lookup.js."
   (context-coloring-test-assert-region-level 20 26 0)
   (context-coloring-test-assert-region-level 26 38 1)
   (context-coloring-test-assert-region-level 38 44 0)
@@ -685,12 +684,14 @@ theme THEME is signaled."
 (context-coloring-test-deftest-js2-mode property-lookup)
 
 (defun context-coloring-test-js-key-values ()
+  "Test fixtures/key-values.js."
   (context-coloring-test-assert-region-level 78 79 1))
 
 (context-coloring-test-deftest-js-mode key-values)
 (context-coloring-test-deftest-js2-mode key-values)
 
 (defun context-coloring-test-js-comments-and-strings ()
+  "Test fixtures/comments-and-strings.js."
   (context-coloring-test-assert-region-comment-delimiter 1 4)
   (context-coloring-test-assert-region-comment 4 8)
   (context-coloring-test-assert-region-comment-delimiter 9 12)