0

I'm trying to figure out how I can use ERT and libraries like el-mock to test simple functions that manipulate my org-document.

Consider a function like this:

(defun promote-next-heading ()
  (org-next-visible-heading 1)
  (org-metaright))

How would I write a test for this? Should I have a .org file dedicated for tests like these? And then when the headline gets promoted, how do I confirm this? And then revert it for the next test?

While the example above is contrived, I'm writing quite a few functions that manipulate or query my org document, and I really would feel better if I could test for it.

1

One way is by string comparisons, e.g.

(require 'ert)

(ert-deftest test-1 ()
  (should
   (string=
    (with-temp-buffer
      (org-mode)
      (insert "

* test heading
")
      (goto-char (point-min))
      (promote-next-heading)
      (buffer-string))
    "

** test heading
")))

You could also have content in files and do it like this:

(ert-deftest test-2 ()
  (should
   (string=
    (with-temp-buffer
      (insert-file-contents "ref-1.org")
      (org-mode)
      (goto-char (point-min))
      (my-promote-next-heading)
      (buffer-string))
    ;; this file is what the transformed text should look like.
    (with-temp-buffer
      (insert-file-contents "ref-2.org")
      (buffer-string)))))

You run the tests with M-x ert

  • I don't find it an elegant solution, however, at the moment I think it serves my purposes. I wish Org Mode had functions that took in text rather than rely on where my point is - leads to a lot of boilerplate in my tests: Creating buffers, moving the point, etc. Perhaps I can abstract it into a function/macro. – Beetle B. May 24 at 19:12
  • you might find some abstractions at repo.or.cz/org-mode.git/tree/HEAD:/testing For some reason these aren't on my load path, or I would have pointed to them. – John Kitchin May 24 at 21:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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