2016-08-22 19:51:46 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2016 Intel Corporation
|
|
|
|
*
|
2017-01-19 09:01:01 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2016-08-22 19:51:46 +08:00
|
|
|
*/
|
|
|
|
|
2016-08-26 18:26:10 +08:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
*
|
|
|
|
* @brief Ztest mocking support
|
|
|
|
*/
|
|
|
|
|
2016-08-22 19:51:46 +08:00
|
|
|
#ifndef __ZTEST_MOCK_H__
|
|
|
|
#define __ZTEST_MOCK_H__
|
|
|
|
|
2016-08-26 18:26:10 +08:00
|
|
|
/**
|
|
|
|
* @defgroup ztest_mock Ztest mocking support
|
|
|
|
* @ingroup ztest
|
|
|
|
*
|
|
|
|
* This module provides simple mocking functions for unit testing. These
|
|
|
|
* need CONFIG_ZTEST_MOCKING=y.
|
|
|
|
*
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Tell function @a func to expect the value @a value for @a param
|
|
|
|
*
|
|
|
|
* When using ztest_check_expected_value(), tell that the value of @a param
|
|
|
|
* should be @a value. The value will internally be stored as an `uintptr_t`.
|
|
|
|
*
|
|
|
|
* @param func Function in question
|
|
|
|
* @param param Parameter for which the value should be set
|
|
|
|
* @param value Value for @a param
|
|
|
|
*/
|
2016-08-22 19:51:46 +08:00
|
|
|
#define ztest_expect_value(func, param, value) \
|
|
|
|
_ztest_expect_value(STRINGIFY(func), STRINGIFY(param), \
|
|
|
|
(uintptr_t)(value))
|
|
|
|
|
2016-08-26 18:26:10 +08:00
|
|
|
/**
|
|
|
|
* @brief If @a param doesn't match the value set by ztest_expect_value(),
|
|
|
|
* fail the test
|
|
|
|
*
|
|
|
|
* This will first check that does @a param have a value to be expected, and
|
|
|
|
* then checks whether the value of the parameter is equal to the expected
|
|
|
|
* value. If either of these checks fail, the current test will fail. This
|
|
|
|
* must be called from the called function.
|
|
|
|
*
|
|
|
|
* @param param Parameter to check
|
|
|
|
*/
|
2016-08-22 19:51:46 +08:00
|
|
|
#define ztest_check_expected_value(param) \
|
|
|
|
_ztest_check_expected_value(__func__, STRINGIFY(param), \
|
|
|
|
(uintptr_t)(param))
|
|
|
|
|
2016-08-26 18:26:10 +08:00
|
|
|
/**
|
|
|
|
* @brief Tell @a func that it should return @a value
|
|
|
|
*
|
|
|
|
* @param func Function that should return @a value
|
|
|
|
* @param value Value to return from @a func
|
|
|
|
*/
|
2016-08-22 19:51:46 +08:00
|
|
|
#define ztest_returns_value(func, value) \
|
|
|
|
_ztest_returns_value(STRINGIFY(func), (uintptr_t)(value))
|
|
|
|
|
2016-08-26 18:26:10 +08:00
|
|
|
/**
|
|
|
|
* @brief Get the return value for current function
|
|
|
|
*
|
|
|
|
* The return value must have been set previously with ztest_returns_value().
|
|
|
|
* If no return value exists, the current test will fail.
|
|
|
|
*
|
|
|
|
* @returns The value the current function should return
|
|
|
|
*/
|
2016-08-22 19:51:46 +08:00
|
|
|
#define ztest_get_return_value() \
|
|
|
|
_ztest_get_return_value(__func__)
|
|
|
|
|
2016-08-26 18:26:10 +08:00
|
|
|
/**
|
|
|
|
* @brief Get the return value as a pointer for current function
|
|
|
|
*
|
|
|
|
* The return value must have been set previously with ztest_returns_value().
|
|
|
|
* If no return value exists, the current test will fail.
|
|
|
|
*
|
|
|
|
* @returns The value the current function should return as a `void *`
|
|
|
|
*/
|
2016-08-22 19:51:46 +08:00
|
|
|
#define ztest_get_return_value_ptr() \
|
|
|
|
((void *)_ztest_get_return_value(__func__))
|
|
|
|
|
2016-08-26 18:26:10 +08:00
|
|
|
/**
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
2016-08-22 19:51:46 +08:00
|
|
|
#ifdef CONFIG_ZTEST_MOCKING
|
|
|
|
|
Introduce new sized integer typedefs
This is a start to move away from the C99 {u}int{8,16,32,64}_t types to
Zephyr defined u{8,16,32,64}_t and s{8,16,32,64}_t. This allows Zephyr
to define the sized types in a consistent manor across all the
architectures we support and not conflict with what various compilers
and libc might do with regards to the C99 types.
We introduce <zephyr/types.h> as part of this and have it include
<stdint.h> for now until we transition all the code away from the C99
types.
We go with u{8,16,32,64}_t and s{8,16,32,64}_t as there are some
existing variables defined u8 & u16 as well as to be consistent with
Zephyr naming conventions.
Jira: ZEP-2051
Change-Id: I451fed0623b029d65866622e478225dfab2c0ca8
Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2017-04-19 23:32:08 +08:00
|
|
|
#include <zephyr/types.h>
|
2016-08-22 19:51:46 +08:00
|
|
|
|
|
|
|
void _init_mock(void);
|
|
|
|
int _cleanup_mock(void);
|
|
|
|
|
|
|
|
void _ztest_expect_value(const char *fn, const char *name, uintptr_t value);
|
|
|
|
void _ztest_check_expected_value(const char *fn, const char *param,
|
|
|
|
uintptr_t value);
|
|
|
|
|
|
|
|
void _ztest_returns_value(const char *fn, uintptr_t value);
|
|
|
|
uintptr_t _ztest_get_return_value(const char *fn);
|
|
|
|
|
|
|
|
#else /* !CONFIG_ZTEST_MOCKING */
|
|
|
|
|
|
|
|
#define _init_mock()
|
|
|
|
#define _cleanup_mock() 0
|
|
|
|
|
|
|
|
#endif /* CONFIG_ZTEST_MOCKING */
|
|
|
|
|
|
|
|
#endif /* __ZTEST_H__ */
|