branch: master commit 6c6c83a705dcb0ad3cfbab2c06b013f8ed58ee03 Author: rocky <ro...@gnu.org> Commit: rocky <ro...@gnu.org>
Version 1.2.0 Sync with github github syohex: * Switch from Carton to Cask rubiki...@ruby-lang.org: * test-simple.el: test-simple-run: make it a command. * README.md: Mention test-simple-run * example/gcd-tests.el: gcd-tests.el: Add test-simple-run comment line * test-simple.el: Emacs exits abnormally when noninteractive test fails. * test-simple.el: New function `test-simple-run': register test You can run tests easily by pressing C-x C-z. * test-basic.el: fix botched joke --- packages/test-simple/Carton | 4 - packages/test-simple/Cask | 4 + packages/test-simple/README.md | 15 ++++ packages/test-simple/THANKS | 1 + .../example/{test-gcd.el => gcd-tests.el} | 3 +- packages/test-simple/test-simple.el | 84 ++++++++++++++++---- packages/test-simple/test/test-basic.el | 2 +- 7 files changed, 90 insertions(+), 23 deletions(-) diff --git a/packages/test-simple/Carton b/packages/test-simple/Carton deleted file mode 100644 index b039f40..0000000 --- a/packages/test-simple/Carton +++ /dev/null @@ -1,4 +0,0 @@ -(source "melpa" "http://melpa.milkbox.net/packages/") - -(package "test-simple" "0.2.1" - "Unit tests for GNU emacs that work interactively and in batch") diff --git a/packages/test-simple/Cask b/packages/test-simple/Cask new file mode 100644 index 0000000..ee870dc --- /dev/null +++ b/packages/test-simple/Cask @@ -0,0 +1,4 @@ +(source gnu) +(source melpa) + +(package-file "test-simple.el") diff --git a/packages/test-simple/README.md b/packages/test-simple/README.md index 7a473b0..431e054 100644 --- a/packages/test-simple/README.md +++ b/packages/test-simple/README.md @@ -67,5 +67,20 @@ Now let's try from a command line: ...... 0 failures in 6 assertions (0.000723 seconds) +You can run noninteractive tests inside Emacs by `test-simple-run`. +Add the following at a test file: + + ;;;; (test-simple-run "emacs -batch -L %s -l %s" (file-name-directory (locate-library "test-simple.elc")) buffer-file-name) + +Press C-x C-e at the `test-simple-run` line to run this test file. +Then press C-x C-z, which is customizable by `test-simple-runner-key`, to run it more. +If you have installed `bpr` package, use it by default because it only pops up window when the running program exits abnormally. + +`test-simple-run` can be called interactively. +In this case, the command line is set above as the simplest case. +But you run test with dependency, you must use the sexp comment form. + + ;;;; (test-simple-run "emacs -batch -L %s -L %s -l %s" (file-name-directory (locate-library "test-simple.elc")) (file-name-directory (locate-library "foo")) buffer-file-name) + *Author:* Rocky Bernstein <ro...@gnu.org> <br> [](https://coderwall.com/rocky) diff --git a/packages/test-simple/THANKS b/packages/test-simple/THANKS index 4dae07d..a0e7ef1 100644 --- a/packages/test-simple/THANKS +++ b/packages/test-simple/THANKS @@ -1 +1,2 @@ Lars Andersen (expez) - Getting this packaged and put on to Melpa. +rubyikitch: Greatly improved noninteractive testing. diff --git a/packages/test-simple/example/test-gcd.el b/packages/test-simple/example/gcd-tests.el similarity index 90% rename from packages/test-simple/example/test-gcd.el rename to packages/test-simple/example/gcd-tests.el index 8ffdce8..b4e58d6 100644 --- a/packages/test-simple/example/test-gcd.el +++ b/packages/test-simple/example/gcd-tests.el @@ -1,4 +1,4 @@ -;;; test-simple.el --- Simple Unit Test Framework for Emacs Lisp +;;; gcd-tests.el ;; Copyright (C) 2015 Free Software Foundation, Inc ;; Author: Rocky Bernstein <ro...@gnu.org> @@ -19,6 +19,7 @@ ;; You should have received a copy of the GNU General Public License ;; along with this program. If not, see ;; <http://www.gnu.org/licenses/>. +;;;; (test-simple-run "emacs -batch -L %s -l %s" (file-name-directory (locate-library "test-simple.elc")) buffer-file-name) (require 'test-simple) (test-simple-start) diff --git a/packages/test-simple/test-simple.el b/packages/test-simple/test-simple.el index bd576e1..1914ac0 100644 --- a/packages/test-simple/test-simple.el +++ b/packages/test-simple/test-simple.el @@ -1,13 +1,13 @@ -;;; test-simple.el --- Simple Unit Test Framework for Emacs Lisp +;;; test-simple.el --- Simple Unit Test Framework for Emacs Lisp -*- lexical-binding: t -*- ;; Rewritten from Phil Hagelberg's behave.el by rocky -;; Copyright (C) 2015 Free Software Foundation, Inc +;; Copyright (C) 2015, 2016 Free Software Foundation, Inc ;; Author: Rocky Bernstein <ro...@gnu.org> ;; URL: http://github.com/rocky/emacs-test-simple ;; Keywords: unit-test ;; Package-Requires: ((cl-lib "0")) -;; Version: 1.1 +;; Version: 1.2.0 ;; This program is free software: you can redistribute it and/or ;; modify it under the terms of the GNU General Public License as @@ -36,7 +36,7 @@ ;; add customized assert failure messages, or add summary messages ;; before a group of tests. ;; -;; * Accomodates both interactive and non-interactive use. +;; * Accommodates both interactive and non-interactive use. ;; - For interactive use, one can use `eval-last-sexp', `eval-region', ;; and `eval-buffer'. One can `edebug' the code. ;; - For non-interactive use, run: @@ -64,17 +64,17 @@ ;; (assert-equal 8 (gcd 8 32) "gcd(8,32)") ;; (end-tests) ;; Stop the clock and print a summary ;; -;; Edit (with Emacs of course) test-gcd.el and run M-x eval-current-buffer +;; Edit (with Emacs of course) gcd-tests.el and run M-x eval-current-buffer ;; ;; You should see in buffer *test-simple*: ;; -;; test-gcd.el +;; gcd-tests.el ;; ...... ;; 0 failures in 6 assertions (0.002646 seconds) ;; ;; Now let us try from a command line: ;; -;; $ emacs --batch --no-site-file --no-splash --load test-gcd.el +;; $ emacs --batch --no-site-file --no-splash --load gcd-tests.el ;; Loading /src/external-vcs/emacs-test-simple/example/gcd.el (source)... ;; *scratch* ;; ...... @@ -91,6 +91,26 @@ (eval-when-compile (require 'cl-lib)) +(defgroup test-simple nil + "Simple Unit Test Framework for Emacs Lisp" + :group 'lisp) + +(defcustom test-simple-runner-interface (if (fboundp 'bpr-spawn) + 'bpr-spawn + 'compile) + "Function with one string argument when running tests non-interactively. +Command line started with `emacs --batch' is passed as the argument. + +`bpr-spawn', which is in bpr package, is preferable because of no window popup. +If bpr is not installed, fall back to `compile'." + :type 'function + :group 'test-simple) + +(defcustom test-simple-runner-key "C-x C-z" + "Key to run non-interactive test after defining command line by `test-simple-run'." + :type 'string + :group 'test-simple) + (defvar test-simple-debug-on-error nil "If non-nil raise an error on the first failure.") @@ -156,7 +176,7 @@ clears out information from the previous run." ;; Assertion tests ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -(defmacro assert-raises (error-condition body &optional fail-message test-info) +(defmacro assert-raises (error-condition body &optional fail-message) (let ((fail-message (or fail-message (format "assert-raises did not get expected %s" error-condition)))) @@ -175,7 +195,7 @@ clears out information from the previous run." (format "Message: %s" fail-message) "")) (expect-message - (format "\n Expected: %s\n Got: %s" expected actual)) + (format "\n Expected: %S\n Got: %S" expected actual)) (test-info-mess (if (boundp 'test-info) (test-info-description test-info) @@ -217,14 +237,12 @@ clears out information from the previous run." (progn (test-simple-msg ".") t))) (defun assert-t (actual &optional fail-message test-info) - "Expectation is that ACTUAL is not nil." - (assert-nil (not actual) fail-message test-info "assert-t")) - -(defun assert-nil (actual &optional fail-message test-info _assert-type) - "Expectation is that ACTUAL is nil. -FAIL-MESSAGE is an optional additional message to be displayed. -Since several assertions funnel down to this one, ASSERT-TYPE is an -optional type." + "expectation is that ACTUAL is not nil." + (assert-nil (not actual) fail-message test-info)) + +(defun assert-nil (actual &optional fail-message test-info) + "expectation is that ACTUAL is nil. FAIL-MESSAGE is an optional +additional message to be displayed." (unless test-info (setq test-info test-simple-info)) (cl-incf (test-info-assert-count test-info)) (if actual @@ -321,5 +339,37 @@ optional type." (goto-char (point-max)) (test-simple-msg (test-simple-summary-line test-info))) +;;;###autoload +(defun test-simple-run (&rest command-line-formats) + "Register command line to run tests non-interactively and bind key to run test. +After calling this function, you can run test by key specified by `test-simple-runner-key'. + +It is preferable to write at the first line of test files as a comment, e.g, +;;;; (test-simple-run \"emacs -batch -L %s -l %s\" (file-name-directory (locate-library \"test-simple.elc\")) buffer-file-name) + +Calling this function interactively, COMMAND-LINE-FORMATS is set above." + (interactive) + (setq command-line-formats + (or command-line-formats + (list "emacs -batch -L %s -l %s" + (file-name-directory (locate-library "test-simple.elc")) + buffer-file-name))) + (let ((func (lambda () + (interactive) + (funcall test-simple-runner-interface + (apply 'format command-line-formats))))) + (global-set-key (kbd test-simple-runner-key) func) + (funcall func))) + +(defun test-simple-noninteractive-kill-emacs-hook () + "Emacs exits abnormally when noninteractive test fails." + (when (and noninteractive test-simple-info + (<= 1 (test-info-failure-count test-simple-info))) + (let (kill-emacs-hook) + (kill-emacs 1)))) +(when noninteractive + (add-hook 'kill-emacs-hook 'test-simple-noninteractive-kill-emacs-hook)) + + (provide 'test-simple) ;;; test-simple.el ends here diff --git a/packages/test-simple/test/test-basic.el b/packages/test-simple/test/test-basic.el index f14a385..723779a 100644 --- a/packages/test-simple/test/test-basic.el +++ b/packages/test-simple/test/test-basic.el @@ -22,7 +22,7 @@ (assert-t (memq 'test-simple features) "'test-simple provided") (assert-nil nil "assert-nil failure test") -(assert-nil nil "Knights if ni") +(assert-nil nil "Knights of ni") (assert-equal 5 (+ 1 4) "assert-equal") (assert-raises error (error "you should not see this") "assert-raises")