1 ;;;; org-test.el --- Tests for Org-mode
3 ;; Copyright (c) 2010 Sebastian Rose, Eric Schulte
5 ;; Sebastian Rose, Hannover, Germany, sebastian_rose gmx de
6 ;; Eric Schulte, Santa Fe, New Mexico, USA, schulte.eric gmail com
8 ;; Released under the GNU General Public License version 3
9 ;; see: http://www.gnu.org/licenses/gpl-3.0.html
13 ;; Interactive testing for Org mode.
15 ;; The heart of all this is the commands `org-test-current-defun'. If
16 ;; called while in a `defun' all ert tests with names matching the
17 ;; name of the function are run.
21 ;; ERT and jump.el are both included as git submodules, install with
22 ;; $ git submodule init
23 ;; $ git submodule update
27 (let* ((org-test-dir (expand-file-name
29 (or load-file-name buffer-file-name
))))
31 (expand-file-name "ert" org-test-dir
)
33 (expand-file-name "jump" org-test-dir
)
41 (defconst org-test-default-test-file-name
"tests.el"
42 "For each defun a separate file with tests may be defined.
43 tests.el is the fallback or default if you like.")
45 (defconst org-test-default-directory-name
"testing"
46 "Basename or the directory where the tests live.
47 org-test searches this directory up the directory tree.")
49 (defconst org-test-dir
50 (expand-file-name (file-name-directory (or load-file-name buffer-file-name
))))
52 (defconst org-base-dir
53 (expand-file-name ".." org-test-dir
))
55 (defconst org-test-example-dir
56 (expand-file-name "examples" org-test-dir
))
58 (defconst org-test-file
59 (expand-file-name "normal.org" org-test-example-dir
))
61 (defconst org-test-no-heading-file
62 (expand-file-name "no-heading.org" org-test-example-dir
))
64 (defconst org-test-link-in-heading-file
65 (expand-file-name "link-in-heading.org" org-test-dir
))
68 ;;; Functions for writing tests
69 (defun org-test-buffer (&optional file
)
70 "TODO: Setup and return a buffer to work with.
71 If file is non-nil insert it's contents in there.")
73 (defun org-test-compare-with-file (&optional file
)
74 "TODO: Compare the contents of the test buffer with FILE.
75 If file is not given, search for a file named after the test
78 (defmacro org-test-at-id
(id &rest body
)
79 "Run body after placing the point in the headline identified by ID."
81 `(let* ((id-location (org-id-find ,id
))
82 (id-file (car id-location
))
83 (visited-p (get-file-buffer id-file
))
85 (save-window-excursion
88 (setq to-be-removed
(current-buffer))
94 (save-restriction ,@body
)))
96 (kill-buffer to-be-removed
))))
98 (defmacro org-test-in-example-file
(file &rest body
)
99 "Execute body in the Org-mode example file."
101 `(let* ((my-file (or ,file org-test-file
))
102 (visited-p (get-file-buffer my-file
))
104 (save-window-excursion
107 (setq to-be-removed
(current-buffer))
108 (goto-char (point-min))
111 (outline-next-visible-heading 1)
113 (org-show-block-all))
115 (save-restriction ,@body
)))
117 (kill-buffer to-be-removed
))))
119 (defmacro org-test-at-marker
(file marker
&rest body
)
120 "Run body after placing the point at MARKER in FILE.
121 Note the uuidgen command-line command can be useful for
122 generating unique markers for insertion as anchors into org
125 `(org-test-in-example-file ,file
126 (goto-char (point-min))
127 (re-search-forward (regexp-quote ,marker
))
131 ;;; Navigation Functions
132 (defjump org-test-jump
133 (("lisp/\\1.el" .
"testing/lisp/test-\\1.el")
134 ("lisp/\\1.el" .
"testing/lisp/\\1.el/test.*.el")
135 ("contrib/lisp/\\1.el" .
"testing/contrib/lisp/test-\\1.el")
136 ("contrib/lisp/\\1.el" .
"testing/contrib/lisp/\\1.el/test.*.el")
137 ("testing/lisp/test-\\1.el" .
"lisp/\\1.el")
138 ("testing/lisp/\\1.el" .
"lisp/\\1.el/test.*.el")
139 ("testing/contrib/lisp/test-\\1.el" .
"contrib/lisp/\\1.el")
140 ("testing/contrib/lisp/test-\\1.el" .
"contrib/lisp/\\1.el/test.*.el"))
141 (concat org-base-dir
"/")
142 "Jump between org-mode files and their tests."
144 (let* ((full-path (expand-file-name path org-base-dir
))
145 (file-name (file-name-nondirectory path
))
146 (name (file-name-sans-extension file-name
)))
147 (find-file full-path
)
149 ";;; " file-name
"\n\n"
150 ";; Copyright (c) " (nth 5 (decode-time (current-time)))
151 " " user-full-name
"\n"
152 ";; Authors: " user-full-name
"\n\n"
153 ";; Released under the GNU General Public License version 3\n"
154 ";; see: http://www.gnu.org/licenses/gpl-3.0.html\n\n"
156 ";; Template test file for Org-mode tests\n\n"
159 "(let ((load-path (cons (expand-file-name\n"
160 " \"..\" (file-name-directory\n"
161 " (or load-file-name buffer-file-name)))\n"
163 " (require 'org-test)\n"
164 " (require 'org-test-ob-consts))\n\n"
167 "(ert-deftest " name
"/example-test ()\n"
168 " \"Just an example to get you started.\"\n"
170 " (should-not nil)\n"
171 " (should-error (error \"errr...\")))\n\n\n"
172 "(provide '" name
")\n\n"
173 ";;; " file-name
" ends here\n") full-path
))
174 (lambda () ((lambda (res) (if (listp res
) (car res
) res
)) (which-function))))
176 (define-key emacs-lisp-mode-map
"\M-\C-j" 'org-test-jump
)
179 ;;; Miscellaneous helper functions
180 (defun org-test-strip-text-props (s)
181 "Return S without any text properties."
182 (let ((noprop (copy-sequence s
)))
183 (set-text-properties 0 (length noprop
) nil noprop
)
187 (defun org-test-string-exact-match (regex string
&optional start
)
188 "case sensative string-match"
189 (let ((case-fold-search nil
)
191 (if(and (equal regex
"")
192 (not(equal string
"")))
194 (if (equal 0 (string-match regex string start
))
198 ;;; Load and Run tests
199 (defun org-test-load ()
200 "Load up the org-mode test suite."
205 (if (file-directory-p path
) (rload path
) (load-file path
)))
206 (directory-files base
'full
207 "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*\\.el$"))))
208 (rload (expand-file-name "lisp" org-test-dir
))
209 (rload (expand-file-name "lisp"
210 (expand-file-name "contrib" org-test-dir
)))))
212 (defun org-test-current-defun ()
213 "Test the current function."
215 (ert (which-function)))
217 (defun org-test-current-file ()
218 "Run all tests for current file."
221 (file-name-sans-extension
222 (file-name-nondirectory (buffer-file-name)))
225 (defun org-test-touch-all-examples ()
226 (dolist (file (directory-files
227 org-test-example-dir
'full
228 "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*\\.org$"))
231 (defun org-test-run-batch-tests ()
232 "Run all defined tests matching \"\\(org\\|ob\\)\".
233 Load all test files first."
235 (org-test-touch-all-examples)
237 (ert-run-tests-batch-and-exit "\\(org\\|ob\\)"))
239 (defun org-test-run-all-tests ()
240 "Run all defined tests matching \"\\(org\\|ob\\)\".
241 Load all test files first."
243 (org-test-touch-all-examples)
245 (ert "\\(org\\|ob\\)"))
249 ;;; org-test.el ends here