org-test.el 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 installed as git submodules to install
  15. ;; them run
  16. ;; $ git submodule init
  17. ;; $ git submodule update
  18. ;;;; Code:
  19. (require 'ert-batch)
  20. (require 'ert)
  21. (require 'ert-exp)
  22. (require 'ert-exp-t)
  23. (require 'ert-run)
  24. (require 'ert-ui)
  25. (require 'which-func)
  26. (require 'org)
  27. (defconst org-test-default-test-file-name "tests.el"
  28. "For each defun a separate file with tests may be defined.
  29. tests.el is the fallback or default if you like.")
  30. (defconst org-test-default-directory-name "testing"
  31. "Basename or the directory where the tests live.
  32. org-test searches this directory up the directory tree.")
  33. (defconst org-test-dir
  34. (expand-file-name (file-name-directory (or load-file-name buffer-file-name))))
  35. (defconst org-test-example-file-name
  36. (expand-file-name "example-file.org" org-test-dir))
  37. ;;; Functions for writing tests
  38. ;; TODO
  39. (defun org-test-buffer (&optional file)
  40. "TODO: Setup and return a buffer to work with.
  41. If file is non-nil insert it's contents in there.")
  42. ;; TODO
  43. (defun org-test-compare-with-file (&optional file)
  44. "TODO: Compare the contents of the test buffer with FILE.
  45. If file is not given, search for a file named after the test
  46. currently executed.")
  47. (defmacro in-org-example-file (&rest body)
  48. "Execute body in the Org-mode example file."
  49. (declare (indent 0))
  50. `(let ((visited-p (get-file-buffer org-test-example-file-name))
  51. to-be-removed)
  52. (save-window-excursion
  53. (save-match-data
  54. (find-file org-test-example-file-name)
  55. (setq to-be-removed (current-buffer))
  56. (goto-char (point-min))
  57. (outline-next-visible-heading 1)
  58. (org-show-subtree)
  59. (org-show-block-all)
  60. ,@body))
  61. (unless visited-p
  62. (kill-buffer to-be-removed))))
  63. ;;; Load and Run tests
  64. (defun org-load-tests ()
  65. "Load up the org-mode test suite."
  66. (interactive)
  67. (mapc (lambda (file) (load-file file))
  68. (directory-files (expand-file-name "lisp" org-test-dir)
  69. 'full "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*\\.el")))
  70. (defun org-test-current-defun ()
  71. "Test the current function."
  72. (interactive)
  73. (ert (car (which-function))))
  74. (defun org-test-run-all-tests ()
  75. "Run all defined tests matching \"^org\".
  76. Load all test files first."
  77. (interactive)
  78. (org-load-tests)
  79. (ert "^org"))
  80. (provide 'org-test)
  81. ;;; org-test.el ends here