xref: /openbmc/linux/lib/kunit/assert.c (revision 55fd7e02)
1 // 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 #include <kunit/test.h>
10 
11 #include "string-stream.h"
12 
13 void kunit_base_assert_format(const struct kunit_assert *assert,
14 			      struct string_stream *stream)
15 {
16 	const char *expect_or_assert = NULL;
17 
18 	switch (assert->type) {
19 	case KUNIT_EXPECTATION:
20 		expect_or_assert = "EXPECTATION";
21 		break;
22 	case KUNIT_ASSERTION:
23 		expect_or_assert = "ASSERTION";
24 		break;
25 	}
26 
27 	string_stream_add(stream, "%s FAILED at %s:%d\n",
28 			 expect_or_assert, assert->file, assert->line);
29 }
30 EXPORT_SYMBOL_GPL(kunit_base_assert_format);
31 
32 void kunit_assert_print_msg(const struct kunit_assert *assert,
33 			    struct string_stream *stream)
34 {
35 	if (assert->message.fmt)
36 		string_stream_add(stream, "\n%pV", &assert->message);
37 }
38 EXPORT_SYMBOL_GPL(kunit_assert_print_msg);
39 
40 void kunit_fail_assert_format(const struct kunit_assert *assert,
41 			      struct string_stream *stream)
42 {
43 	kunit_base_assert_format(assert, stream);
44 	string_stream_add(stream, "%pV", &assert->message);
45 }
46 EXPORT_SYMBOL_GPL(kunit_fail_assert_format);
47 
48 void kunit_unary_assert_format(const struct kunit_assert *assert,
49 			       struct string_stream *stream)
50 {
51 	struct kunit_unary_assert *unary_assert = container_of(
52 			assert, struct kunit_unary_assert, assert);
53 
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);
59 	else
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);
64 }
65 EXPORT_SYMBOL_GPL(kunit_unary_assert_format);
66 
67 void kunit_ptr_not_err_assert_format(const struct kunit_assert *assert,
68 				     struct string_stream *stream)
69 {
70 	struct kunit_ptr_not_err_assert *ptr_assert = container_of(
71 			assert, struct kunit_ptr_not_err_assert, assert);
72 
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",
77 				  ptr_assert->text);
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",
81 				  ptr_assert->text,
82 				  PTR_ERR(ptr_assert->value));
83 	}
84 	kunit_assert_print_msg(assert, stream);
85 }
86 EXPORT_SYMBOL_GPL(kunit_ptr_not_err_assert_format);
87 
88 void kunit_binary_assert_format(const struct kunit_assert *assert,
89 				struct string_stream *stream)
90 {
91 	struct kunit_binary_assert *binary_assert = container_of(
92 			assert, struct kunit_binary_assert, assert);
93 
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);
107 }
108 EXPORT_SYMBOL_GPL(kunit_binary_assert_format);
109 
110 void kunit_binary_ptr_assert_format(const struct kunit_assert *assert,
111 				    struct string_stream *stream)
112 {
113 	struct kunit_binary_ptr_assert *binary_assert = container_of(
114 			assert, struct kunit_binary_ptr_assert, assert);
115 
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);
129 }
130 EXPORT_SYMBOL_GPL(kunit_binary_ptr_assert_format);
131 
132 void kunit_binary_str_assert_format(const struct kunit_assert *assert,
133 				    struct string_stream *stream)
134 {
135 	struct kunit_binary_str_assert *binary_assert = container_of(
136 			assert, struct kunit_binary_str_assert, assert);
137 
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);
151 }
152 EXPORT_SYMBOL_GPL(kunit_binary_str_assert_format);
153