I would like to unit test my font-face keyword rules against some source code data sets.

It sounds like it should be quite easy: open the source file, enable the mode, force fontification, dump out the properties to a file and then compare. However, it is only possible (AFAICT) to font-lock-fontify-buffer which has an ambiguous region... it may just be the visible region.

Is there a way to force fontification of every single character in the buffer, even when it is too large to view? Or are the docs just ambiguous, but it actually does what I want?

The ideal situation would be if somebody has already written a test framework for this, in which case, please let me know!

Maybe I could hack it with a custom font-lock-mark-block-function or font-lock-fontify-region-function to select the entire buffer just for the test, but that limits my ability to test user interactions.

  • 1
    (font-lock-ensure (point-min) (point-max))? Nov 5, 2018 at 21:57
  • @CroadLangshan thank you! I may be overthinking this :-D
    – fommil
    Nov 5, 2018 at 21:58
  • 1
    Have a look at the comment in the declare for font-lock-fontify-buffer in font-lock.el Nov 5, 2018 at 21:59
  • @CroadLangshan LOL
    – fommil
    Nov 5, 2018 at 22:07

2 Answers 2


There is indeed an existing test framework: https://github.com/Lindydancer/faceup

Make sure to check out the author's other packages, many of them are related to the intricacies of font-lock.


groovy-mode has several unit tests for font face rules. There's a macro for ensuring the whole buffer is fontified, then we check the face at different positions in the resulting buffer.

(defmacro with-highlighted-groovy (src &rest body)
  "Insert SRC in a temporary groovy-mode buffer, apply syntax highlighting,
then run BODY."
  (declare (indent 1) (debug t))
     (insert ,src)
     (goto-char (point-min))
     ;; Activate groovy-mode, but don't run any hooks. This doesn't
     ;; matter on Travis, but is defensive when running tests in the
     ;; current Emacs instance.
     (delay-mode-hooks (groovy-mode))
     ;; Ensure we've syntax-highlighted the whole buffer.
     (if (fboundp 'font-lock-ensure)

(ert-deftest groovy-highlight-triple-double-quote ()
  ;; Ensure we handle single " correctly inside a triple-double-quoted string.
  (with-highlighted-groovy "x = \"\"\"foo \" bar \"\"\""
    (search-forward "bar")
    (should (eq (face-at-point) 'font-lock-string-face))))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.