org-test.el 7.9 KB

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