Previous: , Up: How to Write Tests   [Contents][Index]

3.6 Syntax Highlighting Tests

Syntax highlighting is normally provided by the Font Lock minor mode that assigns face properties to parts of the buffer. The ert-font-lock package makes it possible to introduce unit tests checking face assignment. Test assertions are included in code-level comments directly and can be read either from inline strings or files. The parser expects the input string to contain at least one assertion.

Test assertion parser extracts tests from comment-only lines. Every comment assertion line starts either with a caret (‘^’) or an arrow (‘<-’). A single caret/arrow or carets should be followed immediately by the name of a face or a list of faces to be checked against the :face property at point.

The test then checks if the first non-assertion column above the caret contains a face expected by the assertion:

var variable = 11;
//   ^ font-lock-variable-name-face
//             ^ font-lock-literal-face
//               ^ font-lock-punctuation-face
// this is not an assertion, it's just a comment
//   ^ font-lock-comment-face

// multiple carets per line
// ^^^^     ^    ^ font-lock-comment-face

Both symbol-only :face property values and assertion face values are normalized to single element lists so assertions below are equivalent:

// single
// ^ font-lock-comment-face
// single
// ^ (font-lock-comment-face)

Assertions can be negated:

var variable = 11;
//  ^ !font-lock-comment-face

It is possible to specify face lists in assertions:

// TODO
// ^^^^ (font-lock-comment-face hl-todo)
     var test = 1;
// ^    ()
// ^    nil
//   negation works as expected
//   ^  !nil

The arrow (‘<-’) means that the first non-empty column of the assertion line will be used for the check:

var variable = 1;
// <- font-lock-keyword-face
  11;
   // <- font-lock-literal-face

The ert-font-lock-test-string function extracts ERT assertions from an inline string. The javascript-mode symbol below specifies the major mode used for comments and font locking:

(ert-deftest test-font-lock-test-string--correct ()
  (ert-font-lock-test-string
   "
var abc = function(d) {
// <- font-lock-keyword-face
//   ^ font-lock-variable-name-face
    //        ^ font-lock-keyword-face
    //             ^ font-lock-variable-name-face
};
"
   'javascript-mode))

It is also possible to extract test assertions from a file:

(ert-deftest test-font-lock-test-file--correct ()
  (ert-font-lock-test-file
   (ert-resource-file "correct.js")
   'javascript-mode))

The ert-font-lock-deftest macro simplifies inline test definition:

(ert-font-lock-deftest test-macro-test--inline
    emacs-lisp-mode
  "
(defun fun ())
;; ^ font-lock-keyword-face
;;      ^ font-lock-function-name-face")

The ert-font-lock-deftest-file macro reads assertions from a file:

(ert-font-lock-deftest-file test-macro-test--file
    "Test reading correct assertions from a file"
  javascript-mode
  "correct.js")

The ert-font-lock-deftest and ert-font-lock-deftest-file macros accept the same keyword parameters as ert-deftest i.e., :tag and :expected-result.

Previous: erts files, Up: How to Write Tests   [Contents][Index]