Skip to content

Checkers within prerequisites

marick edited this page Apr 23, 2012 · 3 revisions

Suppose you want a prerequisite function to return 3 no matter what its argument. That's done like this:

    (fact 
      (f 33) => 3
      (provided
         (g anything) => 3))  ;; 3 is returned any time g is called.

Most of the time, a function not a predefined checker is matched exactly, as that seems to be what people naturally expect. Consider this function:

    (fact 
      (f 33) => 3
      (provided
         (filter even? [1 2 3]) => [2]))

Here, the main purpose of the prerequisite is probably to document that filter is called a certain way. If you do want to use even? as a checker, wrap it in as-checker:

    (fact 
      (f 33) => 3
      (provided
         (g (as-checker even?)) => 3))  ;; g will be called with some even number and should return 3.

You can use the as-checker wrapper with anonymous functions, but you can also use checker as shorthand:

    (fact 
      (f 33) => 3
      (provided
         (g (checker [actual] (...))) => 3))

That is, just replace the fn in the function definition with checker.

If you'll be using a predicate in many prerequisites, you can make a named checker with defchecker:

     (defchecker prime
       "Checks whether the actual value is a prime"
       [actual]
       (...))

To emphasize: you only need to define checkers specially for prerequisites. If you want to describe facts, like this:

     (fact 17 => prime?)

... you don't need as-checker, checker, or defchecker.

Clone this wiki locally