unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Why isn't ert-buffer.el part of Emacs?
@ 2014-06-13 14:54 Thorsten Jolitz
  2014-06-15  1:43 ` Stefan Monnier
  0 siblings, 1 reply; 13+ messages in thread
From: Thorsten Jolitz @ 2014-06-13 14:54 UTC (permalink / raw)
  To: help-gnu-emacs


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




^ permalink raw reply	[flat|nested] 13+ messages in thread
[parent not found: <mailman.3576.1402671325.1147.help-gnu-emacs@gnu.org>]

end of thread, other threads:[~2014-06-17 13:32 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-06-13 14:54 Why isn't ert-buffer.el part of Emacs? Thorsten Jolitz
2014-06-15  1:43 ` Stefan Monnier
2014-06-17 10:25   ` Thorsten Jolitz
2014-06-17 12:55     ` Stefan Monnier
2014-06-17 13:32       ` Thorsten Jolitz
     [not found] <mailman.3576.1402671325.1147.help-gnu-emacs@gnu.org>
2014-06-13 23:15 ` Emanuel Berg
2014-06-14  0:24   ` Grant Rettke
     [not found]   ` <mailman.3618.1402705500.1147.help-gnu-emacs@gnu.org>
2014-06-14  0:39     ` Emanuel Berg
2014-06-14  0:53     ` Emanuel Berg
2014-06-14  2:19       ` Grant Rettke
2014-06-14 12:22   ` Thorsten Jolitz
     [not found]   ` <mailman.3645.1402748578.1147.help-gnu-emacs@gnu.org>
2014-06-14 12:54     ` Emanuel Berg
2014-06-14 16:22       ` Drew Adams

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).