summaryrefslogtreecommitdiff
path: root/lib/kunit/assert.c
blob: 02ecd0d7d80ab3582a7fea833e55aca591ccb0a0 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// SPDX-License-Identifier: GPL-2.0
/*
 * Assertion and expectation serialization API.
 *
 * Copyright (C) 2019, Google LLC.
 * Author: Brendan Higgins <brendanhiggins@google.com>
 */
#include <kunit/assert.h>

#include "string-stream.h"

void kunit_base_assert_format(const struct kunit_assert *assert,
			      struct string_stream *stream)
{
	const char *expect_or_assert = NULL;

	switch (assert->type) {
	case KUNIT_EXPECTATION:
		expect_or_assert = "EXPECTATION";
		break;
	case KUNIT_ASSERTION:
		expect_or_assert = "ASSERTION";
		break;
	}

	string_stream_add(stream, "%s FAILED at %s:%d\n",
			 expect_or_assert, assert->file, assert->line);
}
EXPORT_SYMBOL_GPL(kunit_base_assert_format);

void kunit_assert_print_msg(const struct kunit_assert *assert,
			    struct string_stream *stream)
{
	if (assert->message.fmt)
		string_stream_add(stream, "\n%pV", &assert->message);
}
EXPORT_SYMBOL_GPL(kunit_assert_print_msg);

void kunit_fail_assert_format(const struct kunit_assert *assert,
			      struct string_stream *stream)
{
	kunit_base_assert_format(assert, stream);
	string_stream_add(stream, "%pV", &assert->message);
}
EXPORT_SYMBOL_GPL(kunit_fail_assert_format);

void kunit_unary_assert_format(const struct kunit_assert *assert,
			       struct string_stream *stream)
{
	struct kunit_unary_assert *unary_assert = container_of(
			assert, struct kunit_unary_assert, assert);

	kunit_base_assert_format(assert, stream);
	if (unary_assert->expected_true)
		string_stream_add(stream,
				 "\tExpected %s to be true, but is false\n",
				 unary_assert->condition);
	else
		string_stream_add(stream,
				 "\tExpected %s to be false, but is true\n",
				 unary_assert->condition);
	kunit_assert_print_msg(assert, stream);
}
EXPORT_SYMBOL_GPL(kunit_unary_assert_format);

void kunit_ptr_not_err_assert_format(const struct kunit_assert *assert,
				     struct string_stream *stream)
{
	struct kunit_ptr_not_err_assert *ptr_assert = container_of(
			assert, struct kunit_ptr_not_err_assert, assert);

	kunit_base_assert_format(assert, stream);
	if (!ptr_assert->value) {
		string_stream_add(stream,
				 "\tExpected %s is not null, but is\n",
				 ptr_assert->text);
	} else if (IS_ERR(ptr_assert->value)) {
		string_stream_add(stream,
				 "\tExpected %s is not error, but is: %ld\n",
				 ptr_assert->text,
				 PTR_ERR(ptr_assert->value));
	}
	kunit_assert_print_msg(assert, stream);
}
EXPORT_SYMBOL_GPL(kunit_ptr_not_err_assert_format);

void kunit_binary_assert_format(const struct kunit_assert *assert,
				struct string_stream *stream)
{
	struct kunit_binary_assert *binary_assert = container_of(
			assert, struct kunit_binary_assert, assert);

	kunit_base_assert_format(assert, stream);
	string_stream_add(stream,
			 "\tExpected %s %s %s, but\n",
			 binary_assert->left_text,
			 binary_assert->operation,
			 binary_assert->right_text);
	string_stream_add(stream, "\t\t%s == %lld\n",
			 binary_assert->left_text,
			 binary_assert->left_value);
	string_stream_add(stream, "\t\t%s == %lld",
			 binary_assert->right_text,
			 binary_assert->right_value);
	kunit_assert_print_msg(assert, stream);
}
EXPORT_SYMBOL_GPL(kunit_binary_assert_format);

void kunit_binary_ptr_assert_format(const struct kunit_assert *assert,
				    struct string_stream *stream)
{
	struct kunit_binary_ptr_assert *binary_assert = container_of(
			assert, struct kunit_binary_ptr_assert, assert);

	kunit_base_assert_format(assert, stream);
	string_stream_add(stream,
			 "\tExpected %s %s %s, but\n",
			 binary_assert->left_text,
			 binary_assert->operation,
			 binary_assert->right_text);
	string_stream_add(stream, "\t\t%s == %px\n",
			 binary_assert->left_text,
			 binary_assert->left_value);
	string_stream_add(stream, "\t\t%s == %px",
			 binary_assert->right_text,
			 binary_assert->right_value);
	kunit_assert_print_msg(assert, stream);
}
EXPORT_SYMBOL_GPL(kunit_binary_ptr_assert_format);

void kunit_binary_str_assert_format(const struct kunit_assert *assert,
				    struct string_stream *stream)
{
	struct kunit_binary_str_assert *binary_assert = container_of(
			assert, struct kunit_binary_str_assert, assert);

	kunit_base_assert_format(assert, stream);
	string_stream_add(stream,
			 "\tExpected %s %s %s, but\n",
			 binary_assert->left_text,
			 binary_assert->operation,
			 binary_assert->right_text);
	string_stream_add(stream, "\t\t%s == %s\n",
			 binary_assert->left_text,
			 binary_assert->left_value);
	string_stream_add(stream, "\t\t%s == %s",
			 binary_assert->right_text,
			 binary_assert->right_value);
	kunit_assert_print_msg(assert, stream);
}
EXPORT_SYMBOL_GPL(kunit_binary_str_assert_format);