diff options
author | Rae Moar <rmoar@google.com> | 2023-04-03 20:19:30 +0000 |
---|---|---|
committer | Shuah Khan <skhan@linuxfoundation.org> | 2023-04-05 12:51:30 -0600 |
commit | a42077b787680cbc365a96446b30f32399fa3f6f (patch) | |
tree | e54b89d16d310ab3914cea2b96cb32daf2f308b5 /lib | |
parent | 8110a3cab05ee4a26f36015f0423fb2b5cea1392 (diff) | |
download | linux-rpi-a42077b787680cbc365a96446b30f32399fa3f6f.tar.gz linux-rpi-a42077b787680cbc365a96446b30f32399fa3f6f.tar.bz2 linux-rpi-a42077b787680cbc365a96446b30f32399fa3f6f.zip |
kunit: add tests for using current KUnit test field
Create test suite called "kunit_current" to add test coverage for the use
of current->kunit_test, which returns the current KUnit test.
Add two test cases:
- kunit_current_test to test current->kunit_test and the method
kunit_get_current_test(), which utilizes current->kunit_test.
- kunit_current_fail_test to test the method
kunit_fail_current_test(), which utilizes current->kunit_test.
Signed-off-by: Rae Moar <rmoar@google.com>
Reviewed-by: Daniel Latypov <dlatypov@google.com>
Reviewed-by: David Gow <davidgow@google.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/kunit/kunit-test.c | 42 |
1 files changed, 41 insertions, 1 deletions
diff --git a/lib/kunit/kunit-test.c b/lib/kunit/kunit-test.c index b63595d3e241..42e44caa1bdd 100644 --- a/lib/kunit/kunit-test.c +++ b/lib/kunit/kunit-test.c @@ -6,6 +6,7 @@ * Author: Brendan Higgins <brendanhiggins@google.com> */ #include <kunit/test.h> +#include <kunit/test-bug.h> #include "try-catch-impl.h" @@ -532,7 +533,46 @@ static struct kunit_suite kunit_status_test_suite = { .test_cases = kunit_status_test_cases, }; +static void kunit_current_test(struct kunit *test) +{ + /* Check results of both current->kunit_test and + * kunit_get_current_test() are equivalent to current test. + */ + KUNIT_EXPECT_PTR_EQ(test, test, current->kunit_test); + KUNIT_EXPECT_PTR_EQ(test, test, kunit_get_current_test()); +} + +static void kunit_current_fail_test(struct kunit *test) +{ + struct kunit fake; + + kunit_init_test(&fake, "fake test", NULL); + KUNIT_EXPECT_EQ(test, fake.status, KUNIT_SUCCESS); + + /* Set current->kunit_test to fake test. */ + current->kunit_test = &fake; + + kunit_fail_current_test("This should make `fake` test fail."); + KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_FAILURE); + kunit_cleanup(&fake); + + /* Reset current->kunit_test to current test. */ + current->kunit_test = test; +} + +static struct kunit_case kunit_current_test_cases[] = { + KUNIT_CASE(kunit_current_test), + KUNIT_CASE(kunit_current_fail_test), + {} +}; + +static struct kunit_suite kunit_current_test_suite = { + .name = "kunit_current", + .test_cases = kunit_current_test_cases, +}; + kunit_test_suites(&kunit_try_catch_test_suite, &kunit_resource_test_suite, - &kunit_log_test_suite, &kunit_status_test_suite); + &kunit_log_test_suite, &kunit_status_test_suite, + &kunit_current_test_suite); MODULE_LICENSE("GPL v2"); |