all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Thorsten Jolitz <tjolitz@gmail.com>
To: help-gnu-emacs@gnu.org
Subject: Why isn't ert-buffer.el part of Emacs?
Date: Fri, 13 Jun 2014 16:54:51 +0200	[thread overview]
Message-ID: <87lht0kgg4.fsf@gmail.com> (raw)


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	other threads:[~2014-06-13 14:54 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-06-13 14:54 Thorsten Jolitz [this message]
2014-06-15  1:43 ` Why isn't ert-buffer.el part of Emacs? 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87lht0kgg4.fsf@gmail.com \
    --to=tjolitz@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.