ztest: Fix incorrect use of this as fixture

Update `this` to `fixture` to avoid C++ keyword error.

Fixes #46459

Signed-off-by: Yuval Peress <peress@google.com>
This commit is contained in:
Yuval Peress 2022-06-16 11:37:22 -06:00 committed by Anas Nashif
commit d813e9b39f
6 changed files with 55 additions and 9 deletions

View file

@ -220,7 +220,7 @@ static inline void unit_test_noop(void)
#define Z_TEST(suite, fn, t_options, use_fixture) \
static void _##suite##_##fn##_wrapper(void *data); \
static void suite##_##fn( \
COND_CODE_1(use_fixture, (struct suite##_fixture *this), (void))); \
COND_CODE_1(use_fixture, (struct suite##_fixture *fixture), (void))); \
static STRUCT_SECTION_ITERABLE(ztest_unit_test, z_ztest_unit_test_##suite##_##fn) = { \
.test_suite_name = STRINGIFY(suite), \
.name = STRINGIFY(fn), \
@ -233,7 +233,7 @@ static inline void unit_test_noop(void)
(ARG_UNUSED(data); suite##_##fn();)) \
} \
static inline void suite##_##fn( \
COND_CODE_1(use_fixture, (struct suite##_fixture *this), (void)))
COND_CODE_1(use_fixture, (struct suite##_fixture *fixture), (void)))
#define Z_ZTEST(suite, fn, t_options) Z_TEST(suite, fn, t_options, 0)
#define Z_ZTEST_F(suite, fn, t_options) Z_TEST(suite, fn, t_options, 1)

View file

@ -10,7 +10,12 @@ else()
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(base)
if(CONFIG_CPLUSPLUS)
message(STATUS "adding main.cpp")
target_sources(app PRIVATE src/main.cpp)
else()
target_sources(app PRIVATE src/main.c)
target_sources_ifdef(CONFIG_USERSPACE app PRIVATE src/main_userspace.c)
endif()
endif()

View file

@ -0,0 +1,5 @@
CONFIG_ZTEST=y
CONFIG_ZTEST_NEW_API=y
CONFIG_CPLUSPLUS=y
CONFIG_LIB_CPLUSPLUS=y

View file

@ -59,8 +59,8 @@ ZTEST_SUITE(fixture_tests, NULL, fixture_tests_setup, NULL, NULL, NULL);
ZTEST_F(fixture_tests, test_fixture_pointer)
{
zassert_equal_ptr(&test_fixture, this, "Test fixture should be at 0x%x but was at 0x%x",
&test_fixture, this);
zassert_equal_ptr(&test_fixture, fixture, "Test fixture should be at 0x%x but was at 0x%x",
&test_fixture, fixture);
}
/***************************************************************************************************
@ -138,7 +138,8 @@ ZTEST_SUITE(rules_tests, NULL, rule_test_setup, NULL, NULL, rule_test_teardown);
ZTEST_F(rules_tests, test_rules_before_after)
{
zassert_equal(this->state, RULE_STATE_BEFORE_EACH, "Unexpected state");
this->state = RULE_STATE_TEST;
this->run_count++;
zassert_equal(fixture->state, RULE_STATE_BEFORE_EACH,
"Unexpected state");
fixture->state = RULE_STATE_TEST;
fixture->run_count++;
}

View file

@ -0,0 +1,31 @@
/*
* Copyright (c) 2022 Google Inc
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <ztest.h>
struct cpp_fixture {
int x;
};
void *cpp_setup(void)
{
auto fixture = new struct cpp_fixture;
fixture->x = 5;
return fixture;
}
void cpp_teardown(void *fixture)
{
delete static_cast<struct cpp_fixture *>(fixture);
}
ZTEST_SUITE(cpp, NULL, cpp_setup, NULL, NULL, cpp_teardown);
ZTEST_F(cpp, test_fixture_created_and_initialized)
{
zassert_equal(5, fixture->x, NULL);
}

View file

@ -2,6 +2,10 @@ tests:
testing.ztest.base:
tags: test_framework
type: unit
testing.ztest.base.cpp:
extra_args: CONF_FILE=prj_cpp.conf
tags: test_framework
platform_allow: native_posix
testing.ztest.base.verbose_0:
extra_args: CONF_FILE=prj_verbose_0.conf
tags: test_framework