]> code.delx.au - gnu-emacs-elpa/blob - packages/test-simple/README.md
Merge commit '0cda39255827f283e7578cd469ae42daad9556a2' from js2-mode
[gnu-emacs-elpa] / packages / test-simple / README.md
1 [![Build Status](https://travis-ci.org/rocky/emacs-test-simple.png)](https://travis-ci.org/rocky/emacs-test-simple)
2
3 *test-simple.el* is :
4
5 * Simple -- no need for context macros, enclosing specifications, or required test tags. But if you want, you still can add custom assert failure messages or add notes before a group of tests.
6 * Accomodates both interactive and non-interactive use:
7 * For interactive use one can use `eval-last-sexp`, `eval-region`, and `eval-buffer`
8 * For non-interactive use run as: `emacs --batch --no-site-file --no-splash --load <test-lisp-code.el>`
9
10 I use this in my [Debugger front end](https://github.com/rocky/emacs-dbgr).
11
12 Here is an example found in the [examples directory](https://github.com/rocky/emacs-test-simple/tree/master/test).
13
14 In file `gcd.el`:
15
16 (defun gcd(a b)
17 "Greatest Common Divisor of A and B"
18 ;; Make a < b
19 (if (> a b)
20 (let ((c a))
21 (setq a b)
22 (setq b c)))
23 (cond
24 ((< a 0) nil)
25 ((or (= 0 (- b a)) (= a 1)) a)
26 (t (gcd (- b a) a))
27 )
28 )
29
30
31 In file `test-gcd.el` in the same directory:
32
33 (require 'test-simple)
34 (test-simple-start) ;; Zero counters and start the stop watch.
35
36 ;; Use (load-file) below because we want to always to read the source.
37 ;; Also, we don't want no stinking compiled source.
38 (assert-t (load-file "./gcd.el")
39 "Can't load gcd.el - are you in the right directory?" )
40
41 (note "degenerate cases")
42
43 (assert-nil (gcd 5 -1) "using positive numbers")
44 (assert-nil (gcd -4 1) "using positive numbers, switched order")
45 (assert-raises error (gcd "a" 32)
46 "Passing a string value should raise an error")
47
48 (note "GCD computations")
49 (assert-equal 1 (gcd 3 5) "gcd(3,5)")
50 (assert-equal 8 (gcd 8 32) "gcd(8,32)")
51
52 (end-tests) ;; Stop the clock and print a summary
53
54 Edit (with Emacs of course) `test-gcd.el` and run `M-x eval-current-buffer`
55
56 You should see in buffer `*test-simple*`:
57
58 test-gcd.el
59 ......
60 0 failures in 6 assertions (0.002646 seconds)
61
62 Now let's try from a command line:
63
64 $ emacs --batch --no-site-file --no-splash --load test-gcd.el
65 Loading /src/external-vcs/emacs-test-simple/example/gcd.el (source)...
66 *scratch*
67 ......
68 0 failures in 6 assertions (0.000723 seconds)
69
70 You can run noninteractive tests inside Emacs by `test-simple-run`.
71 Add the following at a test file:
72
73 ;;;; (test-simple-run "emacs -batch -L %s -l %s" (file-name-directory (locate-library "test-simple.elc")) buffer-file-name)
74
75 Press C-x C-e at the `test-simple-run` line to run this test file.
76 Then press C-x C-z, which is customizable by `test-simple-runner-key`, to run it more.
77 If you have installed `bpr` package, use it by default because it only pops up window when the running program exits abnormally.
78
79 `test-simple-run` can be called interactively.
80 In this case, the command line is set above as the simplest case.
81 But you run test with dependency, you must use the sexp comment form.
82
83 ;;;; (test-simple-run "emacs -batch -L %s -L %s -l %s" (file-name-directory (locate-library "test-simple.elc")) (file-name-directory (locate-library "foo")) buffer-file-name)
84
85 *Author:* Rocky Bernstein <rocky@gnu.org> <br>
86 [![endorse](https://api.coderwall.com/rocky/endorsecount.png)](https://coderwall.com/rocky)