Dropped is-equal and is-string=, added is-every
[fiveam.git] / src / check.lisp
index b8ec760..5244704 100644 (file)
@@ -147,16 +147,14 @@ Wrapping the TEST form in a NOT simply preducse a negated reason string."
          (setf bindings (list (list v ?value))
                effective-test `(,?satisfies ,v)
                default-reason-args (list "~S did not satisfy ~S" v `',?satisfies)))
-        (t
+        (?_
          (setf bindings '()
                effective-test test
-               default-reason-args "No reason supplied.")))
+               default-reason-args (list "No reason supplied"))))
       `(let ,bindings
          (if ,effective-test
              (add-result 'test-passed :test-expr ',test)
-             (process-failure :reason ,(if (null reason-args)
-                                           `(format nil ,@default-reason-args)
-                                           `(format nil ,@reason-args))
+             (process-failure :reason (format nil ,@(or reason-args default-reason-args))
                               :test-expr ',test))))))
 
 ;;;; *** Other checks
@@ -167,6 +165,18 @@ Wrapping the TEST form in a NOT simply preducse a negated reason string."
      (format *test-dribble* "s")
      (add-result 'test-skipped :reason (format nil ,@reason))))
 
+(defmacro is-every (predicate &body clauses)
+  "The input is either a list of lists, or a list of pairs. Generates (is (,predicate ,expr ,value))
+   for each pair of elements or (is (,predicate ,expr ,value) ,@reason) for each list."
+  `(progn
+    ,@(if (every #'consp clauses)
+          (loop for (expected actual &rest reason) in clauses
+                collect `(is (,predicate ,expected ,actual) ,@reason))
+          (progn
+            (assert (evenp (list-length clauses)))
+            (loop for (expr value) on clauses by #'cddr
+                  collect `(is (,predicate ,expr ,value)))))))
+
 (defmacro is-true (condition &rest reason-args)
   "Like IS this check generates a pass if CONDITION returns true
   and a failure if CONDITION returns false. Unlike IS this check
@@ -193,24 +203,29 @@ Wrapping the TEST form in a NOT simply preducse a negated reason string."
      :test-expr ',condition)
     (add-result 'test-passed :test-expr ',condition)))
 
-(defmacro signals (condition &body body)
+(defmacro signals (condition-spec
+                   &body body)
   "Generates a pass if BODY signals a condition of type
 CONDITION. BODY is evaluated in a block named NIL, CONDITION is
 not evaluated."
   (let ((block-name (gensym)))
-    `(block ,block-name
-       (handler-bind ((,condition (lambda (c)
-                                    (declare (ignore c))
-                                    ;; ok, body threw condition
-                                    (add-result 'test-passed 
-                                                :test-expr ',condition)
-                                    (return-from ,block-name t))))
-        (block nil
-          ,@body
-           (process-failure
-            :reason (format nil "Failed to signal a ~S" ',condition)
-            :test-expr ',condition)
-          (return-from ,block-name nil))))))
+    (destructuring-bind (condition &optional reason-control reason-args)
+        (ensure-list condition-spec)
+      `(block ,block-name
+         (handler-bind ((,condition (lambda (c)
+                                      (declare (ignore c))
+                                      ;; ok, body threw condition
+                                      (add-result 'test-passed 
+                                                  :test-expr ',condition)
+                                      (return-from ,block-name t))))
+           (block nil
+             ,@body))
+         (process-failure
+          :reason ,(if reason-control
+                       `(format nil ,reason-control ,@reason-args)
+                       `(format nil "Failed to signal a ~S" ',condition))
+          :test-expr ',condition)
+         (return-from ,block-name nil)))))
 
 (defmacro finishes (&body body)
   "Generates a pass if BODY executes to normal completion. In