forked from marick/Midje
-
Notifications
You must be signed in to change notification settings - Fork 0
Checking maps and records
marick edited this page Feb 14, 2013
·
2 revisions
Using a map on the right-hand side of a prediction means you care about contents, not type:
(defrecord R [x y])
(fact "using a map implies that you care about contents, not type"
(R. 1 2) => {:x 1, :y 2}
;; the above uses the same comparision logic as a map comparison:
{:x 1, :y 2} => {:x 1, :y 2})
When testing code that generates records, you may require that the result be one of those records, not, say, some random map:
(fact "using a record implies that you care about both contents and type"
{:x 1, :y 1} =not=> (R. 1 2)
(NotR. 1 2) =not=> (R. 1 2)
(R. 1 2) => (R. 1 2)))
Use just
if you want more than equality comparison for values in maps or records. For example:
(fact "`just` provides extended equality"
{:a 1, :b 2, :c "some text"} => (just {:a odd?, :b 2, :c #"text"}))
Use contains
when you don't care about parts of a map or record:
(fact "contains ignores unmentioned keys"
(R. 1 'IGNORE!) => (contains {:x 1}))
contains
also provides extended equality:
(fact "`contains` provides extended equality"
(R. 1 'IGNORE) => (contains {:x odd?}))
has
lets you make quantified ("every" "some") claims about the values of a map or record:
(fact "`'has` quantifies over values"
{:a 1, :b 3} => (has every? odd?))
The just
and contains
right-hand sides can take arrays of pairs (or Java MapEntry
objects) instead of a map or record:
(fact "a sequence of key/value pairs is OK on the right-hand side"
{:a 1, :b 2} => (just [[:a 1] [:b 2]])
(R. 1 nil) => (contains [[:x 1]]))