etest.texinfo: A few details about etest-execute.el.
[ETest.git] / etest.texinfo
blobbcbf64db7f21ef161f22e887ae678f45008f174e
1 \input texinfo   @c -*-texinfo-*-
3 @setfilename etest.info
4 @settitle Emacs Testing Framework
6 @dircategory Emacs
7 @direntry
8 * ETest: (etest).           The Emacs Testing Framework
9 @end direntry
11 @copying
12 Copyright @copyright{} 2008 Philip Jackson.
13 @quotation
14 Permission is granted to copy, distribute and/or modify this document
15 under the terms of the GNU Free Documentation License, Version 1.1 or
16 any later version published by the Free Software Foundation; with no
17 Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A
18 copy of the license is included in the section entitled "GNU Free
19 Documentation License".
20 @end quotation
21 @end copying
23 @titlepage
24 @title ETest - The Emacs Testing Framework
26 @page
27 @vskip 0pt plus 1filll
28 @insertcopying
29 @end titlepage
31 @ifhtml
32 @contents
33 @end ifhtml
35 @ifnottex
36 @node Top, Introduction, (dir), (dir)
37 @top ETest Manual 0.1
39 @menu
40 * Introduction::                Introduction to ETest.
41 * Fetching ETest::              How to download ETest.
42 * Installation::                How to get emacs to wear ETest.
43 * Usage::                       How to use ETest.
44 * The results buffer::          Display of test results.
45 * The Tests::                   Functions available for testing.
46 @end menu
47 @end ifnottex
49 @node Introduction, Fetching ETest, Top, Top
50 @chapter Introduction
52 ETest (or etest, if you like) is the Emacs Testing Framework. It is a
53 small modular system for writing unit tests in Emacs Lisp.
55 At time of writing ETest consists of two files. @file{etest.el}
56 provides the core test functionality. It defines the function
57 (actually, macro) @code{etest} which is usually one's entrance into a
58 test run. The file @file{etest-result-mode.el} adds functions that
59 allow the visualisation of a run with syntax highlighting, folding and
60 comment toggling.
62 @node Fetching ETest, Installation, Introduction, Top
63 @chapter Fetching ETest
65 At the time of writing you must download ETest using git:
67 @code{git clone http://repo.or.cz/r/ETest.git etest}
69 Though it will always be bleeding edge @code{master} should always be
70 in a @emph{working} state.
72 @node Installation, Usage, Fetching ETest, Top
73 @chapter Installation
75 To install ETest use one of the following methods:
77 @section Compiling ETest
79 Once you've fetched ETest change into the new directory and run
80 @code{make} if all goes well run @code{make install} (you may need
81 escalated privileges for this step).
83 @section Manual Installation
85 If you don't want to byte-compile for some reason then you can just
86 copy the @code{.el} files into a directory in your @var{load-path}.
88 If you would rather add the unpacked library @emph{to} your load path
89 then the following will work assuming the etest directory is in your
90 home directory:
92 @lisp
93 (add-to-list 'load-path "~/etest")
94 @end lisp
96 @node Usage, The results buffer, Installation, Top
97 @chapter Usage
99 First you must evaluate @code{(require 'etest)} then you can start
100 using the @code{etest} macro.
102 Using ETest is very simple. Once you have installed the modules then
103 you can simply run your first test like this:
105 @lisp
106 (etest (ok 1))
107 @end lisp
108 @noindent
110 This should pop up a results buffer showing you the outcome of the
111 run. In this case all should be ok because, well, 1 is a pass
112 according to the @code{ok} test.
114 @menu
115 * Example working practice::    How one might use etest.
116 @end menu
118 @node Example working practice
119 @section Example working practice
121 Generally (at least the way I work) is to have a @code{.etest} file
122 which matches all but the extension of the filename of the file I am
123 to test. So if I were testing a file called @code{find-cmd.el} I would
124 write my tests in a file called @code{find-cmd.etest} (in the same
125 directory as @code{find-cmd.el}) and then when I hit @kbd{C-c t} (in
126 @code{emacs-lisp-mode}) the run would happen and the results pop up in
127 their normal fashion.
129 Thanks to @file{etest-execute.el} there are several options for the
130 location of an etest file (one at a time). In order of execution they
131 are:
133 1. The buffer local variable @code{etest-file} is set to the filename
134 of the etest file.
136 2. A matching etest file is in the directories listed in
137 @code{etest-load-path}.
139 3. A matching etest file is in the current working directory
140 (@code{default-directory}).
142 @node The results buffer, The Tests, Usage, Top
143 @chapter The results buffer
145 The results buffer is where you can see (and manipulate) the results
146 of a test run in a human friendly format. It will always popup when
147 etest is run and let you know how things went.
149 @menu
150 * Example output::              What the output looks like.
151 * Bindings::                    What commands you have at your disposal.
152 @end menu
154 @node Example output, Bindings, The results buffer, The results buffer
155 @section Example output
157 Given the hypothetical tests:
159 @lisp
160 (etest
161  ("Numeric comparisons"
162   ("Integers"
163    (ok (> 1 0) "one is more than 0")
164    (ok (< 1 0) "one is less than 0"))
165   ("Floats"
166    (ok (> 10 9.99))
167    (ok (< 1 -5.2) "one is less than 5.2"))))
168 @end lisp
169 @noindent
171 Once run give us the following in the results buffer:
173 @example
174 * Numeric comparisons
175 ** Integers
176    ok ................ one is more than 0
177                        # got: 't'
178    not ok ............ one is less than 0
179                        # got: 'nil'
180 ** Floats
181    ok ................ (ok (> 10 9.99))
182                        # got: 't'
183    not ok ............ one is less than 5.2
184                        # got: 'nil'
185 @end example
186 @noindent
188 All headings are foldable as are comments.
190 @node Bindings,  , Example output, The results buffer
191 @section Bindings
193 @table @kbd
194 @item q
195 @findex bury-buffer
196 Bury this results buffer.
197 @item #
198 @findex etest-rm-etest-rm-cycle-comments
199 Shift the values in @code{etest-rm-comment-visibility-types} and use
200 the @code{car} of that list to determine the visibility of comments.
201 @item <tab>
202 @findex etest-rm-toggle-headline
203 Toggle the visibility of a heading or test comment.
204 @end table
206 @node The Tests,  , The results buffer, Top
207 @chapter The Tests
209 Tests are always run within the @code{etest} form and usually always
210 evaluate their arguments. Tests will always have a defined number of
211 required arguments (for example @code{ok} requires one argument. Each
212 test also allows for one optional argument which is a custom
213 documentation string. If this argument is omitted then ETest will
214 generate one in its place. So, for example, if you used
215 @code{(etest (ok 1))} the doc string would be @code{"(ok 1)"} if you used
216 @code{(etest (ok 1 "Foo"))} the doc string would be @code{"Foo"}.
218 @menu
219 * Test Structure::              Basic structure of tests.
220 * Builtin Simple Tests::        Boolean checks.
221 * Builtin Equality Tests::      Are two things similar?
222 * Builtin Error Tests::         Test exception handling.
223 * Builtin String Tests::        Check a string matches an RE.
224 * Defining your own tests::     Extend ETest.
225 @end menu
227 @node Test Structure, Builtin Simple Tests, The Tests, The Tests
228 @section Test Structure
230 Tests can be grouped within headings by simply using a string as the
231 first element of a form like this:
233 @lisp
234 (etest
235  ("Simple tests"
236   (ok 1)))
237 @end lisp
238 @noindent
240 You can nest headings to your hearts content.
242 This will produce a set of results that follow the same hierarchical
243 pattern as the tests themselves. For example the above produces the
244 following structure:
246 @lisp
247 (("Simple tests"
248   (:result t :comments "got: '1'" :doc "(ok 1)")))
249 @end lisp
250 @noindent
252 The output in the results buffer is:
254 @example
255 * Simple tests
256   ok ................. (ok 1)
257                        # got: '1'
258 @end example
260 @node Builtin Simple Tests, Builtin Equality Tests, Test Structure, The Tests
261 @section Builtin Simple Tests
263 These basic tests allow you, basically, to check if a value is either
264 non-nil or nil.
266 @subsection ok
268 @code{ok} will only pass if its argument produces a non-nil result.
270 @lisp
271 (etest (ok (+ 1 1)))
272 @end lisp
273 @noindent
275 @subsection null
277 @code{null} will only pass if its argument produces a nil result.
279 @lisp
280 (etest (null nil))
281 @end lisp
282 @noindent
284 @node Builtin Equality Tests, Builtin Error Tests, Builtin Simple Tests, The Tests
285 @section Builtin Equality Tests
287 The following functions map to their lisp counterparts and so don't
288 really require much explanation. Evaluate each of the examples to
289 watch them pass.
291 Each take two forms which, post evaluation, are the objects to
292 compare.
294 @subsection eq
296 @lisp
297 (etest (eq 1 1))
298 @end lisp
299 @noindent
301 @subsection eql
303 @lisp
304 (etest (eql 1.1 1.1))
305 @end lisp
306 @noindent
308 @subsection equal
310 @lisp
311 (etest (equal '(1 2) '(1 2)))
312 @end lisp
313 @noindent
315 @node Builtin Error Tests, Builtin String Tests, Builtin Equality Tests, The Tests
316 @section Builtin Error Tests
318 These two functions each take one form.
320 @subsection error
322 This test will pass if an exception is raised. For example, cause a
323 divide by zero error (@code{(arith-error)}):
325 @lisp
326 (etest (error (/ 1 0)))
327 @end lisp
328 @noindent
330 @subsection noerror
332 This test will pass if no exception is raised. For example, a valid
333 division will not raise an error:
335 @lisp
336 (etest (noerror (/ 0 1)))
337 @end lisp
338 @noindent
340 @node Builtin String Tests, Defining your own tests, Builtin Error Tests, The Tests
341 @section Builtin String Tests
343 @subsection like
345 @code{like} takes two arguments a string and a regexp to test it against:
347 @lisp
348 (etest (like "Hello" "^H\\(e\\)"))
349 @end lisp
350 @noindent
352 Produces this in the results buffer:
354 @example
355  ok .................. (like "Hello" "^H\\(e\\)")
356                        # searching: 'Hello'
357                        # match   1: 'e'
358 @end example
359 @noindent
361 The grouping within the regular expression only affects the comments.
363 @node Defining your own tests,  , Builtin String Tests, The Tests
364 @section Defining your own tests
366 Defining your own tests is fairly trivial and where ETest becomes
367 really useful.
369 Each test must return a plist that has @code{:result} and, optionally,
370 @code{:comments} in it.
372 @code{:result} represents whether the test passed or failed non-nil
373 for a pass and nil for a fail.
375 The optional @code{:comments} are newline separated strings that might
376 help the user in their diagnosis of a problem. Comments should follow
377 the conventions set by the 'builtin' tests using keywords such as
378 'got:'.
380 To have ETest recognise the test as valid the @code{deftest} function
381 should be used. For example if I wanted to create a function that
382 took two arguments and tested the first was numerically greater than
383 the other I might do this:
385 @lisp
386 (defun etest-greater-than (one two)
387   (let* ((res (> one two)))
388     (list :result res
389           :comments (unless res
390                       (format "one: '%d'\ntwo: '%d'" one two)))))
391 @end lisp
392 @noindent
394 We let emacs take care of type errors (and any other type of error)
395 which is what all of the builtins do. Also it's worth noting that if
396 you want your arguments evaling you have to do it yourself.
398 Now we let ETest know this new function exists:
400 @lisp
401 (deftest '(> 2) 'etest-greater-than)
402 @end lisp
403 @noindent
405 So, the new function will be called @code{>}, it will take two arguments
406 and it calls maps to @code{etest-greater-than}.
408 @lisp
409 (deftest '(> 2) 'etest-greater-than)
410 @end lisp
411 @noindent
413 Now you can mix @code{>} with other tests:
415 @lisp
416 (etest
417  (ok "something")
418  (> 1 2 "one is more than two"))
419 @end lisp
420 @noindent
422 Which in the results buffer produces:
424 @example
425  ok .................. (ok "something")
426                        # got: '"something"'
427  not ok .............. one is more than two
428                        # one: '1'
429                        # two: '2'
430 @end example
431 @noindent
433 @bye