emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/buttercup ac0d199c3e 2/4: tests: Add basic tests for the i


From: ELPA Syncer
Subject: [nongnu] elpa/buttercup ac0d199c3e 2/4: tests: Add basic tests for the included matchers
Date: Mon, 26 Dec 2022 19:58:27 -0500 (EST)

branch: elpa/buttercup
commit ac0d199c3e61f2f9463821d4f3f14ba28b0ef1ca
Author: Ola Nilsson <ola.nilsson@gmail.com>
Commit: Ola Nilsson <ola.nilsson@gmail.com>

    tests: Add basic tests for the included matchers
    
    Only pending tests for :to-throw as it is not possible to test that at
    the moment.
---
 tests/test-buttercup.el | 359 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 359 insertions(+)

diff --git a/tests/test-buttercup.el b/tests/test-buttercup.el
index 46f11e16e5..44ae900e42 100644
--- a/tests/test-buttercup.el
+++ b/tests/test-buttercup.el
@@ -341,6 +341,364 @@ text properties using `ansi-color-apply'."
     (expect t :test-to-be-eq t)
     (expect (match-end 0) :to-equal 2)))
 
+(describe "The included matcher"
+  (describe ":to-be-truthy"
+    :var (matcher-function)
+    (before-all
+      (setq matcher-function (buttercup--find-matcher-function :to-be-truthy)))
+    (it "to match for a truthy expression"
+      (expect (buttercup--apply-matcher :to-be-truthy
+                                        (mapcar #'buttercup--wrap-expr '((not 
nil))))
+              :to-equal
+              '(t . "Expected `(not nil)' to be nil, but instead it was 
`t'.")))
+    (it "to not match for an untruthy expression"
+      (expect (buttercup--apply-matcher :to-be-truthy
+                                        (mapcar #'buttercup--wrap-expr 
'((ignore))))
+              :to-equal
+              '(nil . "Expected `(ignore)' to be non-nil, but instead it was 
nil."))))
+
+  (describe ":to-be"
+    (it "to match if the args are `eq'"
+      (cl-destructuring-bind
+          (status . msg)
+          (buttercup--apply-matcher :to-be (mapcar #'buttercup--wrap-expr '('a 
'a)))
+        (expect status)
+        (expect msg :to-match
+                (rx "Expected `"
+                    (or "'a" "(quote a)")
+                    "' not to be `eq' to `a', but it was."))))
+    (it "to not match if the args are not `eq'"
+      (cl-destructuring-bind
+          (status . msg)
+          (buttercup--apply-matcher :to-be (mapcar #'buttercup--wrap-expr '('a 
'b)))
+        (expect status :not :to-be-truthy)
+        (expect msg :to-match
+                (rx "Expected `"
+                    (or "'a" "(quote a)")
+                    "' to be `eq' to `b', but instead it was `a'.")))))
+  (describe ":to-equal"
+    ;; Assumes (get 'equal 'ert-explainer) => 'ert--explain-equal
+    (before-each (spy-on 'ert--explain-equal :and-call-through))
+    (it "to match if the args are `equal'"
+      (let ((res (buttercup--apply-matcher :to-equal (mapcar 
#'buttercup--wrap-expr '(0.2 0.2)))))
+        ;; Check before using :to-equal to verify the return value
+        (expect 'ert--explain-equal :to-have-been-called-times 1)
+        (expect res :to-equal
+                '(t . "Expected `0.2' not to be `equal' to `0.2', but it 
was."))))
+    (it "to not match if the args are not `equal'"
+      (let ((res (buttercup--apply-matcher :to-equal (mapcar 
#'buttercup--wrap-expr '(0.2 1.0)))))
+        ;; Check before using :to-equal to verify the return value
+        (expect 'ert--explain-equal :to-have-been-called-times 1)
+        (expect
+         res :to-equal
+         '(nil . "Expected `0.2' to be `equal' to `1.0', but instead it was 
`0.2' which does not match because: (different-atoms 0.2 1.0).")))))
+
+  (describe ":not"
+    (it "to switch the car of the nested matchers return value"
+      (expect
+       (buttercup--apply-matcher
+        :not (mapcar #'buttercup--wrap-expr '(1 :to-equal 2)))
+       :to-equal
+       (cl-destructuring-bind (res . msg)
+           (buttercup--apply-matcher
+            :to-equal (mapcar #'buttercup--wrap-expr '(1 2)))
+         (cons (not res) msg)))))
+
+  (describe ":to-have-same-items-as"
+    (it "to match equal sets"
+      (cl-destructuring-bind
+          (status . msg)
+          (buttercup--apply-matcher
+           :to-have-same-items-as
+           (mapcar #'buttercup--wrap-expr '('(1 1 2 3 4) '(4 2 1 3))))
+        (expect status)
+        (expect msg :to-match
+                (rx "Expected `"
+                    (or "'(1 1 2 3 4)" "(quote (1 1 2 3 4))")
+                    "' not to have same items as `(4 2 1 3)'"))))
+    (it "to notice missing elements in the second argument"
+      (cl-destructuring-bind
+          (status . msg)
+          (buttercup--apply-matcher
+           :to-have-same-items-as
+           (mapcar #'buttercup--wrap-expr '('(1 2 3 4) '(4 2 3))))
+        (expect status :not :to-be-truthy)
+        (expect msg :to-match
+                (rx "Expected `"
+                    (or "'(1 2 3 4)" "(quote (1 2 3 4))")
+                    "' to contain the same items as `(4 2 3)', "
+                    "but `(1)' are present unexpectedly."))))
+    (it "to notice extra items in the second argument"
+      (cl-destructuring-bind
+          (status . msg)
+          (buttercup--apply-matcher
+           :to-have-same-items-as
+           (mapcar #'buttercup--wrap-expr '('(1 2 3 4) '(4 1 2 3 5))))
+        (expect status :not :to-be-truthy)
+        (expect msg :to-match
+                (rx "Expected `"
+                    (or "'(1 2 3 4)" "(quote (1 2 3 4))")
+                    "' to contain the same items as `(4 1 2 3 5)', "
+                    "but `(5)' are missing."))))
+    (it "to notice extra items in both arguments"
+      (cl-destructuring-bind
+          (status . msg)
+          (buttercup--apply-matcher
+           :to-have-same-items-as
+           (mapcar #'buttercup--wrap-expr '('(1 2 3 4) '(4 1 3 5))))
+        (expect status :not :to-be-truthy)
+        (expect msg :to-match
+                (rx "Expected `"
+                    (or "'(1 2 3 4)" "(quote (1 2 3 4))")
+                    "' to contain the same items as `(4 1 3 5)', "
+                    "but `(5)' are missing and `(2)' are present 
unexpectedly.")))))
+  (describe ":to-match"
+    (it "to match the first argument against a regex"
+      (expect
+       (buttercup--apply-matcher
+        :to-match
+        (mapcar #'buttercup--wrap-expr '("some string" ".")))
+       :to-equal
+       '(t . "Expected some string not to match the regexp \".\", but it 
matched the substring \"s\" from position 0 to 1.")))
+    (it "to show mismatches"
+      (expect
+       (buttercup--apply-matcher
+        :to-match
+        (mapcar #'buttercup--wrap-expr '("some string" "[0-9]+")))
+       :to-equal
+       '(nil . "Expected some string to match the regexp \"[0-9]+\", but 
instead it was \"some string\"."))))
+  (describe ":to-be-in"
+    (it "to match when the first argument is a member of the second argument"
+      (cl-destructuring-bind
+          (status . msg)
+          (buttercup--apply-matcher
+           :to-be-in
+           (mapcar #'buttercup--wrap-expr '('a '(b a c))))
+        (expect status)
+        (expect msg :to-match
+                (rx "Expected `"
+                    (or "'a" "(quote a)")
+                    "' not to be an element of `(b a c)', but it was `a'."))))
+    (it "to not match when the first argument is not a member of the second 
argument"
+      (cl-destructuring-bind
+          (status . msg)
+          (buttercup--apply-matcher
+           :to-be-in
+           (mapcar #'buttercup--wrap-expr '( ''a '(b d c))))
+        (expect status :not :to-be-truthy)
+        (expect msg :to-match
+                (rx "Expected `"
+                    (or "''a"
+                        "(quote (quote a))")
+                    "' to be an element of `(b d c)', but it was `"
+                    (or "'a"
+                        "(quote a)")
+                    "'.")))))
+  (describe ":to-contain"
+    (it "to match when the second argument is a member of the first argument"
+      (cl-destructuring-bind
+          (status . msg)
+       (buttercup--apply-matcher
+        :to-contain
+        (mapcar #'buttercup--wrap-expr '('(b a c) 'a)))
+       (expect status)
+       (expect msg :to-match "Expected `\\('(b a c)\\|(quote (b a c))\\)' to 
be a list not containing `a', but instead it was `(b a c)'.")))
+    (it "to not match when the second argument is not a member of the first 
argument"
+      (cl-destructuring-bind
+          (status . msg)
+          (buttercup--apply-matcher
+           :to-contain
+           (mapcar #'buttercup--wrap-expr '('(b d c) 'a)))
+        (expect status :not :to-be-truthy)
+        (expect msg :to-match
+                "Expected `\\('(b d c)\\|(quote (b d c))\\)' to be a list 
containing `a', but instead it was `(b d c)'."))))
+  (describe ":to-be-less-than"
+    (it "to match when the first argument is less than the second argument"
+      (expect (buttercup--apply-matcher :to-be-less-than
+                                        (mapcar #'buttercup--wrap-expr '(1 2)))
+              :to-equal
+              '(t . "Expected `1' >= 2, but `1' was 1.")))
+    (it "to not match when the first argument is equal to the second argument"
+      (expect (buttercup--apply-matcher :to-be-less-than
+                                        (mapcar #'buttercup--wrap-expr '(2 2)))
+              :to-equal
+              '(nil . "Expected `2' < 2, but `2' was 2.")))
+    (it "to not match when the first argument is greater than the second 
argument"
+      (expect (buttercup--apply-matcher :to-be-less-than
+                                        (mapcar #'buttercup--wrap-expr '(3 2)))
+              :to-equal
+              '(nil . "Expected `3' < 2, but `3' was 3."))))
+  (describe ":to-be-greater-than"
+    (it "to match when the first argument is greater than the second argument"
+      (expect (buttercup--apply-matcher :to-be-greater-than
+                                        (mapcar #'buttercup--wrap-expr '(2 1)))
+              :to-equal
+              '(t . "Expected `2' <= 1, but `2' was 2.")))
+    (it "to not match when the first argument is equal to the second argument"
+      (expect (buttercup--apply-matcher :to-be-greater-than
+                                        (mapcar #'buttercup--wrap-expr '(2 2)))
+              :to-equal
+              '(nil . "Expected `2' > 2, but `2' was 2.")))
+    (it "to not match when the first argument is greater than the second 
argument"
+      (expect (buttercup--apply-matcher :to-be-greater-than
+                                        (mapcar #'buttercup--wrap-expr '(2 3)))
+              :to-equal
+              '(nil . "Expected `2' > 3, but `2' was 2."))))
+  (describe ":to-be-weakly-less-than"
+    (it "to match when the first argument is less than the second argument"
+      (expect (buttercup--apply-matcher :to-be-weakly-less-than
+                                        (mapcar #'buttercup--wrap-expr '(1 2)))
+              :to-equal
+              '(t . "Expected `1' > 2, but `1' was 1.")))
+    (it "to match when the first argument is equal to the second argument"
+      (expect (buttercup--apply-matcher :to-be-weakly-less-than
+                                        (mapcar #'buttercup--wrap-expr '(2 2)))
+              :to-equal
+              '(t . "Expected `2' > 2, but `2' was 2.")))
+    (it "to not match when the first argument is greater than the second 
argument"
+      (expect (buttercup--apply-matcher :to-be-weakly-less-than
+                                        (mapcar #'buttercup--wrap-expr '(3 2)))
+              :to-equal
+              '(nil . "Expected `3' <= 2, but `3' was 3."))))
+  (describe ":to-be-weakly-greater-than"
+    (it "to match when the first argument is greater than the second argument"
+      (expect (buttercup--apply-matcher :to-be-weakly-greater-than
+                                        (mapcar #'buttercup--wrap-expr '(2 1)))
+              :to-equal
+              '(t . "Expected `2' < 1, but `2' was 2.")))
+    (it "to match when the first argument is equal to the second argument"
+      (expect (buttercup--apply-matcher :to-be-weakly-greater-than
+                                        (mapcar #'buttercup--wrap-expr '(2 2)))
+              :to-equal
+              '(t . "Expected `2' < 2, but `2' was 2.")))
+    (it "to not match when the first argument is greater than the second 
argument"
+      (expect (buttercup--apply-matcher :to-be-weakly-greater-than
+                                        (mapcar #'buttercup--wrap-expr '(2 3)))
+              :to-equal
+              '(nil . "Expected `2' >= 3, but `2' was 2."))))
+  (describe ":to-be-close-to"
+    (it "to match when value difference is less than precision"
+      (cl-destructuring-bind
+          (status . msg)
+          (buttercup--apply-matcher :to-be-close-to
+                                    (mapcar #'buttercup--wrap-expr '(0.01 
0.011 2)))
+        (expect status)
+        (expect
+         msg :to-match
+         "Expected `0.01' to differ from 0.011 by more than 0.01, but instead 
it was 0.01, with a difference of 0.00[0-9]+")))
+    (it "to not match when value difference is larger than precision"
+      (cl-destructuring-bind
+          (status . msg)
+          (buttercup--apply-matcher :to-be-close-to
+                                    (mapcar #'buttercup--wrap-expr '(0.01 
0.011 4)))
+        (expect status :not :to-be-truthy)
+        (expect
+         msg :to-match
+         "Expected `0.01' to be within 0.0001 of 0.011, but instead it was 
0.01, with a difference of 0.00[0-9]+"))))
+  (describe ":to-throw"
+    (xit "is not possible to test"))
+  (describe ":to-have-been-called"
+    (before-each
+      (spy-on 'i-spy-with-my-little-eye))
+    (it "to not match if the spy has not been called"
+      (expect (buttercup--apply-matcher
+               :to-have-been-called
+               (mapcar #'buttercup--wrap-expr '('i-spy-with-my-little-eye)))
+              :not :to-be-truthy))
+    (it "to not match if the spy has been called once"
+      (i-spy-with-my-little-eye)
+      (expect (buttercup--apply-matcher
+               :to-have-been-called
+               (mapcar #'buttercup--wrap-expr '('i-spy-with-my-little-eye)))
+              :to-be-truthy))
+    (it "to not match if the spy has been called multiple times"
+      (dotimes (x 1000)
+        (i-spy-with-my-little-eye))
+      (expect (buttercup--apply-matcher
+               :to-have-been-called
+               (mapcar #'buttercup--wrap-expr '('i-spy-with-my-little-eye)))
+              :to-be-truthy))
+    )
+  (describe ":to-have-been-called-with"
+    (before-each
+      (spy-on 'i-spy-with-my-little-eye))
+    (it "to not match if the spy has not been called at all"
+      (expect
+       (buttercup--apply-matcher
+        :to-have-been-called-with
+        (mapcar #'buttercup--wrap-expr '('i-spy-with-my-little-eye 123)))
+       :to-equal
+       '(nil
+         .
+         "Expected `i-spy-with-my-little-eye' to have been called with (123), 
but it was not called at all")))
+    (it "to not match if the spy has not been called with the specified 
arguments"
+      (i-spy-with-my-little-eye 123)
+      (i-spy-with-my-little-eye 456)
+      (i-spy-with-my-little-eye 789)
+      (i-spy-with-my-little-eye 'ABC)
+      (i-spy-with-my-little-eye 'DEF)
+      (i-spy-with-my-little-eye 'HIJ)
+      (i-spy-with-my-little-eye 'KLM)
+      (expect
+       (buttercup--apply-matcher
+        :to-have-been-called-with
+        (mapcar #'buttercup--wrap-expr '('i-spy-with-my-little-eye 234)))
+       :to-equal
+       '(nil
+         .
+         "Expected `i-spy-with-my-little-eye' to have been called with (234), 
but it was called with (123), (456), (789), (ABC), (DEF), (HIJ), (KLM)")))
+    (it "to match if the spy has been called once with the specified arguments"
+      (i-spy-with-my-little-eye 123)
+      (i-spy-with-my-little-eye 456)
+      (i-spy-with-my-little-eye 789)
+      (i-spy-with-my-little-eye 'ABC)
+      (i-spy-with-my-little-eye 'DEF)
+      (i-spy-with-my-little-eye 'HIJ)
+      (i-spy-with-my-little-eye 'KLM)
+      (expect (buttercup--apply-matcher
+               :to-have-been-called-with
+               (mapcar #'buttercup--wrap-expr '('i-spy-with-my-little-eye 
789)))
+              :to-be-truthy))
+    (it "to not match if the spy has been called multiple times with the 
specified arguments"
+      (dotimes (x 10)
+        (i-spy-with-my-little-eye 123)
+        (i-spy-with-my-little-eye 456))
+      (expect (buttercup--apply-matcher
+               :to-have-been-called-with
+               (mapcar #'buttercup--wrap-expr '('i-spy-with-my-little-eye 
456)))
+              :to-be-truthy))
+    )
+  (describe ":to-have-been-called-times"
+    (before-each
+      (spy-on 'i-spy-with-my-little-eye))
+    (it "to not match if the spy has been called less times"
+      (i-spy-with-my-little-eye)
+      (expect (buttercup--apply-matcher
+               :to-have-been-called-times
+               (mapcar #'buttercup--wrap-expr '('i-spy-with-my-little-eye 2)))
+              :to-equal
+              '(nil . "Expected `i-spy-with-my-little-eye' to have been called 
2 times, but it was called 1 time")))
+    (it "to not match if the spy has been called more times"
+      (dotimes (x 6)
+        (i-spy-with-my-little-eye))
+      (expect (buttercup--apply-matcher
+               :to-have-been-called-times
+               (mapcar #'buttercup--wrap-expr '('i-spy-with-my-little-eye 4)))
+              :to-equal
+              '(nil . "Expected `i-spy-with-my-little-eye' to have been called 
4 times, but it was called 6 times")))
+    (it "to match if the spy has been called the correct number of times"
+      (dotimes (x 6)
+        (i-spy-with-my-little-eye))
+      (expect (buttercup--apply-matcher
+               :to-have-been-called-times
+               (mapcar #'buttercup--wrap-expr '('i-spy-with-my-little-eye 6)))
+              :to-be-truthy))
+    (it "to match if the spy has been called 0 times"
+      (expect (buttercup--apply-matcher
+               :to-have-been-called-times
+               (mapcar #'buttercup--wrap-expr '('i-spy-with-my-little-eye 0)))
+              :to-be-truthy))))
+
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;;; Suite and spec data structures
 
@@ -2160,6 +2518,7 @@ text properties using `ansi-color-apply'."
 ;; indent-tabs-mode: nil
 ;; sentence-end-double-space: nil
 ;; eval: (buttercup-minor-mode)
+;; tab-width: 8
 ;; End:
 (provide 'test-buttercup)
 ;;; test-buttercup.el ends here



reply via email to

[Prev in Thread] Current Thread [Next in Thread]