From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: phillip.lord@russet.org.uk (Phillip Lord) Newsgroups: gmane.emacs.devel Subject: Re: testing macros and fixtures Date: Tue, 13 Oct 2015 12:34:49 +0100 Message-ID: <87lhb7roh2.fsf@russet.org.uk> References: <87h9lvkcxx.fsf@russet.org.uk> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Trace: ger.gmane.org 1444736172 6301 80.91.229.3 (13 Oct 2015 11:36:12 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 13 Oct 2015 11:36:12 +0000 (UTC) Cc: emacs-devel To: Artur Malabarba Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Tue Oct 13 13:36:05 2015 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1Zlxs2-0001tI-Vz for ged-emacs-devel@m.gmane.org; Tue, 13 Oct 2015 13:35:55 +0200 Original-Received: from localhost ([::1]:33895 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zlxs2-0001Lt-4e for ged-emacs-devel@m.gmane.org; Tue, 13 Oct 2015 07:35:54 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:39940) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zlxr5-0001FZ-WE for emacs-devel@gnu.org; Tue, 13 Oct 2015 07:34:56 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Zlxr1-00007Y-36 for emacs-devel@gnu.org; Tue, 13 Oct 2015 07:34:55 -0400 Original-Received: from cheviot22.ncl.ac.uk ([128.240.234.22]:52839) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zlxr0-00007P-QA for emacs-devel@gnu.org; Tue, 13 Oct 2015 07:34:51 -0400 Original-Received: from smtpauth-vm.ncl.ac.uk ([10.8.233.129] helo=smtpauth.ncl.ac.uk) by cheviot22.ncl.ac.uk with esmtp (Exim 4.63) (envelope-from ) id 1Zlxqz-00079q-Fu; Tue, 13 Oct 2015 12:34:49 +0100 Original-Received: from jangai.ncl.ac.uk ([10.66.67.223] helo=localhost) by smtpauth.ncl.ac.uk with esmtpsa (TLSv1:AES128-SHA:128) (Exim 4.63) (envelope-from ) id 1Zlxqz-0002ij-Rq; Tue, 13 Oct 2015 12:34:49 +0100 In-Reply-To: (Artur Malabarba's message of "Tue, 13 Oct 2015 10:16:56 +0100") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x X-Received-From: 128.240.234.22 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:191447 Archived-At: Artur Malabarba writes: > 2015-10-12 22:13 GMT+01:00 Phillip Lord > >> ERT is quite nice, but one of the things that I have found lacking is a >> nice set of predicates, for use within should. >> >> So, when I wrote test for my "lentic" package I needed some functions >> like, so that I could do things like: >> >> (should >> (test-eq-after-this >> "blah-before.txt" >> "blah-after.txt" >> (insert "hello"))) >> >> which opens "blah-before.txt" runs (insert "hello") then compares the >> result with "blah-after.txt". > > I think that's a pretty specific use-case. So it's best to let people wri= te > their own macros for this. > > (defmacro should-eq-after-body (file-before file-after &rest body) > `(let ((bef > (with-temp-buffer > (insert-file-contents file-before) > ,@body > (buffer-string)))) > (should (string=3D bef (with-temp-buffer > (insert-file-contents file-after) > (buffer-string)))))) I'd imagine it's pretty common -- assuming that (insert "hello") is anything at all of course. >> My version of this also does a diff of the >> results if the two are not equal. > > This certainly seems very useful. Maybe ert should do this on *all* > multi-line string comparisons instead of doing its default > =E2=80=9Cdifferent-strings=E2=80=9D report (which, most of the time, just= says =E2=80=9Cstrings are > of different length=E2=80=9D). Yep. I use a function to replace "string=3D" that looks like this. (defun lentic-test-equal-loudly (a b) "Actually, this just tests equality and shouts if not." ;; change this to t to disable noisy printout (if lentic-test-quiet (string=3D a b) (if (string=3D a b) t (message "Results:\n%s\n:Complete\nShouldbe:\n%s\nComplete:" cloned-r= esults cloned-file) (let* ((a-buffer (generate-new-buffer "a")) (b-buffer (generate-new-buffer "b")) (a-file (make-temp-file (buffer-name a-buffer))) (b-file (make-temp-file (buffer-name b-buffer)))) (with-current-buffer a-buffer (insert a) (write-file a-file)) (with-current-buffer b-buffer (insert b) (write-file b-file)) (message "diff:%senddiff:" (with-temp-buffer (call-process "diff" nil (current-buffer) nil "-c" a-file b-file) (buffer-string)))) nil))) An egregious hack, of course, but it was quick and does what I need. > Yes, there are many packages that use custom-deisgned temp-buffers for > testing. In fact, most non-trivial packages do. I'd really like to see ert > offer a common interface for this. The difficulty for that is that each > package has very different needs when it comes to testing in buffers, so I > have no idea what this common interface could be. I think I'll put this next into my todo list. Maintaining the current (dreadful) infrastructure that I have for lentic is painful to say the least, and I think a lot of this should be reusuable. As you say, there are lots of examples. Phil