Skip to content
Kazuki Yoshida edited this page Feb 22, 2021 · 3 revisions

FAQ

I upgraded to buttercup 1.9 and my self-written matchers break, why?

Buttercup 1.9 changed the way matchers work by turning arguments into closures. This allows the new syntax for :to-raise to simply accept an expression instead of requiring a lambda form. Sadly, this requires you to change the way you write matchers.

To get the real value of arguments passed to your matcher, you have to call funcall on them.

Example from lua-mode, see Debian bug #877398: Before:

(defun to-be-fontified-as (text faces)
  (let ((expected-faces (lua-mk-font-lock-faces faces))
        (result-faces (lua-get-line-faces text))
        (lineno 1))
    (when (/= (length expected-faces) (length result-faces))
        (buttercup-fail "\
Fontification check failed for:
%S
  Text contains %d lines, face list contains %d lines"
                        text (length result-faces) (length expected-faces)))
    (while expected-faces
      (unless (equal (car expected-faces) (car result-faces))
        (buttercup-fail "\
Fontification check failed on line %d for:
%S
  Result faces:   %S
  Expected faces: %S"
                        lineno text (car expected-faces) (car result-faces)))
      (setq expected-faces (cdr expected-faces)
            result-faces (cdr result-faces)
            lineno (1+ lineno)))
    (cons t "Fontification check passed")))

has been replaced with

(defun to-be-fontified-as (text faces)
  (let ((expected-faces (lua-mk-font-lock-faces (funcall faces)))
        (result-faces (lua-get-line-faces (funcall text)))
        (lineno 1))
    (when (/= (length expected-faces) (length result-faces))
        (buttercup-fail "\
Fontification check failed for:
%S
  Text contains %d lines, face list contains %d lines"
                        (funcall text) (length result-faces) (length expected-faces)))
    (while expected-faces
      (unless (equal (car expected-faces) (car result-faces))
        (buttercup-fail "\
Fontification check failed on line %d for:
%S
  Result faces:   %S
  Expected faces: %S"
                        lineno (funcall text) (car expected-faces) (car result-faces)))
      (setq expected-faces (cdr expected-faces)
            result-faces (cdr result-faces)
            lineno (1+ lineno)))
    (cons t "Fontification check passed")))

How do I test a major mode for indentation?

You can use a pattern involving with-temp-buffer, major-mode, and indent-region and compare it to a gold standard. The following is used in stan-mode.

;; Adopted from https://github.com/Groovy-Emacs-Modes/groovy-emacs-modes/tree/master/test
(defun test-stan--indent-string (source)
  "Indent string assuming it is a stan file content.
Returns indented version of SOURCE."
  (with-temp-buffer
    (insert source)
    (stan-mode)
    (shut-up
      (indent-region (point-min) (point-max)))
    (buffer-substring-no-properties (point-min) (point-max))))