blob: 86013d4cf891c1dd3cdc132194ce8fc36a686bdf [file] [log] [blame]
Brendan Higgins6b229e52019-09-23 02:02:34 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Assertion and expectation serialization API.
4 *
5 * Copyright (C) 2019, Google LLC.
6 * Author: Brendan Higgins <brendanhiggins@google.com>
7 */
8#include <kunit/assert.h>
9
10void kunit_base_assert_format(const struct kunit_assert *assert,
11 struct string_stream *stream)
12{
13 const char *expect_or_assert = NULL;
14
15 switch (assert->type) {
16 case KUNIT_EXPECTATION:
17 expect_or_assert = "EXPECTATION";
18 break;
19 case KUNIT_ASSERTION:
20 expect_or_assert = "ASSERTION";
21 break;
22 }
23
24 string_stream_add(stream, "%s FAILED at %s:%d\n",
25 expect_or_assert, assert->file, assert->line);
26}
27
28void kunit_assert_print_msg(const struct kunit_assert *assert,
29 struct string_stream *stream)
30{
31 if (assert->message.fmt)
32 string_stream_add(stream, "\n%pV", &assert->message);
33}
34
35void kunit_fail_assert_format(const struct kunit_assert *assert,
36 struct string_stream *stream)
37{
38 kunit_base_assert_format(assert, stream);
39 string_stream_add(stream, "%pV", &assert->message);
40}
41
42void kunit_unary_assert_format(const struct kunit_assert *assert,
43 struct string_stream *stream)
44{
45 struct kunit_unary_assert *unary_assert = container_of(
46 assert, struct kunit_unary_assert, assert);
47
48 kunit_base_assert_format(assert, stream);
49 if (unary_assert->expected_true)
50 string_stream_add(stream,
51 "\tExpected %s to be true, but is false\n",
52 unary_assert->condition);
53 else
54 string_stream_add(stream,
55 "\tExpected %s to be false, but is true\n",
56 unary_assert->condition);
57 kunit_assert_print_msg(assert, stream);
58}
59
60void kunit_ptr_not_err_assert_format(const struct kunit_assert *assert,
61 struct string_stream *stream)
62{
63 struct kunit_ptr_not_err_assert *ptr_assert = container_of(
64 assert, struct kunit_ptr_not_err_assert, assert);
65
66 kunit_base_assert_format(assert, stream);
67 if (!ptr_assert->value) {
68 string_stream_add(stream,
69 "\tExpected %s is not null, but is\n",
70 ptr_assert->text);
71 } else if (IS_ERR(ptr_assert->value)) {
72 string_stream_add(stream,
73 "\tExpected %s is not error, but is: %ld\n",
74 ptr_assert->text,
75 PTR_ERR(ptr_assert->value));
76 }
77 kunit_assert_print_msg(assert, stream);
78}
79
80void kunit_binary_assert_format(const struct kunit_assert *assert,
81 struct string_stream *stream)
82{
83 struct kunit_binary_assert *binary_assert = container_of(
84 assert, struct kunit_binary_assert, assert);
85
86 kunit_base_assert_format(assert, stream);
87 string_stream_add(stream,
88 "\tExpected %s %s %s, but\n",
89 binary_assert->left_text,
90 binary_assert->operation,
91 binary_assert->right_text);
92 string_stream_add(stream, "\t\t%s == %lld\n",
93 binary_assert->left_text,
94 binary_assert->left_value);
95 string_stream_add(stream, "\t\t%s == %lld",
96 binary_assert->right_text,
97 binary_assert->right_value);
98 kunit_assert_print_msg(assert, stream);
99}
100
101void kunit_binary_ptr_assert_format(const struct kunit_assert *assert,
102 struct string_stream *stream)
103{
104 struct kunit_binary_ptr_assert *binary_assert = container_of(
105 assert, struct kunit_binary_ptr_assert, assert);
106
107 kunit_base_assert_format(assert, stream);
108 string_stream_add(stream,
109 "\tExpected %s %s %s, but\n",
110 binary_assert->left_text,
111 binary_assert->operation,
112 binary_assert->right_text);
113 string_stream_add(stream, "\t\t%s == %pK\n",
114 binary_assert->left_text,
115 binary_assert->left_value);
116 string_stream_add(stream, "\t\t%s == %pK",
117 binary_assert->right_text,
118 binary_assert->right_value);
119 kunit_assert_print_msg(assert, stream);
120}
121
122void kunit_binary_str_assert_format(const struct kunit_assert *assert,
123 struct string_stream *stream)
124{
125 struct kunit_binary_str_assert *binary_assert = container_of(
126 assert, struct kunit_binary_str_assert, assert);
127
128 kunit_base_assert_format(assert, stream);
129 string_stream_add(stream,
130 "\tExpected %s %s %s, but\n",
131 binary_assert->left_text,
132 binary_assert->operation,
133 binary_assert->right_text);
134 string_stream_add(stream, "\t\t%s == %s\n",
135 binary_assert->left_text,
136 binary_assert->left_value);
137 string_stream_add(stream, "\t\t%s == %s",
138 binary_assert->right_text,
139 binary_assert->right_value);
140 kunit_assert_print_msg(assert, stream);
141}