org-test.el 14 KB

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