Brendan Higgins | 6b229e5 | 2019-09-23 02:02:34 -0700 | [diff] [blame] | 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> |
Alan Maguire | c3bba69 | 2020-03-26 14:25:09 +0000 | [diff] [blame] | 9 | #include <kunit/test.h> |
Brendan Higgins | 6b229e5 | 2019-09-23 02:02:34 -0700 | [diff] [blame] | 10 | |
Alan Maguire | 109fb06 | 2020-01-06 22:28:18 +0000 | [diff] [blame] | 11 | #include "string-stream.h" |
| 12 | |
Brendan Higgins | 6b229e5 | 2019-09-23 02:02:34 -0700 | [diff] [blame] | 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 | } |
Alan Maguire | c475c77 | 2020-01-06 22:28:20 +0000 | [diff] [blame] | 30 | EXPORT_SYMBOL_GPL(kunit_base_assert_format); |
Brendan Higgins | 6b229e5 | 2019-09-23 02:02:34 -0700 | [diff] [blame] | 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 | } |
Alan Maguire | c475c77 | 2020-01-06 22:28:20 +0000 | [diff] [blame] | 38 | EXPORT_SYMBOL_GPL(kunit_assert_print_msg); |
Brendan Higgins | 6b229e5 | 2019-09-23 02:02:34 -0700 | [diff] [blame] | 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 | } |
Alan Maguire | c475c77 | 2020-01-06 22:28:20 +0000 | [diff] [blame] | 46 | EXPORT_SYMBOL_GPL(kunit_fail_assert_format); |
Brendan Higgins | 6b229e5 | 2019-09-23 02:02:34 -0700 | [diff] [blame] | 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, |
Alan Maguire | c3bba69 | 2020-03-26 14:25:09 +0000 | [diff] [blame] | 57 | KUNIT_SUBTEST_INDENT "Expected %s to be true, but is false\n", |
| 58 | unary_assert->condition); |
Brendan Higgins | 6b229e5 | 2019-09-23 02:02:34 -0700 | [diff] [blame] | 59 | else |
| 60 | string_stream_add(stream, |
Alan Maguire | c3bba69 | 2020-03-26 14:25:09 +0000 | [diff] [blame] | 61 | KUNIT_SUBTEST_INDENT "Expected %s to be false, but is true\n", |
| 62 | unary_assert->condition); |
Brendan Higgins | 6b229e5 | 2019-09-23 02:02:34 -0700 | [diff] [blame] | 63 | kunit_assert_print_msg(assert, stream); |
| 64 | } |
Alan Maguire | c475c77 | 2020-01-06 22:28:20 +0000 | [diff] [blame] | 65 | EXPORT_SYMBOL_GPL(kunit_unary_assert_format); |
Brendan Higgins | 6b229e5 | 2019-09-23 02:02:34 -0700 | [diff] [blame] | 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, |
Alan Maguire | c3bba69 | 2020-03-26 14:25:09 +0000 | [diff] [blame] | 76 | KUNIT_SUBTEST_INDENT "Expected %s is not null, but is\n", |
| 77 | ptr_assert->text); |
Brendan Higgins | 6b229e5 | 2019-09-23 02:02:34 -0700 | [diff] [blame] | 78 | } else if (IS_ERR(ptr_assert->value)) { |
| 79 | string_stream_add(stream, |
Alan Maguire | c3bba69 | 2020-03-26 14:25:09 +0000 | [diff] [blame] | 80 | KUNIT_SUBTEST_INDENT "Expected %s is not error, but is: %ld\n", |
| 81 | ptr_assert->text, |
| 82 | PTR_ERR(ptr_assert->value)); |
Brendan Higgins | 6b229e5 | 2019-09-23 02:02:34 -0700 | [diff] [blame] | 83 | } |
| 84 | kunit_assert_print_msg(assert, stream); |
| 85 | } |
Alan Maguire | c475c77 | 2020-01-06 22:28:20 +0000 | [diff] [blame] | 86 | EXPORT_SYMBOL_GPL(kunit_ptr_not_err_assert_format); |
Brendan Higgins | 6b229e5 | 2019-09-23 02:02:34 -0700 | [diff] [blame] | 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, |
Alan Maguire | c3bba69 | 2020-03-26 14:25:09 +0000 | [diff] [blame] | 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); |
Brendan Higgins | 6b229e5 | 2019-09-23 02:02:34 -0700 | [diff] [blame] | 106 | kunit_assert_print_msg(assert, stream); |
| 107 | } |
Alan Maguire | c475c77 | 2020-01-06 22:28:20 +0000 | [diff] [blame] | 108 | EXPORT_SYMBOL_GPL(kunit_binary_assert_format); |
Brendan Higgins | 6b229e5 | 2019-09-23 02:02:34 -0700 | [diff] [blame] | 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, |
Alan Maguire | c3bba69 | 2020-03-26 14:25:09 +0000 | [diff] [blame] | 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); |
Brendan Higgins | 6b229e5 | 2019-09-23 02:02:34 -0700 | [diff] [blame] | 128 | kunit_assert_print_msg(assert, stream); |
| 129 | } |
Alan Maguire | c475c77 | 2020-01-06 22:28:20 +0000 | [diff] [blame] | 130 | EXPORT_SYMBOL_GPL(kunit_binary_ptr_assert_format); |
Brendan Higgins | 6b229e5 | 2019-09-23 02:02:34 -0700 | [diff] [blame] | 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, |
Alan Maguire | c3bba69 | 2020-03-26 14:25:09 +0000 | [diff] [blame] | 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); |
Brendan Higgins | 6b229e5 | 2019-09-23 02:02:34 -0700 | [diff] [blame] | 150 | kunit_assert_print_msg(assert, stream); |
| 151 | } |
Alan Maguire | c475c77 | 2020-01-06 22:28:20 +0000 | [diff] [blame] | 152 | EXPORT_SYMBOL_GPL(kunit_binary_str_assert_format); |