Checking sets

marick edited this page Mar 19, 2013 · 4 revisions
Clone this wiki locally

Executable examples

Checking the whole set

You can use just to apply Midje's notion of extended equality to an entire set:

(fact "`just` provides extended equality for sets"
  #{3 8 1} => (just odd? 3 even?))

Notice that Midje goes to some effort not to commit too early to a match. For example, suppose it decided that odd? matched 3. Then there would be no way for the remainder to match. Rather than failing, Midje will backtrack and find the match (odd? => 1, 3 => 3, even? => 8).

If you require that there be a specific number of elements, all of which share the same property, you can use the n-of family of checkers:

(fact "checking properties of known number of elements"
  #{1 3 5} => (three-of odd?))

If you don't care about the number of elements, use has:

(fact "number irrelevant"
  #{1 3 5} => (has every? odd?))

Checking a subset

When you want to work with a subset of the original set, use contains. Here's an example that doesn't take advantage of extended equality:

(fact "subsets of literal values"
  #{1 2 3} => (contains 3))

Here's one that does:

  (fact "subsets of checkers"
    #{1 2 3} => (contains odd? even?)))