diff options
author | Joseph Ates <joseph.ates@msasafety.com> | 2016-02-10 12:16:13 +0100 |
---|---|---|
committer | Andreas Schneider <asn@cryptomilk.org> | 2016-02-10 13:18:11 +0100 |
commit | f8bba4b3a519147de59a2a7c3dc6262549e7e2bb (patch) | |
tree | 646f92fcc8b778916a6b380e06ab7bef2cb857da /tests/test_returns.c | |
parent | c971d6e5c26549c421d7fc4cc2965cea4c628866 (diff) | |
download | cmocka-f8bba4b3a519147de59a2a7c3dc6262549e7e2bb.tar.gz cmocka-f8bba4b3a519147de59a2a7c3dc6262549e7e2bb.tar.xz cmocka-f8bba4b3a519147de59a2a7c3dc6262549e7e2bb.zip |
cmocka: Add will_return_maybe() for ignoring mock returns
As both parameter and function call order checking allow for ignoring
cases where they are never invoked, the mock return values are at
somewhat of a mismatch in that they must always be returned at least
once (even in the case of will_return_always()). Therefore, the ability
to set the count to -2 on will_return_count was added with a new macro
(will_return_maybe) that indicates that that the value field may never
be returned and still allow a successful test.
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
Diffstat (limited to 'tests/test_returns.c')
-rw-r--r-- | tests/test_returns.c | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/tests/test_returns.c b/tests/test_returns.c new file mode 100644 index 0000000..b9370c9 --- /dev/null +++ b/tests/test_returns.c @@ -0,0 +1,69 @@ +#include "config.h" + +#include <stdarg.h> +#include <stddef.h> +#include <setjmp.h> +#include <cmocka.h> +#include <cmocka_private.h> + +#include <stdlib.h> + +int mock_function(void); +void mock_function_call_times(size_t times, int expectedValue); + +int mock_function(void) +{ + return (int) mock(); +} + +void mock_function_call_times(size_t times, int expectedValue) +{ + size_t i; + for (i = 0u; i < times; ++i) + { + assert_int_equal(expectedValue, mock_function()); + } +} + +static void test_will_return_maybe_for_no_calls(void **state) +{ + (void) state; + + will_return_maybe(mock_function, 32); +} + +static void test_will_return_maybe_for_one_mock_call(void **state) +{ + int value; + + (void) state; + + value = rand(); + will_return_maybe(mock_function, value); + mock_function_call_times(1u, value); +} + +static void test_will_return_maybe_for_more_than_one_call(void **state) +{ + int value; + size_t numberOfCalls; + (void)state; + + value = rand(); + numberOfCalls = (size_t) ((rand()) % 20 + 2); + will_return_maybe(mock_function, value); + mock_function_call_times(numberOfCalls, value); +} + +int main(int argc, char **argv) { + const struct CMUnitTest alloc_tests[] = { + cmocka_unit_test(test_will_return_maybe_for_no_calls) + ,cmocka_unit_test(test_will_return_maybe_for_one_mock_call) + ,cmocka_unit_test(test_will_return_maybe_for_more_than_one_call) + }; + + (void)argc; + (void)argv; + + return cmocka_run_group_tests(alloc_tests, NULL, NULL); +} |