[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/buttercup 243b71f 245/340: Merge branch 'spy-scope-error'
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/buttercup 243b71f 245/340: Merge branch 'spy-scope-error' |
Date: |
Thu, 16 Dec 2021 14:59:43 -0500 (EST) |
branch: elpa/buttercup
commit 243b71f25ab8c638c3aeac644672fe670e69e321
Merge: d8ab09d 83141f7
Author: Ola Nilsson <ola.nilsson@gmail.com>
Commit: Ola Nilsson <ola.nilsson@gmail.com>
Merge branch 'spy-scope-error'
* spy-scope-error:
Raise an error if spy-on is used in the wrong context
---
buttercup.el | 20 +++++++++++++-------
docs/writing-tests.md | 6 +++---
tests/test-buttercup.el | 17 +++++++++++++++--
3 files changed, 31 insertions(+), 12 deletions(-)
diff --git a/buttercup.el b/buttercup.el
index ed58dc3..45ce245 100644
--- a/buttercup.el
+++ b/buttercup.el
@@ -1135,14 +1135,16 @@ responsibility to ensure ARG is a command."
nil))
(_
(error "Invalid `spy-on' keyword: `%S'" keyword)))))
- (buttercup--spy-on-and-call-replacement symbol replacement)))
+ (unless (buttercup--spy-on-and-call-replacement symbol replacement)
+ (error "Spies can only be created in `before-each'"))))
+
(defun buttercup--spy-on-and-call-replacement (spy fun)
"Replace the function in symbol SPY with a spy calling FUN."
(let ((orig-function (symbol-function spy)))
- (fset spy (buttercup--make-spy fun))
- (buttercup--add-cleanup (lambda ()
- (fset spy orig-function)))))
+ (when (buttercup--add-cleanup (lambda ()
+ (fset spy orig-function)))
+ (fset spy (buttercup--make-spy fun)))))
(defun buttercup--make-spy (fun)
"Create a new spy function wrapping FUN and tracking calls to itself."
@@ -1182,7 +1184,10 @@ responsibility to ensure ARG is a command."
(apply ',this-spy-function args))))
this-spy-function))
-(defvar buttercup--cleanup-functions nil)
+(defvar buttercup--cleanup-functions :inactive
+ "Stack of cleanup operations.
+Should always be set to a value that is not `listp', except while
+in a `buttercup-with-cleanup' environment.")
(defmacro buttercup-with-cleanup (&rest body)
"Execute BODY, cleaning spys and the rest afterwards."
@@ -1194,8 +1199,9 @@ responsibility to ensure ARG is a command."
(defun buttercup--add-cleanup (function)
"Register FUNCTION for cleanup in `buttercup-with-cleanup'."
- (setq buttercup--cleanup-functions
- (cons function buttercup--cleanup-functions)))
+ (when (listp buttercup--cleanup-functions)
+ (setq buttercup--cleanup-functions
+ (cons function buttercup--cleanup-functions))))
(defun spy-calls-all (spy)
"Return the contexts of calls to SPY."
diff --git a/docs/writing-tests.md b/docs/writing-tests.md
index 8ff86e0..a78ac83 100644
--- a/docs/writing-tests.md
+++ b/docs/writing-tests.md
@@ -367,9 +367,9 @@ frameworks call these mocks and similar, we call them
spies, because
their main job is to spy in on function calls. Also, Jasmine calls
them spies, and so do we. A spy can stub any function - whether it
already exists or not - and tracks calls
-to it and all arguments. A spy only exists in the `describe` or `it`
-block it is defined in, and will be activated before and deactivated
-and reset after each spec. There are
+to it and all arguments. Spies may only be created in `before-each` or
+`it` blocks. Spies are removed and all counters reset after each spec
+and its `after-each` blocks have completed. There are
special matchers for interacting with spies. The
`:to-have-been-called` matcher will return true if the spy was called
at all. The `:to-have-been-called-with` matcher will return true if
diff --git a/tests/test-buttercup.el b/tests/test-buttercup.el
index 03aa6bb..a73f2ee 100644
--- a/tests/test-buttercup.el
+++ b/tests/test-buttercup.el
@@ -329,7 +329,7 @@
buttercup--after-each
buttercup--before-all
buttercup--before-each
- buttercup--cleanup-functions
+ (buttercup--cleanup-functions :invalid)
buttercup--current-suite
(buttercup-reporter #'ignore)
buttercup-suites
@@ -696,7 +696,20 @@
:not :to-throw)
(expect
(test-function 1 2)
- :to-throw 'error)))
+ :to-throw 'error))
+
+ (describe "will signal en error if"
+ (it "used in before-all"
+ (with-local-buttercup
+ (let ((suite (describe "A bad spy scope"
+ (before-all
+ (spy-on 'some-function)))))
+ (expect (run--suite suite)
+ :to-throw))))
+ (it "used directly in describe"
+ (with-local-buttercup
+ (expect (describe "Not in describe"
+ (spy-on 'foo)) :to-throw)))))
(describe ":to-have-been-called matcher"
(before-each
- [nongnu] elpa/buttercup 1c50a80 196/340: Fix the buttercup-run tests, (continued)
- [nongnu] elpa/buttercup 1c50a80 196/340: Fix the buttercup-run tests, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 810fa6f 197/340: Bump version: 1.15 → 1.16, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 4459f37 199/340: Add test for :var* argument, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup ff16814 216/340: travis: Always test on xenial, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup bd851d4 217/340: Merge pull request #155 from snogge/travis-xenial, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 246ddd7 218/340: Merge pull request #152 from DarwinAwardWinner/suppress-warning-redirect, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 9408a72 220/340: Merge pull request #154 from DarwinAwardWinner/test-emacs-26.3, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 26e2ded 222/340: Rename some innacurate references to "fake" functions, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 9b87afc 234/340: Merge branch 'snogge/spy-context-tweaks', ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup d8b596c 240/340: Merge branch 'doc-improve', ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 243b71f 245/340: Merge branch 'spy-scope-error',
ELPA Syncer <=
- [nongnu] elpa/buttercup f3c2b00 261/340: test: Move with-local-buttercup to top of file, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 0dbd474 263/340: Bump version: 1.20 → 1.21, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 1c9c82f 265/340: Fixed misspelling in the documentation, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 360953b 266/340: test: Only run tests found in the tests/ directory, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 25bc00d 273/340: test: Set up suite "The batch reporter" to use fake terminal, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 5c9f80d 279/340: test: Add color tests for buttercup-suite-started and -done, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 2be9ad3 280/340: test: Add color tests for spec-done, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 0e94f6e 303/340: Lift buttercup-done handling into buttercup-reporter-batch, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup f1f403b 308/340: Remove buttercup-reporter-batch-color, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup fce5fa5 313/340: Optionally fail when loading old elc files, ELPA Syncer, 2021/12/16