-
Notifications
You must be signed in to change notification settings - Fork 129
Extended equality
Midje checks the right-hand side of a checkable's=>
using extended equality. That adds some special cases onto Clojure's =
:
-
Functions: When the right-hand-side is a function, it's applied to the actual result:
1 => even?
See Checkers for a prepackaged set of useful functions.
-
Regular expressions: When the left-hand-side is a string and the right-hand-side is a regular expression, the behavior is that of
re-find
:"the entire string need not match" => #"ne*d"
If you want the entire string to match, you can either write the regular expression with
^
and$
, or you can use one of the prepackaged collection checkers:"the entire string need not match" => (just #"ne*d") ; fails
Unlike with Clojure's equality, two regular expressions formed from
=
strings are counted as equal. -
Maps and records: "Classic" maps can be compared to records. Using a record on the right-hand side means you want the left-hand side's value to have the same type as well as the same key-value pairs. Using a map means you care only about key-value pairs. Examples:
(defrecord AB [a b]) (fact (AB. 1 2) => (AB. 1 2)) ; true (fact {:a 1, :b 2} => (AB. 1 2)) ; false, as with normal Clojure equality (fact (AB. 1 2) => {:a 1, :b 2}) ; true: all that matters is keys and values.
-
Exceptions: If a comparison would normally blow up, it returns false:
"five" => odd? ; false instead of a java.lang.ClassCastException
-
Collections: The collection checkers can be used to apply extended equality to each collection element. The following example insists a collection be exactly two even numbers followed by two odd:
[2 4 1 3] => (just [even? even? odd? odd?])
-
Direct access: If you want to use extended equality in your own checkers, include this in your namespace declaration:
(:use [midje.checking.core :only [extended-=]])