org-test.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. ;;;; org-test.el --- Tests for Org-mode
  2. ;; Copyright (c) 2010-2012 Sebastian Rose, Eric Schulte
  3. ;; Authors:
  4. ;; Sebastian Rose, Hannover, Germany, sebastian_rose gmx de
  5. ;; Eric Schulte, Santa Fe, New Mexico, USA, schulte.eric gmail com
  6. ;; David Maus, Brunswick, Germany, dmaus ictsoc de
  7. ;; Released under the GNU General Public License version 3
  8. ;; see: http://www.gnu.org/licenses/gpl-3.0.html
  9. ;; Definition of `special-mode' copied from Emacs23's simple.el to be
  10. ;; provide a testing environment for Emacs22.
  11. ;;;; Comments:
  12. ;; Interactive testing for Org mode.
  13. ;; The heart of all this is the commands `org-test-current-defun'. If
  14. ;; called while in a `defun' all ert tests with names matching the
  15. ;; name of the function are run.
  16. ;;; Test Development
  17. ;; For test development purposes a number of navigation and test
  18. ;; function construction routines are available as a git submodule
  19. ;; (jump.el)
  20. ;; Install with...
  21. ;; $ git submodule init
  22. ;; $ git submodule update
  23. ;;;; Code:
  24. (require 'org-test-ob-consts)
  25. (let* ((org-test-dir (expand-file-name
  26. (file-name-directory
  27. (or load-file-name buffer-file-name))))
  28. (org-lisp-dir (expand-file-name
  29. (concat org-test-dir "../lisp"))))
  30. (unless (featurep 'org)
  31. (setq load-path (cons org-lisp-dir load-path))
  32. (require 'org)
  33. (require 'org-id)
  34. (org-babel-do-load-languages
  35. 'org-babel-load-languages '((sh . t) (org . t))))
  36. (let* ((load-path (cons
  37. org-test-dir
  38. (cons
  39. (expand-file-name "jump" org-test-dir)
  40. load-path))))
  41. (require 'cl)
  42. (when (= emacs-major-version 22)
  43. (defvar special-mode-map
  44. (let ((map (make-sparse-keymap)))
  45. (suppress-keymap map)
  46. (define-key map "q" 'quit-window)
  47. (define-key map " " 'scroll-up)
  48. (define-key map "\C-?" 'scroll-down)
  49. (define-key map "?" 'describe-mode)
  50. (define-key map "h" 'describe-mode)
  51. (define-key map ">" 'end-of-buffer)
  52. (define-key map "<" 'beginning-of-buffer)
  53. (define-key map "g" 'revert-buffer)
  54. (define-key map "z" 'kill-this-buffer)
  55. map))
  56. (put 'special-mode 'mode-class 'special)
  57. (define-derived-mode special-mode nil "Special"
  58. "Parent major mode from which special major modes should inherit."
  59. (setq buffer-read-only t)))
  60. (require 'ert)
  61. (require 'ert-x)
  62. (when (file-exists-p
  63. (expand-file-name "jump/jump.el" org-test-dir))
  64. (require 'jump)
  65. (require 'which-func))))
  66. (defconst org-test-default-test-file-name "tests.el"
  67. "For each defun a separate file with tests may be defined.
  68. tests.el is the fallback or default if you like.")
  69. (defconst org-test-default-directory-name "testing"
  70. "Basename or the directory where the tests live.
  71. org-test searches this directory up the directory tree.")
  72. (defconst org-test-dir
  73. (expand-file-name (file-name-directory (or load-file-name buffer-file-name))))
  74. (defconst org-base-dir
  75. (expand-file-name ".." org-test-dir))
  76. (defconst org-test-example-dir
  77. (expand-file-name "examples" org-test-dir))
  78. (defconst org-test-file
  79. (expand-file-name "normal.org" org-test-example-dir))
  80. (defconst org-test-no-heading-file
  81. (expand-file-name "no-heading.org" org-test-example-dir))
  82. (defconst org-test-link-in-heading-file
  83. (expand-file-name "link-in-heading.org" org-test-dir))
  84. ;;; Functions for writing tests
  85. (put 'missing-test-dependency
  86. 'error-conditions
  87. '(error missing-test-dependency))
  88. (defun org-test-for-executable (exe)
  89. "Throw an error if EXE is not available.
  90. This can be used at the top of code-block-language specific test
  91. files to avoid loading the file on systems without the
  92. executable."
  93. (unless (reduce
  94. (lambda (acc dir)
  95. (or acc (file-exists-p (expand-file-name exe dir))))
  96. exec-path :initial-value nil)
  97. (signal 'missing-test-dependency (list exe))))
  98. (defun org-test-buffer (&optional file)
  99. "TODO: Setup and return a buffer to work with.
  100. If file is non-nil insert it's contents in there.")
  101. (defun org-test-compare-with-file (&optional file)
  102. "TODO: Compare the contents of the test buffer with FILE.
  103. If file is not given, search for a file named after the test
  104. currently executed.")
  105. (defmacro org-test-at-id (id &rest body)
  106. "Run body after placing the point in the headline identified by ID."
  107. (declare (indent 1))
  108. `(let* ((id-location (org-id-find ,id))
  109. (id-file (car id-location))
  110. (visited-p (get-file-buffer id-file))
  111. to-be-removed)
  112. (save-window-excursion
  113. (save-match-data
  114. (org-id-goto ,id)
  115. (setq to-be-removed (current-buffer))
  116. (condition-case nil
  117. (progn
  118. (org-show-subtree)
  119. (org-show-block-all))
  120. (error nil))
  121. (save-restriction ,@body)))
  122. (unless visited-p
  123. (kill-buffer to-be-removed))))
  124. (def-edebug-spec org-test-at-id (form body))
  125. (defmacro org-test-in-example-file (file &rest body)
  126. "Execute body in the Org-mode example file."
  127. (declare (indent 1))
  128. `(let* ((my-file (or ,file org-test-file))
  129. (visited-p (get-file-buffer my-file))
  130. to-be-removed)
  131. (save-window-excursion
  132. (save-match-data
  133. (find-file my-file)
  134. (unless (eq major-mode 'org-mode)
  135. (org-mode))
  136. (setq to-be-removed (current-buffer))
  137. (goto-char (point-min))
  138. (condition-case nil
  139. (progn
  140. (outline-next-visible-heading 1)
  141. (org-show-subtree)
  142. (org-show-block-all))
  143. (error nil))
  144. (save-restriction ,@body)))
  145. (unless visited-p
  146. (kill-buffer to-be-removed))))
  147. (def-edebug-spec org-test-in-example-file (form body))
  148. (defmacro org-test-at-marker (file marker &rest body)
  149. "Run body after placing the point at MARKER in FILE.
  150. Note the uuidgen command-line command can be useful for
  151. generating unique markers for insertion as anchors into org
  152. files."
  153. (declare (indent 2))
  154. `(org-test-in-example-file ,file
  155. (goto-char (point-min))
  156. (re-search-forward (regexp-quote ,marker))
  157. ,@body))
  158. (def-edebug-spec org-test-at-marker (form form body))
  159. (defmacro org-test-with-temp-text (text &rest body)
  160. "Run body in a temporary buffer with Org-mode as the active
  161. mode holding TEXT. If the string \"<point>\" appears in TEXT
  162. then remove it and place the point there before running BODY,
  163. otherwise place the point at the beginning of the inserted text."
  164. (declare (indent 1))
  165. (let ((inside-text (if (stringp text) text (eval text))))
  166. `(with-temp-buffer
  167. (org-mode)
  168. ,(let ((point (string-match (regexp-quote "<point>") inside-text)))
  169. (if point
  170. `(progn (insert `(replace-match "" nil nil inside-text))
  171. (goto-char ,(match-beginning 0)))
  172. `(progn (insert ,inside-text)
  173. (goto-char (point-min)))))
  174. (prog1 ,@body (kill-buffer)))))
  175. (def-edebug-spec org-test-with-temp-text (form body))
  176. (defmacro org-test-with-temp-text-in-file (text &rest body)
  177. "Run body in a temporary file buffer with Org-mode as the active mode."
  178. (declare (indent 1))
  179. (let ((file (make-temp-file "org-test"))
  180. (inside-text (if (stringp text) text (eval text)))
  181. (results (gensym)))
  182. `(let ((kill-buffer-query-functions nil) ,results)
  183. (with-temp-file ,file (insert ,inside-text))
  184. (find-file ,file)
  185. (org-mode)
  186. (setq ,results ,@body)
  187. (save-buffer) (kill-buffer (current-buffer))
  188. (delete-file ,file)
  189. ,results)))
  190. (def-edebug-spec org-test-with-temp-text-in-file (form body))
  191. ;;; Navigation Functions
  192. (when (featurep 'jump)
  193. (defjump org-test-jump
  194. (("lisp/\\1.el" . "testing/lisp/test-\\1.el")
  195. ("lisp/\\1.el" . "testing/lisp/\\1.el/test.*.el")
  196. ("testing/lisp/test-\\1.el" . "lisp/\\1.el")
  197. ("testing/lisp/\\1.el" . "lisp/\\1.el/test.*.el"))
  198. (concat org-base-dir "/")
  199. "Jump between org-mode files and their tests."
  200. (lambda (path)
  201. (let* ((full-path (expand-file-name path org-base-dir))
  202. (file-name (file-name-nondirectory path))
  203. (name (file-name-sans-extension file-name)))
  204. (find-file full-path)
  205. (insert
  206. ";;; " file-name "\n\n"
  207. ";; Copyright (c) " (nth 5 (decode-time (current-time)))
  208. " " user-full-name "\n"
  209. ";; Authors: " user-full-name "\n\n"
  210. ";; Released under the GNU General Public License version 3\n"
  211. ";; see: http://www.gnu.org/licenses/gpl-3.0.html\n\n"
  212. ";;;; Comments:\n\n"
  213. ";; Template test file for Org-mode tests\n\n"
  214. " \n"
  215. ";;; Code:\n"
  216. "(let ((load-path (cons (expand-file-name\n"
  217. " \"..\" (file-name-directory\n"
  218. " (or load-file-name buffer-file-name)))\n"
  219. " load-path)))\n"
  220. " (require 'org-test)\n"
  221. " (require 'org-test-ob-consts))\n\n"
  222. " \n"
  223. ";;; Tests\n"
  224. "(ert-deftest " name "/example-test ()\n"
  225. " \"Just an example to get you started.\"\n"
  226. " (should t)\n"
  227. " (should-not nil)\n"
  228. " (should-error (error \"errr...\")))\n\n\n"
  229. "(provide '" name ")\n\n"
  230. ";;; " file-name " ends here\n") full-path))
  231. (lambda () ((lambda (res) (if (listp res) (car res) res)) (which-function)))))
  232. (define-key emacs-lisp-mode-map "\M-\C-j" 'org-test-jump)
  233. ;;; Miscellaneous helper functions
  234. (defun org-test-strip-text-props (s)
  235. "Return S without any text properties."
  236. (let ((noprop (copy-sequence s)))
  237. (set-text-properties 0 (length noprop) nil noprop)
  238. noprop))
  239. (defun org-test-string-exact-match (regex string &optional start)
  240. "case sensative string-match"
  241. (let ((case-fold-search nil)
  242. (case-replace nil))
  243. (if(and (equal regex "")
  244. (not(equal string "")))
  245. nil
  246. (if (equal 0 (string-match regex string start))
  247. t
  248. nil))))
  249. ;;; Load and Run tests
  250. (defun org-test-load ()
  251. "Load up the org-mode test suite."
  252. (interactive)
  253. (flet ((rld (base)
  254. ;; Recursively load all files, if files throw errors
  255. ;; then silently ignore the error and continue to the
  256. ;; next file. This allows files to error out if
  257. ;; required executables aren't available.
  258. (mapc
  259. (lambda (path)
  260. (if (file-directory-p path)
  261. (rld path)
  262. (condition-case err
  263. (when (string-match "^[A-Za-z].*\\.el$"
  264. (file-name-nondirectory path))
  265. (load-file path))
  266. (missing-test-dependency
  267. (let ((name (intern
  268. (concat "org-missing-dependency/"
  269. (file-name-nondirectory
  270. (file-name-sans-extension path))))))
  271. (eval `(ert-deftest ,name ()
  272. :expected-result :failed (should nil))))))))
  273. (directory-files base 'full
  274. "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*\\.el$"))))
  275. (rld (expand-file-name "lisp" org-test-dir))))
  276. (defun org-test-current-defun ()
  277. "Test the current function."
  278. (interactive)
  279. (ert (which-function)))
  280. (defun org-test-current-file ()
  281. "Run all tests for current file."
  282. (interactive)
  283. (ert (concat "test-"
  284. (file-name-sans-extension
  285. (file-name-nondirectory (buffer-file-name)))
  286. "/")))
  287. (defvar org-test-buffers nil
  288. "Hold buffers open for running Org-mode tests.")
  289. (defun org-test-touch-all-examples ()
  290. (dolist (file (directory-files
  291. org-test-example-dir 'full
  292. "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*\\.org$"))
  293. (unless (get-file-buffer file)
  294. (add-to-list 'org-test-buffers (find-file file)))))
  295. (defun org-test-kill-all-examples ()
  296. (while org-test-buffers
  297. (let ((b (pop org-test-buffers)))
  298. (when (buffer-live-p b) (kill-buffer b)))))
  299. (defun org-test-update-id-locations ()
  300. (org-id-update-id-locations
  301. (directory-files
  302. org-test-example-dir 'full
  303. "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*\\.org$")))
  304. (defun org-test-run-batch-tests ()
  305. "Run all defined tests matching \"\\(org\\|ob\\)\".
  306. Load all test files first."
  307. (interactive)
  308. (let ((org-id-track-globally t)
  309. (org-id-locations-file
  310. (convert-standard-filename
  311. (expand-file-name
  312. "testing/.test-org-id-locations"
  313. org-base-dir))))
  314. (org-test-touch-all-examples)
  315. (org-test-update-id-locations)
  316. (org-test-load)
  317. (ert-run-tests-batch-and-exit "\\(org\\|ob\\)")))
  318. (defun org-test-run-all-tests ()
  319. "Run all defined tests matching \"\\(org\\|ob\\)\".
  320. Load all test files first."
  321. (interactive)
  322. (org-test-touch-all-examples)
  323. (org-test-load)
  324. (ert "\\(org\\|ob\\)")
  325. (org-test-kill-all-examples))
  326. (provide 'org-test)
  327. ;;; org-test.el ends here