help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Why isn't ert-buffer.el part of Emacs?


From: Thorsten Jolitz
Subject: Why isn't ert-buffer.el part of Emacs?
Date: Fri, 13 Jun 2014 16:54:51 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)

Hi List, 

I recently discovered 'ert-buffer.el' and find it very useful. It is
introduced and discussed here:

http://emacs.1067599.n5.nabble.com/Running-ert-tests-on-buffers-in-rst-el-and-elsewhere-td217849.html

I wonder why such a generally useful library isn't included in Emacs (I
don't even find it in the package repos)? It allows to easily compare
buffer content, point and mark position before and after some action.

E.g. for testing the outorg conversion between programming-modes and
org-mode, I used it for a kind of 'do/undo test' (name coined by me)
that really saves me a lot of test-writing work:

It works like this:

#+begin_src emacs-lisp
(ert-deftest outorg-test-conversion ()
  "Test outorg conversion to and from Org."
  (let ((curr-buf-initial-state
         (with-current-buffer "*outorg-test-buffer*"
           (ert-Buf-from-buffer))))
    (should
     (ert-equal-buffer
      (outorg-test-cmd)
      curr-buf-initial-state
      t))))
#+end_src

With `ert-Buf-from-buffer' I can take a snapshot of the buffer before
conversion (-> curr-buf-initial-state), and the 't' in 

#+BEGIN_SRC emacs-lisp
  (ert-equal-buffer
   (outorg-test-cmd)
   curr-buf-initial-state
   t)
#+END_SRC

says that this same initial snapshot is used for comparison with the
buffer after the test.

The `outorg-test-cmd' does the following steps:

 1. convert programming-mode buffer to org (in temp buffer)
 2. activate undo-tree-mode
 3. call some given org command interactively
 4. store the buffer-undo-tree
 5. copy changes from modified org buffer back to programming-mode
    buffer
 6. convert programming-mode buffer to (temp) org buffer again
    (i.e. repeat step 1.)
 7. set saved undo-tree as buffer-undo-tree of new temp-buffer
 8. call undo-tree-undo to revert all changes
 9. copy changes from modified org buffer back to programming-mode
    buffer (repeat step 5.)

When the conversion itself has no permanent side-effects, the
programming-mode buffer should look exactly as before after these
steps, no matter what org command was called in the temporary org-mode
edit buffer. 

I would have had to reimplement 'ert-buffer.el' for this if it had not
existed already, and I can imagine others find themselves in the same
situation. 

PS

I had to introduce a few hacks because inside

#+BEGIN_SRC emacs-lisp
(ert-deftest outorg-test-conversion ()
  "Test outorg conversion to and from Org."
  (let ((curr-buf-initial-state
         (with-current-buffer "*outorg-test-buffer*"
           (ert-Buf-from-buffer)))) ...))
#+END_SRC

it seems not possible to get info from current-buffer (the buffer that
is current when `ert-run-tests-interactively' is called), because
(current-buffer) inside the `let' expression is already a different
(temp) buffer, and the empty argument list () seems to be there only
for cosmetic reasons. 

I ended up using hardcoded references to global variables and names,
and writing a wrapper function `outorg-test-run-ert' that would set
these globals first and then call `ert-run-tests-interactively', but
that feels like a hack. Is there a way to avoid it?

-- 
cheers,
Thorsten




reply via email to

[Prev in Thread] Current Thread [Next in Thread]