2010-10-02 17:23:27 -04:00
|
|
|
|
;;;; org-test.el --- Tests for Org-mode
|
|
|
|
|
|
2010-10-03 12:45:07 -04:00
|
|
|
|
;; Copyright (c) 2010 Sebastian Rose, Eric Schulte
|
2010-10-02 17:23:27 -04:00
|
|
|
|
;; Authors:
|
|
|
|
|
;; Sebastian Rose, Hannover, Germany, sebastian_rose gmx de
|
2010-10-03 12:45:07 -04:00
|
|
|
|
;; Eric Schulte, Santa Fe, New Mexico, USA, schulte.eric gmail com
|
2010-10-02 17:23:27 -04:00
|
|
|
|
|
|
|
|
|
;; Released under the GNU General Public License version 3
|
|
|
|
|
;; see: http://www.gnu.org/licenses/gpl-3.0.html
|
|
|
|
|
|
|
|
|
|
;;;; Comments:
|
|
|
|
|
|
|
|
|
|
;; Interactive testing for Org mode.
|
|
|
|
|
|
2010-10-03 12:45:07 -04:00
|
|
|
|
;; The heart of all this is the commands `org-test-current-defun'. If
|
|
|
|
|
;; called while in a `defun' all ert tests with names matching the
|
|
|
|
|
;; name of the function are run.
|
2010-10-02 17:23:27 -04:00
|
|
|
|
|
|
|
|
|
;;; Prerequisites:
|
|
|
|
|
|
2010-10-03 20:44:10 -04:00
|
|
|
|
;; ERT and jump.el are both included as git submodules, install with
|
2010-10-03 12:45:07 -04:00
|
|
|
|
;; $ git submodule init
|
|
|
|
|
;; $ git submodule update
|
2010-10-02 17:23:27 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;;; Code:
|
2010-10-03 12:53:25 -04:00
|
|
|
|
(let* ((org-test-dir (expand-file-name
|
|
|
|
|
(file-name-directory
|
|
|
|
|
(or load-file-name buffer-file-name))))
|
|
|
|
|
(load-path (cons
|
|
|
|
|
(expand-file-name "ert" org-test-dir)
|
|
|
|
|
(cons
|
|
|
|
|
(expand-file-name "jump" org-test-dir)
|
|
|
|
|
load-path))))
|
|
|
|
|
(require 'ert-batch)
|
|
|
|
|
(require 'ert)
|
|
|
|
|
(require 'ert-exp)
|
|
|
|
|
(require 'ert-exp-t)
|
|
|
|
|
(require 'ert-run)
|
|
|
|
|
(require 'ert-ui)
|
|
|
|
|
(require 'jump)
|
|
|
|
|
(require 'which-func)
|
|
|
|
|
(require 'org))
|
2010-10-02 17:23:27 -04:00
|
|
|
|
|
|
|
|
|
(defconst org-test-default-test-file-name "tests.el"
|
|
|
|
|
"For each defun a separate file with tests may be defined.
|
|
|
|
|
tests.el is the fallback or default if you like.")
|
|
|
|
|
|
|
|
|
|
(defconst org-test-default-directory-name "testing"
|
|
|
|
|
"Basename or the directory where the tests live.
|
|
|
|
|
org-test searches this directory up the directory tree.")
|
|
|
|
|
|
2010-10-03 12:45:07 -04:00
|
|
|
|
(defconst org-test-dir
|
|
|
|
|
(expand-file-name (file-name-directory (or load-file-name buffer-file-name))))
|
2010-10-02 17:23:27 -04:00
|
|
|
|
|
2010-10-03 20:44:10 -04:00
|
|
|
|
(defconst org-base-dir
|
|
|
|
|
(expand-file-name ".." org-test-dir))
|
|
|
|
|
|
2010-10-04 21:36:39 -04:00
|
|
|
|
(defconst org-test-example-dir
|
|
|
|
|
(expand-file-name "examples" org-test-dir))
|
2010-10-02 17:23:27 -04:00
|
|
|
|
|
2010-10-04 21:36:39 -04:00
|
|
|
|
(defconst org-test-file
|
|
|
|
|
(expand-file-name "normal.org" org-test-example-dir))
|
|
|
|
|
|
2010-10-04 22:07:51 -04:00
|
|
|
|
(defconst org-test-no-heading-file
|
|
|
|
|
(expand-file-name "no-heading.org" org-test-example-dir))
|
2010-10-04 21:36:39 -04:00
|
|
|
|
|
|
|
|
|
(defconst org-test-link-in-heading-file
|
|
|
|
|
(expand-file-name "link-in-heading.org" org-test-dir))
|
2010-10-04 10:29:00 -04:00
|
|
|
|
|
2010-10-02 17:23:27 -04:00
|
|
|
|
|
2010-10-03 12:45:07 -04:00
|
|
|
|
;;; Functions for writing tests
|
2010-10-02 17:23:27 -04:00
|
|
|
|
(defun org-test-buffer (&optional file)
|
|
|
|
|
"TODO: Setup and return a buffer to work with.
|
|
|
|
|
If file is non-nil insert it's contents in there.")
|
|
|
|
|
|
|
|
|
|
(defun org-test-compare-with-file (&optional file)
|
|
|
|
|
"TODO: Compare the contents of the test buffer with FILE.
|
|
|
|
|
If file is not given, search for a file named after the test
|
|
|
|
|
currently executed.")
|
|
|
|
|
|
2010-10-06 12:23:50 -04:00
|
|
|
|
(defmacro org-test-at-id (id &rest body)
|
|
|
|
|
"Run body after placing the point in the headline identified by ID."
|
|
|
|
|
(declare (indent 1))
|
|
|
|
|
`(let* ((id-location (org-id-find ,id))
|
|
|
|
|
(id-file (car id-location))
|
|
|
|
|
(visited-p (get-file-buffer id-file))
|
|
|
|
|
to-be-removed)
|
|
|
|
|
(save-window-excursion
|
|
|
|
|
(save-match-data
|
|
|
|
|
(org-id-goto ,id)
|
|
|
|
|
(setq to-be-removed (current-buffer))
|
|
|
|
|
(condition-case nil
|
|
|
|
|
(progn
|
|
|
|
|
(org-show-subtree)
|
|
|
|
|
(org-show-block-all))
|
|
|
|
|
(error nil))
|
|
|
|
|
(save-restriction ,@body)))
|
|
|
|
|
(unless visited-p
|
|
|
|
|
(kill-buffer to-be-removed))))
|
|
|
|
|
|
2010-10-04 10:06:16 -04:00
|
|
|
|
(defmacro org-test-in-example-file (file &rest body)
|
2010-10-03 12:45:07 -04:00
|
|
|
|
"Execute body in the Org-mode example file."
|
2010-10-04 10:06:16 -04:00
|
|
|
|
(declare (indent 1))
|
2010-10-04 21:36:39 -04:00
|
|
|
|
`(let* ((my-file (or ,file org-test-file))
|
2010-10-04 10:29:00 -04:00
|
|
|
|
(visited-p (get-file-buffer my-file))
|
|
|
|
|
to-be-removed)
|
2010-10-03 12:45:07 -04:00
|
|
|
|
(save-window-excursion
|
|
|
|
|
(save-match-data
|
2010-10-04 10:29:00 -04:00
|
|
|
|
(find-file my-file)
|
2010-10-03 12:45:07 -04:00
|
|
|
|
(setq to-be-removed (current-buffer))
|
|
|
|
|
(goto-char (point-min))
|
2010-10-04 10:29:00 -04:00
|
|
|
|
(condition-case nil
|
|
|
|
|
(progn
|
|
|
|
|
(outline-next-visible-heading 1)
|
|
|
|
|
(org-show-subtree)
|
|
|
|
|
(org-show-block-all))
|
|
|
|
|
(error nil))
|
2010-10-06 12:23:50 -04:00
|
|
|
|
(save-restriction ,@body)))
|
2010-10-03 12:45:07 -04:00
|
|
|
|
(unless visited-p
|
|
|
|
|
(kill-buffer to-be-removed))))
|
2010-10-02 17:23:27 -04:00
|
|
|
|
|
2010-10-04 10:06:16 -04:00
|
|
|
|
(defmacro org-test-at-marker (file marker &rest body)
|
|
|
|
|
"Run body after placing the point at MARKER in FILE.
|
|
|
|
|
Note the uuidgen command-line command can be useful for
|
|
|
|
|
generating unique markers for insertion as anchors into org
|
|
|
|
|
files."
|
|
|
|
|
(declare (indent 2))
|
|
|
|
|
`(org-test-in-example-file ,file
|
|
|
|
|
(goto-char (point-min))
|
|
|
|
|
(re-search-forward (regexp-quote ,marker))
|
|
|
|
|
,@body))
|
|
|
|
|
|
2010-10-02 17:23:27 -04:00
|
|
|
|
|
2010-10-03 20:44:10 -04:00
|
|
|
|
;;; Navigation Functions
|
2010-10-15 14:34:44 -04:00
|
|
|
|
(defjump org-test-jump
|
|
|
|
|
(("lisp/\\1.el" . "testing/lisp/test-\\1.el")
|
|
|
|
|
("lisp/\\1.el" . "testing/lisp/\\1.el/test.*.el")
|
|
|
|
|
("contrib/lisp/\\1.el" . "testing/contrib/lisp/test-\\1.el")
|
|
|
|
|
("contrib/lisp/\\1.el" . "testing/contrib/lisp/\\1.el/test.*.el")
|
|
|
|
|
("testing/lisp/test-\\1.el" . "lisp/\\1.el")
|
|
|
|
|
("testing/lisp/\\1.el" . "lisp/\\1.el/test.*.el")
|
|
|
|
|
("testing/contrib/lisp/test-\\1.el" . "contrib/lisp/\\1.el")
|
|
|
|
|
("testing/contrib/lisp/test-\\1.el" . "contrib/lisp/\\1.el/test.*.el"))
|
2010-10-03 20:44:10 -04:00
|
|
|
|
(concat org-base-dir "/")
|
|
|
|
|
"Jump between org-mode files and their tests."
|
|
|
|
|
(lambda (path)
|
2010-10-04 10:29:00 -04:00
|
|
|
|
(let* ((full-path (expand-file-name path org-base-dir))
|
|
|
|
|
(file-name (file-name-nondirectory path))
|
|
|
|
|
(name (file-name-sans-extension file-name)))
|
2010-10-03 20:44:10 -04:00
|
|
|
|
(find-file full-path)
|
|
|
|
|
(insert
|
2010-10-04 10:29:00 -04:00
|
|
|
|
";;; " file-name "\n\n"
|
2010-10-15 20:16:52 -04:00
|
|
|
|
";; Copyright (c) " (nth 5 (decode-time (current-time)))
|
|
|
|
|
" " user-full-name "\n"
|
2010-10-03 20:44:10 -04:00
|
|
|
|
";; Authors: " user-full-name "\n\n"
|
|
|
|
|
";; Released under the GNU General Public License version 3\n"
|
|
|
|
|
";; see: http://www.gnu.org/licenses/gpl-3.0.html\n\n"
|
|
|
|
|
";;;; Comments:\n\n"
|
|
|
|
|
";; Template test file for Org-mode tests\n\n"
|
|
|
|
|
"\n"
|
|
|
|
|
";;; Code:\n"
|
2010-10-06 11:02:25 -04:00
|
|
|
|
"(let ((load-path (cons (expand-file-name\n"
|
|
|
|
|
" \"..\" (file-name-directory\n"
|
|
|
|
|
" (or load-file-name buffer-file-name)))\n"
|
|
|
|
|
" load-path)))\n"
|
|
|
|
|
" (require 'org-test)\n"
|
|
|
|
|
" (require 'org-test-ob-consts))\n\n"
|
2010-10-03 20:44:10 -04:00
|
|
|
|
"\n"
|
|
|
|
|
";;; Tests\n"
|
|
|
|
|
"(ert-deftest " name "/example-test ()\n"
|
|
|
|
|
" \"Just an example to get you started.\"\n"
|
|
|
|
|
" (should t)\n"
|
|
|
|
|
" (should-not nil)\n"
|
2010-10-04 10:29:00 -04:00
|
|
|
|
" (should-error (error \"errr...\")))\n\n\n"
|
|
|
|
|
"(provide '" name ")\n\n"
|
|
|
|
|
";;; " file-name " ends here\n") full-path))
|
2010-10-04 10:06:16 -04:00
|
|
|
|
(lambda () ((lambda (res) (if (listp res) (car res) res)) (which-function))))
|
2010-10-02 18:50:57 -04:00
|
|
|
|
|
2010-10-05 22:51:04 -04:00
|
|
|
|
(define-key emacs-lisp-mode-map "\M-\C-j" 'org-test-jump)
|
|
|
|
|
|
2011-02-01 00:28:34 -05:00
|
|
|
|
|
|
|
|
|
;;; Miscellaneous helper functions
|
|
|
|
|
(defun org-test-strip-text-props (s)
|
|
|
|
|
"Return S without any text properties."
|
|
|
|
|
(let ((noprop (copy-sequence s)))
|
|
|
|
|
(set-text-properties 0 (length noprop) nil noprop)
|
|
|
|
|
noprop))
|
2010-10-03 20:44:10 -04:00
|
|
|
|
|
|
|
|
|
;;; Load and Run tests
|
2010-10-03 21:03:41 -04:00
|
|
|
|
(defun org-test-load ()
|
2010-10-03 12:45:07 -04:00
|
|
|
|
"Load up the org-mode test suite."
|
2010-10-02 17:23:27 -04:00
|
|
|
|
(interactive)
|
2010-10-03 21:03:41 -04:00
|
|
|
|
(flet ((rload (base)
|
|
|
|
|
(mapc
|
|
|
|
|
(lambda (path)
|
|
|
|
|
(if (file-directory-p path) (rload path) (load-file path)))
|
|
|
|
|
(directory-files base 'full
|
|
|
|
|
"^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*\\.el"))))
|
2010-10-04 10:06:16 -04:00
|
|
|
|
(rload (expand-file-name "lisp" org-test-dir))
|
|
|
|
|
(rload (expand-file-name "lisp"
|
|
|
|
|
(expand-file-name "contrib" org-test-dir)))))
|
2010-10-02 17:23:27 -04:00
|
|
|
|
|
2010-10-03 12:45:07 -04:00
|
|
|
|
(defun org-test-current-defun ()
|
|
|
|
|
"Test the current function."
|
2010-10-02 17:23:27 -04:00
|
|
|
|
(interactive)
|
2010-11-02 16:11:16 -04:00
|
|
|
|
(ert (which-function)))
|
2010-10-02 17:23:27 -04:00
|
|
|
|
|
2010-11-22 13:29:05 -05:00
|
|
|
|
(defun org-test-current-file ()
|
|
|
|
|
"Run all tests for current file."
|
|
|
|
|
(interactive)
|
|
|
|
|
(ert (concat "test-"
|
|
|
|
|
(file-name-sans-extension
|
|
|
|
|
(file-name-nondirectory (buffer-file-name)))
|
|
|
|
|
"/")))
|
|
|
|
|
|
2010-10-03 12:45:07 -04:00
|
|
|
|
(defun org-test-run-all-tests ()
|
2010-10-06 12:23:50 -04:00
|
|
|
|
"Run all defined tests matching \"\\(org\\|ob\\)\".
|
2010-10-03 12:45:07 -04:00
|
|
|
|
Load all test files first."
|
2010-10-02 18:50:57 -04:00
|
|
|
|
(interactive)
|
2010-10-03 21:03:41 -04:00
|
|
|
|
(org-test-load)
|
2010-10-06 12:23:50 -04:00
|
|
|
|
(ert "\\(org\\|ob\\)"))
|
2010-10-02 17:23:27 -04:00
|
|
|
|
|
|
|
|
|
(provide 'org-test)
|
2010-10-03 12:45:07 -04:00
|
|
|
|
|
2010-10-02 17:23:27 -04:00
|
|
|
|
;;; org-test.el ends here
|