Call Ordering

It is often beneficial to make sure that functions are called in an order. More...

Collaboration diagram for Call Ordering:


#define function_called()   _function_called(__func__, __FILE__, __LINE__)
#define expect_function_calls(function, times)   _expect_function_call(#function, __FILE__, __LINE__, times)
#define expect_function_call(function)   _expect_function_call(#function, __FILE__, __LINE__, 1)
#define expect_function_call_any(function)   _expect_function_call(#function, __FILE__, __LINE__, -1)
#define ignore_function_calls(function)   _expect_function_call(#function, __FILE__, __LINE__, -2)

Detailed Description

It is often beneficial to make sure that functions are called in an order.

This is independent of mock returns and parameter checking as both of the aforementioned do not check the order in which they are called from different functions.

expect_function_call() and function_called() are intended to be used in pairs. Cmocka will fail a test if there are more or less expected calls created (e.g. expect_function_call()) than consumed with function_called(). There are provisions such as ignore_function_calls() which allow this restriction to be circumvented in tests where mock calls for the code under test are not the focus of the test.

The following example illustrates how a unit test instructs cmocka to expect a function_called() from a particular mock, chef_sing():

void chef_sing(void);
void code_under_test()
void some_test(void **state)

The implementation of the mock then must check whether it was meant to be called by invoking function_called():

void chef_sing()

Macro Definition Documentation

◆ expect_function_call

#define expect_function_call (   function)    _expect_function_call(#function, __FILE__, __LINE__, 1)

◆ expect_function_call_any

#define expect_function_call_any (   function)    _expect_function_call(#function, __FILE__, __LINE__, -1)

◆ expect_function_calls

#define expect_function_calls (   function,
)    _expect_function_call(#function, __FILE__, __LINE__, times)

◆ function_called

#define function_called ( )    _function_called(__func__, __FILE__, __LINE__)

Definition at line 1434 of file cmocka.h.

Referenced by mock_test_a_called(), mock_test_b_called(), and mock_test_c_called().

◆ ignore_function_calls

#define ignore_function_calls (   function)    _expect_function_call(#function, __FILE__, __LINE__, -2)