-
Notifications
You must be signed in to change notification settings - Fork 129
Checkers
Midje checkables can have functions on the right-hand side. Like this:
(fact (f 33) => even?)
The function is called with the result of the left-hand side. To distinguish such uses from other ones, we say that even?
is a checker in the above checkable.
Often, checkers will take arguments that describe the expected value. These are built with function-generating functions:
user=> (defn each-element-is-one-of [expected-elements]
(fn [actual]
(every? (set expected-elements) actual)))
user=> (fact [1 2] => (each-element-is-one-of [1 2 3 4]))
true
Midje predefines some checkers that are particularly useful on the right-hand side. (Checkers are also used to match prerequisite arguments in top-down TDD, but that's not covered here.) The rest of this page describes the simpler predefined checkers.
-
truthy or TRUTHY
(fact (f) => truthy)
Use
truthy
when you don't care about a particular truth value, just that it's neithernil
norfalse
. -
falsey or FALSEY
(fact (f) => falsey)
Use
falsey
when you care that the value is eithernil
orfalse
, but don't care which. -
anything or irrelevant
(fact (f) => anything)
anything
is used for "don't care" arguments in prerequisites but you could also use it to say that you make no assertions about the result of a function.irrelevant
is a synonym. -
(exactly fn )
(fact (make-function-with-goedel-number 3) => (exactly odd?))
Use
exactly
when you want to talk about a particular function as a value, rather than applying it as a checker. -
(roughly number range )
True if the actual value is within
number
±range
. Ifrange
is not given, it's taken to be 1/1000th ofnumber
.
- (throws class message predicate )
;; check exception class
(fact (explosion) => (throws Exception))
;; check exception class and message
(fact (explosion) => (throws Exception "boom!"))
;; check exception satisfies predicate
(fact (explosion) => (throws trace-of-c4?))
;; check exception class and that it satisfies predicate
(fact (explosion) => (throws ExceptionInfo trace-of-c4?))
;; check with multiple predicates
(fact (explosion) => (throws #"boom" #"bang" #"smash"))
Use throws
when you expect the function to raise an exception.
- The class argument names a class.
- The message argument can be either a string or a regular expression. In the latter case, the rules of extended equality apply.
- The predicate is applied to the exception object to return either a truthy or falsey value.
If there is more than one argument, all of them must check successfully against the message. None of the arguments are required. There can be at most one class argument, but there can be more than one message or predicate argument.