org-test.el 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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-file-name
  46. (expand-file-name "example-file.org" org-test-dir))
  47. (defconst org-test-no-header-example-file-name
  48. (expand-file-name "example-file-no-header.org" org-test-dir))
  49. (defconst test-org-code-block-anchor
  50. "94839181-184f-4ff4-a72f-94214df6f5ba")
  51. ;;; Functions for writing tests
  52. (defun org-test-buffer (&optional file)
  53. "TODO: Setup and return a buffer to work with.
  54. If file is non-nil insert it's contents in there.")
  55. (defun org-test-compare-with-file (&optional file)
  56. "TODO: Compare the contents of the test buffer with FILE.
  57. If file is not given, search for a file named after the test
  58. currently executed.")
  59. (defmacro org-test-in-example-file (file &rest body)
  60. "Execute body in the Org-mode example file."
  61. (declare (indent 1))
  62. `(let* ((my-file (or ,file org-test-example-file-name))
  63. (visited-p (get-file-buffer my-file))
  64. to-be-removed)
  65. (save-window-excursion
  66. (save-match-data
  67. (find-file my-file)
  68. (setq to-be-removed (current-buffer))
  69. (goto-char (point-min))
  70. (condition-case nil
  71. (progn
  72. (outline-next-visible-heading 1)
  73. (org-show-subtree)
  74. (org-show-block-all))
  75. (error nil))
  76. ,@body))
  77. (unless visited-p
  78. (kill-buffer to-be-removed))))
  79. (defmacro org-test-at-marker (file marker &rest body)
  80. "Run body after placing the point at MARKER in FILE.
  81. Note the uuidgen command-line command can be useful for
  82. generating unique markers for insertion as anchors into org
  83. files."
  84. (declare (indent 2))
  85. `(org-test-in-example-file ,file
  86. (goto-char (point-min))
  87. (re-search-forward (regexp-quote ,marker))
  88. ,@body))
  89. ;;; Navigation Functions
  90. (defjump 'org-test-jump
  91. '(("lisp/\\1.el" . "testing/lisp/test-\\1.el")
  92. ("lisp/\\1.el" . "testing/lisp/\\1.el/test.*.el")
  93. ("contrib/lisp/\\1.el" . "testing/contrib/lisp/test-\\1.el")
  94. ("contrib/lisp/\\1.el" . "testing/contrib/lisp/\\1.el/test.*.el")
  95. ("testing/lisp/test-\\1.el" . "lisp/\\1.el")
  96. ("testing/lisp/\\1.el" . "lisp/\\1.el/test.*.el")
  97. ("testing/contrib/lisp/test-\\1.el" . "contrib/lisp/\\1.el")
  98. ("testing/contrib/lisp/test-\\1.el" . "contrib/lisp/\\1.el/test.*.el"))
  99. (concat org-base-dir "/")
  100. "Jump between org-mode files and their tests."
  101. (lambda (path)
  102. (let* ((full-path (expand-file-name path org-base-dir))
  103. (file-name (file-name-nondirectory path))
  104. (name (file-name-sans-extension file-name)))
  105. (find-file full-path)
  106. (insert
  107. ";;; " file-name "\n\n"
  108. ";; Copyright (c) 2010 " user-full-name "\n"
  109. ";; Authors: " user-full-name "\n\n"
  110. ";; Released under the GNU General Public License version 3\n"
  111. ";; see: http://www.gnu.org/licenses/gpl-3.0.html\n\n"
  112. ";;;; Comments:\n\n"
  113. ";; Template test file for Org-mode tests\n\n"
  114. " \n"
  115. ";;; Code:\n"
  116. "(require 'org-test)\n\n"
  117. " \n"
  118. ";;; Tests\n"
  119. "(ert-deftest " name "/example-test ()\n"
  120. " \"Just an example to get you started.\"\n"
  121. " (should t)\n"
  122. " (should-not nil)\n"
  123. " (should-error (error \"errr...\")))\n\n\n"
  124. "(provide '" name ")\n\n"
  125. ";;; " file-name " ends here\n") full-path))
  126. (lambda () ((lambda (res) (if (listp res) (car res) res)) (which-function))))
  127. ;;; Load and Run tests
  128. (defun org-test-load ()
  129. "Load up the org-mode test suite."
  130. (interactive)
  131. (flet ((rload (base)
  132. (mapc
  133. (lambda (path)
  134. (if (file-directory-p path) (rload path) (load-file path)))
  135. (directory-files base 'full
  136. "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*\\.el"))))
  137. (rload (expand-file-name "lisp" org-test-dir))
  138. (rload (expand-file-name "lisp"
  139. (expand-file-name "contrib" org-test-dir)))))
  140. (defun org-test-current-defun ()
  141. "Test the current function."
  142. (interactive)
  143. (ert (car (which-function))))
  144. (defun org-test-run-all-tests ()
  145. "Run all defined tests matching \"^org\".
  146. Load all test files first."
  147. (interactive)
  148. (org-test-load)
  149. (ert "org"))
  150. (provide 'org-test)
  151. ;;; org-test.el ends here