Skip to content

Using metadata to filter facts

marick edited this page Feb 15, 2013 · 10 revisions

This feature is available in Midje 1.5.

You can find a complete description of metadata elsewhere. This page describes only the parts relevant to filtering facts.

Metadata is a map stored in the usual way, so you can use meta to fetch it:

user=> (meta (last-fact-checked))
{:midje/top-level-fact? true, :midje/guid "f9065fa7389750e16fe00d7ba36748f61d3e0df6", ...
 :priority 5, :slow true, :core true}

As you can see, the metadata is a mixture of Midje-generated key/value pairs (the first line) and user-defined pairs (the second). Here is how you could define a fact like the above:

(fact :slow :core {:priority 5}
  ...)

If a keyword's value is to be true, you need only mention the keyword. Otherwise, you provide the key and value in a map.

Using the repl tools

The repl tools filter facts according to keyword arguments (other than :all) and function arguments.

Here is how you would would load all the facts whose :core metadata is truthy:

(load-facts :core)

Now let's suppose you use (load-facts :all) to load all the facts in your project. You can check only the ones not marked :slow like this:

(check-facts (complement slow))

Note that the facts you wanted to check are remembered as the working set. If you change some code and want to recheck the not-slow tests, you need only type this:

(check-facts)

If you want to check all the facts with a priority higher than 3, you'd type this:

(check-facts #(> (:priority %) 3))

If more than one argument is given, any matching fact is checked. (Consider the arguments to be or'ed together.) The following will run all non-slow tests, but also slow tests with a priority greater than three.

(check-facts (complement :slow) #(> (:priority %) 3))

Lein midje

Lein-midje filters according to arguments after the :filter keyword. The following selects tests that are core or not slow:

% lein midje :filter core -slow

Notice that, like the repl tools, multiple arguments mean "select tests that match any of these".

lein midje only allows you to specify whether a keyword is truthy or falsey. There's no way to instruct it to check all facts with a priority greater than 3.

Note that lein midje doesn't use the Unix conventions for options. Keywords come after the main arguments. So that if you wanted to check all the core facts in a particular namespace, you'd type:

% lein midje quux.f-transmogrification :filter :core
Clone this wiki locally