pw_unit_test: Add FAIL/SUCCEED macros
- Implement standard ADD_FAILURE, FAIL, and SUCCEED macros.
- Wrap _PW_TEST_ASSERT in a do-while to require a terminating semicolon.
- Declare pw_unit_test's test as a pw_test.
Change-Id: I22f5243dfb691de1597547ad9aaf703c3cc3498e
diff --git a/BUILD.gn b/BUILD.gn
index 8b691d9..1b37dd6 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -67,6 +67,7 @@
"$dir_pw_span:tests",
"$dir_pw_status:tests",
"$dir_pw_string:tests",
+ "$dir_pw_unit_test:tests",
"$dir_pw_varint:tests",
]
diff --git a/pw_unit_test/BUILD.gn b/pw_unit_test/BUILD.gn
index c9c44ca..47f2ed3 100644
--- a/pw_unit_test/BUILD.gn
+++ b/pw_unit_test/BUILD.gn
@@ -13,6 +13,7 @@
# the License.
import("$dir_pw_build/pw_executable.gni")
+import("$dir_pw_unit_test/test.gni")
config("default_config") {
include_dirs = [
@@ -67,14 +68,12 @@
]
}
-# TODO(frolv): These targets are temporarily here until unit test support is
-# integrated into the build system.
-pw_executable("framework_test") {
- deps = [
- ":main",
- ":pw_unit_test",
- ]
+pw_test("framework_test") {
sources = [
"framework_test.cc",
]
}
+
+pw_test_group("tests") {
+ tests = [ ":framework_test" ]
+}
diff --git a/pw_unit_test/framework_test.cc b/pw_unit_test/framework_test.cc
index 98dde79..2b16b37 100644
--- a/pw_unit_test/framework_test.cc
+++ b/pw_unit_test/framework_test.cc
@@ -67,6 +67,15 @@
ASSERT_STRNE("yes", no);
}
+TEST(PigweedTest, ExpectSucceedFail) {
+ SUCCEED();
+
+ if (false) {
+ ADD_FAILURE();
+ FAIL();
+ }
+}
+
class NonCopyable {
public:
NonCopyable(int value) : value_(value) {}
diff --git a/pw_unit_test/public/pw_unit_test/framework.h b/pw_unit_test/public/pw_unit_test/framework.h
index d09dee9..6111dba 100644
--- a/pw_unit_test/public/pw_unit_test/framework.h
+++ b/pw_unit_test/public/pw_unit_test/framework.h
@@ -33,7 +33,7 @@
// GTEST_DONT_DEFINE_TEST is also accepted for compatibility.
#if !PW_TEST_DONT_DEFINE_TEST && !GTEST_DONT_DEFINE_TEST
#define TEST PW_TEST
-#endif
+#endif // !PW_TEST_DONT_DEFINE_TEST && !GTEST_DONT_DEFINE_TEST
#define TEST_F(test_fixture, test_name) \
_PW_TEST(test_fixture, test_name, test_fixture)
@@ -60,6 +60,28 @@
#define ASSERT_STREQ(lhs, rhs) _PW_TEST_STREQ(_PW_TEST_ASSERT, lhs, rhs)
#define ASSERT_STRNE(lhs, rhs) _PW_TEST_STRNE(_PW_TEST_ASSERT, lhs, rhs)
+// Generates a non-fatal failure with a generic message.
+#define ADD_FAILURE() \
+ _PW_TEST_MESSAGE("(line is not executed)", "(line was executed)", false)
+
+// Generates a fatal failure with a generic message.
+#define GTEST_FAIL() return ADD_FAILURE()
+
+// Define either macro to 1 to omit the definition of FAIL(), which is a
+// generic name and clashes with some other libraries.
+#if !PW_TEST_DONT_DEFINE_FAIL && !GTEST_DONT_DEFINE_FAIL
+#define FAIL() GTEST_FAIL()
+#endif // !PW_TEST_DONT_DEFINE_FAIL && !GTEST_DONT_DEFINE_FAIL
+
+// Generates a success with a generic message.
+#define GTEST_SUCCEED() _PW_TEST_MESSAGE("(success)", "(success)", true)
+
+// Define either macro to 1 to omit the definition of SUCCEED(), which
+// is a generic name and clashes with some other libraries.
+#if !PW_TEST_DONT_DEFINE_SUCCEED && !GTEST_DONT_DEFINE_SUCCEED
+#define SUCCEED() GTEST_SUCCEED()
+#endif // !PW_TEST_DONT_DEFINE_SUCCEED && !GTEST_DONT_DEFINE_SUCCEED
+
// pw_unit_test framework entry point. Runs every registered test case and
// dispatches the results through the event handler. Returns a status of zero
// if all tests passed, or nonzero if there were any failures.
@@ -195,6 +217,12 @@
return result;
}
+ // Dispatches an event indicating the result of an expectation.
+ void ExpectationResult(const char* expression,
+ const std::string_view& evaluated_expression,
+ int line,
+ bool success);
+
private:
// Dispatches an event indicating that a test started running.
void StartTest(Test* test);
@@ -202,12 +230,6 @@
// Dispatches an event indicating that a test finished running.
void EndTest(Test* test);
- // Dispatches an event indicating the result of an expectation.
- void ExpectationResult(const char* expression,
- const std::string_view& evaluated_expression,
- int line,
- bool success);
-
// Singleton instance of the framework class.
static Framework framework_;
@@ -351,10 +373,16 @@
#lhs " " expectation_string " " #rhs, \
__LINE__)
-#define _PW_TEST_ASSERT(lhs, rhs, expectation, expectation_string) \
- if (!_PW_TEST_EXPECT(lhs, rhs, expectation, expectation_string)) { \
- return; \
- }
+#define _PW_TEST_ASSERT(lhs, rhs, expectation, expectation_string) \
+ do { \
+ if (!_PW_TEST_EXPECT(lhs, rhs, expectation, expectation_string)) { \
+ return; \
+ } \
+ } while (0)
+
+#define _PW_TEST_MESSAGE(expected, actual, success) \
+ ::pw::unit_test::internal::Framework::Get().ExpectationResult( \
+ expected, actual, __LINE__, success)
#define _PW_TEST_OP(expect_or_assert, lhs, rhs, op) \
expect_or_assert( \