Nested checkers

marick edited this page Feb 10, 2012 · 1 revision
Clone this wiki locally

The use of collection checkers like just allows you to use checkers within a description of a collection. So, for example, you can use the roughly checker to do approximate comparisons of a collection of floating-point numbers:

(fact [0.1 0.2 0.3] => (just [(roughly 0.1)
                              (roughly 0.2)
                              (roughly 0.3)]))

However, that interpretation of what's inside a just is not recursive. So this does not check out:

(fact
  [
     ["value" 0.1 "value"]
     ["value" 0.2 "value"]
  ]
  => (just [
              ["value" (roughly 0.1) "value"]     ; The `roughly` is treated as an object, not a checker.
              ["value" (roughly 0.2) "value"]
           ]))

To make it work, you have to "wrap" each of the nested descriptions of collections in a just:

(fact
  [
     ["value" 0.1 "value"]
     ["value" 0.2 "value"]
  ]
  => (just [
              (just ["value" (roughly 0.1) "value"])
              (just ["value" (roughly 0.2) "value"])
           ]))

However, this is more verbose than necessary. Collection checkers like just can take either a single collection or N arguments that they assume should be combined into a collection. That lets you drop one level of delimiters:

(fact
  [
     ["value" 0.1 "value"]
     ["value" 0.2 "value"]
  ]
  => (just 
         (just "value" (roughly 0.1) "value")
         (just "value" (roughly 0.2) "value")
     ))