org-test.el 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. ;;;; org-test.el --- Tests for Org
  2. ;; Copyright (c) 2010-2015 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 '((shell . t) (org . t))))
  42. (let ((load-path (cons org-test-dir
  43. (cons (expand-file-name "jump" org-test-dir)
  44. load-path))))
  45. (require 'cl-lib)
  46. (require 'ert)
  47. (require 'ert-x)
  48. (when (file-exists-p (expand-file-name "jump/jump.el" org-test-dir))
  49. (require 'jump)
  50. (require 'which-func)))))
  51. (defconst org-test-default-test-file-name "tests.el"
  52. "For each defun a separate file with tests may be defined.
  53. tests.el is the fallback or default if you like.")
  54. (defconst org-test-default-directory-name "testing"
  55. "Basename or the directory where the tests live.
  56. org-test searches this directory up the directory tree.")
  57. (defconst org-test-dir
  58. (expand-file-name (file-name-directory (or load-file-name buffer-file-name))))
  59. (defconst org-base-dir
  60. (expand-file-name ".." org-test-dir))
  61. (defconst org-test-example-dir
  62. (expand-file-name "examples" org-test-dir))
  63. (defconst org-test-file
  64. (expand-file-name "normal.org" org-test-example-dir))
  65. (defconst org-test-no-heading-file
  66. (expand-file-name "no-heading.org" org-test-example-dir))
  67. (defconst org-test-attachments-file
  68. (expand-file-name "attachments.org" org-test-example-dir))
  69. (defconst org-test-link-in-heading-file
  70. (expand-file-name "link-in-heading.org" org-test-dir))
  71. (defconst org-id-locations-file
  72. (expand-file-name ".test-org-id-locations" org-test-dir))
  73. ;;; Functions for writing tests
  74. (put 'missing-test-dependency
  75. 'error-conditions
  76. '(error missing-test-dependency))
  77. (defun org-test-for-executable (exe)
  78. "Throw an error if EXE is not available.
  79. This can be used at the top of code-block-language specific test
  80. files to avoid loading the file on systems without the
  81. executable."
  82. (unless (cl-reduce
  83. (lambda (acc dir)
  84. (or acc (file-exists-p (expand-file-name exe dir))))
  85. exec-path :initial-value nil)
  86. (signal 'missing-test-dependency (list exe))))
  87. (defun org-test-buffer (&optional file)
  88. "TODO: Setup and return a buffer to work with.
  89. If file is non-nil insert its contents in there.")
  90. (defun org-test-compare-with-file (&optional file)
  91. "TODO: Compare the contents of the test buffer with FILE.
  92. If file is not given, search for a file named after the test
  93. currently executed.")
  94. (defmacro org-test-at-id (id &rest body)
  95. "Run body after placing the point in the headline identified by ID."
  96. (declare (indent 1))
  97. `(let* ((id-location (org-id-find ,id))
  98. (id-file (car id-location))
  99. (visited-p (get-file-buffer id-file))
  100. to-be-removed)
  101. (unwind-protect
  102. (save-window-excursion
  103. (save-match-data
  104. (org-id-goto ,id)
  105. (setq to-be-removed (current-buffer))
  106. (condition-case nil
  107. (progn
  108. (org-show-subtree)
  109. (org-show-all '(blocks)))
  110. (error nil))
  111. (save-restriction ,@body)))
  112. (unless (or visited-p (not to-be-removed))
  113. (kill-buffer to-be-removed)))))
  114. (def-edebug-spec org-test-at-id (form body))
  115. (defmacro org-test-in-example-file (file &rest body)
  116. "Execute body in the Org example file."
  117. (declare (indent 1))
  118. `(let* ((my-file (or ,file org-test-file))
  119. (visited-p (get-file-buffer my-file))
  120. to-be-removed
  121. results)
  122. (save-window-excursion
  123. (save-match-data
  124. (find-file my-file)
  125. (unless (eq major-mode 'org-mode)
  126. (org-mode))
  127. (setq to-be-removed (current-buffer))
  128. (goto-char (point-min))
  129. (condition-case nil
  130. (progn
  131. (outline-next-visible-heading 1)
  132. (org-show-subtree)
  133. (org-show-all '(blocks)))
  134. (error nil))
  135. (setq results (save-restriction ,@body))))
  136. (unless visited-p
  137. (kill-buffer to-be-removed))
  138. results))
  139. (def-edebug-spec org-test-in-example-file (form body))
  140. (defmacro org-test-at-marker (file marker &rest body)
  141. "Run body after placing the point at MARKER in FILE.
  142. Note the uuidgen command-line command can be useful for
  143. generating unique markers for insertion as anchors into org
  144. files."
  145. (declare (indent 2))
  146. `(org-test-in-example-file ,file
  147. (goto-char (point-min))
  148. (re-search-forward (regexp-quote ,marker))
  149. ,@body))
  150. (def-edebug-spec org-test-at-marker (form form body))
  151. (defmacro org-test-with-temp-text (text &rest body)
  152. "Run body in a temporary buffer with Org mode as the active
  153. mode holding TEXT. If the string \"<point>\" appears in TEXT
  154. then remove it and place the point there before running BODY,
  155. otherwise place the point at the beginning of the inserted text."
  156. (declare (indent 1))
  157. `(let ((inside-text (if (stringp ,text) ,text (eval ,text)))
  158. (org-mode-hook nil))
  159. (with-temp-buffer
  160. (org-mode)
  161. (let ((point (string-match "<point>" inside-text)))
  162. (if point
  163. (progn
  164. (insert (replace-match "" nil nil inside-text))
  165. (goto-char (1+ (match-beginning 0))))
  166. (insert inside-text)
  167. (goto-char (point-min))))
  168. (font-lock-ensure (point-min) (point-max))
  169. ,@body)))
  170. (def-edebug-spec org-test-with-temp-text (form body))
  171. (defmacro org-test-with-temp-text-in-file (text &rest body)
  172. "Run body in a temporary file buffer with Org mode as the active mode.
  173. If the string \"<point>\" appears in TEXT then remove it and
  174. place the point there before running BODY, otherwise place the
  175. point at the beginning of the buffer."
  176. (declare (indent 1))
  177. `(let ((file (make-temp-file "org-test"))
  178. (inside-text (if (stringp ,text) ,text (eval ,text)))
  179. buffer)
  180. (with-temp-file file (insert inside-text))
  181. (unwind-protect
  182. (progn
  183. (setq buffer (find-file file))
  184. (when (re-search-forward "<point>" nil t)
  185. (replace-match ""))
  186. (org-mode)
  187. (progn ,@body))
  188. (let ((kill-buffer-query-functions nil))
  189. (when buffer
  190. (set-buffer buffer)
  191. ;; Ignore changes, we're deleting the file in the next step
  192. ;; anyways.
  193. (set-buffer-modified-p nil)
  194. (kill-buffer))
  195. (delete-file file)))))
  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. (require 'ert)
  216. (let ((back pp-escape-newlines) (current-tblfm))
  217. (unless tblfm
  218. (should-not laps)
  219. (push "" tblfm)) ; Dummy formula.
  220. (unless expect (setq expect target))
  221. (while (setq current-tblfm (pop tblfm))
  222. (org-test-with-temp-text (concat target current-tblfm)
  223. ;; Search the last of possibly several tables, let the ERT
  224. ;; test fail if not found.
  225. (goto-char (point-max))
  226. (while (not (org-at-table-p))
  227. (should (eq 0 (forward-line -1))))
  228. (when laps
  229. (if (and (symbolp laps) (eq laps 'iterate))
  230. (should (org-table-recalculate 'iterate t))
  231. (should (integerp laps))
  232. (should (< 0 laps))
  233. (let ((cnt laps))
  234. (while (< 0 cnt)
  235. (should (org-table-recalculate 'all t))
  236. (setq cnt (1- cnt))))))
  237. (org-table-align)
  238. (setq pp-escape-newlines nil)
  239. ;; Declutter the ERT results buffer by giving only variables
  240. ;; and not directly the forms to `should'.
  241. (let ((expect (concat expect current-tblfm))
  242. (result (buffer-substring-no-properties
  243. (point-min) (point-max))))
  244. (should (equal expect result)))
  245. ;; If `should' passed then set back `pp-escape-newlines' here,
  246. ;; else leave it nil as a side effect to see the failed table
  247. ;; on multiple lines in the ERT results buffer.
  248. (setq pp-escape-newlines back)))))
  249. ;;; Navigation Functions
  250. (defmacro org--compile-when (test &rest body)
  251. (declare (debug t) (indent 1))
  252. (let ((exp `(progn ,@body)))
  253. (if (eval test t)
  254. exp
  255. `(when ,test (eval exp t)))))
  256. (org--compile-when (featurep 'jump)
  257. (defjump org-test-jump
  258. (("lisp/\\1.el" . "testing/lisp/test-\\1.el")
  259. ("lisp/\\1.el" . "testing/lisp/\\1.el/test.*.el")
  260. ("testing/lisp/test-\\1.el" . "lisp/\\1.el")
  261. ("testing/lisp/\\1.el" . "lisp/\\1.el/test.*.el"))
  262. (concat org-base-dir "/")
  263. "Jump between Org files and their tests."
  264. (lambda (path)
  265. (let* ((full-path (expand-file-name path org-base-dir))
  266. (file-name (file-name-nondirectory path))
  267. (name (file-name-sans-extension file-name)))
  268. (find-file full-path)
  269. (insert
  270. ";;; " file-name "\n\n"
  271. ";; Copyright (c) " (nth 5 (decode-time (current-time)))
  272. " " user-full-name "\n"
  273. ";; Authors: " user-full-name "\n\n"
  274. ";; Released under the GNU General Public License version 3\n"
  275. ";; see: http://www.gnu.org/licenses/gpl-3.0.html\n\n"
  276. ";;;; Comments:\n\n"
  277. ";; Template test file for Org tests\n\n"
  278. " \n"
  279. ";;; Code:\n"
  280. "(let ((load-path (cons (expand-file-name\n"
  281. " \"..\" (file-name-directory\n"
  282. " (or load-file-name buffer-file-name)))\n"
  283. " load-path)))\n"
  284. " (require 'org-test)\n\n"
  285. " \n"
  286. ";;; Tests\n"
  287. "(ert-deftest " name "/example-test ()\n"
  288. " \"Just an example to get you started.\"\n"
  289. " (should t)\n"
  290. " (should-not nil)\n"
  291. " (should-error (error \"errr...\")))\n\n\n"
  292. "(provide '" name ")\n\n"
  293. ";;; " file-name " ends here\n")
  294. full-path))
  295. (lambda () ((lambda (res) (if (listp res) (car res) res)) (which-function)))))
  296. (define-key emacs-lisp-mode-map "\M-\C-j" 'org-test-jump)
  297. ;;; Miscellaneous helper functions
  298. (defun org-test-strip-text-props (s)
  299. "Return S without any text properties."
  300. (let ((noprop (copy-sequence s)))
  301. (set-text-properties 0 (length noprop) nil noprop)
  302. noprop))
  303. (defun org-test-string-exact-match (regex string &optional start)
  304. "Case sensitive string-match"
  305. (let ((case-fold-search nil)
  306. (case-replace nil))
  307. (if(and (equal regex "")
  308. (not(equal string "")))
  309. nil
  310. (if (equal 0 (string-match regex string start))
  311. t
  312. nil))))
  313. ;;; Load and Run tests
  314. (defun org-test-load ()
  315. "Load up the Org test suite."
  316. (interactive)
  317. (cl-flet ((rld (base)
  318. ;; Recursively load all files, if files throw errors
  319. ;; then silently ignore the error and continue to the
  320. ;; next file. This allows files to error out if
  321. ;; required executables aren't available.
  322. (mapc
  323. (lambda (path)
  324. (if (file-directory-p path)
  325. (rld path)
  326. (condition-case err
  327. (when (string-match "^[A-Za-z].*\\.el$"
  328. (file-name-nondirectory path))
  329. (load-file path))
  330. (missing-test-dependency
  331. (let ((name (intern
  332. (concat "org-missing-dependency/"
  333. (file-name-nondirectory
  334. (file-name-sans-extension path))))))
  335. (eval `(ert-deftest ,name ()
  336. :expected-result :failed (should nil))))))))
  337. (directory-files base 'full
  338. "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*\\.el$"))))
  339. (rld (expand-file-name "lisp" org-test-dir))))
  340. (defun org-test-current-defun ()
  341. "Test the current function."
  342. (interactive)
  343. (ert (which-function)))
  344. (defun org-test-current-file ()
  345. "Run all tests for current file."
  346. (interactive)
  347. (ert (concat "test-"
  348. (file-name-sans-extension
  349. (file-name-nondirectory (buffer-file-name)))
  350. "/")))
  351. (defvar org-test-buffers nil
  352. "Hold buffers open for running Org tests.")
  353. (defun org-test-touch-all-examples ()
  354. (dolist (file (directory-files
  355. org-test-example-dir 'full
  356. "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*\\.org$"))
  357. (unless (get-file-buffer file)
  358. (add-to-list 'org-test-buffers (find-file file)))))
  359. (defun org-test-kill-all-examples ()
  360. (while org-test-buffers
  361. (let ((b (pop org-test-buffers)))
  362. (when (buffer-live-p b) (kill-buffer b)))))
  363. (defun org-test-update-id-locations ()
  364. (org-id-update-id-locations
  365. (directory-files
  366. org-test-example-dir 'full
  367. "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*\\.org$")))
  368. (defun org-test-run-batch-tests (&optional org-test-selector)
  369. "Run all tests matching an optional regex which defaults to \"\\(org\\|ob\\)\".
  370. Load all test files first."
  371. (interactive)
  372. (let ((org-id-track-globally t)
  373. (org-test-selector
  374. (if org-test-selector org-test-selector "\\(org\\|ob\\)"))
  375. org-confirm-babel-evaluate org-startup-folded vc-handled-backends)
  376. (org-test-touch-all-examples)
  377. (org-test-update-id-locations)
  378. (org-test-load)
  379. (message "selected tests: %s" org-test-selector)
  380. (ert-run-tests-batch-and-exit org-test-selector)))
  381. (defun org-test-run-all-tests ()
  382. "Run all defined tests matching \"\\(org\\|ob\\)\".
  383. Load all test files first."
  384. (interactive)
  385. (org-test-touch-all-examples)
  386. (org-test-update-id-locations)
  387. (org-test-load)
  388. (ert "\\(org\\|ob\\)")
  389. (org-test-kill-all-examples))
  390. (defmacro org-test-at-time (time &rest body)
  391. "Run body while pretending that the current time is TIME.
  392. TIME can be a non-nil Lisp time value, or a string specifying a date and time."
  393. (declare (indent 1))
  394. (let ((tm (cl-gensym))
  395. (at (cl-gensym)))
  396. `(let* ((,tm ,time)
  397. (,at (if (stringp ,tm)
  398. (apply #'encode-time (org-parse-time-string ,tm))
  399. ,tm)))
  400. (cl-letf
  401. ;; Wrap builtins whose behavior can depend on the current time.
  402. (((symbol-function 'current-time)
  403. (lambda () ,at))
  404. ((symbol-function 'current-time-string)
  405. (lambda (&optional time &rest args)
  406. (apply ,(symbol-function 'current-time-string)
  407. (or time ,at) args)))
  408. ((symbol-function 'current-time-zone)
  409. (lambda (&optional time &rest args)
  410. (apply ,(symbol-function 'current-time-zone)
  411. (or time ,at) args)))
  412. ((symbol-function 'decode-time)
  413. (lambda (&optional time) (funcall ,(symbol-function 'decode-time)
  414. (or time ,at))))
  415. ((symbol-function 'encode-time)
  416. (lambda (time &rest args)
  417. (apply ,(symbol-function 'encode-time) (or time ,at) args)))
  418. ((symbol-function 'float-time)
  419. (lambda (&optional time)
  420. (funcall ,(symbol-function 'float-time) (or time ,at))))
  421. ((symbol-function 'format-time-string)
  422. (lambda (format &optional time &rest args)
  423. (apply ,(symbol-function 'format-time-string)
  424. format (or time ,at) args)))
  425. ((symbol-function 'set-file-times)
  426. (lambda (file &optional time)
  427. (funcall ,(symbol-function 'set-file-times) file (or time ,at))))
  428. ((symbol-function 'time-add)
  429. (lambda (a b) (funcall ,(symbol-function 'time-add)
  430. (or a ,at) (or b ,at))))
  431. ((symbol-function 'time-equal-p)
  432. (lambda (a b) (funcall ,(symbol-function 'time-equal-p)
  433. (or a ,at) (or b ,at))))
  434. ((symbol-function 'time-less-p)
  435. (lambda (a b) (funcall ,(symbol-function 'time-less-p)
  436. (or a ,at) (or b ,at))))
  437. ((symbol-function 'time-subtract)
  438. (lambda (a b) (funcall ,(symbol-function 'time-subtract)
  439. (or a ,at) (or b ,at)))))
  440. ,@body))))
  441. (provide 'org-test)
  442. ;;; org-test.el ends here