kunit: add kunit/Kconfig and <kunit/test.h> to ease upgrade from alpha
Provide macros to allow using KUNIT_EXPECT_EQ instead of EXPECT_EQ, etc.
And add a Kconfig to allow users to modernize their KConfig.
Update the example and template tests.
With this, the code can compile with a kunitconfig of
CONFIG_KUNIT=y
CONFIG_KUNIT_TEST=y
CONFIG_KUNIT_EXAMPLE_TEST=y
which is exactly what the upstream version would use.
Unfortunately we can't have "struct kunit" map back to "struct test"
Stuff that isn't upstream like <test/mock.h> wasn't touched.
This should hopefully make it easier for alpha users to do partial,
simple migrations now.
And it should make it a bit clearer what's not upstream.
Change-Id: I9b13a871e527125e40969beefbc939f4c9e0801d
diff --git a/include/kunit/test.h b/include/kunit/test.h
new file mode 100644
index 0000000..58217d4
--- /dev/null
+++ b/include/kunit/test.h
@@ -0,0 +1,60 @@
+// TRANSITIONAL ONLY.
+// Provide some macros to make it easier to transition some code relying on the
+// older non-upstream kunit version.
+
+#include <test/test.h>
+
+#define KUNIT_SUCCEED SUCCEED
+#define KUNIT_FAIL FAIL
+
+#define KUNIT_EXPECT EXPECT
+#define KUNIT_EXPECT_TRUE EXPECT_TRUE
+#define KUNIT_EXPECT_FALSE EXPECT_FALSE
+#define KUNIT_EXPECT_NOT_NULL EXPECT_NOT_NULL
+#define KUNIT_EXPECT_NULL EXPECT_NULL
+#define KUNIT_EXPECT_SUCCESS EXPECT_SUCCESS
+#define KUNIT_EXPECT_ERROR EXPECT_ERROR
+#define KUNIT_EXPECT_BINARY EXPECT_BINARY
+#define KUNIT_EXPECT_EQ EXPECT_EQ
+#define KUNIT_EXPECT_NE EXPECT_NE
+#define KUNIT_EXPECT_LT EXPECT_LT
+#define KUNIT_EXPECT_LE EXPECT_LE
+#define KUNIT_EXPECT_GT EXPECT_GT
+#define KUNIT_EXPECT_GE EXPECT_GE
+#define KUNIT_EXPECT_STREQ EXPECT_STREQ
+#define KUNIT_EXPECT_NOT_ERR_OR_NULL EXPECT_NOT_ERR_OR_NULL
+
+#define KUNIT_ASSERT ASSERT
+#define KUNIT_ASSERT_TRUE ASSERT_TRUE
+#define KUNIT_ASSERT_FALSE ASSERT_FALSE
+#define KUNIT_ASSERT_NOT_NULL ASSERT_NOT_NULL
+#define KUNIT_ASSERT_NULL ASSERT_NULL
+#define KUNIT_ASSERT_SUCCESS ASSERT_SUCCESS
+#define KUNIT_ASSERT_ERROR ASSERT_ERROR
+#define KUNIT_ASSERT_BINARY ASSERT_BINARY
+#define KUNIT_ASSERT_EQ ASSERT_EQ
+#define KUNIT_ASSERT_NE ASSERT_NE
+#define KUNIT_ASSERT_LT ASSERT_LT
+#define KUNIT_ASSERT_LE ASSERT_LE
+#define KUNIT_ASSERT_GT ASSERT_GT
+#define KUNIT_ASSERT_GE ASSERT_GE
+#define KUNIT_ASSERT_STREQ ASSERT_STREQ
+#define KUNIT_ASSERT_NOT_ERR_OR_NULL ASSERT_NOT_ERR_OR_NULL
+#define KUNIT_ASSERT_SIGSEGV ASSERT_SIGSEGV
+
+#define KUNIT_CASE TEST_CASE
+#define kunit_test_suites module_test
+
+#define kunit_info test_info
+#define kunit_warn test_warn
+#define kunit_err test_err
+#define kunit_printk test_printk
+
+// Note: the following functions don't quite have a 1:1 equivalent.
+// * test_alloc_resource
+// * test_free_resource
+#define kunit_kmalloc test_kmalloc
+#define kunit_kzalloc test_kzalloc
+#define kunit_cleanup test_cleanup
+
+#define kunit_kfree(test, ptr) kfree(ptr)
diff --git a/lib/Kconfig b/lib/Kconfig
index a3928d4..8fb84d0 100644
--- a/lib/Kconfig
+++ b/lib/Kconfig
@@ -621,3 +621,5 @@
config GENERIC_LIB_UCMPDI2
bool
+
+source "lib/kunit/Kconfig"
diff --git a/lib/kunit/Kconfig b/lib/kunit/Kconfig
new file mode 100644
index 0000000..b333f65
--- /dev/null
+++ b/lib/kunit/Kconfig
@@ -0,0 +1,31 @@
+# TRANSITIONAL ONLY.
+# Provide newer names for old config options to make it easier to transition
+# code relying on the older non-upstream kunit version.
+
+menu "(transitional) KUnit support"
+
+config KUNIT
+ bool "(transitional) Enable support for unit tests (KUnit)"
+ select TEST
+ help
+ Enables support for KUnit via the new name.
+
+if KUNIT
+
+config KUNIT_TEST
+ bool "(transitional) KUnit test for KUnit"
+ select TEST_TEST
+ depends on KUNIT
+ help
+ Enables KUnit test to test KUnit.
+
+config KUNIT_EXAMPLE_TEST
+ bool "(transitional) Example test for KUnit"
+ select EXAMPLE_TEST
+ depends on KUNIT
+ help
+ Enables example KUnit test to demo features of KUnit.
+
+endif # KUNIT
+
+endmenu
diff --git a/test/example-test.c b/test/example-test.c
index 6f15cbb..8d743b3 100644
--- a/test/example-test.c
+++ b/test/example-test.c
@@ -6,7 +6,7 @@
* Author: Brendan Higgins <brendanhiggins@google.com>
*/
-#include <test/test.h>
+#include <kunit/test.h>
#include <test/mock.h>
struct example {
@@ -36,7 +36,7 @@
static void example_simple_test(struct test *test)
{
- EXPECT_EQ(test, 1, 1);
+ KUNIT_EXPECT_EQ(test, 1, 1);
}
static void example_mock_test(struct test *test)
@@ -48,12 +48,12 @@
handle = EXPECT_CALL(foo(mock_get_ctrl(mock_example), int_eq(test, 5)));
handle->action = int_return(test, 2);
- EXPECT_EQ(test, 2, example_bar(example, 5));
+ KUNIT_EXPECT_EQ(test, 2, example_bar(example, 5));
}
static int example_test_init(struct test *test)
{
- test_info(test, "initializing");
+ kunit_info(test, "initializing");
test->priv = CONSTRUCT_MOCK(example, test);
if (!test->priv)
@@ -63,8 +63,8 @@
}
static struct test_case example_test_cases[] = {
- TEST_CASE(example_simple_test),
- TEST_CASE(example_mock_test),
+ KUNIT_CASE(example_simple_test),
+ KUNIT_CASE(example_mock_test),
{},
};
@@ -73,4 +73,4 @@
.init = example_test_init,
.test_cases = example_test_cases,
};
-module_test(example_test_module);
+kunit_test_suites(example_test_module);
diff --git a/tools/testing/kunit/test_template.c b/tools/testing/kunit/test_template.c
index b2169cc..6530334 100644
--- a/tools/testing/kunit/test_template.c
+++ b/tools/testing/kunit/test_template.c
@@ -3,7 +3,7 @@
* TODO: Add test description.
*/
-#include <test/test.h>
+#include <kunit/test.h>
#include <test/mock.h>
/*
@@ -24,7 +24,7 @@
* code should do. KUnit then runs the test and verifies that the code's
* behavior matched what was expected.
*/
- EXPECT_EQ(test, 1, 2); // Obvious failure.
+ KUNIT_EXPECT_EQ(test, 1, 2); // Obvious failure.
}
/*
@@ -55,7 +55,7 @@
* use KUnit, just know that this is how you associate test cases with a
* test module.
*/
- TEST_CASE(${test_prefix}_foo),
+ KUNIT_CASE(${test_prefix}_foo),
{},
};
@@ -90,4 +90,4 @@
* This registers the above test module telling KUnit that this is a suite of
* tests that need to be run.
*/
-module_test(${test_prefix}_module);
+kunit_test_suites(${test_prefix}_module);