Skip to content

Defining checkers for use in prerequisites

marick edited this page Feb 28, 2013 · 2 revisions
Clone this wiki locally

The discussion of prerequisites pointed out a special case for prerequisite arguments. Consider this:

  (f even?) => 3)

The prerequisite applies when f is called with the function even?. It would not apply to a call like (f 2). That's different than checkables, where functions are applied to arguments to judge them:

(fact 4 => even?)

However, Midje's predefined checkers work the same in both positions:

  (f truthy) => 3))  ; looking for a non-nil, non-false value

(fact (f) => truthy) ; looking for a non-nil, non-false value

What makes Midje's predefined checkers special? Metadata:

user=> (meta truthy)
{:midje/checker true}

The as-checker function adds this metadata to its argument, allowing prerequisites like this:

  (f (as-checker even?)) => 3)

If you're defining a function that'll often be used to match prerequisite arguments, define it the way Midje's built-in checkers are. Here's the definition for a simple checker:

(defchecker anything [actual] ...)

Here's the definition for a checker that takes an argument and produces a function:

  (defchecker roughly [expected delta]
    (checker [actual] ...))
Something went wrong with that request. Please try again.