1 // SPDX-License-Identifier: GPL-2.0
3 * Assertion and expectation serialization API.
5 * Copyright (C) 2019, Google LLC.
6 * Author: Brendan Higgins <brendanhiggins@google.com>
8 #include <kunit/assert.h>
10 #include "string-stream.h"
12 void kunit_base_assert_format(const struct kunit_assert
*assert,
13 struct string_stream
*stream
)
15 const char *expect_or_assert
= NULL
;
17 switch (assert->type
) {
18 case KUNIT_EXPECTATION
:
19 expect_or_assert
= "EXPECTATION";
22 expect_or_assert
= "ASSERTION";
26 string_stream_add(stream
, "%s FAILED at %s:%d\n",
27 expect_or_assert
, assert->file
, assert->line
);
29 EXPORT_SYMBOL_GPL(kunit_base_assert_format
);
31 void kunit_assert_print_msg(const struct kunit_assert
*assert,
32 struct string_stream
*stream
)
34 if (assert->message
.fmt
)
35 string_stream_add(stream
, "\n%pV", &assert->message
);
37 EXPORT_SYMBOL_GPL(kunit_assert_print_msg
);
39 void kunit_fail_assert_format(const struct kunit_assert
*assert,
40 struct string_stream
*stream
)
42 kunit_base_assert_format(assert, stream
);
43 string_stream_add(stream
, "%pV", &assert->message
);
45 EXPORT_SYMBOL_GPL(kunit_fail_assert_format
);
47 void kunit_unary_assert_format(const struct kunit_assert
*assert,
48 struct string_stream
*stream
)
50 struct kunit_unary_assert
*unary_assert
= container_of(
51 assert, struct kunit_unary_assert
, assert);
53 kunit_base_assert_format(assert, stream
);
54 if (unary_assert
->expected_true
)
55 string_stream_add(stream
,
56 "\tExpected %s to be true, but is false\n",
57 unary_assert
->condition
);
59 string_stream_add(stream
,
60 "\tExpected %s to be false, but is true\n",
61 unary_assert
->condition
);
62 kunit_assert_print_msg(assert, stream
);
64 EXPORT_SYMBOL_GPL(kunit_unary_assert_format
);
66 void kunit_ptr_not_err_assert_format(const struct kunit_assert
*assert,
67 struct string_stream
*stream
)
69 struct kunit_ptr_not_err_assert
*ptr_assert
= container_of(
70 assert, struct kunit_ptr_not_err_assert
, assert);
72 kunit_base_assert_format(assert, stream
);
73 if (!ptr_assert
->value
) {
74 string_stream_add(stream
,
75 "\tExpected %s is not null, but is\n",
77 } else if (IS_ERR(ptr_assert
->value
)) {
78 string_stream_add(stream
,
79 "\tExpected %s is not error, but is: %ld\n",
81 PTR_ERR(ptr_assert
->value
));
83 kunit_assert_print_msg(assert, stream
);
85 EXPORT_SYMBOL_GPL(kunit_ptr_not_err_assert_format
);
87 void kunit_binary_assert_format(const struct kunit_assert
*assert,
88 struct string_stream
*stream
)
90 struct kunit_binary_assert
*binary_assert
= container_of(
91 assert, struct kunit_binary_assert
, assert);
93 kunit_base_assert_format(assert, stream
);
94 string_stream_add(stream
,
95 "\tExpected %s %s %s, but\n",
96 binary_assert
->left_text
,
97 binary_assert
->operation
,
98 binary_assert
->right_text
);
99 string_stream_add(stream
, "\t\t%s == %lld\n",
100 binary_assert
->left_text
,
101 binary_assert
->left_value
);
102 string_stream_add(stream
, "\t\t%s == %lld",
103 binary_assert
->right_text
,
104 binary_assert
->right_value
);
105 kunit_assert_print_msg(assert, stream
);
107 EXPORT_SYMBOL_GPL(kunit_binary_assert_format
);
109 void kunit_binary_ptr_assert_format(const struct kunit_assert
*assert,
110 struct string_stream
*stream
)
112 struct kunit_binary_ptr_assert
*binary_assert
= container_of(
113 assert, struct kunit_binary_ptr_assert
, assert);
115 kunit_base_assert_format(assert, stream
);
116 string_stream_add(stream
,
117 "\tExpected %s %s %s, but\n",
118 binary_assert
->left_text
,
119 binary_assert
->operation
,
120 binary_assert
->right_text
);
121 string_stream_add(stream
, "\t\t%s == %pK\n",
122 binary_assert
->left_text
,
123 binary_assert
->left_value
);
124 string_stream_add(stream
, "\t\t%s == %pK",
125 binary_assert
->right_text
,
126 binary_assert
->right_value
);
127 kunit_assert_print_msg(assert, stream
);
129 EXPORT_SYMBOL_GPL(kunit_binary_ptr_assert_format
);
131 void kunit_binary_str_assert_format(const struct kunit_assert
*assert,
132 struct string_stream
*stream
)
134 struct kunit_binary_str_assert
*binary_assert
= container_of(
135 assert, struct kunit_binary_str_assert
, assert);
137 kunit_base_assert_format(assert, stream
);
138 string_stream_add(stream
,
139 "\tExpected %s %s %s, but\n",
140 binary_assert
->left_text
,
141 binary_assert
->operation
,
142 binary_assert
->right_text
);
143 string_stream_add(stream
, "\t\t%s == %s\n",
144 binary_assert
->left_text
,
145 binary_assert
->left_value
);
146 string_stream_add(stream
, "\t\t%s == %s",
147 binary_assert
->right_text
,
148 binary_assert
->right_value
);
149 kunit_assert_print_msg(assert, stream
);
151 EXPORT_SYMBOL_GPL(kunit_binary_str_assert_format
);