kunit: test: add the concept of expectations
Add support for expectations, which allow properties to be specified and then verified in tests. Signed-off-by: Brendan Higgins <brendanhiggins@google.com> Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Reviewed-by: Logan Gunthorpe <logang@deltatee.com> Reviewed-by: Stephen Boyd <sboyd@kernel.org> Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
This commit is contained in:

committed by
Shuah Khan

parent
6b229e593f
commit
73cda7bb8b
@@ -120,6 +120,68 @@ static void kunit_print_test_case_ok_not_ok(struct kunit_case *test_case,
|
||||
test_case->name);
|
||||
}
|
||||
|
||||
static void kunit_print_string_stream(struct kunit *test,
|
||||
struct string_stream *stream)
|
||||
{
|
||||
struct string_stream_fragment *fragment;
|
||||
char *buf;
|
||||
|
||||
buf = string_stream_get_string(stream);
|
||||
if (!buf) {
|
||||
kunit_err(test,
|
||||
"Could not allocate buffer, dumping stream:\n");
|
||||
list_for_each_entry(fragment, &stream->fragments, node) {
|
||||
kunit_err(test, fragment->fragment);
|
||||
}
|
||||
kunit_err(test, "\n");
|
||||
} else {
|
||||
kunit_err(test, buf);
|
||||
kunit_kfree(test, buf);
|
||||
}
|
||||
}
|
||||
|
||||
static void kunit_fail(struct kunit *test, struct kunit_assert *assert)
|
||||
{
|
||||
struct string_stream *stream;
|
||||
|
||||
kunit_set_failure(test);
|
||||
|
||||
stream = alloc_string_stream(test, GFP_KERNEL);
|
||||
if (!stream) {
|
||||
WARN(true,
|
||||
"Could not allocate stream to print failed assertion in %s:%d\n",
|
||||
assert->file,
|
||||
assert->line);
|
||||
return;
|
||||
}
|
||||
|
||||
assert->format(assert, stream);
|
||||
|
||||
kunit_print_string_stream(test, stream);
|
||||
|
||||
WARN_ON(string_stream_destroy(stream));
|
||||
}
|
||||
|
||||
void kunit_do_assertion(struct kunit *test,
|
||||
struct kunit_assert *assert,
|
||||
bool pass,
|
||||
const char *fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
|
||||
if (pass)
|
||||
return;
|
||||
|
||||
va_start(args, fmt);
|
||||
|
||||
assert->message.fmt = fmt;
|
||||
assert->message.va = &args;
|
||||
|
||||
kunit_fail(test, assert);
|
||||
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
void kunit_init_test(struct kunit *test, const char *name)
|
||||
{
|
||||
spin_lock_init(&test->lock);
|
||||
|
Reference in New Issue
Block a user