org-test.el 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. ;;;; org-test.el --- Tests for Org-mode
  2. ;; Copyright (c) 2010 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. ;; Released under the GNU General Public License version 3
  7. ;; see: http://www.gnu.org/licenses/gpl-3.0.html
  8. ;;;; Comments:
  9. ;; Interactive testing for Org mode.
  10. ;; The heart of all this is the commands `org-test-current-defun'. If
  11. ;; called while in a `defun' all ert tests with names matching the
  12. ;; name of the function are run.
  13. ;;; Prerequisites:
  14. ;; ERT and jump.el are both included as git submodules, install with
  15. ;; $ git submodule init
  16. ;; $ git submodule update
  17. ;;;; Code:
  18. (let* ((org-test-dir (expand-file-name
  19. (file-name-directory
  20. (or load-file-name buffer-file-name))))
  21. (load-path (cons
  22. (expand-file-name "ert" org-test-dir)
  23. (cons
  24. (expand-file-name "jump" org-test-dir)
  25. load-path))))
  26. (require 'ert-batch)
  27. (require 'ert)
  28. (require 'ert-exp)
  29. (require 'ert-exp-t)
  30. (require 'ert-run)
  31. (require 'ert-ui)
  32. (require 'jump)
  33. (require 'which-func)
  34. (require 'org))
  35. (defconst org-test-default-test-file-name "tests.el"
  36. "For each defun a separate file with tests may be defined.
  37. tests.el is the fallback or default if you like.")
  38. (defconst org-test-default-directory-name "testing"
  39. "Basename or the directory where the tests live.
  40. org-test searches this directory up the directory tree.")
  41. (defconst org-test-dir
  42. (expand-file-name (file-name-directory (or load-file-name buffer-file-name))))
  43. (defconst org-base-dir
  44. (expand-file-name ".." org-test-dir))
  45. (defconst org-test-example-dir
  46. (expand-file-name "examples" org-test-dir))
  47. (defconst org-test-file
  48. (expand-file-name "normal.org" org-test-example-dir))
  49. (defconst org-test-no-heading-file
  50. (expand-file-name "no-heading.org" org-test-example-dir))
  51. (defconst org-test-link-in-heading-file
  52. (expand-file-name "link-in-heading.org" org-test-dir))
  53. ;;; Functions for writing tests
  54. (defun org-test-buffer (&optional file)
  55. "TODO: Setup and return a buffer to work with.
  56. If file is non-nil insert it's contents in there.")
  57. (defun org-test-compare-with-file (&optional file)
  58. "TODO: Compare the contents of the test buffer with FILE.
  59. If file is not given, search for a file named after the test
  60. currently executed.")
  61. (defmacro org-test-at-id (id &rest body)
  62. "Run body after placing the point in the headline identified by ID."
  63. (declare (indent 1))
  64. `(let* ((id-location (org-id-find ,id))
  65. (id-file (car id-location))
  66. (visited-p (get-file-buffer id-file))
  67. to-be-removed)
  68. (save-window-excursion
  69. (save-match-data
  70. (org-id-goto ,id)
  71. (setq to-be-removed (current-buffer))
  72. (condition-case nil
  73. (progn
  74. (org-show-subtree)
  75. (org-show-block-all))
  76. (error nil))
  77. (save-restriction ,@body)))
  78. (unless visited-p
  79. (kill-buffer to-be-removed))))
  80. (defmacro org-test-in-example-file (file &rest body)
  81. "Execute body in the Org-mode example file."
  82. (declare (indent 1))
  83. `(let* ((my-file (or ,file org-test-file))
  84. (visited-p (get-file-buffer my-file))
  85. to-be-removed)
  86. (save-window-excursion
  87. (save-match-data
  88. (find-file my-file)
  89. (setq to-be-removed (current-buffer))
  90. (goto-char (point-min))
  91. (condition-case nil
  92. (progn
  93. (outline-next-visible-heading 1)
  94. (org-show-subtree)
  95. (org-show-block-all))
  96. (error nil))
  97. (save-restriction ,@body)))
  98. (unless visited-p
  99. (kill-buffer to-be-removed))))
  100. (defmacro org-test-at-marker (file marker &rest body)
  101. "Run body after placing the point at MARKER in FILE.
  102. Note the uuidgen command-line command can be useful for
  103. generating unique markers for insertion as anchors into org
  104. files."
  105. (declare (indent 2))
  106. `(org-test-in-example-file ,file
  107. (goto-char (point-min))
  108. (re-search-forward (regexp-quote ,marker))
  109. ,@body))
  110. ;;; Navigation Functions
  111. (defjump org-test-jump
  112. (("lisp/\\1.el" . "testing/lisp/test-\\1.el")
  113. ("lisp/\\1.el" . "testing/lisp/\\1.el/test.*.el")
  114. ("contrib/lisp/\\1.el" . "testing/contrib/lisp/test-\\1.el")
  115. ("contrib/lisp/\\1.el" . "testing/contrib/lisp/\\1.el/test.*.el")
  116. ("testing/lisp/test-\\1.el" . "lisp/\\1.el")
  117. ("testing/lisp/\\1.el" . "lisp/\\1.el/test.*.el")
  118. ("testing/contrib/lisp/test-\\1.el" . "contrib/lisp/\\1.el")
  119. ("testing/contrib/lisp/test-\\1.el" . "contrib/lisp/\\1.el/test.*.el"))
  120. (concat org-base-dir "/")
  121. "Jump between org-mode files and their tests."
  122. (lambda (path)
  123. (let* ((full-path (expand-file-name path org-base-dir))
  124. (file-name (file-name-nondirectory path))
  125. (name (file-name-sans-extension file-name)))
  126. (find-file full-path)
  127. (insert
  128. ";;; " file-name "\n\n"
  129. ";; Copyright (c) " (nth 5 (decode-time (current-time)))
  130. " " user-full-name "\n"
  131. ";; Authors: " user-full-name "\n\n"
  132. ";; Released under the GNU General Public License version 3\n"
  133. ";; see: http://www.gnu.org/licenses/gpl-3.0.html\n\n"
  134. ";;;; Comments:\n\n"
  135. ";; Template test file for Org-mode tests\n\n"
  136. " \n"
  137. ";;; Code:\n"
  138. "(let ((load-path (cons (expand-file-name\n"
  139. " \"..\" (file-name-directory\n"
  140. " (or load-file-name buffer-file-name)))\n"
  141. " load-path)))\n"
  142. " (require 'org-test)\n"
  143. " (require 'org-test-ob-consts))\n\n"
  144. " \n"
  145. ";;; Tests\n"
  146. "(ert-deftest " name "/example-test ()\n"
  147. " \"Just an example to get you started.\"\n"
  148. " (should t)\n"
  149. " (should-not nil)\n"
  150. " (should-error (error \"errr...\")))\n\n\n"
  151. "(provide '" name ")\n\n"
  152. ";;; " file-name " ends here\n") full-path))
  153. (lambda () ((lambda (res) (if (listp res) (car res) res)) (which-function))))
  154. (define-key emacs-lisp-mode-map "\M-\C-j" 'org-test-jump)
  155. ;;; Load and Run tests
  156. (defun org-test-load ()
  157. "Load up the org-mode test suite."
  158. (interactive)
  159. (flet ((rload (base)
  160. (mapc
  161. (lambda (path)
  162. (if (file-directory-p path) (rload path) (load-file path)))
  163. (directory-files base 'full
  164. "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*\\.el"))))
  165. (rload (expand-file-name "lisp" org-test-dir))
  166. (rload (expand-file-name "lisp"
  167. (expand-file-name "contrib" org-test-dir)))))
  168. (defun org-test-current-defun ()
  169. "Test the current function."
  170. (interactive)
  171. (ert (which-function)))
  172. (defun org-test-current-file ()
  173. "Run all tests for current file."
  174. (interactive)
  175. (ert (concat "test-"
  176. (file-name-sans-extension
  177. (file-name-nondirectory (buffer-file-name)))
  178. "/")))
  179. (defun org-test-run-all-tests ()
  180. "Run all defined tests matching \"\\(org\\|ob\\)\".
  181. Load all test files first."
  182. (interactive)
  183. (org-test-load)
  184. (ert "\\(org\\|ob\\)"))
  185. (provide 'org-test)
  186. ;;; org-test.el ends here