emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/buttercup bf48137 224/340: Add some tests for error-call r


From: ELPA Syncer
Subject: [nongnu] elpa/buttercup bf48137 224/340: Add some tests for error-call recording
Date: Thu, 16 Dec 2021 14:59:39 -0500 (EST)

branch: elpa/buttercup
commit bf481374073a408835f8caffadc85511f420faf2
Author: Ryan C. Thompson <rct@thompsonclan.org>
Commit: Ryan C. Thompson <rct@thompsonclan.org>

    Add some tests for error-call recording
---
 tests/test-buttercup.el | 68 +++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 63 insertions(+), 5 deletions(-)

diff --git a/tests/test-buttercup.el b/tests/test-buttercup.el
index e99a964..8c27bf8 100644
--- a/tests/test-buttercup.el
+++ b/tests/test-buttercup.el
@@ -613,27 +613,40 @@
 ;;; Spies
 
 (describe "The Spy "
-  (let (saved-test-function saved-test-command)
+  (let (saved-test-function
+        saved-test-command
+        saved-test-function-throws-on-negative)
     ;; We use `before-all' here because some tests need to access the
     ;; same function as previous tests in order to work, so overriding
     ;; the function before each test would invalidate those tests.
+    ;; Unfortunately there's no way to do this lexically, so these
+    ;; function definitions are leaked after the tests run.
     (before-all
       (setq saved-test-function (and (fboundp 'test-function)
                                      (symbol-function 'test-function))
             saved-test-command (and (fboundp 'test-command)
-                                    (symbol-function 'test-command)))
+                                    (symbol-function 'test-command))
+            saved-test-function-throws-on-negative
+            (and (fboundp 'test-function-throws-on-negative)
+                 (symbol-function 'test-function-throws-on-negative)))
       (fset 'test-function (lambda (a b)
                              (+ a b)))
       (fset 'test-command (lambda ()
                             (interactive)
-                            t)))
+                            t))
+      (fset 'test-function-throws-on-negative
+            (lambda (x) (if (>= x 0) x (error "x is less than zero")))))
     (after-all
       (if saved-test-function
           (fset 'test-function saved-test-function)
         (fmakunbound 'test-function))
       (if saved-test-command
           (fset 'test-command saved-test-command)
-        (fmakunbound 'test-command)))
+        (fmakunbound 'test-command))
+      (if saved-test-function-throws-on-negative
+          (fset 'test-test-function-throws-on-negative
+                test-function-throws-on-negative)
+        (fmakunbound 'test-function-throws-on-negative)))
 
     (describe "`spy-on' function"
       (it "replaces a symbol's function slot"
@@ -881,7 +894,52 @@
       (it "throws an error when called"
         (expect (test-function 1 2)
                 :to-throw
-                'error "Stubbed error")))))
+                'error "Stubbed error")))
+
+    (describe "error-recording functionality"
+      (before-each
+        (spy-on 'test-function-throws-on-negative :and-call-through))
+
+      (it "records the function as called even if it throws an error"
+        (expect (test-function-throws-on-negative -5) :to-throw)
+        (expect (buttercup--apply-matcher
+                 :to-have-been-called
+                 (list (lambda () 'test-function-throws-on-negative)))
+                :to-be
+                t))
+
+      (it "counts both successful calls and calls that threw errors"
+        (test-function-throws-on-negative 5)
+        (expect (test-function-throws-on-negative -5) :to-throw)
+        (expect (buttercup--apply-matcher
+                 :to-have-been-called-times
+                 (make-list-of-closures '(test-function-throws-on-negative 2)))
+                :to-equal t))
+
+      (it "records args to the function whether it throw an error or not"
+        (test-function-throws-on-negative 5)
+        (expect (test-function-throws-on-negative -5) :to-throw)
+        (expect (buttercup--apply-matcher
+                 :to-have-been-called-with
+                 (make-list-of-closures '(test-function-throws-on-negative 5)))
+                :to-be
+                t)
+        (expect (buttercup--apply-matcher
+                 :to-have-been-called-with
+                 (make-list-of-closures '(test-function-throws-on-negative 
-5)))
+                :to-be
+                t))
+
+      (it "records the signal thrown by a call to the function"
+        (test-function-throws-on-negative 5)
+        (expect (test-function-throws-on-negative -5) :to-throw 'error)
+        (expect (spy-context-thrown-signal
+                 (spy-calls-first 'test-function-throws-on-negative))
+                :to-be nil)
+        (expect (car
+                 (spy-context-thrown-signal
+                  (spy-calls-most-recent 'test-function-throws-on-negative)))
+                :to-be 'error)))))
 
 ;;;;;;;;;;;;;
 ;;; Reporters



reply via email to

[Prev in Thread] Current Thread [Next in Thread]