branch: elpa/buttercup
commit edcffdecc164d9fbf2fb8083bfa7019ada7f1d6e
Author: Ola Nilsson <[email protected]>
Commit: Jorgen Schäfer <[email protected]>
Add start and end times to each suite or spec
The timestamps and the new function buttercup-elapsed-time can be used
by reporters to print elapsed time.
---
buttercup.el | 24 +++++++++++++++++--
tests/test-buttercup.el | 64 +++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 86 insertions(+), 2 deletions(-)
diff --git a/buttercup.el b/buttercup.el
index b1fcef7..e030472 100644
--- a/buttercup.el
+++ b/buttercup.el
@@ -670,7 +670,8 @@ See also `buttercup-define-matcher'."
(status 'passed)
failure-description
failure-stack
- )
+ time-started
+ time-ended)
(cl-defstruct (buttercup-suite (:include buttercup-suite-or-spec))
;; Any children of this suite, both suites and specs
@@ -771,6 +772,21 @@ See also `buttercup-define-matcher'."
(push name duplicates)
(push name seen)))))
+(defun buttercup--set-start-time (suite-or-spec)
+ "Set time-started of SUITE-OR-SPEC to `current-time'."
+ (setf (buttercup-suite-or-spec-time-started suite-or-spec) (current-time)))
+
+(defun buttercup--set-end-time (suite-or-spec)
+ "Set time-ended of SUITE-OR-SPEC to `current-time'."
+ (setf (buttercup-suite-or-spec-time-ended suite-or-spec) (current-time)))
+
+(defun buttercup-elapsed-time (suite-or-spec)
+ "Get elapsed time of SUITE-OR-SPEC."
+ ;; time-subtract does not handle nil arguments until Emacs 25.1
+ (time-subtract
+ (or (buttercup-suite-or-spec-time-ended suite-or-spec) (current-time))
+ (or (buttercup-suite-or-spec-time-started suite-or-spec) (current-time))))
+
;;;;;;;;;;;;;;;;;;;;
;;; Suites: describe
@@ -1349,6 +1365,7 @@ Do not change the global value.")
(defun buttercup--run-suite (suite)
"Run SUITE. A suite is a sequence of suites and specs."
+ (buttercup--set-start-time suite)
(let* ((buttercup--before-each (append buttercup--before-each
(buttercup-suite-before-each suite)))
(buttercup--after-each (append (buttercup-suite-after-each suite)
@@ -1364,9 +1381,11 @@ Do not change the global value.")
(buttercup--run-spec sub))))
(dolist (f (buttercup-suite-after-all suite))
(buttercup--update-with-funcall suite f))
+ (buttercup--set-end-time suite)
(funcall buttercup-reporter 'suite-done suite)))
(defun buttercup--run-spec (spec)
+ (buttercup--set-start-time spec)
(unwind-protect
(progn
;; Kill any previous warning buffer, just in case
@@ -1391,7 +1410,8 @@ Do not change the global value.")
(buffer-string)
'yellow)))))
(when (get-buffer buttercup-warning-buffer-name)
- (kill-buffer buttercup-warning-buffer-name))))
+ (kill-buffer buttercup-warning-buffer-name))
+ (buttercup--set-end-time spec)))
(defun buttercup--update-with-funcall (suite-or-spec function &rest args)
"Update SUITE-OR-SPEC with the result of calling FUNCTION with ARGS.
diff --git a/tests/test-buttercup.el b/tests/test-buttercup.el
index c32a384..80c5721 100644
--- a/tests/test-buttercup.el
+++ b/tests/test-buttercup.el
@@ -302,6 +302,70 @@
:to-equal
"su1 su2 sp2"))))
+(describe "The `buttercup-elapsed-time' function"
+ (let ((spytime (current-time)))
+ (before-each
+ (spy-on 'current-time
+ :and-call-fake
+ (lambda ()
+ (setq spytime (time-add spytime (seconds-to-time 1.5))))))
+ (it "should report elapsed time for suites"
+ (let ((suite (make-buttercup-suite)))
+ (buttercup--set-start-time suite)
+ (buttercup--set-end-time suite)
+ (expect (buttercup-elapsed-time suite)
+ :to-equal (seconds-to-time 1.5))))
+ (it "should report elapsed time for specs"
+ (let ((spec (make-buttercup-spec)))
+ (buttercup--set-start-time spec)
+ (buttercup--set-end-time spec)
+ (expect (buttercup-elapsed-time spec)
+ :to-equal (seconds-to-time 1.5))))))
+
+(defmacro with-local-buttercup (&rest body)
+ "Execute BODY with local buttercup state variables."
+ (declare (debug t) (indent defun))
+ `(let (buttercup--after-all
+ buttercup--after-each
+ buttercup--before-all
+ buttercup--before-each
+ buttercup--cleanup-functions
+ buttercup--current-suite
+ (buttercup-reporter #'ignore)
+ buttercup-suites
+ (buttercup-warning-buffer-name " *ignored buttercup warnings*"))
+ ,@body))
+
+(describe "The `buttercup--run-suite' function"
+ (before-each
+ (spy-on 'buttercup--set-start-time :and-call-through)
+ (spy-on 'buttercup--set-end-time :and-call-through))
+ (it "should set start and end time of the suite"
+ (with-local-buttercup
+ (let ((suite (make-buttercup-suite)))
+ (buttercup--run-suite suite)
+ (expect 'buttercup--set-start-time :to-have-been-called-times 1)
+ (expect (buttercup-suite-or-spec-time-started suite)
+ :not :to-be nil)
+ (expect 'buttercup--set-end-time :to-have-been-called-times 1)
+ (expect (buttercup-suite-or-spec-time-ended suite)
+ :not :to-be nil)))))
+
+(describe "The `buttercup--run-spec' function"
+ (before-each
+ (spy-on 'buttercup--set-start-time :and-call-through)
+ (spy-on 'buttercup--set-end-time :and-call-through))
+ (it "should set start and end time of the spec"
+ (with-local-buttercup
+ (let ((spec (make-buttercup-spec)))
+ (buttercup--run-spec spec)
+ (expect 'buttercup--set-start-time :to-have-been-called-times 1)
+ (expect (buttercup-suite-or-spec-time-started spec)
+ :not :to-be nil)
+ (expect 'buttercup--set-end-time :to-have-been-called-times 1)
+ (expect (buttercup-suite-or-spec-time-ended spec)
+ :not :to-be nil)))))
+
;;;;;;;;;;;;;;;;;;;;
;;; Suites: describe