The bytecurry.mocks Reference Manual

Table of Contents

Next: , Previous: , Up: (dir)   [Contents][Index]

The bytecurry.mocks Reference Manual

This is the bytecurry.mocks Reference Manual, version 1.0.0, generated automatically by Declt version 2.4 "Will Decker" on Wed Jun 20 10:51:21 2018 GMT+0.


Next: , Previous: , Up: Top   [Contents][Index]

1 Introduction

Table of Contents

bytecurry.mocks

bytecurry.mocks is a small library for using mock functions and methods in unit tests. It is framework agnostic. That is, it doesn't depend on any specific testing framework, nor is it designed with any specific framework in mind. In fact bytecurry.mocks has no external dependencies.

bytecurry.mocks contains two macros: with-mocked-functions and with-added-methods. These macros allow you to create mock functions and methods respectively in a limited scope so that you can control the outputs of functions while running a unit test.

WITH-MOCKED-FUNCTIONS

with-mocked-functions is a macro which acts very similarly to flet, but binds the functions with dynamic scope rather than lexical scope. It does this by redefining the function before executing the body, then restores the original definition afterwards.

There are a couple of things to note when using this macro:

  1. You can't mock functions in a locked package (such as CL on sbcl)
  2. The compiler may inline function calls, in which case changing the function definition will have no effect on the inlined calls.

WITH-ADDED-METHODS

with-added-methods is somewhat similar to with-mocked-functions, but allows you to define methods for a generic function in a limited scope (again with dynamic scope). It defines new methods before executing the body, and then removes the method after the body.

Since it removes the method afterwards, and defmethod replaces any previous method with the same specifiers, replacing an existing method will effectively remove the original method outside the scope of with-added-methods.

More Info

For more information see the documentation strings.

The API manual is available here.


Next: , Previous: , Up: Top   [Contents][Index]

2 Systems

The main system appears first, followed by any subsystem dependency.


Previous: , Up: Systems   [Contents][Index]

2.1 bytecurry.mocks

Author

Thayne McCombs <astrothayne@gmail.com>

License

MIT

Description

Tools to mock functions for unit tests

Version

1.0.0

Defsystem Dependency

bytecurry.asdf-ext

Source

bytecurry.mocks.asd (file)

Components

Next: , Previous: , Up: Top   [Contents][Index]

3 Files

Files are sorted by type and then listed depth-first from the systems components trees.


Previous: , Up: Files   [Contents][Index]

3.1 Lisp


Next: , Previous: , Up: Lisp files   [Contents][Index]

3.1.1 bytecurry.mocks.asd

Location

bytecurry.mocks.asd

Systems

bytecurry.mocks (system)


Next: , Previous: , Up: Lisp files   [Contents][Index]

3.1.2 bytecurry.mocks/package.lisp

Parent

bytecurry.mocks (system)

Location

package.lisp

Packages

bytecurry.mocks


Previous: , Up: Lisp files   [Contents][Index]

3.1.3 bytecurry.mocks/mocks.lisp

Dependency

package.lisp (file)

Parent

bytecurry.mocks (system)

Location

mocks.lisp

Exported Definitions
Internal Definitions

Next: , Previous: , Up: Top   [Contents][Index]

4 Packages

Packages are listed by definition order.


Previous: , Up: Packages   [Contents][Index]

4.1 bytecurry.mocks

Package to provide tools to mock functions
in common lisp unit tests.

Provides WITH-MOCKED-FUNCTIONS, which acts like FLET or LABELS, but shadows functions in the global scope, so that the function bindings are available outside of the lexical scope of the body. See docstring for more information.

Source

package.lisp (file)

Nickname

mocks

Use List

common-lisp

Exported Definitions
Internal Definitions

Next: , Previous: , Up: Top   [Contents][Index]

5 Definitions

Definitions are sorted by export status, category, package, and then by lexicographic order.


Next: , Previous: , Up: Definitions   [Contents][Index]

5.1 Exported definitions


Previous: , Up: Exported definitions   [Contents][Index]

5.1.1 Macros

Macro: with-added-methods BINDINGS &body BODY

Execute BODY with some extra methods.

This works similarly to WITH-MOCKED-FUNCTIONS. But defines methods instead of functions.
The bindings should look like DEFMETHOD definitions without the defmethod symbol.

One particularly useful scenario is to mock up an instance of a class by using eql specializers
for a local variable.

For now, it only supports adding new methods, not replacing existing methods, since it doesn’t restored the previous method. (mostly because I don’t know a good way to extract the specifiers from the definition form.)

Package

bytecurry.mocks

Source

mocks.lisp (file)

Macro: with-mocked-functions BINDINGS &body BODY

Execute BODY with some functions mocked up.

BINDINGS is a list of function bindings, similar to those in FLET or LABELS.
Before executing BODY, the bindings are used to replace the definitions of functions, and after BODY has finished, the original function definitions are restored.

You can use this macro to mock funtions for unit tests. For example, if function A calls function B, you can replace the definition of B while testing A, to isolate the test on just the behavior of A.

There are two things to note when using this macro:
1. You can’t mock functions in a locked package (such as CL)
2. The compiler may inline function calls, in which case changing the definition will have no effect.

Package

bytecurry.mocks

Source

mocks.lisp (file)


Previous: , Up: Definitions   [Contents][Index]

5.2 Internal definitions


Previous: , Up: Internal definitions   [Contents][Index]

5.2.1 Functions

Function: %get-binding-names BINDING
Package

bytecurry.mocks

Source

mocks.lisp (file)

Function: %let-expr NAMES

Get the let binding for a mock function binding

Package

bytecurry.mocks

Source

mocks.lisp (file)

Function: %setf-for-binding BINDING

Store the mocked function.

Package

bytecurry.mocks

Source

mocks.lisp (file)

Function: %setf-for-original NAMES
Package

bytecurry.mocks

Source

mocks.lisp (file)


Previous: , Up: Top   [Contents][Index]

Appendix A Indexes


Next: , Previous: , Up: Indexes   [Contents][Index]

A.1 Concepts

Jump to:   B   F   L  
Index Entry  Section

B
bytecurry.mocks.asd: The bytecurry<dot>mocks<dot>asd file
bytecurry.mocks/mocks.lisp: The bytecurry<dot>mocks/mocks<dot>lisp file
bytecurry.mocks/package.lisp: The bytecurry<dot>mocks/package<dot>lisp file

F
File, Lisp, bytecurry.mocks.asd: The bytecurry<dot>mocks<dot>asd file
File, Lisp, bytecurry.mocks/mocks.lisp: The bytecurry<dot>mocks/mocks<dot>lisp file
File, Lisp, bytecurry.mocks/package.lisp: The bytecurry<dot>mocks/package<dot>lisp file

L
Lisp File, bytecurry.mocks.asd: The bytecurry<dot>mocks<dot>asd file
Lisp File, bytecurry.mocks/mocks.lisp: The bytecurry<dot>mocks/mocks<dot>lisp file
Lisp File, bytecurry.mocks/package.lisp: The bytecurry<dot>mocks/package<dot>lisp file

Jump to:   B   F   L  

Next: , Previous: , Up: Indexes   [Contents][Index]

A.2 Functions

Jump to:   %  
F   M   W  
Index Entry  Section

%
%get-binding-names: Internal functions
%let-expr: Internal functions
%setf-for-binding: Internal functions
%setf-for-original: Internal functions

F
Function, %get-binding-names: Internal functions
Function, %let-expr: Internal functions
Function, %setf-for-binding: Internal functions
Function, %setf-for-original: Internal functions

M
Macro, with-added-methods: Exported macros
Macro, with-mocked-functions: Exported macros

W
with-added-methods: Exported macros
with-mocked-functions: Exported macros

Jump to:   %  
F   M   W  

Next: , Previous: , Up: Indexes   [Contents][Index]

A.3 Variables


Previous: , Up: Indexes   [Contents][Index]

A.4 Data types

Jump to:   B   P   S  
Index Entry  Section

B
bytecurry.mocks: The bytecurry<dot>mocks system
bytecurry.mocks: The bytecurry<dot>mocks package

P
Package, bytecurry.mocks: The bytecurry<dot>mocks package

S
System, bytecurry.mocks: The bytecurry<dot>mocks system

Jump to:   B   P   S