linux/lib/kunit/string-stream-test.c
Brendan Higgins e4aea8f853 kunit: test: add the concept of assertions
Add support for assertions which are like expectations except the test
terminates if the assertion is not satisfied.

The idea with assertions is that you use them to state all the
preconditions for your test. Logically speaking, these are the premises
of the test case, so if a premise isn't true, there is no point in
continuing the test case because there are no conclusions that can be
drawn without the premises. Whereas, the expectation is the thing you
are trying to prove. It is not used universally in x-unit style test
frameworks, but I really like it as a convention.  You could still
express the idea of a premise using the above idiom, but I think
KUNIT_ASSERT_* states the intended idea perfectly.

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>
2019-09-30 17:35:01 -06:00

53 lines
1.4 KiB
C

// SPDX-License-Identifier: GPL-2.0
/*
* KUnit test for struct string_stream.
*
* Copyright (C) 2019, Google LLC.
* Author: Brendan Higgins <brendanhiggins@google.com>
*/
#include <kunit/string-stream.h>
#include <kunit/test.h>
#include <linux/slab.h>
static void string_stream_test_empty_on_creation(struct kunit *test)
{
struct string_stream *stream = alloc_string_stream(test, GFP_KERNEL);
KUNIT_EXPECT_TRUE(test, string_stream_is_empty(stream));
}
static void string_stream_test_not_empty_after_add(struct kunit *test)
{
struct string_stream *stream = alloc_string_stream(test, GFP_KERNEL);
string_stream_add(stream, "Foo");
KUNIT_EXPECT_FALSE(test, string_stream_is_empty(stream));
}
static void string_stream_test_get_string(struct kunit *test)
{
struct string_stream *stream = alloc_string_stream(test, GFP_KERNEL);
char *output;
string_stream_add(stream, "Foo");
string_stream_add(stream, " %s", "bar");
output = string_stream_get_string(stream);
KUNIT_ASSERT_STREQ(test, output, "Foo bar");
}
static struct kunit_case string_stream_test_cases[] = {
KUNIT_CASE(string_stream_test_empty_on_creation),
KUNIT_CASE(string_stream_test_not_empty_after_add),
KUNIT_CASE(string_stream_test_get_string),
{}
};
static struct kunit_suite string_stream_test_suite = {
.name = "string-stream-test",
.test_cases = string_stream_test_cases
};
kunit_test_suite(string_stream_test_suite);