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