org-test.el 7.2 KB

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