[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/buttercup 7e8898b 127/340: Implement :to-have-been-called-
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/buttercup 7e8898b 127/340: Implement :to-have-been-called-times matcher (close #90) |
Date: |
Thu, 16 Dec 2021 14:59:18 -0500 (EST) |
branch: elpa/buttercup
commit 7e8898bd6699205ae5c65c8dd7e602a9ed5ab8cd
Author: Damien Cassou <damien@cassou.me>
Commit: Jorgen Schäfer <Jorgen.Schaefer@gmail.com>
Implement :to-have-been-called-times matcher (close #90)
---
buttercup.el | 12 ++++++++++++
docs/writing-tests.md | 20 +++++++++++++++++++
tests/test-buttercup.el | 51 +++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 83 insertions(+)
diff --git a/buttercup.el b/buttercup.el
index 423b675..8b1eb46 100644
--- a/buttercup.el
+++ b/buttercup.el
@@ -617,6 +617,18 @@ KEYWORD can have one of the following values:
(t
t))))
+(buttercup-define-matcher :to-have-been-called-times (spy number)
+ (let* ((call-count (length (spy-calls-all spy))))
+ (cond
+ ((= number call-count)
+ t)
+ (t
+ (cons nil
+ (format "Expected `%s' to have been called %s %s, but it was
called %s %s"
+ spy
+ number (if (= number 1) "time" "times")
+ call-count (if (= call-count 1) "time" "times")))))))
+
(defun spy-calls-any (spy)
"Return t iff SPY has been called at all, nil otherwise."
(if (spy-calls-all spy)
diff --git a/docs/writing-tests.md b/docs/writing-tests.md
index 8b965cd..007d1d9 100644
--- a/docs/writing-tests.md
+++ b/docs/writing-tests.md
@@ -359,6 +359,26 @@ the argument list matches any of the recorded calls to the
spy.
(expect bar :to-be nil)))
```
+The `:to-have-been-called-times` matcher will return true if the spy
+was called a certain number of times.
+
+```Lisp
+(describe "A spy"
+ :var (foo bar)
+ (before-each
+ (setf (symbol-function 'foo)
+ (lambda (value)
+ (setq bar value)))
+
+ (spy-on 'foo)
+
+ (foo 123)
+ (foo 456 "another param"))
+
+ (it "tracks that the spy was called twice"
+ (expect 'foo :to-have-been-called-times 2)))
+```
+
### Spies: `:and-call-through`
The keyword argument `:and-call-through` to `spy-on` will make the spy
diff --git a/tests/test-buttercup.el b/tests/test-buttercup.el
index b03109f..f591c5e 100644
--- a/tests/test-buttercup.el
+++ b/tests/test-buttercup.el
@@ -556,6 +556,57 @@
:to-be
t)))
+ (describe ":to-have-been-called-times matcher"
+ (before-each
+ (spy-on 'test-function))
+
+ (it "returns error if the spy was called less than expected"
+ (expect (buttercup--apply-matcher
+ :to-have-been-called-times '(test-function 1))
+ :to-equal
+ (cons nil
+ "Expected `test-function' to have been called 1 time,
but it was called 0 times")))
+
+ (it "returns error if the spy was called more than expected"
+ (test-function)
+ (test-function)
+ (expect (buttercup--apply-matcher
+ :to-have-been-called-times '(test-function 1))
+ :to-equal
+ (cons nil
+ "Expected `test-function' to have been called 1 time,
but it was called 2 times")))
+
+ (it "returns true if the spy was called the expected number of times"
+ (test-function)
+ (test-function)
+ (expect (buttercup--apply-matcher
+ :to-have-been-called-times '(test-function 2))
+ :to-equal t))
+
+ (it "use plural words in error message"
+ (test-function)
+ (test-function)
+ (expect (buttercup--apply-matcher
+ :to-have-been-called-times '(test-function 3))
+ :to-equal
+ (cons nil
+ "Expected `test-function' to have been called 3 times,
but it was called 2 times")))
+
+ (it "use singular expected word in error message"
+ (expect (buttercup--apply-matcher
+ :to-have-been-called-times '(test-function 1))
+ :to-equal
+ (cons nil
+ "Expected `test-function' to have been called 1 time,
but it was called 0 times")))
+
+ (it "use singular actual word in error message"
+ (test-function)
+ (expect (buttercup--apply-matcher
+ :to-have-been-called-times '(test-function 2))
+ :to-equal
+ (cons nil
+ "Expected `test-function' to have been called 2 times,
but it was called 1 time"))))
+
(describe ":and-call-through keyword functionality"
(before-each
(spy-on 'test-function :and-call-through))
- [nongnu] elpa/buttercup e0b5d9f 324/340: Correct error message in buttercup--format-stack-frame, (continued)
- [nongnu] elpa/buttercup e0b5d9f 324/340: Correct error message in buttercup--format-stack-frame, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 843fa6f 327/340: Keep the actual variable definition with the reporter, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 315a891 326/340: Fix stack frame collection in buttercup--backtrace, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup cccdedf 334/340: Merge pull request #197 from snogge/omit-traceback, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup bef49f5 323/340: Remove extra newline in error backtrace, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 6ef715f 328/340: Use buttercup--mark-stackframe to mark the start of test code, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 2f24a44 338/340: Make formatting of backtrace frames closer to that standard for Emacs, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup f6f9335 337/340: test: Add tests for buttercup-run-discovery, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 553ff83 103/340: Add :to-have-same-items-as to compare lists as sets., ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 657acef 102/340: Bump version: 1.4 → 1.5, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 7e8898b 127/340: Implement :to-have-been-called-times matcher (close #90),
ELPA Syncer <=
- [nongnu] elpa/buttercup ca09040 130/340: Improve the Makefile, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup a535e51 134/340: Fix a logic error in spy tests, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 6ad9565 135/340: Fix several edge cases in "spy-on", ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 389dc43 139/340: Fix a problem with reporter tests suppressing failure messages, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 929a904 143/340: Implement closure-based expect macro, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 11d7fdc 148/340: Add "--help" option to buttercup command-line script, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 4106adc 170/340: Add release script, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 9b6cc79 177/340: travis: Test on 26.1, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 0d742b0 187/340: Set failure-description for pending specs, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 1c50a80 196/340: Fix the buttercup-run tests, ELPA Syncer, 2021/12/16