org-test.el 14 KB

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