|
| 1 | +#include <stdarg.h> |
| 2 | +#include <stddef.h> |
| 3 | +#include <setjmp.h> |
| 4 | +#include <cmocka.h> |
| 5 | +#include <stdint.h> |
| 6 | + |
| 7 | +#include "led.h" |
| 8 | + |
| 9 | +char *mock_gpio_config_name_to_return; |
| 10 | + |
| 11 | +static void test_when_led_init_is_called_it_calls_gpio_init_and_returns_success(void **state) |
| 12 | +{ |
| 13 | + (void)state; /* unused */ |
| 14 | + will_return(gpio_init, 0); |
| 15 | + |
| 16 | + assert_int_equal(0, led_init()); |
| 17 | +} |
| 18 | + |
| 19 | +static void test_when_gpio_init_returns_failure_led_init_returns_failure(void **state) |
| 20 | +{ |
| 21 | + (void)state; /* unused */ |
| 22 | + will_return(gpio_init, -1); |
| 23 | + |
| 24 | + assert_int_equal(-1, led_init()); |
| 25 | +} |
| 26 | + |
| 27 | +static void test_when_led_fancy_blink_is_called_variadic_module_get_is_called(void **state) |
| 28 | +{ |
| 29 | + (void)state; /* unused */ |
| 30 | + |
| 31 | + expect_function_call(variadic_module_init); |
| 32 | + |
| 33 | + will_return(variadic_module_get, 0); |
| 34 | + expect_value(variadic_module_get, count, 2); |
| 35 | + expect_string(variadic_module_get, fmt, "%d %s"); |
| 36 | + |
| 37 | + assert_int_equal(0, led_fancy_blink()); |
| 38 | +} |
| 39 | + |
| 40 | +/* Test to demonstrate the workaround to mock inline function.*/ |
| 41 | +static void test_when_led_light_up_is_called_the_get_inline_is_called(void **state) |
| 42 | +{ |
| 43 | + (void)state; /* unused */ |
| 44 | + |
| 45 | + will_return(get_inline, 0); |
| 46 | + expect_value(get_inline, val, 1); |
| 47 | + |
| 48 | + led_light_up(); |
| 49 | +} |
| 50 | + |
| 51 | +/* Tests that demonstrate how to validate multiple parms and return array by pointer. */ |
| 52 | +static void test_when_gpio_config_returns_MagicName_led_config_returns_0(void **state) |
| 53 | +{ |
| 54 | + (void)state; /* unused */ |
| 55 | + |
| 56 | + const uint8_t exp_name_len = 10; |
| 57 | + const uint8_t exp_level = 1; |
| 58 | + const uint8_t exp_drive = 2; |
| 59 | + char name_to_return[] = "MagicName"; |
| 60 | + |
| 61 | + expect_value(gpio_config, name_len, exp_name_len); |
| 62 | + expect_value(gpio_config, level, exp_level); |
| 63 | + expect_value(gpio_config, drive, exp_drive); |
| 64 | + |
| 65 | + will_return(gpio_config, NULL); |
| 66 | + mock_gpio_config_name_to_return = name_to_return; |
| 67 | + |
| 68 | + assert_int_equal(0, led_config()); |
| 69 | +} |
| 70 | + |
| 71 | +static void |
| 72 | +test_when_gpio_config_does_not_return_MagicName_led_config_returns_minus_one(void **state) |
| 73 | +{ |
| 74 | + (void)state; /* unused */ |
| 75 | + |
| 76 | + const uint8_t exp_name_len = 10; |
| 77 | + const uint8_t exp_level = 1; |
| 78 | + const uint8_t exp_drive = 2; |
| 79 | + char name_to_return[] = "NotMagic"; |
| 80 | + |
| 81 | + expect_value(gpio_config, name_len, exp_name_len); |
| 82 | + expect_value(gpio_config, level, exp_level); |
| 83 | + expect_value(gpio_config, drive, exp_drive); |
| 84 | + |
| 85 | + will_return(gpio_config, NULL); |
| 86 | + mock_gpio_config_name_to_return = name_to_return; |
| 87 | + |
| 88 | + assert_int_equal(-1, led_config()); |
| 89 | +} |
| 90 | + |
| 91 | +const struct CMUnitTest test_suite[] = { |
| 92 | + cmocka_unit_test(test_when_led_init_is_called_it_calls_gpio_init_and_returns_success), |
| 93 | + cmocka_unit_test(test_when_gpio_init_returns_failure_led_init_returns_failure), |
| 94 | + cmocka_unit_test(test_when_led_fancy_blink_is_called_variadic_module_get_is_called), |
| 95 | + cmocka_unit_test(test_when_led_light_up_is_called_the_get_inline_is_called), |
| 96 | + cmocka_unit_test(test_when_gpio_config_returns_MagicName_led_config_returns_0), |
| 97 | + cmocka_unit_test( |
| 98 | + test_when_gpio_config_does_not_return_MagicName_led_config_returns_minus_one), |
| 99 | +}; |
| 100 | + |
| 101 | +int main(void) |
| 102 | +{ |
| 103 | + return cmocka_run_group_tests(test_suite, NULL, NULL); |
| 104 | +} |
0 commit comments