[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/buttercup ad901b5 149/340: Do not modify global match data
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/buttercup ad901b5 149/340: Do not modify global match data in matchers |
Date: |
Thu, 16 Dec 2021 14:59:23 -0500 (EST) |
branch: elpa/buttercup
commit ad901b55e39c6b28e0adaafb00645ecafdd23620
Author: Jorgen Schaefer <contact@jorgenschaefer.de>
Commit: Jorgen Schaefer <contact@jorgenschaefer.de>
Do not modify global match data in matchers
---
buttercup.el | 116 ++++++++++++++++++++++++++----------------------
tests/test-buttercup.el | 19 +++++++-
2 files changed, 82 insertions(+), 53 deletions(-)
diff --git a/buttercup.el b/buttercup.el
index 567f24f..e5e5207 100644
--- a/buttercup.el
+++ b/buttercup.el
@@ -95,6 +95,17 @@ environment)."
(buttercup--enclosed-expr fun))
(error nil)))
+;;;;;;;;;;;;;;;;;;;;
+;;; Helper functions
+
+(defun buttercup-format-spec (format specification)
+ "Return a string based on FORMAT and SPECIFICATION.
+
+This is a wrapper around `format-spec', which see. This also adds
+a call to `save-match-data', as `format-spec' modifies that."
+ (save-match-data
+ (format-spec format specification)))
+
;;;;;;;;;;
;;; expect
@@ -309,8 +320,8 @@ See also `buttercup-define-matcher'."
?a (format "%S" value)
?x (format "%S" explanation))))
(buttercup--test-expectation (funcall ',function value)
- :expect-match-phrase (format-spec ,expect-match-phrase spec)
- :expect-mismatch-phrase (format-spec ,expect-mismatch-phrase
spec))))))
+ :expect-match-phrase (buttercup-format-spec ,expect-match-phrase
spec)
+ :expect-mismatch-phrase (buttercup-format-spec
,expect-mismatch-phrase spec))))))
(cl-defmacro buttercup-define-matcher-for-binary-function
(matcher function &key
@@ -388,8 +399,8 @@ See also `buttercup-define-matcher'."
?b (format "%S" b)
?x (format "%S" explanation))))
(buttercup--test-expectation (funcall #',function a b)
- :expect-match-phrase (format-spec ,expect-match-phrase spec)
- :expect-mismatch-phrase (format-spec ,expect-mismatch-phrase
spec)))))))
+ :expect-match-phrase (buttercup-format-spec ,expect-match-phrase
spec)
+ :expect-mismatch-phrase (buttercup-format-spec
,expect-mismatch-phrase spec)))))))
;;;;;;;;;;;;;;;;;;;;;
;;; Built-in matchers
@@ -424,19 +435,19 @@ See also `buttercup-define-matcher'."
?p (format "%S" a-uniques))))
(cond
((and a-uniques b-uniques)
- (cons nil (format-spec
+ (cons nil (buttercup-format-spec
"Expected `%A' to contain the same items as `%b', but `%m'
are missing and `%p' are present unexpectedly."
spec)))
(a-uniques
- (cons nil (format-spec
+ (cons nil (buttercup-format-spec
"Expected `%A' to contain the e items as `%b', but `%p' are
present unexprctedly."
spec)))
(b-uniques
- (cons nil (format-spec
+ (cons nil (buttercup-format-spec
"Expected `%A' to contain the same items as `%b', but `%m'
are missing."
spec)))
(t
- (cons t (format-spec
+ (cons t (buttercup-format-spec
"Expected `%A' not to have same items as `%b'"
spec)))))))
@@ -444,46 +455,47 @@ See also `buttercup-define-matcher'."
(cl-destructuring-bind
((text-expr . text) (regexp-expr . regexp))
(mapcar #'buttercup--expr-and-value (list text regexp))
- (let* (;; For string literals, juse use them normally, but for
- ;; expressions, show both the expr and its string value
- (text-is-literal (equal text-expr text))
- (regexp-is-literal (equal regexp-expr regexp))
- (text-desc
- (if text-is-literal
- text-expr
- (format "`%S' with value %S"
- text-expr text)))
- (regexp-desc
- (if regexp-is-literal
- regexp-expr
- (format "`%S' with value %S"
- regexp-expr regexp)))
- (match-p (string-match regexp text))
- ;; Get some more details about the match
- (start
- (when match-p
- (match-beginning 0)))
- (end
- (when match-p
- (match-end 0)))
- (matched-substring
- (when match-p
- (substring text start end)))
- (spec (format-spec-make
- ?T text-desc
- ?t (format "%S" text)
- ?R regexp-desc
- ?r (format "%S" regexp)
- ?m (format "%S" matched-substring)
- ?a start
- ?z end)))
- (buttercup--test-expectation match-p
- :expect-match-phrase
- (format-spec "Expected %T to match the regexp %r, but instead it was
%t."
- spec)
- :expect-mismatch-phrase
- (format-spec "Expected %T not to match the regexp %r, but it matched
the substring %m from position %a to %z."
- spec)))))
+ (save-match-data
+ (let* (;; For string literals, juse use them normally, but for
+ ;; expressions, show both the expr and its string value
+ (text-is-literal (equal text-expr text))
+ (regexp-is-literal (equal regexp-expr regexp))
+ (text-desc
+ (if text-is-literal
+ text-expr
+ (format "`%S' with value %S"
+ text-expr text)))
+ (regexp-desc
+ (if regexp-is-literal
+ regexp-expr
+ (format "`%S' with value %S"
+ regexp-expr regexp)))
+ (match-p (string-match regexp text))
+ ;; Get some more details about the match
+ (start
+ (when match-p
+ (match-beginning 0)))
+ (end
+ (when match-p
+ (match-end 0)))
+ (matched-substring
+ (when match-p
+ (substring text start end)))
+ (spec (format-spec-make
+ ?T text-desc
+ ?t (format "%S" text)
+ ?R regexp-desc
+ ?r (format "%S" regexp)
+ ?m (format "%S" matched-substring)
+ ?a start
+ ?z end)))
+ (buttercup--test-expectation match-p
+ :expect-match-phrase
+ (buttercup-format-spec "Expected %T to
match the regexp %r, but instead it was %t."
+ spec)
+ :expect-mismatch-phrase
+ (buttercup-format-spec "Expected %T not
to match the regexp %r, but it matched the substring %m from position %a to %z."
+ spec))))))
(buttercup-define-matcher-for-binary-function
:to-be-in member
@@ -576,15 +588,15 @@ See also `buttercup-define-matcher'."
"")))
(result-text
(if thrown-signal
- (format-spec "it threw %t" spec)
- (format-spec "it evaluated successfully, returning value `%e'"
spec)))
+ (buttercup-format-spec "it threw %t" spec)
+ (buttercup-format-spec "it evaluated successfully, returning value
`%e'" spec)))
(expect-match-text
- (concat (format-spec "Expected `%E' to throw %s%a" spec)
+ (concat (buttercup-format-spec "Expected `%E' to throw %s%a" spec)
", but instead "
result-text))
(expect-mismatch-text
- (concat (format-spec "Expected `%E' not to throw %s%a" spec)
+ (concat (buttercup-format-spec "Expected `%E' not to throw %s%a"
spec)
", but "
result-text)))
(buttercup--test-expectation matched
diff --git a/tests/test-buttercup.el b/tests/test-buttercup.el
index ef316b6..6c867f7 100644
--- a/tests/test-buttercup.el
+++ b/tests/test-buttercup.el
@@ -112,7 +112,7 @@
(expect (assume 'non-nil "Explanation")
:not :to-throw)))
-(describe "The `buttercup-skip function"
+(describe "The `buttercup-skip' function"
(it "should raise a signal with its arguments"
(expect (buttercup-skip "Explanation" )
:to-throw
@@ -152,6 +152,23 @@
;; Are tested in README.md
+
+(buttercup-define-matcher-for-unary-function :test-to-be-truthy identity)
+
+(describe "The :buttercup-define-matcher-for-unary-function helper"
+ (it "should not modify match data"
+ (string-match ".." "foo")
+ (expect t :test-to-be-truthy)
+ (expect (match-end 0) :to-equal 2)))
+
+(buttercup-define-matcher-for-binary-function :test-to-be-eq eq)
+
+(describe "The :buttercup-define-matcher-for-binary-function helper"
+ (it "should not modify match data"
+ (string-match ".." "foo")
+ (expect t :test-to-be-eq t)
+ (expect (match-end 0) :to-equal 2)))
+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Suite and spec data structures
- [nongnu] elpa/buttercup 3ef90b4 100/340: :to-match: Don't switch t/nil!, (continued)
- [nongnu] elpa/buttercup 3ef90b4 100/340: :to-match: Don't switch t/nil!, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup ab2a558 111/340: Add projectile integration info to Readme, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 0124cc4 129/340: Signal an error when ":to-throw" is used on a non-function (#97), ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 31e765b 142/340: Do not suppress errors while loading test files, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup bc13b53 183/340: Merge pull request #133 from snogge/fix-specs-in-pending-suites, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup ca3ba8e 185/340: Merge pull request #130 from snogge/travis-update, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup d6cf530 072/340: Clean up the interactive reporter., ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 68c5ef5 076/340: Only load .el files during discovery., ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 14718c0 081/340: Use the new Travis infrastructure, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup d00c164 083/340: Display description for pending specs, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup ad901b5 149/340: Do not modify global match data in matchers,
ELPA Syncer <=
- [nongnu] elpa/buttercup c1187b3 152/340: Extract function buttercup-suites-total-spces-status, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup f565eda 157/340: Extract super-struct buttercup-suite-or-spec, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 0c1dcda 161/340: Fix docstring of buttercup--specs-and-suite, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 8d27bb9 169/340: Bump version: 1.10 → 1.11, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 45f40c7 174/340: Restore obsoletion version for buttercup-*-parents, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup d7ab592 080/340: Bump version: 1.1 → 1.2, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup e1f71ac 086/340: Bump version: 1.3 → 1.4, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 673f84d 087/340: Add assume form to cancel tests if conditions fail, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 2aa840c 088/340: Merge pull request #40 from lunaryorn/assume-form, ELPA Syncer, 2021/12/16
- [nongnu] elpa/buttercup 6c11d8e 097/340: Debian and Ubuntu installation instructions, ELPA Syncer, 2021/12/16