1 /* 2 * Copyright (c) 2016 Intel Corporation 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 7 /** 8 * @file 9 * 10 * @brief Ztest mocking support 11 */ 12 13 #ifndef ZEPHYR_TESTSUITE_ZTEST_MOCK_H_ 14 #define ZEPHYR_TESTSUITE_ZTEST_MOCK_H_ 15 16 /** 17 * @defgroup ztest_mock Ztest mocking support 18 * @ingroup ztest 19 * 20 * This module provides simple mocking functions for unit testing. These 21 * need CONFIG_ZTEST_MOCKING=y. 22 * 23 * @{ 24 */ 25 26 /** 27 * @brief Tell function @a func to expect the value @a value for @a param 28 * 29 * When using ztest_check_expected_value(), tell that the value of @a param 30 * should be @a value. The value will internally be stored as an `uintptr_t`. 31 * 32 * @param func Function in question 33 * @param param Parameter for which the value should be set 34 * @param value Value for @a param 35 */ 36 #define ztest_expect_value(func, param, value) \ 37 z_ztest_expect_value(STRINGIFY(func), STRINGIFY(param), \ 38 (uintptr_t)(value)) 39 40 /** 41 * @brief If @a param doesn't match the value set by ztest_expect_value(), 42 * fail the test 43 * 44 * This will first check that does @a param have a value to be expected, and 45 * then checks whether the value of the parameter is equal to the expected 46 * value. If either of these checks fail, the current test will fail. This 47 * must be called from the called function. 48 * 49 * @param param Parameter to check 50 */ 51 #define ztest_check_expected_value(param) \ 52 z_ztest_check_expected_value(__func__, STRINGIFY(param), \ 53 (uintptr_t)(param)) 54 55 /** 56 * @brief Tell function @a func to expect the data @a data for @a param 57 * 58 * When using ztest_check_expected_data(), the data pointed to by 59 * @a param should be same @a data in this function. Only data pointer is stored 60 * by this function, so it must still be valid when ztest_check_expected_data is 61 * called. 62 * 63 * @param func Function in question 64 * @param param Parameter for which the data should be set 65 * @param data pointer for the data for parameter @a param 66 */ 67 #define ztest_expect_data(func, param, data) \ 68 z_ztest_expect_data(STRINGIFY(func), STRINGIFY(param), (void *)(data)) 69 70 /** 71 * @brief If data pointed by @a param don't match the data set by 72 * ztest_expect_data(), fail the test 73 * 74 * This will first check that @a param is expected to be null or non-null and 75 * then check whether the data pointed by parameter is equal to expected data. 76 * If either of these checks fail, the current test will fail. This 77 * must be called from the called function. 78 * 79 * @param param Parameter to check 80 * @param length Length of the data to compare 81 */ 82 #define ztest_check_expected_data(param, length) \ 83 z_ztest_check_expected_data(__func__, STRINGIFY(param), \ 84 (void *)(param), (length)) 85 86 /** 87 * @brief Tell function @a func to return the data @a data for @a param 88 * 89 * When using ztest_return_data(), the data pointed to by @a param should be 90 * same @a data in this function. Only data pointer is stored by this function, 91 * so it must still be valid when ztest_copy_return_data is called. 92 * 93 * @param func Function in question 94 * @param param Parameter for which the data should be set 95 * @param data pointer for the data for parameter @a param 96 */ 97 #define ztest_return_data(func, param, data) \ 98 z_ztest_return_data(STRINGIFY(func), STRINGIFY(param), (void *)(data)) 99 100 /** 101 * @brief Copy the data set by ztest_return_data to the memory pointed by 102 * @a param 103 * 104 * This will first check that @a param is not null and then copy the data. 105 * This must be called from the called function. 106 * 107 * @param param Parameter to return data for 108 * @param length Length of the data to return 109 */ 110 #define ztest_copy_return_data(param, length) \ 111 z_ztest_copy_return_data(__func__, STRINGIFY(param), \ 112 (void *)(param), (length)) 113 /** 114 * @brief Tell @a func that it should return @a value 115 * 116 * @param func Function that should return @a value 117 * @param value Value to return from @a func 118 */ 119 #define ztest_returns_value(func, value) \ 120 z_ztest_returns_value(STRINGIFY(func), (uintptr_t)(value)) 121 122 /** 123 * @brief Get the return value for current function 124 * 125 * The return value must have been set previously with ztest_returns_value(). 126 * If no return value exists, the current test will fail. 127 * 128 * @returns The value the current function should return 129 */ 130 #define ztest_get_return_value() z_ztest_get_return_value(__func__) 131 132 /** 133 * @brief Get the return value as a pointer for current function 134 * 135 * The return value must have been set previously with ztest_returns_value(). 136 * If no return value exists, the current test will fail. 137 * 138 * @returns The value the current function should return as a `void *` 139 */ 140 #define ztest_get_return_value_ptr() \ 141 ((void *)z_ztest_get_return_value(__func__)) 142 143 /** 144 * @} 145 */ 146 147 #ifdef CONFIG_ZTEST_MOCKING 148 149 #include <zephyr/types.h> 150 151 #ifdef __cplusplus 152 extern "C" { 153 #endif 154 155 void z_init_mock(void); 156 int z_cleanup_mock(void); 157 158 void z_ztest_expect_value(const char *fn, const char *name, uintptr_t value); 159 void z_ztest_check_expected_value(const char *fn, const char *param, 160 uintptr_t value); 161 162 void z_ztest_expect_data(const char *fn, const char *name, void *val); 163 void z_ztest_check_expected_data(const char *fn, const char *name, void *data, 164 uint32_t length); 165 166 void z_ztest_return_data(const char *fn, const char *name, void *val); 167 void z_ztest_copy_return_data(const char *fn, const char *name, void *data, 168 uint32_t length); 169 170 void z_ztest_returns_value(const char *fn, uintptr_t value); 171 uintptr_t z_ztest_get_return_value(const char *fn); 172 173 #ifdef __cplusplus 174 } 175 #endif 176 177 #else /* !CONFIG_ZTEST_MOCKING */ 178 179 #define z_init_mock() 180 #define z_cleanup_mock() 0 181 182 #endif /* CONFIG_ZTEST_MOCKING */ 183 184 #endif /* ZEPHYR_TESTSUITE_ZTEST_MOCK_H_ */ 185