blob: 33acdaa28a7d9c6412ff4ec6a470615217da6dd2 [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>
Alan Maguirec3bba692020-03-26 14:25:09 +00009#include <kunit/test.h>
Brendan Higgins6b229e52019-09-23 02:02:34 -070010
Alan Maguire109fb062020-01-06 22:28:18 +000011#include "string-stream.h"
12
Brendan Higgins6b229e52019-09-23 02:02:34 -070013void 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}
Alan Maguirec475c772020-01-06 22:28:20 +000030EXPORT_SYMBOL_GPL(kunit_base_assert_format);
Brendan Higgins6b229e52019-09-23 02:02:34 -070031
32void 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}
Alan Maguirec475c772020-01-06 22:28:20 +000038EXPORT_SYMBOL_GPL(kunit_assert_print_msg);
Brendan Higgins6b229e52019-09-23 02:02:34 -070039
40void 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}
Alan Maguirec475c772020-01-06 22:28:20 +000046EXPORT_SYMBOL_GPL(kunit_fail_assert_format);
Brendan Higgins6b229e52019-09-23 02:02:34 -070047
48void 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,
Alan Maguirec3bba692020-03-26 14:25:09 +000057 KUNIT_SUBTEST_INDENT "Expected %s to be true, but is false\n",
58 unary_assert->condition);
Brendan Higgins6b229e52019-09-23 02:02:34 -070059 else
60 string_stream_add(stream,
Alan Maguirec3bba692020-03-26 14:25:09 +000061 KUNIT_SUBTEST_INDENT "Expected %s to be false, but is true\n",
62 unary_assert->condition);
Brendan Higgins6b229e52019-09-23 02:02:34 -070063 kunit_assert_print_msg(assert, stream);
64}
Alan Maguirec475c772020-01-06 22:28:20 +000065EXPORT_SYMBOL_GPL(kunit_unary_assert_format);
Brendan Higgins6b229e52019-09-23 02:02:34 -070066
67void 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,
Alan Maguirec3bba692020-03-26 14:25:09 +000076 KUNIT_SUBTEST_INDENT "Expected %s is not null, but is\n",
77 ptr_assert->text);
Brendan Higgins6b229e52019-09-23 02:02:34 -070078 } else if (IS_ERR(ptr_assert->value)) {
79 string_stream_add(stream,
Alan Maguirec3bba692020-03-26 14:25:09 +000080 KUNIT_SUBTEST_INDENT "Expected %s is not error, but is: %ld\n",
81 ptr_assert->text,
82 PTR_ERR(ptr_assert->value));
Brendan Higgins6b229e52019-09-23 02:02:34 -070083 }
84 kunit_assert_print_msg(assert, stream);
85}
Alan Maguirec475c772020-01-06 22:28:20 +000086EXPORT_SYMBOL_GPL(kunit_ptr_not_err_assert_format);
Brendan Higgins6b229e52019-09-23 02:02:34 -070087
88void 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,
Alan Maguirec3bba692020-03-26 14:25:09 +000096 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);
Brendan Higgins6b229e52019-09-23 02:02:34 -0700106 kunit_assert_print_msg(assert, stream);
107}
Alan Maguirec475c772020-01-06 22:28:20 +0000108EXPORT_SYMBOL_GPL(kunit_binary_assert_format);
Brendan Higgins6b229e52019-09-23 02:02:34 -0700109
110void 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,
Alan Maguirec3bba692020-03-26 14:25:09 +0000118 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);
Brendan Higgins6b229e52019-09-23 02:02:34 -0700128 kunit_assert_print_msg(assert, stream);
129}
Alan Maguirec475c772020-01-06 22:28:20 +0000130EXPORT_SYMBOL_GPL(kunit_binary_ptr_assert_format);
Brendan Higgins6b229e52019-09-23 02:02:34 -0700131
132void 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,
Alan Maguirec3bba692020-03-26 14:25:09 +0000140 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);
Brendan Higgins6b229e52019-09-23 02:02:34 -0700150 kunit_assert_print_msg(assert, stream);
151}
Alan Maguirec475c772020-01-06 22:28:20 +0000152EXPORT_SYMBOL_GPL(kunit_binary_str_assert_format);