clj-fakes.context

API for working in explicit context.

any

Matcher which matches any value. Implements both ArgsMatcher and ImplicitArgMatcher.

arg

macro

(arg matcher)

Creates an ImplicitArgMatcher from the specified ArgMatcher to be used in vector args matcher.

arg*

(arg* matcher matcher-str)

The same as arg macro, but string for printing the matcher must be specified explicitly.

ArgMatcher

protocol

Protocol for explicit arg matchers.

Also see: arg.

members

arg-matches?

(arg-matches? this arg)

Should return true or false.

ArgsMatcher

protocol

Protocol for multiple args matching.

members

args-match?

(args-match? this args)

Should return true or false.

args-matcher->str

(args-matcher->str this)

Should return a string for debug messages.

calls

(calls ctx)(calls ctx f)

For the specified recorded fake returns a vector of its calls. Returned vector is ordered by call time: the earliest call will be at the head.

A call is a map with keys: :args, :return-value.

If fake is not specified then it will return all the calls recorded in the context:

[[recorded-fake1 call1]
 [recorded-fake2 call2]
 ...]

context

(context)

Creates a new context atom.

Do not alter the atom manually, context fields should be considered a private API.

Also see: clj-fakes.core/with-fakes.

cyclically

(cyclically coll)

Returns a function that:

  1. takes any number of arguments;
  2. on each call returns the next value from coll, cyclically.

default-fake-config

With this config fake will return a new FakeReturnValue type instance for any combination of args.

fake

macro

(fake ctx config)

Creates a fake function. The created function must be called; otherwise, self-test-unused-fakes will fail.

Config is a vector of pairs: [args-matcher1 fn-or-value1 args-matcher2 fn-or-value2 ...]

Use fake* in case this macro is called from another macro.

fake*

macro

(fake* ctx form config)

The same as fake macro but for reuse in other macros.

form must be passed from the parent macro in order to correctly detect position.

ImplicitArgMatcher

protocol

Most likely you shouldn’t use this protocol. Consider creating explicit custom matchers by implementing ArgMatcher protocol and using arg macro.

members

arg-matcher->str

(arg-matcher->str this)

Should return a string for debug messages.

arg-matches-implicitly?

(arg-matches-implicitly? this arg)

Should return true or false.

mark-checked

(mark-checked ctx f)

After using this function self-test-unchecked-fakes will not warn about the specified recorded fake.

method

(method ctx obj f)

Returns a hash-key by which calls are recorded for the specified fake protocol instance method. Can be used in functions which take recorded fake as an input.

method-was-called

(method-was-called ctx f obj args-matcher)

was-called for protocol method fakes.

method-was-called-once

(method-was-called-once ctx f obj args-matcher)

was-called-once for protocol method fakes.

method-was-matched-once

(method-was-matched-once ctx f obj args-matcher)

was-matched-once for protocol method fakes.

method-was-not-called

(method-was-not-called ctx f obj)

was-not-called for protocol method fakes.

methods-were-called-in-order

(methods-were-called-in-order ctx & fns-objs-and-matchers)

were-called-in-order for protocol method fakes.

Syntax:

(methods-were-called-in-order ctx
  f1 obj1 args-matcher1
  f2 obj2 args-matcher2
  ...)

optional-fake

(optional-fake ctx)(optional-fake ctx config)

Creates an optional fake function which will not be checked by self-test-unused-fakes, i.e. created fake is allowed to be never called.

Config is a vector of pairs: [args-matcher1 fn-or-value1 args-matcher2 fn-or-value2 ...]

If config is not specified then fake will be created with default-fake-config.

original-val

(original-val ctx a-var)

Given a patched variable returns its original value.

patch!

macro

(patch! ctx var-expr val)

Changes variable value in all threads.

recorded-fake

macro

(recorded-fake ctx)(recorded-fake ctx config)

Creates a fake function whose calls will be recorded.

Config is a vector of pairs: [args-matcher1 fn-or-value1 args-matcher2 fn-or-value2 ...]

Ultimatelly, the created function must be marked checked (see mark-checked); otherwise, self-test-unchecked-fakes will fail.

Use recorded-fake* in case this macro is called from another macro.

recorded-fake*

macro

(recorded-fake* ctx form)(recorded-fake* ctx form config)

This is the same as recorded-fake macro but for reuse in other macros.

form must be passed from the parent macro in order to correctly detect position.

reify-fake

macro

(reify-fake ctx & specs)

Works similarly to reify macro, but implements methods in terms of fake functions. Created instance will raise an exception on calling protocol method which is not defined.

Supported fake types: :optional-fake, :fake, :recorded-fake.

Syntax example:

(reify-fake my-context
  protocol-or-interface-or-Object
  [method1 :optional-fake [any 123]]

  protocol-or-interface-or-Object
  [method2 :recorded-fake]

  ; reification of Object with arbitrary methods works only in ClojureScript, note how arglist must be explicitly provided:
  Object
  [new-method3 [x y z] :recorded-fake])

reify-fake*

macro

(reify-fake* ctx form env & specs)

The same as reify-fake but to be used inside macros.

form is needed to correctly determine fake positions, env is needed to determine target language.

reify-nice-fake

macro

(reify-nice-fake ctx & specs)

Works similarly to reify-fake, but automatically generates :optional-fake implementations for methods which are not explicitly defined by user.

It cannot yet automatically fake Java interface and Object methods.

reify-nice-fake*

macro

(reify-nice-fake* ctx form env & specs)

The same as reify-nice-fake but to be used inside macros.

form is needed to correctly determine fake positions, env is needed to determine target language.

self-test

(self-test ctx)

Runs all available self-tests.

self-test-unchecked-fakes

(self-test-unchecked-fakes ctx)

Raises an exception if some recorded fake was never marked checked, i.e. you forgot to assert its calls.

self-test-unused-fakes

(self-test-unused-fakes ctx)

Raises an exception when some fake was never called after its creation.

unpatch!

(unpatch! ctx a-var)

Restores original variable value.

unpatch-all!

(unpatch-all! ctx)

Restores original values for all the variables patched in the specified context.

was-called

(was-called ctx f args-matcher)

Checks that recorded fake was called at least once with the specified args. Returns true or raises an exception.

was-called-once

(was-called-once ctx f args-matcher)

Checks that recorded fake was called strictly once and that the call was with the specified args. Returns true or raises an exception.

was-matched-once

(was-matched-once ctx f args-matcher)

Checks that recorded fake was called at least once and only a single call satisfies the provided args matcher. Returns true or raises an exception.

was-not-called

(was-not-called ctx f)

Checks that recorded fake was never called. Returns true or raises an exception.

were-called-in-order

(were-called-in-order ctx & fns-and-matchers)

Checks that recorded fakes were called in the specified order with the specified args. It does not check that there were no other calls.

Syntax: (were-called-in-order ctx f1 args-matcher1 f2 args-matcher2 ...)

Returns true or raises an exception.