Explorar o código

Create a test file per defun.

* org-test-which-func: New function.  Find name of defun around point.

* org-test-ensure-buffer-emacs-lisp-p: New function.  Ensure a buffer
  hold an elisp file based on filename extension.
Sebastian Rose, Hannover, Germany %!s(int64=15) %!d(string=hai) anos
pai
achega
2b52d1911c
Modificáronse 2 ficheiros con 61 adicións e 26 borrados
  1. 5 0
      testing/README.org
  2. 56 26
      testing/org-test.el

+ 5 - 0
testing/README.org

@@ -15,6 +15,11 @@
       not yet exist, create them.  If the file did not yet exist,
       not yet exist, create them.  If the file did not yet exist,
       insert a little template test to get started.
       insert a little template test to get started.
 
 
+  * =org-test-edit-current-defuns-tests= :: Open the file with tests for
+      the current defun.  Note: this opens a file with the name of the
+      current defun.  If you do not expect many tests for a file to be
+      written, you could as call `org-test-edit-buffer-file-tests'.
+
   * =org-test-test-current-defun= :: Look up test files for defun at
   * =org-test-test-current-defun= :: Look up test files for defun at
        point and execute the tests.  This is done by searching an
        point and execute the tests.  This is done by searching an
        elisp file with the name of the current defun plus ".el".  In
        elisp file with the name of the current defun plus ".el".  In

+ 56 - 26
testing/org-test.el

@@ -157,7 +157,7 @@ currently executed.")
   "Run all tests matched by SELECTOR.
   "Run all tests matched by SELECTOR.
 SELECTOR defaults to \"^org\".
 SELECTOR defaults to \"^org\".
 See the docstring of `ert-select-tests' for valid selectors.
 See the docstring of `ert-select-tests' for valid selectors.
-Tests are run inside
+Unless `ert', this function runs all tests inside
  (let ((deactivate-mark nil))
  (let ((deactivate-mark nil))
     (save-excursion
     (save-excursion
       (save-match-data
       (save-match-data
@@ -192,6 +192,31 @@ Uses `org-test-run-tests' to run the actual tests."
   (org-test-run-tests)))
   (org-test-run-tests)))
 
 
 
 
+
+;;; Utility functions:
+
+(defun org-test-which-func ()
+  "Return the name of the current defun."
+  (save-excursion
+    (save-match-data
+      (end-of-line)
+      (beginning-of-defun)
+      (if (looking-at "(defun[[:space:]]+\\([^([:space:]]*\\)[[:space:]]*(")
+	  (match-string-no-properties 1)
+	(error "No defun found around point.")))))
+
+(defun org-test-ensure-buffer-emacs-lisp-p (&optional buffer)
+  "Ensure BUFFER contains an elisp file based on extension.
+If BUFFER is nil, use the current buffer.
+Error if not."
+  (save-excursion
+    (save-match-data
+      ;; Check, if editing an emacs-lisp file
+      (with-current-buffer (or buffer (current-buffer))
+	(unless
+	    (string-match "\\.el$" buffer-file-name)
+	(error "Not an emacs lisp file: %s" buffer-file-name))))))
+
 
 
 ;;; Commands:
 ;;; Commands:
 
 
@@ -199,22 +224,17 @@ Uses `org-test-run-tests' to run the actual tests."
   "Execute all tests for function at point if tests exist."
   "Execute all tests for function at point if tests exist."
   (interactive)
   (interactive)
   (ert-delete-all-tests)
   (ert-delete-all-tests)
-  (save-excursion
-    (save-match-data
-      (end-of-line)
-      (beginning-of-defun)
-      (when (looking-at "(defun[[:space:]]+\\([^([:space:]]*\\)[[:space:]]*(")
-        (let* ((fun (match-string-no-properties 1))
-	       (dir (org-test-test-directory-for-file buffer-file-name))
-               (tf (or (org-test-test-file-name-for-defun
-			dir fun buffer-file-name)
-		       (org-test-test-file-name-for-file dir buffer-file-name))))
-          (if tf
-	      (progn
-		(load-file tf)
-		(org-test-run-tests
-		 (concat "^" fun)))
-            (error "No test files found for \"%s\"" fun)))))))
+  (let* ((fun (org-test-wich-func))
+	 (dir (org-test-test-directory-for-file buffer-file-name))
+	 (tf (or (org-test-test-file-name-for-defun
+		  dir fun buffer-file-name)
+		 (org-test-test-file-name-for-file dir buffer-file-name))))
+    (if tf
+	(progn
+	  (load-file tf)
+	  (org-test-run-tests
+	   (concat "^" fun)))
+      (error "No test files found for \"%s\"" fun))))
 
 
 (defun org-test-test-buffer-file (&optional only)
 (defun org-test-test-buffer-file (&optional only)
   "Run all tests for current `buffer-file-name' if tests exist.
   "Run all tests for current `buffer-file-name' if tests exist.
@@ -245,16 +265,12 @@ file only."
 	       "test files")
 	       "test files")
 	     buffer-file-name))))
 	     buffer-file-name))))
 
 
-(defun org-test-edit-buffer-file-tests ()
+(defun org-test-edit-buffer-file-tests (&optional func)
   "Open the `org-test-default-test-file-name' file for editing.
   "Open the `org-test-default-test-file-name' file for editing.
 If the file (and parent directories) do not yet exist,
 If the file (and parent directories) do not yet exist,
 create them."
 create them."
   (interactive)
   (interactive)
-  (save-match-data
-    ;; Check, if editing an emacs-lisp file
-    (unless
-	(string-match "\\.el$" buffer-file-name)
-      (error "Not an emacs lisp file: %s" buffer-file-name)))
+  (org-test-ensure-buffer-emacs-lisp-p)
 
 
   (let ((dir (org-test-test-directory-for-file
   (let ((dir (org-test-test-directory-for-file
 	      buffer-file-name)))
 	      buffer-file-name)))
@@ -262,7 +278,11 @@ create them."
       (error "Directory %s not found. Sorry."
       (error "Directory %s not found. Sorry."
 	     org-test-default-directory-name))
 	     org-test-default-directory-name))
 
 
-    (let* ((tf     (concat dir org-test-default-test-file-name))
+    (let* ((tf     (concat
+		    dir
+		    (if func
+			(concat func ".el")
+			org-test-default-test-file-name)))
 	  (exists  (file-exists-p tf))
 	  (exists  (file-exists-p tf))
 	  (rel     (file-relative-name buffer-file-name dir))
 	  (rel     (file-relative-name buffer-file-name dir))
 	  (tprefix (file-name-nondirectory
 	  (tprefix (file-name-nondirectory
@@ -272,9 +292,11 @@ create them."
       (find-file tf)
       (find-file tf)
       (unless exists
       (unless exists
 	(insert
 	(insert
-	 ";;; " org-test-default-test-file-name " --- Tests for "
+	 ";;; " (file-name-nondirectory tf) "\n"
+	 ";; Tests for `"
+	 (if func (concat  func "' in `") "")
 	 (replace-regexp-in-string "^\\(?:\\.+/\\)+" "" rel)
 	 (replace-regexp-in-string "^\\(?:\\.+/\\)+" "" rel)
-	 "\n\n"
+	 "'\n\n"
 	 "\n"
 	 "\n"
 	 ";;; Code:\n"
 	 ";;; Code:\n"
 	 "(require 'org-test)\n"
 	 "(require 'org-test)\n"
@@ -289,6 +311,14 @@ create them."
 	 "  (should-not nil)\n"
 	 "  (should-not nil)\n"
 	 "  (should-error (error \"errr...\")))\n")))))
 	 "  (should-error (error \"errr...\")))\n")))))
 
 
+(defun org-test-edit-current-defuns-tests ()
+  "Open the file with tests related to the current defun.
+If the file (and parent directories) do not yet exist,
+create them."
+  (interactive)
+  (org-test-ensure-buffer-emacs-lisp-p)
+  (org-test-edit-buffer-file-tests
+   (org-test-which-func)))
 
 
 
 
 (provide 'org-test)
 (provide 'org-test)