RclexTesting.Matchers (RclexTesting (Experimental) v0.1.0)

Copy Markdown View Source

Declarative matchers for use with expect/2, refute_message/3, and expect_sequence/2.

Each matcher function returns a predicate fn value -> boolean end that can be passed directly to any DSL assertion:

expect "/robot/state",
  matches(%{
    mode: :running,
    speed: gt(0),
    battery: between(20, 100)
  })

Note on not/1

not/1 shadows Kernel.not/1. Add the following to your test module if you also need Kernel's boolean not:

import Kernel, except: [not: 1]

Summary

Functions

Match lists where every element satisfies matcher.

Match lists where at least one element satisfies matcher.

Match numeric values within delta of n (default delta: 1.0e-6).

Match values in the inclusive range [low, high].

Match strings containing substring, or lists containing element.

Match empty enumerables or strings.

Match values strictly greater than n.

Match values greater than or equal to n.

Match the value false exactly.

Match nil.

Match the value true exactly.

Match values strictly less than n.

Match values less than or equal to n.

Match a struct or map against a spec map.

Invert a matcher.

Match values that are NOT a member of list.

Match non-empty enumerables or strings.

Match any non-nil value.

Match values that are a member of list.

Wrap a custom predicate as a matcher (identity — the predicate is already a matcher).

Functions

all(matcher)

Match lists where every element satisfies matcher.

any(matcher)

Match lists where at least one element satisfies matcher.

approx(n, opts \\ [])

Match numeric values within delta of n (default delta: 1.0e-6).

approx(42.0)
approx(42.0, delta: 0.01)

between(low, high)

Match values in the inclusive range [low, high].

contains(item)

Match strings containing substring, or lists containing element.

contains("mission")   # string substring check
contains(:obstacle)   # list membership check

empty()

Match empty enumerables or strings.

gt(n)

Match values strictly greater than n.

gte(n)

Match values greater than or equal to n.

is_false()

Match the value false exactly.

is_nil()

Match nil.

is_true()

Match the value true exactly.

lt(n)

Match values strictly less than n.

lte(n)

Match values less than or equal to n.

matches(spec)

@spec matches(map()) :: (any() -> boolean())

Match a struct or map against a spec map.

Each key in spec must exist in the value. Map values that are matcher functions are applied; plain values are compared with ==.

matches(%{mode: :running})
matches(%{speed: gt(0), battery: between(20, 100)})
matches(%{pose: matches(%{x: approx(10.0, delta: 0.1)})})

negate(matcher)

Invert a matcher.

none_of(list)

Match values that are NOT a member of list.

not_empty()

Match non-empty enumerables or strings.

not_nil()

Match any non-nil value.

one_of(list)

Match values that are a member of list.

satisfies(predicate)

Wrap a custom predicate as a matcher (identity — the predicate is already a matcher).