Add ERT for table alignment within Org buffer
[org-mode.git] / testing / org-test.el
blob56bcb10c6687b5b07b561252d93914200a984b91
1 ;;;; org-test.el --- Tests for Org-mode
3 ;; Copyright (c) 2010-2012 Sebastian Rose, Eric Schulte
4 ;; Authors:
5 ;; Sebastian Rose, Hannover, Germany, sebastian_rose gmx de
6 ;; Eric Schulte, Santa Fe, New Mexico, USA, schulte.eric gmail com
7 ;; David Maus, Brunswick, Germany, dmaus ictsoc de
9 ;; Released under the GNU General Public License version 3
10 ;; see: http://www.gnu.org/licenses/gpl-3.0.html
12 ;; Definition of `special-mode' copied from Emacs23's simple.el to be
13 ;; provide a testing environment for Emacs22.
15 ;;;; Comments:
17 ;; Interactive testing for Org mode.
19 ;; The heart of all this is the commands `org-test-current-defun'. If
20 ;; called while in a `defun' all ert tests with names matching the
21 ;; name of the function are run.
23 ;;; Test Development
24 ;; For test development purposes a number of navigation and test
25 ;; function construction routines are available as a git submodule
26 ;; (jump.el)
27 ;; Install with...
28 ;; $ git submodule init
29 ;; $ git submodule update
32 ;;;; Code:
33 (require 'org-test-ob-consts)
35 (let* ((org-test-dir (expand-file-name
36 (file-name-directory
37 (or load-file-name buffer-file-name))))
38 (org-lisp-dir (expand-file-name
39 (concat org-test-dir "../lisp"))))
41 (unless (featurep 'org)
42 (setq load-path (cons org-lisp-dir load-path))
43 (require 'org)
44 (require 'org-id)
45 (org-babel-do-load-languages
46 'org-babel-load-languages '((sh . t) (org . t))))
48 (let* ((load-path (cons
49 org-test-dir
50 (cons
51 (expand-file-name "jump" org-test-dir)
52 load-path))))
53 (require 'cl)
54 (when (= emacs-major-version 22)
55 (defvar special-mode-map
56 (let ((map (make-sparse-keymap)))
57 (suppress-keymap map)
58 (define-key map "q" 'quit-window)
59 (define-key map " " 'scroll-up)
60 (define-key map "\C-?" 'scroll-down)
61 (define-key map "?" 'describe-mode)
62 (define-key map "h" 'describe-mode)
63 (define-key map ">" 'end-of-buffer)
64 (define-key map "<" 'beginning-of-buffer)
65 (define-key map "g" 'revert-buffer)
66 (define-key map "z" 'kill-this-buffer)
67 map))
69 (put 'special-mode 'mode-class 'special)
70 (define-derived-mode special-mode nil "Special"
71 "Parent major mode from which special major modes should inherit."
72 (setq buffer-read-only t)))
73 (require 'ert)
74 (require 'ert-x)
75 (when (file-exists-p
76 (expand-file-name "jump/jump.el" org-test-dir))
77 (require 'jump)
78 (require 'which-func))))
80 (defconst org-test-default-test-file-name "tests.el"
81 "For each defun a separate file with tests may be defined.
82 tests.el is the fallback or default if you like.")
84 (defconst org-test-default-directory-name "testing"
85 "Basename or the directory where the tests live.
86 org-test searches this directory up the directory tree.")
88 (defconst org-test-dir
89 (expand-file-name (file-name-directory (or load-file-name buffer-file-name))))
91 (defconst org-base-dir
92 (expand-file-name ".." org-test-dir))
94 (defconst org-test-example-dir
95 (expand-file-name "examples" org-test-dir))
97 (defconst org-test-file
98 (expand-file-name "normal.org" org-test-example-dir))
100 (defconst org-test-no-heading-file
101 (expand-file-name "no-heading.org" org-test-example-dir))
103 (defconst org-test-link-in-heading-file
104 (expand-file-name "link-in-heading.org" org-test-dir))
107 ;;; Functions for writing tests
108 (put 'missing-test-dependency
109 'error-conditions
110 '(error missing-test-dependency))
112 (defun org-test-for-executable (exe)
113 "Throw an error if EXE is not available.
114 This can be used at the top of code-block-language specific test
115 files to avoid loading the file on systems without the
116 executable."
117 (unless (reduce
118 (lambda (acc dir)
119 (or acc (file-exists-p (expand-file-name exe dir))))
120 exec-path :initial-value nil)
121 (signal 'missing-test-dependency (list exe))))
123 (defun org-test-buffer (&optional file)
124 "TODO: Setup and return a buffer to work with.
125 If file is non-nil insert it's contents in there.")
127 (defun org-test-compare-with-file (&optional file)
128 "TODO: Compare the contents of the test buffer with FILE.
129 If file is not given, search for a file named after the test
130 currently executed.")
132 (defmacro org-test-at-id (id &rest body)
133 "Run body after placing the point in the headline identified by ID."
134 (declare (indent 1))
135 `(let* ((id-location (org-id-find ,id))
136 (id-file (car id-location))
137 (visited-p (get-file-buffer id-file))
138 to-be-removed)
139 (save-window-excursion
140 (save-match-data
141 (org-id-goto ,id)
142 (setq to-be-removed (current-buffer))
143 (condition-case nil
144 (progn
145 (org-show-subtree)
146 (org-show-block-all))
147 (error nil))
148 (save-restriction ,@body)))
149 (unless visited-p
150 (kill-buffer to-be-removed))))
151 (def-edebug-spec org-test-at-id (form body))
153 (defmacro org-test-in-example-file (file &rest body)
154 "Execute body in the Org-mode example file."
155 (declare (indent 1))
156 `(let* ((my-file (or ,file org-test-file))
157 (visited-p (get-file-buffer my-file))
158 to-be-removed)
159 (save-window-excursion
160 (save-match-data
161 (find-file my-file)
162 (unless (eq major-mode 'org-mode)
163 (org-mode))
164 (setq to-be-removed (current-buffer))
165 (goto-char (point-min))
166 (condition-case nil
167 (progn
168 (outline-next-visible-heading 1)
169 (org-show-subtree)
170 (org-show-block-all))
171 (error nil))
172 (save-restriction ,@body)))
173 (unless visited-p
174 (kill-buffer to-be-removed))))
175 (def-edebug-spec org-test-in-example-file (form body))
177 (defmacro org-test-at-marker (file marker &rest body)
178 "Run body after placing the point at MARKER in FILE.
179 Note the uuidgen command-line command can be useful for
180 generating unique markers for insertion as anchors into org
181 files."
182 (declare (indent 2))
183 `(org-test-in-example-file ,file
184 (goto-char (point-min))
185 (re-search-forward (regexp-quote ,marker))
186 ,@body))
187 (def-edebug-spec org-test-at-marker (form form body))
189 (defmacro org-test-with-temp-text (text &rest body)
190 "Run body in a temporary buffer with Org-mode as the active
191 mode holding TEXT. If the string \"<point>\" appears in TEXT
192 then remove it and place the point there before running BODY,
193 otherwise place the point at the beginning of the inserted text."
194 (declare (indent 1))
195 (let ((inside-text (if (stringp text) text (eval text))))
196 `(with-temp-buffer
197 (org-mode)
198 ,(let ((point (string-match (regexp-quote "<point>") inside-text)))
199 (if point
200 `(progn (insert `(replace-match "" nil nil inside-text))
201 (goto-char ,(match-beginning 0)))
202 `(progn (insert ,inside-text)
203 (goto-char (point-min)))))
204 ,@body)))
205 (def-edebug-spec org-test-with-temp-text (form body))
207 (defmacro org-test-with-temp-text-in-file (text &rest body)
208 "Run body in a temporary file buffer with Org-mode as the active mode."
209 (declare (indent 1))
210 (let ((file (make-temp-file "org-test"))
211 (inside-text (if (stringp text) text (eval text)))
212 (results (gensym)))
213 `(let ((kill-buffer-query-functions nil) ,results)
214 (with-temp-file ,file (insert ,inside-text))
215 (find-file ,file)
216 (org-mode)
217 (setq ,results (progn ,@body))
218 (save-buffer) (kill-buffer (current-buffer))
219 (delete-file ,file)
220 ,results)))
221 (def-edebug-spec org-test-with-temp-text-in-file (form body))
223 (defun org-test-table-target-expect (target &optional expect laps
224 &rest tblfm)
225 "For all TBLFM: Apply the formula to TARGET, compare EXPECT with result.
226 Either LAPS and TBLFM are nil and the table will only be aligned
227 or LAPS is the count of recalculations that should be made on
228 each TBLFM. To save ERT run time keep LAPS as low as possible to
229 get the table stable. Anyhow, if LAPS is 'iterate then iterate,
230 but this will run one recalculation longer. When EXPECT is nil
231 it will be set to TARGET.
233 If running a test interactively in ERT is not enough and you need
234 to examine the target table with e. g. the Org formula debugger
235 or an Emacs Lisp debugger (e. g. with point in a data field and
236 calling the instrumented `org-table-eval-formula') then copy and
237 paste the table with formula from the ERT results buffer or
238 temporarily substitute the `org-test-with-temp-text' of this
239 function with `org-test-with-temp-text-in-file'.
241 Consider setting `pp-escape-newlines' to nil manually."
242 (require 'pp)
243 (let ((back pp-escape-newlines) (current-tblfm))
244 (unless tblfm
245 (should-not laps)
246 (push "" tblfm)) ; Dummy formula.
247 (unless expect (setq expect target))
248 (while (setq current-tblfm (pop tblfm))
249 (org-test-with-temp-text (concat target current-tblfm)
250 ;; Search table, stop ERT at end of buffer if not found.
251 (while (not (org-at-table-p))
252 (should (eq 0 (forward-line))))
253 (when laps
254 (if (and (symbolp laps) (eq laps 'iterate))
255 (should (org-table-recalculate 'iterate t))
256 (should (integerp laps))
257 (should (< 0 laps))
258 (let ((cnt laps))
259 (while (< 0 cnt)
260 (should (org-table-recalculate 'all t))
261 (setq cnt (1- cnt))))))
262 (org-table-align)
263 (setq pp-escape-newlines nil)
264 ;; Declutter the ERT results buffer by giving only variables
265 ;; and not directly the forms to `should'.
266 (let ((expect (concat expect current-tblfm))
267 (result (buffer-substring-no-properties
268 (point-min) (point-max))))
269 (should (equal expect result)))
270 ;; If `should' passed then set back `pp-escape-newlines' here,
271 ;; else leave it nil as a side effect to see the failed table
272 ;; on multiple lines in the ERT results buffer.
273 (setq pp-escape-newlines back)))))
276 ;;; Navigation Functions
277 (when (featurep 'jump)
278 (defjump org-test-jump
279 (("lisp/\\1.el" . "testing/lisp/test-\\1.el")
280 ("lisp/\\1.el" . "testing/lisp/\\1.el/test.*.el")
281 ("testing/lisp/test-\\1.el" . "lisp/\\1.el")
282 ("testing/lisp/\\1.el" . "lisp/\\1.el/test.*.el"))
283 (concat org-base-dir "/")
284 "Jump between org-mode files and their tests."
285 (lambda (path)
286 (let* ((full-path (expand-file-name path org-base-dir))
287 (file-name (file-name-nondirectory path))
288 (name (file-name-sans-extension file-name)))
289 (find-file full-path)
290 (insert
291 ";;; " file-name "\n\n"
292 ";; Copyright (c) " (nth 5 (decode-time (current-time)))
293 " " user-full-name "\n"
294 ";; Authors: " user-full-name "\n\n"
295 ";; Released under the GNU General Public License version 3\n"
296 ";; see: http://www.gnu.org/licenses/gpl-3.0.html\n\n"
297 ";;;; Comments:\n\n"
298 ";; Template test file for Org-mode tests\n\n"
299 "\f\n"
300 ";;; Code:\n"
301 "(let ((load-path (cons (expand-file-name\n"
302 " \"..\" (file-name-directory\n"
303 " (or load-file-name buffer-file-name)))\n"
304 " load-path)))\n"
305 " (require 'org-test)\n"
306 " (require 'org-test-ob-consts))\n\n"
307 "\f\n"
308 ";;; Tests\n"
309 "(ert-deftest " name "/example-test ()\n"
310 " \"Just an example to get you started.\"\n"
311 " (should t)\n"
312 " (should-not nil)\n"
313 " (should-error (error \"errr...\")))\n\n\n"
314 "(provide '" name ")\n\n"
315 ";;; " file-name " ends here\n") full-path))
316 (lambda () ((lambda (res) (if (listp res) (car res) res)) (which-function)))))
318 (define-key emacs-lisp-mode-map "\M-\C-j" 'org-test-jump)
321 ;;; Miscellaneous helper functions
322 (defun org-test-strip-text-props (s)
323 "Return S without any text properties."
324 (let ((noprop (copy-sequence s)))
325 (set-text-properties 0 (length noprop) nil noprop)
326 noprop))
329 (defun org-test-string-exact-match (regex string &optional start)
330 "case sensative string-match"
331 (let ((case-fold-search nil)
332 (case-replace nil))
333 (if(and (equal regex "")
334 (not(equal string "")))
336 (if (equal 0 (string-match regex string start))
338 nil))))
340 ;;; Load and Run tests
341 (defun org-test-load ()
342 "Load up the org-mode test suite."
343 (interactive)
344 (flet ((rld (base)
345 ;; Recursively load all files, if files throw errors
346 ;; then silently ignore the error and continue to the
347 ;; next file. This allows files to error out if
348 ;; required executables aren't available.
349 (mapc
350 (lambda (path)
351 (if (file-directory-p path)
352 (rld path)
353 (condition-case err
354 (when (string-match "^[A-Za-z].*\\.el$"
355 (file-name-nondirectory path))
356 (load-file path))
357 (missing-test-dependency
358 (let ((name (intern
359 (concat "org-missing-dependency/"
360 (file-name-nondirectory
361 (file-name-sans-extension path))))))
362 (eval `(ert-deftest ,name ()
363 :expected-result :failed (should nil))))))))
364 (directory-files base 'full
365 "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*\\.el$"))))
366 (rld (expand-file-name "lisp" org-test-dir))))
368 (defun org-test-current-defun ()
369 "Test the current function."
370 (interactive)
371 (ert (which-function)))
373 (defun org-test-current-file ()
374 "Run all tests for current file."
375 (interactive)
376 (ert (concat "test-"
377 (file-name-sans-extension
378 (file-name-nondirectory (buffer-file-name)))
379 "/")))
381 (defvar org-test-buffers nil
382 "Hold buffers open for running Org-mode tests.")
384 (defun org-test-touch-all-examples ()
385 (dolist (file (directory-files
386 org-test-example-dir 'full
387 "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*\\.org$"))
388 (unless (get-file-buffer file)
389 (add-to-list 'org-test-buffers (find-file file)))))
391 (defun org-test-kill-all-examples ()
392 (while org-test-buffers
393 (let ((b (pop org-test-buffers)))
394 (when (buffer-live-p b) (kill-buffer b)))))
396 (defun org-test-update-id-locations ()
397 (org-id-update-id-locations
398 (directory-files
399 org-test-example-dir 'full
400 "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*\\.org$")))
402 (defun org-test-run-batch-tests ()
403 "Run all defined tests matching \"\\(org\\|ob\\)\".
404 Load all test files first."
405 (interactive)
406 (let ((org-id-track-globally t)
407 (org-id-locations-file
408 (convert-standard-filename
409 (expand-file-name
410 "testing/.test-org-id-locations"
411 org-base-dir))))
412 (org-test-touch-all-examples)
413 (org-test-update-id-locations)
414 (org-test-load)
415 (ert-run-tests-batch-and-exit "\\(org\\|ob\\)")))
417 (defun org-test-run-all-tests ()
418 "Run all defined tests matching \"\\(org\\|ob\\)\".
419 Load all test files first."
420 (interactive)
421 (org-test-touch-all-examples)
422 (org-test-load)
423 (ert "\\(org\\|ob\\)")
424 (org-test-kill-all-examples))
426 (provide 'org-test)
428 ;;; org-test.el ends here