Mixing facts and source code

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

Both lein-midje and the repl tools load-facts command load files from the project's :test-paths and :load-paths. That's rarely noticed by people who follow the common "test"/"src" separation because Midje takes care not to load files unnecessarily.

It can be noticed if you have a file in your source path that's not required (either directly or indirectly) by any of your test files. Midje will load it while another tool, like clojure.test, would not.

Midje loads from the source path because some people include tests alongside their source. That looks like this:

(defn chunk-line [line]
  (map (fn [three-characters] (apply str three-characters))
       (partition 3 line)))

(fact "chunk-line splits lines into three-character parcels"
  (chunk-line "111222") => ["111" "222"])

(defn parcel-digits [parcel]
  (let [chunks (map chunk-line parcel)]
    (partition (count parcel) (apply interleave chunks))))

(fact "parcel-digits extracts vertical parcels from a series of lines"
  (parcel-digits ["111222"
                  "bbbccc"])
  => [ ["111"
        "bbb"]
       ["222"
        "ccc"]])

Such tests can serve as ready documentation for the code near them: a sort of literate programming.

To prevent Midje forms from being included in your production builds, set midje.sweet/*include-midje-checks* to false.

midje-mode can hide such facts when you want to look at just the code.