Class Support

Class Documentation

class Support
[source]

Central mock-support object for expectation recording and verification.

One Support instance represents one named scope (or the global scope when the name is empty). Nested scopes are created transparently by mock(“scope”).

Lifecycle within a test

  1. In setup (or at the start of the test): optionally call strict_order() or install_comparator().

  2. Register expectations with expect_one_call() / expect_n_calls().

  3. Production code calls actual_call() when the mocked function executes.

  4. In teardown: call check_expectations() then clear().

Public Types

using FunctionPointerValue = void (*)()

Type alias for a pointer to a function with signature void().

Public Functions

Support(StringView mock_name = "")

Construct a Support with an optional scope name.

Prefer the free function mock() over constructing directly.

Parameters:

mock_name – Scope name; empty string means the global scope.

virtual ~Support()
[source]
virtual void strict_order()
[source]

Require actual calls to arrive in the same order expectations were set.

By default calls may arrive in any order. Call strict_order() before registering expectations to enforce ordering.

virtual ExpectedCall &expect_one_call(StringView function_name)

Expect exactly one call to function_name.

Parameters:

function_name – Name of the function that should be called once.

Returns:

A fluent ExpectedCall for chaining mu::tiny::mock::ExpectedCall::with_parameter() and mu::tiny::mock::ExpectedCall::and_return_value() constraints.

virtual void expect_no_call(StringView function_name)

Assert that function_name is never called.

If actual_call() is invoked with this name, the test fails immediately.

Parameters:

function_name – Function that must not be called.

virtual ExpectedCall &expect_n_calls(unsigned int amount, StringView function_name)

Expect exactly amount calls to function_name.

Parameters:
  • amount – Number of times the function should be called.

  • function_name – Name of the function.

Returns:

A fluent ExpectedCall for chaining parameter/return constraints.

virtual ActualCall &actual_call(const char *function_name)

Report that function_name was actually called.

Call this from inside a mock function implementation, then chain with_parameter() for each argument. Retrieve the configured return value with the appropriate return_*_value() method at the end.

Parameters:

function_name – Name of the function being called.

Returns:

A fluent ActualCall for supplying parameters and reading the return value.

virtual bool has_return_value()
[source]
Returns:

true if the last actual_call() has an associated return value.

virtual NamedValue return_value()
[source]
Returns:

The return value of the last actual_call() as a NamedValue.

template<typename T>
inline T return_value()

Type-safe return-value accessor template.

Template Parameters:

T – The type to retrieve.

Returns:

The stored return value converted to T.

template<typename T>
inline T return_value_or_default(T default_value)

Type-safe return-value accessor with a fallback default.

Template Parameters:

T – The type to retrieve.

Parameters:

default_value – Value returned when no return value was configured.

Returns:

The stored return value or default_value.

bool has_data(StringView name)

Check whether named data exists in this scope.

Parameters:

name – Data key.

Returns:

true if set_data() was previously called with name.

void set_data(StringView name, bool value)

Store a bool value under name for retrieval across mock calls.

Parameters:
  • name – Key.

  • value – Value.

void set_data(StringView name, int value)

Store an int value.

Parameters:
  • name – Key.

  • value – Value.

void set_data(StringView name, unsigned int value)

Store an unsigned int value.

Parameters:
  • name – Key.

  • value – Value.

void set_data(StringView name, long int value)

Store a long int value.

Parameters:
  • name – Key.

  • value – Value.

void set_data(StringView name, unsigned long int value)

Store an unsigned long int value.

Parameters:
  • name – Key.

  • value – Value.

void set_data(StringView name, const char *value)

Store a string value.

Parameters:
  • name – Key.

  • value – Value.

void set_data(StringView name, double value)

Store a double value.

Parameters:
  • name – Key.

  • value – Value.

void set_data(StringView name, void *value)

Store a void* value.

Parameters:
  • name – Key.

  • value – Value.

void set_data(StringView name, const void *value)

Store a const void* value.

Parameters:
  • name – Key.

  • value – Value.

void set_data(StringView name, long long value)

Store a long long value.

Parameters:
  • name – Key.

  • value – Value.

void set_data(StringView name, unsigned long long value)

Store an unsigned long long value.

Parameters:
  • name – Key.

  • value – Value.

void set_data(StringView name, FunctionPointerValue value)

Store a function pointer value.

Parameters:
  • name – Key.

  • value – Value.

void set_data_object(StringView name, StringView type, void *value)

Store a mutable object pointer with a type name.

Parameters:
  • name – Data key.

  • type – Type name string (used for display on failure).

  • value – Pointer to the object.

void set_data_const_object(StringView name, StringView type, const void *value)

Store a const object pointer with a type name.

Parameters:
  • name – Data key.

  • type – Type name string.

  • value – Const pointer to the object.

NamedValue get_data(StringView name)

Retrieve a previously stored data value by name.

Parameters:

name – Data key.

Returns:

The stored NamedValue; type is undefined if name was not set.

Support *get_mock_support_scope(StringView name)

Get (or create) a named child scope.

Equivalent to calling mock(“scope”) but from an existing Support reference. Useful when you already hold a reference to the global scope.

Parameters:

name – Scope name.

Returns:

Pointer to the child Support scope.

virtual void disable()
[source]

Disable this scope; all actual_call() invocations become no-ops.

virtual void enable()
[source]

Re-enable this scope after disable().

virtual void tracing(bool enabled)

Enable or disable call tracing to the test output.

Parameters:

enabled – true to print each actual call and its parameters.

virtual void ignore_other_calls()
[source]

Suppress unexpected-call failures.

All calls to actual_call() are silently accepted without matching an expectation. Useful when testing code that uses a collaborator you do not want to fully specify.

virtual void check_expectations()
[source]

Verify all expected calls were fulfilled; fail the test if not.

Typically called in mu::tiny::test::Test::teardown() after all assertions have run. Reports every unsatisfied expectation as a separate test failure.

virtual bool expected_calls_left()
[source]
Returns:

true if there are registered expectations that have not been called yet.

virtual void clear()
[source]

Remove all expectations, actual calls, data, and child scopes.

Call in mu::tiny::test::Test::teardown() after check_expectations() to leave the mock support in a clean state for the next test.

virtual void crash_on_failure(bool should_crash = true)

Choose whether unexpected call failures crash the process.

Parameters:

should_crash – true to crash on mock failure (useful with a debugger).

virtual void set_mock_failure_standard_reporter(FailureReporter *reporter)

Replace the standard failure reporter.

Parameters:

reporter – New standard reporter; pass nullptr to restore the default.

virtual void set_active_reporter(FailureReporter *active_reporter)

Set the active failure reporter for the current call chain.

Parameters:

active_reporter – Reporter to use; may differ from the standard one.

virtual void set_default_comparators_and_copiers_repository()
[source]

Restore the default comparators-and-copiers repository.

virtual void install_comparator(StringView type_name, NamedValueComparator &comparator)

Register a custom comparator for objects of type type_name.

Required when passing objects via mu::tiny::mock::ExpectedCall::with_parameter_of_type().

Parameters:
virtual void install_copier(StringView type_name, NamedValueCopier &copier)

Register a custom copier for objects of type type_name.

Required when using mu::tiny::mock::ExpectedCall::with_output_parameter_of_type_returning().

Parameters:
  • type_name – Type name string.

  • copier – Copier whose lifetime must exceed the test.

virtual void install_comparators_and_copiers(const NamedValueComparatorsAndCopiersRepository &repository)

Install all comparators and copiers from a repository at once.

Parameters:

repository – Repository containing the comparators and copiers to install.

virtual void remove_all_comparators_and_copiers()
[source]

Remove all installed comparators and copiers from this scope.

Public Static Functions

static const char *get_trace_output()
[source]
Returns:

A C string containing all traced call output, or empty string if tracing was not enabled.

Protected Functions

Support *clone(StringView mock_name)

Create a child Support scope with the given name.

virtual CheckedActualCall *create_actual_call()
[source]

Factory method for the actual-call object (override in tests).

virtual void fail_test(Failure &failure)

Record a mock failure via the active failure reporter.

Protected Static Functions

static void count_check()
[source]

Increment the assertion-check count in the active Result.