[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/buttercup 8aa87c8 026/340: Spy :and-call-through
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/buttercup 8aa87c8 026/340: Spy :and-call-through |
Date: |
Thu, 16 Dec 2021 14:58:58 -0500 (EST) |
branch: elpa/buttercup
commit 8aa87c89eb93474ef784664d5404fec5c9c31c71
Author: Jorgen Schaefer <contact@jorgenschaefer.de>
Commit: Jorgen Schaefer <contact@jorgenschaefer.de>
Spy :and-call-through
---
README.md | 26 ++++++++++++++++++++++++++
buttercup-test.el | 14 ++++++++++++++
buttercup.el | 15 +++++++++++----
3 files changed, 51 insertions(+), 4 deletions(-)
diff --git a/README.md b/README.md
index 20062db..34a56a2 100644
--- a/README.md
+++ b/README.md
@@ -344,6 +344,32 @@ the argument list matches any of the recorded calls to the
spy.
(expect bar :to-be nil))))
```
+### Spies: `:and-call-through`
+
+The keyword argument `:and-call-through` to `spy-on` will make the spy
+call the original function instead of returning `nil`.
+
+```Lisp
+(describe "A spy, when configured to call through"
+ (let (bar set-bar get-bar fetched-bar)
+ (before-each
+ (fset 'set-bar (lambda (val)
+ (setq bar val)))
+ (fset 'get-bar (lambda ()
+ bar))
+
+ (spy-on 'get-bar :and-call-through)
+
+ (set-bar 123)
+ (setq fetched-bar (get-bar)))
+
+ (it "tracks that the spy was called"
+ (expect 'get-bar :to-have-been-called))
+
+ (it "should not affect other functions"
+ (expect bar :to-equal 123))))
+```
+
## Test Runners
Evaluating `describe` forms just stores the suites. You need to use a
diff --git a/buttercup-test.el b/buttercup-test.el
index cb071c4..bb30b9c 100644
--- a/buttercup-test.el
+++ b/buttercup-test.el
@@ -377,3 +377,17 @@
:to-have-been-called-with '(test-function 1 2 3))
:to-be
t)))
+
+(describe "The :and-call-through keyword functionality"
+ (before-each
+ (spy-on 'test-function :and-call-through))
+
+ (it "tracks calls to the function"
+ (test-function 42 23)
+
+ (expect 'test-function :to-have-been-called))
+
+ (it "passes the arguments to the original function"
+ (expect (test-function 2 3)
+ :to-equal
+ 5)))
diff --git a/buttercup.el b/buttercup.el
index 87c6d7f..916dbc8 100644
--- a/buttercup.el
+++ b/buttercup.el
@@ -368,11 +368,18 @@ A disabled spec is not run."
(defvar buttercup--spy-calls (make-hash-table :test 'eq
:weakness 'key))
-(defun spy-on (symbol)
- (letrec ((old-value (symbol-function symbol))
- (new-value (lambda (&rest args)
+(defun spy-on (symbol &rest keyword-args)
+ (let ((old-value (symbol-function symbol))
+ (new-value nil))
+ (cond
+ ((equal keyword-args '(:and-call-through))
+ (setq new-value (lambda (&rest args)
(buttercup--spy-add-call new-value args)
- nil)))
+ (apply old-value args))))
+ ((equal keyword-args nil)
+ (setq new-value (lambda (&rest args)
+ (buttercup--spy-add-call new-value args)
+ nil))))
(fset symbol new-value)
(buttercup--add-cleanup (lambda () (fset symbol old-value)))))
- [nongnu] elpa/buttercup 5db449f 025/340: Spies, (continued)
- [nongnu] elpa/buttercup 5db449f 025/340: Spies, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup d580e5c 011/340: Add build status badge., ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 3637e3d 015/340: Add more extensive matcher tests to the README., ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup a277b0e 022/340: Setup and teardown: before-each, after-each, before-all, after-all, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 1882d6f 013/340: Comment cleanup. Also, make elisp lexically scoped., ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup c5a9f26 003/340: Add first set of unit tests., ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 36522b9 006/340: Add .travis.yml, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup f44e65d 004/340: Unit tests for define-matcher and apply-matcher., ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup d3cd12a 019/340: Tests for the buttercup-describe function, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 3772ad5 014/340: Use eval-region instead of eval-buffer., ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 8aa87c8 026/340: Spy :and-call-through,
ELPA Syncer <=
- [nongnu] elpa/buttercup 683614d 018/340: Tests for the describe macro., ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 0221830 023/340: Remove setup and teardown from ROADMAP.md., ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 695e444 016/340: buttercup-markdown-runner: Use setq, not setq-local., ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 7526198 029/340: Spies: :and-throw-error, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 77d1dac 017/340: Test buttercup-suite-add-child., ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup c1abc2a 033/340: Refactoring of spy context accessors., ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup f314737 035/340: ROADMAP.md: Remove spies section, as it is done., ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 4bd24e7 037/340: ROADMAP cleanup., ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 55b87ec 039/340: Removed ROADMAP.md. This is now tracked in issues., ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 993ddda 040/340: Add .gitignore, ELPA Syncer, 2021/12/16