diff options
author | Joseph Ates <joseph.ates@msasafety.com> | 2015-11-12 13:09:36 -0500 |
---|---|---|
committer | Andreas Schneider <asn@cryptomilk.org> | 2015-12-18 12:21:35 +0100 |
commit | 9a120731ec3831c68bfa88b719395f3ea8a24f54 (patch) | |
tree | b64059a79b5d4fff8fa582d0d1753bbd742812f7 | |
parent | 443b02803dd35f34f6823e31791bc85fc07b9629 (diff) | |
download | cmocka-9a120731ec3831c68bfa88b719395f3ea8a24f54.tar.gz cmocka-9a120731ec3831c68bfa88b719395f3ea8a24f54.tar.bz2 cmocka-9a120731ec3831c68bfa88b719395f3ea8a24f54.zip |
tests: Add test for call ordering functionality
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
-rw-r--r-- | tests/CMakeLists.txt | 12 | ||||
-rw-r--r-- | tests/test_ordering.c | 112 | ||||
-rw-r--r-- | tests/test_ordering_fail.c | 95 |
3 files changed, 218 insertions, 1 deletions
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 2f985a5..faaa604 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -16,7 +16,9 @@ set(CMOCKA_TESTS test_exception_handler test_basics test_skip - test_setup_fail) + test_setup_fail + test_ordering + test_ordering_fail) foreach(_CMOCKA_TEST ${CMOCKA_TESTS}) add_cmocka_test(${_CMOCKA_TEST} ${_CMOCKA_TEST}.c ${CMOCKA_STATIC_LIBRARY}) @@ -46,6 +48,14 @@ set_tests_properties( "\\[ FAILED \\] 1 test" ) +# test_ordering ensure proper failures +set_tests_properties( + test_ordering_fail + PROPERTIES + PASS_REGULAR_EXPRESSION + "\\[ FAILED \\] 7 test" +) + # test_exception_handler if (WIN32) set_tests_properties( diff --git a/tests/test_ordering.c b/tests/test_ordering.c new file mode 100644 index 0000000..817c0ba --- /dev/null +++ b/tests/test_ordering.c @@ -0,0 +1,112 @@ +#include "config.h" + +#include <stdarg.h> +#include <stddef.h> +#include <setjmp.h> +#include <cmocka.h> +#include <cmocka_private.h> + +static void mock_test_a_called(void) +{ + function_called(); +} + +static void mock_test_b_called(void) +{ + function_called(); +} + +static void mock_test_c_called(void) +{ + function_called(); +} + + +static void test_does_succeed_for_expected(void **state) +{ + (void)state; + expect_function_call(mock_test_a_called); + expect_function_call(mock_test_a_called); + + mock_test_a_called(); + mock_test_a_called(); +} + +static void test_does_succeed_for_multiple_calls(void **state) +{ + (void)state; + expect_function_call(mock_test_a_called); + expect_function_calls(mock_test_a_called, 2); + expect_function_call(mock_test_a_called); + + mock_test_a_called(); + mock_test_a_called(); + mock_test_a_called(); + mock_test_a_called(); +} + +static void test_ordering_does_ignore_calls(void **state) +{ + (void)state; + + ignore_function_calls(mock_test_a_called); + + mock_test_a_called(); + mock_test_a_called(); + mock_test_a_called(); +} + +static void test_ordering_does_ignore_no_calls(void **state) +{ + (void)state; + ignore_function_calls(mock_test_a_called); +} + +static void test_ordering_does_expect_at_least_one_call(void **state) +{ + (void)state; + expect_function_call_any(mock_test_a_called); + + mock_test_a_called(); + mock_test_a_called(); + mock_test_a_called(); +} + +static void test_ordering_does_work_across_different_functions(void **state) +{ + (void)state; + expect_function_call(mock_test_a_called); + expect_function_call(mock_test_b_called); + expect_function_call(mock_test_a_called); + + mock_test_a_called(); + mock_test_b_called(); + mock_test_a_called(); +} + +static void test_ordering_ignores_out_of_order_properly(void **state) +{ + (void)state; + ignore_function_calls(mock_test_a_called); + ignore_function_calls(mock_test_b_called); + expect_function_calls(mock_test_c_called, 2); + + + mock_test_c_called(); + mock_test_b_called(); + mock_test_c_called(); +} + +int main(void) { + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_does_succeed_for_expected) + ,cmocka_unit_test(test_does_succeed_for_multiple_calls) + ,cmocka_unit_test(test_ordering_does_ignore_no_calls) + ,cmocka_unit_test(test_ordering_does_ignore_calls) + ,cmocka_unit_test(test_ordering_does_expect_at_least_one_call) + ,cmocka_unit_test(test_ordering_does_work_across_different_functions) + ,cmocka_unit_test(test_ordering_ignores_out_of_order_properly) + }; + + return cmocka_run_group_tests(tests, NULL, NULL); +} diff --git a/tests/test_ordering_fail.c b/tests/test_ordering_fail.c new file mode 100644 index 0000000..652f5ad --- /dev/null +++ b/tests/test_ordering_fail.c @@ -0,0 +1,95 @@ +#include "config.h" + +#include <stdarg.h> +#include <stddef.h> +#include <setjmp.h> +#include <cmocka.h> +#include <cmocka_private.h> + +static void mock_test_a_called(void) +{ + function_called(); +} + +static void mock_test_b_called(void) +{ + function_called(); +} + +static void mock_test_c_called(void) +{ + function_called(); +} + +static void test_does_fail_for_unexpected_call(void **state) +{ + (void)state; + expect_function_call(mock_test_a_called); + expect_function_call(mock_test_a_called); + + mock_test_a_called(); + mock_test_a_called(); + mock_test_a_called(); +} + +static void test_does_fail_for_unmade_expected_call(void **state) +{ + (void)state; + expect_function_call(mock_test_a_called); + expect_function_call(mock_test_a_called); + + mock_test_a_called(); +} + +static void test_ordering_fails_out_of_order(void **state) +{ + (void)state; + expect_function_call(mock_test_a_called); + expect_function_call(mock_test_b_called); + expect_function_call(mock_test_a_called); + + mock_test_b_called(); +} + +static void test_ordering_fails_out_of_order_for_at_least_once_calls(void **state) +{ + (void)state; + expect_function_call_any(mock_test_a_called); + ignore_function_calls(mock_test_b_called); + + mock_test_b_called(); + mock_test_c_called(); +} + +/* Primarily used to test error message */ +static void test_fails_out_of_order_if_no_calls_found_on_any(void **state) +{ + (void)state; + expect_function_call_any(mock_test_a_called); + ignore_function_calls(mock_test_b_called); + + mock_test_a_called(); + mock_test_c_called(); +} + +static void test_fails_if_zero_count_used(void **state) +{ + (void)state; + expect_function_calls(mock_test_a_called, 0); + + mock_test_a_called(); +} + +int main(void) { + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_does_fail_for_unexpected_call) + ,cmocka_unit_test(test_does_fail_for_unmade_expected_call) + ,cmocka_unit_test(test_does_fail_for_unmade_expected_call) + ,cmocka_unit_test(test_ordering_fails_out_of_order) + ,cmocka_unit_test(test_ordering_fails_out_of_order_for_at_least_once_calls) + ,cmocka_unit_test(test_fails_out_of_order_if_no_calls_found_on_any) + ,cmocka_unit_test(test_fails_if_zero_count_used) + }; + + return cmocka_run_group_tests(tests, NULL, NULL); +} |