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>
9 #include <kunit/test.h>
11 #include "string-stream.h"
13 void kunit_base_assert_format(const struct kunit_assert *assert,
14 struct string_stream *stream)
16 const char *expect_or_assert = NULL;
18 switch (assert->type) {
19 case KUNIT_EXPECTATION:
20 expect_or_assert = "EXPECTATION";
23 expect_or_assert = "ASSERTION";
27 string_stream_add(stream, "%s FAILED at %s:%d\n",
28 expect_or_assert, assert->file, assert->line);
30 EXPORT_SYMBOL_GPL(kunit_base_assert_format);
32 void kunit_assert_print_msg(const struct kunit_assert *assert,
33 struct string_stream *stream)
35 if (assert->message.fmt)
36 string_stream_add(stream, "\n%pV", &assert->message);
38 EXPORT_SYMBOL_GPL(kunit_assert_print_msg);
40 void kunit_fail_assert_format(const struct kunit_assert *assert,
41 struct string_stream *stream)
43 kunit_base_assert_format(assert, stream);
44 string_stream_add(stream, "%pV", &assert->message);
46 EXPORT_SYMBOL_GPL(kunit_fail_assert_format);
48 void kunit_unary_assert_format(const struct kunit_assert *assert,
49 struct string_stream *stream)
51 struct kunit_unary_assert *unary_assert = container_of(
52 assert, struct kunit_unary_assert, assert);
54 kunit_base_assert_format(assert, stream);
55 if (unary_assert->expected_true)
56 string_stream_add(stream,
57 KUNIT_SUBTEST_INDENT "Expected %s to be true, but is false\n",
58 unary_assert->condition);
60 string_stream_add(stream,
61 KUNIT_SUBTEST_INDENT "Expected %s to be false, but is true\n",
62 unary_assert->condition);
63 kunit_assert_print_msg(assert, stream);
65 EXPORT_SYMBOL_GPL(kunit_unary_assert_format);
67 void kunit_ptr_not_err_assert_format(const struct kunit_assert *assert,
68 struct string_stream *stream)
70 struct kunit_ptr_not_err_assert *ptr_assert = container_of(
71 assert, struct kunit_ptr_not_err_assert, assert);
73 kunit_base_assert_format(assert, stream);
74 if (!ptr_assert->value) {
75 string_stream_add(stream,
76 KUNIT_SUBTEST_INDENT "Expected %s is not null, but is\n",
78 } else if (IS_ERR(ptr_assert->value)) {
79 string_stream_add(stream,
80 KUNIT_SUBTEST_INDENT "Expected %s is not error, but is: %ld\n",
82 PTR_ERR(ptr_assert->value));
84 kunit_assert_print_msg(assert, stream);
86 EXPORT_SYMBOL_GPL(kunit_ptr_not_err_assert_format);
88 void kunit_binary_assert_format(const struct kunit_assert *assert,
89 struct string_stream *stream)
91 struct kunit_binary_assert *binary_assert = container_of(
92 assert, struct kunit_binary_assert, assert);
94 kunit_base_assert_format(assert, stream);
95 string_stream_add(stream,
96 KUNIT_SUBTEST_INDENT "Expected %s %s %s, but\n",
97 binary_assert->left_text,
98 binary_assert->operation,
99 binary_assert->right_text);
100 string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %lld\n",
101 binary_assert->left_text,
102 binary_assert->left_value);
103 string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %lld",
104 binary_assert->right_text,
105 binary_assert->right_value);
106 kunit_assert_print_msg(assert, stream);
108 EXPORT_SYMBOL_GPL(kunit_binary_assert_format);
110 void kunit_binary_ptr_assert_format(const struct kunit_assert *assert,
111 struct string_stream *stream)
113 struct kunit_binary_ptr_assert *binary_assert = container_of(
114 assert, struct kunit_binary_ptr_assert, assert);
116 kunit_base_assert_format(assert, stream);
117 string_stream_add(stream,
118 KUNIT_SUBTEST_INDENT "Expected %s %s %s, but\n",
119 binary_assert->left_text,
120 binary_assert->operation,
121 binary_assert->right_text);
122 string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %px\n",
123 binary_assert->left_text,
124 binary_assert->left_value);
125 string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %px",
126 binary_assert->right_text,
127 binary_assert->right_value);
128 kunit_assert_print_msg(assert, stream);
130 EXPORT_SYMBOL_GPL(kunit_binary_ptr_assert_format);
132 void kunit_binary_str_assert_format(const struct kunit_assert *assert,
133 struct string_stream *stream)
135 struct kunit_binary_str_assert *binary_assert = container_of(
136 assert, struct kunit_binary_str_assert, assert);
138 kunit_base_assert_format(assert, stream);
139 string_stream_add(stream,
140 KUNIT_SUBTEST_INDENT "Expected %s %s %s, but\n",
141 binary_assert->left_text,
142 binary_assert->operation,
143 binary_assert->right_text);
144 string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %s\n",
145 binary_assert->left_text,
146 binary_assert->left_value);
147 string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %s",
148 binary_assert->right_text,
149 binary_assert->right_value);
150 kunit_assert_print_msg(assert, stream);
152 EXPORT_SYMBOL_GPL(kunit_binary_str_assert_format);