sirherrbatka / Mockery

Quick and simple mocking for common lisp

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Mockery, a CL mocking library

What is Mockery?

Mockery is a as-simple-as-it-gets mocking library built for Common Lisp.

How it works?

Mockable code fragments are wrapped in a thunk macro and passed generic function dispatched on object bound to special variable. Default method will simply call the passed function object, mocks substitute it with their own call.

How to use it?

Example should help.

(defun test (a b c)
  (mockery:block ((:a :b :c) (a b c))
    (print (list a b c))))

(mockery:with-mocking ((:direct (:a :b :c) :times 1)
                       (:mock (:a :b :c)
                        (a b c)
                        (print (+ a b c))
                        :times 1)
                       (:mock (:a :b)
                        (a b c)
                        (print (list c b a))))
  (test 1 2 3)
  (test 1 2 3)
  (test 1 2 3))

Here we have the TEST function, and MOCKERY:BLOCK int it. MOCKERY:BLOCK contains implementation body, label and arguments. Label (:a :b :c) is identifier of the mocked block. It is hierchical in nature, as it is often the case that mocked behavior is grupped in certain categories, for instance (:api-call :user :create-user :from-ui). Arguments, are simply values that will be forwarded to mock.

Next, we have MOCKERY:WITH-MOCKING macro that specifies what mocks actually do. In this example we are sayding that first call to (:a :b :c) will behave as normal, while the next will print sum of a, b and c, and the third one (and all subsequent ones because that’s the default behavior) is gonna print list in a reversed order.

Notice how the third mock is labeled as (:a :b) and not (:a :b :c). This is by design, as a mock label simply need to fit as prefix to the block label. This way programmer can, for instance, mock all api-calls orginating from server/network malfunction.

About

Quick and simple mocking for common lisp

License:BSD 2-Clause "Simplified" License


Languages

Language:Common Lisp 100.0%