[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/buttercup ba40ff8 027/340: Spies: :and-return-value
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/buttercup ba40ff8 027/340: Spies: :and-return-value |
Date: |
Thu, 16 Dec 2021 14:58:58 -0500 (EST) |
branch: elpa/buttercup
commit ba40ff8a23a3e4bd6b6de458bb993d823df728a8
Author: Jorgen Schaefer <contact@jorgenschaefer.de>
Commit: Jorgen Schaefer <contact@jorgenschaefer.de>
Spies: :and-return-value
---
README.md | 34 +++++++++++++++++++++++++++++++++-
buttercup-test.el | 14 ++++++++++++++
buttercup.el | 10 +++++++---
3 files changed, 54 insertions(+), 4 deletions(-)
diff --git a/README.md b/README.md
index 34a56a2..70c6bbd 100644
--- a/README.md
+++ b/README.md
@@ -367,7 +367,39 @@ call the original function instead of returning `nil`.
(expect 'get-bar :to-have-been-called))
(it "should not affect other functions"
- (expect bar :to-equal 123))))
+ (expect bar :to-equal 123))
+
+ (it "when called returns the requested value"
+ (expect fetched-bar :to-equal 123))))
+```
+
+### Spies: `:and-return-value`
+
+The keyword argument `:and-return-value` specifies the value the
+spied-on function should return.
+
+```Lisp
+(describe "A spy, when configured to fake a return value"
+ (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-return-value 745)
+
+ (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))
+
+ (it "when called returns the requested value"
+ (expect fetched-bar :to-equal 745))))
```
## Test Runners
diff --git a/buttercup-test.el b/buttercup-test.el
index bb30b9c..5fe85b8 100644
--- a/buttercup-test.el
+++ b/buttercup-test.el
@@ -391,3 +391,17 @@
(expect (test-function 2 3)
:to-equal
5)))
+
+(describe "The :and-return-value keyword functionality"
+ (before-each
+ (spy-on 'test-function :and-return-value 23))
+
+ (it "tracks calls to the function"
+ (test-function 42 23)
+
+ (expect 'test-function :to-have-been-called))
+
+ (it "returns the specified value"
+ (expect (test-function 2 3)
+ :to-equal
+ 23)))
diff --git a/buttercup.el b/buttercup.el
index 916dbc8..72f02c7 100644
--- a/buttercup.el
+++ b/buttercup.el
@@ -368,15 +368,19 @@ A disabled spec is not run."
(defvar buttercup--spy-calls (make-hash-table :test 'eq
:weakness 'key))
-(defun spy-on (symbol &rest keyword-args)
+(defun spy-on (symbol &optional keyword arg)
(let ((old-value (symbol-function symbol))
(new-value nil))
(cond
- ((equal keyword-args '(:and-call-through))
+ ((eq keyword :and-call-through)
(setq new-value (lambda (&rest args)
(buttercup--spy-add-call new-value args)
(apply old-value args))))
- ((equal keyword-args nil)
+ ((eq keyword :and-return-value)
+ (setq new-value (lambda (&rest args)
+ (buttercup--spy-add-call new-value args)
+ arg)))
+ ((not keyword)
(setq new-value (lambda (&rest args)
(buttercup--spy-add-call new-value args)
nil))))
- [nongnu] elpa/buttercup 89c00aa 009/340: Makefile: Make the emacs command configurable, (continued)
- [nongnu] elpa/buttercup 89c00aa 009/340: Makefile: Make the emacs command configurable, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 7e20ff1 021/340: ROADMAP.md: New file., ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 764d6aa 020/340: Tests for the it macro and buttercup-it function., ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup d5572a6 034/340: Refactoring of the cleanup code., ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup b75b3e9 036/340: Add a discovery test runner., ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 7eecb96 038/340: Remove commented-out backtrace code from buttercup.el., ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 3483449 045/340: Typo fix., ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 84002e9 054/340: Add parent relations to specs and suites., ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup a7d9813 010/340: Add cl-defstruct compatibility alias., ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup fd70ccf 002/340: Specs are now structs., ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup ba40ff8 027/340: Spies: :and-return-value,
ELPA Syncer <=
- [nongnu] elpa/buttercup 20b87dd 031/340: Refactoring of spy-on to be more concise and DRY., ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup bb31c85 043/340: Edebugable., ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 5d369cd 007/340: Do not use emacs-snapshot from EVM, that's slow., ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 07b1ee6 008/340: Add compatibility definition for define-error, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 7cdb219 028/340: Spies: :and-call-fake, ELPA Syncer, 2021/12/16
- [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