summaryrefstree
path: root/kernel/test/example-test.c
blob: eb272c7f2c25babbefae1c8978b2dd49d76e7925 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// SPDX-License-Identifier: GPL-2.0
/*
 * Example KUnit test to show how to use KUnit.
 *
 * Copyright (C) 2018, Google LLC.
 * Author: Brendan Higgins <brendanhiggins@google.com>
 */

#include <kunit/test.h>
#include <kunit/mock.h>

struct example {
	int (*foo)(struct example *example, int num);
};

static int example_bar(struct example *example, int num)
{
	return example->foo(example, num);
}

DECLARE_STRUCT_CLASS_MOCK_PREREQS(example);

DEFINE_STRUCT_CLASS_MOCK(METHOD(foo), CLASS(example),
			 RETURNS(int),
			 PARAMS(struct example *, int));

static int example_init(struct MOCK(example) *mock_example)
{
	struct example *example = mock_get_trgt(mock_example);

	example->foo = foo;
	return 0;
}

DEFINE_STRUCT_CLASS_MOCK_INIT(example, example_init);

static void example_simple_test(struct test *test)
{
	EXPECT_EQ(test, 1, 1);
}

static void example_mock_test(struct test *test)
{
	struct MOCK(example) *mock_example = test->priv;
	struct example *example = mock_get_trgt(mock_example);
	struct mock_expectation *handle;

	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));
}

static int example_test_init(struct test *test)
{
	test_info(test, "initializing");

	test->priv = CONSTRUCT_MOCK(example, test);
	if (!test->priv)
		return -EINVAL;

	return 0;
}

static struct test_case example_test_cases[] = {
	TEST_CASE(example_simple_test),
	TEST_CASE(example_mock_test),
	{},
};

static struct test_module example_test_module = {
	.name = "example",
	.init = example_test_init,
	.test_cases = example_test_cases,
};
module_test(example_test_module);