ztest: Add comfort functions for non-zero return codes

Many functions return non-zero return codes on errors. I added an assert
for the case, when a function is expected to fail. It is just a
shorthand for `zassert_not_equal(0, ret);`, analogous to
`zassert_ok`, introduced in c5d85e175f.

I also added the corresponding `zassume_nok` and `zexpect_nok`.

Signed-off-by: Greter Raffael <rgreter@baumer.com>
This commit is contained in:
Greter Raffael 2023-10-31 11:30:29 +01:00 committed by Anas Nashif
commit 774858c6dc
3 changed files with 38 additions and 0 deletions

View file

@ -282,6 +282,13 @@ static inline bool z_zexpect(bool cond, const char *default_msg, const char *fil
*/ */
#define zassert_ok(cond, ...) zassert(!(cond), #cond " is non-zero", ##__VA_ARGS__) #define zassert_ok(cond, ...) zassert(!(cond), #cond " is non-zero", ##__VA_ARGS__)
/**
* @brief Assert that @a cond is not 0 (failure)
* @param cond Condition to check
* @param ... Optional message and variables to print if the assertion fails
*/
#define zassert_not_ok(cond, ...) zassert(!!(cond), #cond " is zero", ##__VA_ARGS__)
/** /**
* @brief Assert that @a ptr is NULL * @brief Assert that @a ptr is NULL
* @param ptr Pointer to compare * @param ptr Pointer to compare
@ -425,6 +432,16 @@ static inline bool z_zexpect(bool cond, const char *default_msg, const char *fil
*/ */
#define zassume_ok(cond, ...) zassume(!(cond), #cond " is non-zero", ##__VA_ARGS__) #define zassume_ok(cond, ...) zassume(!(cond), #cond " is non-zero", ##__VA_ARGS__)
/**
* @brief Assume that @a cond is not 0 (failure)
*
* If the assumption fails, the test will be marked as "skipped".
*
* @param cond Condition to check
* @param ... Optional message and variables to print if the assumption fails
*/
#define zassume_not_ok(cond, ...) zassume(!!(cond), #cond " is zero", ##__VA_ARGS__)
/** /**
* @brief Assume that @a ptr is NULL * @brief Assume that @a ptr is NULL
* *
@ -578,6 +595,15 @@ static inline bool z_zexpect(bool cond, const char *default_msg, const char *fil
*/ */
#define zexpect_ok(cond, ...) zexpect(!(cond), #cond " is non-zero", ##__VA_ARGS__) #define zexpect_ok(cond, ...) zexpect(!(cond), #cond " is non-zero", ##__VA_ARGS__)
/**
* @brief Expect that @a cond is not 0 (failure), otherwise mark test as failed but continue its
* execution.
*
* @param cond Condition to check
* @param ... Optional message and variables to print if the expectation fails
*/
#define zexpect_not_ok(cond, ...) zexpect(!!(cond), #cond " is zero", ##__VA_ARGS__)
/** /**
* @brief Expect that @a ptr is NULL, otherwise mark test as failed but continue its execution. * @brief Expect that @a ptr is NULL, otherwise mark test as failed but continue its execution.
* *

View file

@ -20,6 +20,7 @@ ZTEST(framework_tests, test_assert_tests)
zassert_not_null("foo", NULL); zassert_not_null("foo", NULL);
zassert_equal(1, 1); zassert_equal(1, 1);
zassert_equal_ptr(NULL, NULL, NULL); zassert_equal_ptr(NULL, NULL, NULL);
zassert_not_ok(-EIO);
} }
ZTEST(framework_tests, test_assert_mem_equal) ZTEST(framework_tests, test_assert_mem_equal)

View file

@ -59,6 +59,17 @@ ZTEST(expect, test_fail_expect_ok)
zexpect_ok(5); zexpect_ok(5);
} }
ZTEST(expect, test_expect_not_ok)
{
zexpect_not_ok(-EIO);
}
ZTEST_EXPECT_FAIL(expect, test_fail_expect_not_ok);
ZTEST(expect, test_fail_expect_not_ok)
{
zexpect_not_ok(0);
}
ZTEST(expect, test_expect_is_null) ZTEST(expect, test_expect_is_null)
{ {
void *ptr = NULL; void *ptr = NULL;