summaryrefslogtreecommitdiff
path: root/include/test_util.h
diff options
context:
space:
mode:
authorPaul Fagerburg <pfagerburg@google.com>2020-10-22 12:34:35 -0600
committerCommit Bot <commit-bot@chromium.org>2020-10-23 03:51:47 +0000
commitce87da13c5d147506f967d4d4dd72b19eeeb78ac (patch)
treef2fe9f7914c2fc7a9bba6d5cfe9318ae6668bc99 /include/test_util.h
parentb2cf27a0d1110454fbdaa5dbf1f00c1db4b08942 (diff)
downloadchrome-ec-ce87da13c5d147506f967d4d4dd72b19eeeb78ac.tar.gz
test: Allow EC unit test to use Ztest API
Provide a translation layer in test_utils.h for the Zephyr zassert macros to map onto EC's TEST_ASSERT macros. With a little bit of duplicated-but-not-quite-duplicated code (test_main vs. run_test) and some extra macros for the return type of the test cases, the tests can build for either the EC framework (by default) or the Zephyr Ztest framework (#ifdef CONFIG_ZEPHYR). Update the unit test documentation to explain why (and a brief "how") developers should use the Ztest API for new unit tests. BUG=b:168032590 BRANCH=none TEST=`TEST_LIST_HOST=base32 make runhosttests` Signed-off-by: Paul Fagerburg <pfagerburg@google.com> Change-Id: I26e60788c1468e44fed565dd31162759c7587ea6 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2492527 Tested-by: Paul Fagerburg <pfagerburg@chromium.org> Reviewed-by: Simon Glass <sjg@chromium.org> Reviewed-by: Jett Rink <jettrink@chromium.org> Commit-Queue: Paul Fagerburg <pfagerburg@chromium.org>
Diffstat (limited to 'include/test_util.h')
-rw-r--r--include/test_util.h54
1 files changed, 54 insertions, 0 deletions
diff --git a/include/test_util.h b/include/test_util.h
index 41299e09d3..d9195e4c97 100644
--- a/include/test_util.h
+++ b/include/test_util.h
@@ -8,6 +8,24 @@
#ifndef __CROS_EC_TEST_UTIL_H
#define __CROS_EC_TEST_UTIL_H
+#ifdef CONFIG_ZEPHYR
+
+#include <ztest_assert.h>
+
+/*
+ * We need these macros so that a test can be built for either Ztest or the
+ * EC test framework.
+ *
+ * Ztest unit tests are void and do not return a value. In the EC framework,
+ * if none of the assertions fail, the test is supposed to return EC_SUCCESS,
+ * so just define that as empty and `return EC_SUCCESS;` will get pre-processed
+ * into `return ;`
+ */
+#define EC_TEST_RETURN void
+#define EC_SUCCESS
+
+#else /* CONFIG_ZEPHYR */
+
#include "common.h"
#include "console.h"
#include "stack_trace.h"
@@ -302,4 +320,40 @@ int test_detach_i2c(const int port, const uint16_t slave_addr_flags);
*/
int test_attach_i2c(const int port, const uint16_t slave_addr_flags);
+/*
+ * We need these macros so that a test can be built for either Ztest or the
+ * EC test framework.
+ *
+ * EC unit tests return an EC_SUCCESS, or a failure code if one of the
+ * asserts in the test fails.
+ */
+#define EC_TEST_RETURN int
+
+/*
+ * Map the Ztest assertions onto EC assertions. There are two significant
+ * issues here.
+ * 1. zassert macros have extra printf-style arguments that the EC macros
+ * don't support, so we just have to drop that.
+ * 2. Some EC macros have an extra `fmt` parameter because they make their
+ * own printf-style string when the assertion fails. For some of them, we
+ * can add the correct format (the zassert_equal_ptr), but others we just
+ * don't know, so I'll just dump out the value in hex.
+ */
+#define zassert(cond, msg, ...) TEST_ASSERT(cond)
+#define zassert_unreachable(msg, ...) TEST_ASSERT(0)
+#define zassert_true(cond, msg, ...) TEST_ASSERT(cond)
+#define zassert_false(cond, msg, ...) TEST_ASSERT(!(cond))
+#define zassert_ok(cond, msg, ...) TEST_ASSERT(cond)
+#define zassert_is_null(ptr, msg, ...) TEST_ASSERT((ptr) == NULL)
+#define zassert_not_null(ptr, msg, ...) TEST_ASSERT((ptr) != NULL)
+#define zassert_equal(a, b, msg, ...) TEST_EQ((a), (b), "0x%x")
+#define zassert_not_equal(a, b, msg, ...) TEST_NE((a), (b), "0x%x")
+#define zassert_equal_ptr(a, b, msg, ...) \
+ TEST_EQ((void *)(a), (void *)(b), "0x%x")
+#define zassert_within(a, b, d, msg, ...) TEST_NEAR((a), (b), (d), "0x%x")
+#define zassert_mem_equal(buf, exp, size, msg, ...) \
+ TEST_ASSERT_ARRAY_EQ(buf, exp, size)
+
+#endif /* CONFIG_ZEPHYR */
+
#endif /* __CROS_EC_TEST_UTIL_H */