diff mbox series

[v2,3/3] kunit: Improve format of the KUNIT_EXPECT_EQ assertion

Message ID 20240822125459.2075-4-michal.wajdeczko@intel.com
State New
Headers show
Series kunit: Improve format of some assertion messages | expand

Commit Message

Michal Wajdeczko Aug. 22, 2024, 12:54 p.m. UTC
Diagnostic message for failed KUNIT_ASSERT|EXPECT_EQ shows in
case of integers only raw values, like for this example:

  bool flag;
  KUNIT_EXPECT_EQ(test, -ENODATA, kstrtobool("dunno", &flag));

we will get:

  [ ] Expected -61 == kstrtobool("dunno", &flag), but
  [ ]     kstrtobool("dunno", &flag) == -22 (0xffffffffffffffea)

but we can improve it if the value is within MAX_ERRNO range by
using more friendly error format:

  [ ] Expected -61 == kstrtobool("dunno", &flag), but
  [ ]     -61 == 0xffffffffffffffc3 (-ENODATA)
  [ ]     kstrtobool("dunno", &flag) == -22 (-EINVAL)

Signed-off-by: Michal Wajdeczko <michal.wajdeczko@intel.com>
Reviewed-by: David Gow <davidgow@google.com> #v1
---
v2: keep '==' and raw values (David)
    use better example (Michal)
---
 lib/kunit/assert.c | 30 ++++++++++++++++++++++++++----
 1 file changed, 26 insertions(+), 4 deletions(-)
diff mbox series

Patch

diff --git a/lib/kunit/assert.c b/lib/kunit/assert.c
index 414474841b61..5f0a2e2f5231 100644
--- a/lib/kunit/assert.c
+++ b/lib/kunit/assert.c
@@ -129,15 +129,37 @@  void kunit_binary_assert_format(const struct kunit_assert *assert,
 			  binary_assert->text->operation,
 			  binary_assert->text->right_text);
 	if (!is_literal(binary_assert->text->left_text, binary_assert->left_value))
-		string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %lld (0x%llx)\n",
+		if (IS_ERR_VALUE(binary_assert->left_value))
+			string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %lld (%pe)\n",
+					  binary_assert->text->left_text,
+					  binary_assert->left_value,
+					  ERR_PTR(binary_assert->left_value));
+		else
+			string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %lld (%#llx)\n",
+					  binary_assert->text->left_text,
+					  binary_assert->left_value,
+					  binary_assert->left_value);
+	else if (IS_ERR_VALUE(binary_assert->left_value))
+		string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %#llx (%pe)\n",
 				  binary_assert->text->left_text,
 				  binary_assert->left_value,
-				  binary_assert->left_value);
+				  ERR_PTR(binary_assert->left_value));
 	if (!is_literal(binary_assert->text->right_text, binary_assert->right_value))
-		string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %lld (0x%llx)",
+		if (IS_ERR_VALUE(binary_assert->right_value))
+			string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %lld (%pe)",
+					  binary_assert->text->right_text,
+					  binary_assert->right_value,
+					  ERR_PTR(binary_assert->right_value));
+		else
+			string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %lld (%#llx)",
+					  binary_assert->text->right_text,
+					  binary_assert->right_value,
+					  binary_assert->right_value);
+	else if (IS_ERR_VALUE(binary_assert->right_value))
+		string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %#llx (%pe)",
 				  binary_assert->text->right_text,
 				  binary_assert->right_value,
-				  binary_assert->right_value);
+				  ERR_PTR(binary_assert->right_value));
 	kunit_assert_print_msg(message, stream);
 }
 EXPORT_SYMBOL_GPL(kunit_binary_assert_format);