Writing your own checkers

marick edited this page Apr 23, 2012 · 1 revision
Clone this wiki locally

Any function can be used as a checker on the right-hand side of an arrow, so defining a checker is easy:

(defn even-length [actual-result]
  (even? (count actual-result)))

(fact [0 1 2 1 3] =not=> even-length)

Often, checkers will take arguments that are used to somehow describe the expected value. These are built with functions that generate functions to evaluate the actual value against what's expected:

(defn only-these [& expected-elements]
  (fn [actual]
    (every? (set expected-elements) actual)))

Identifying functions as checkers

There's another side to an arrow, the left-hand side. Checkers can only be used there if they're defined specially. (See here for why.) But even if you don't do that, it can't hurt to mark a checker as being such. For a checker that takes no arguments, that's done with defchecker, a macro that expands to defn plus a little bit more:

(defchecker even-length [actual-result]
  (even? (count actual-result)))

For checkers that take arguments, you'll use both defchecker and checker (a macro that expands into fn plus a little bit more):

(defchecker only-these [& expected-elements]
  (checker [actual]
           (every? (set expected-elements) actual)))