org-test.el 14 KB

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