Skip to content

Mixing facts and source code

klauern edited this page Feb 24, 2013 · 5 revisions

Both lein-midje and the repl tools load-facts command load files from both 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.

(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.

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

Clone this wiki locally