diff mbox series

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

Message ID 20240821191412.2031-4-michal.wajdeczko@intel.com (mailing list archive)
State New
Delegated to: Brendan Higgins
Headers show
Series kunit: Improve format of some assertion messages | expand

Commit Message

Michal Wajdeczko Aug. 21, 2024, 7:14 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, 0, kstrtobool("dunno", &flag));

we will get:

  [ ] Expected 0 == 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 0 == kstrtobool("dunno", &flag), but
  [ ]     kstrtobool("dunno", &flag) == -22 (-EINVAL)

Signed-off-by: Michal Wajdeczko <michal.wajdeczko@intel.com>
---
Cc: David Gow <davidgow@google.com>
Cc: Rae Moar <rmoar@google.com>
---
 lib/kunit/assert.c | 32 ++++++++++++++++++++++++++------
 1 file changed, 26 insertions(+), 6 deletions(-)

Comments

David Gow Aug. 22, 2024, 6:37 a.m. UTC | #1
On Thu, 22 Aug 2024 at 03:15, Michal Wajdeczko
<michal.wajdeczko@intel.com> wrote:
>
> 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, 0, kstrtobool("dunno", &flag));
>
> we will get:
>
>   [ ] Expected 0 == 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 0 == kstrtobool("dunno", &flag), but
>   [ ]     kstrtobool("dunno", &flag) == -22 (-EINVAL)
>
> Signed-off-by: Michal Wajdeczko <michal.wajdeczko@intel.com>
> ---
> Cc: David Gow <davidgow@google.com>
> Cc: Rae Moar <rmoar@google.com>
> ---

I wasn't sure about this at first, but looking at it, I think I like
it, since the numeric value is still given. _Maybe_ it'd be nicer to
include both the hex and the error name, but I suspect that's just
going to clutter things up more.

So,
Reviewed-by: David Gow <davidgow@google.com>

Cheers,
-- David



>  lib/kunit/assert.c | 32 ++++++++++++++++++++++++++------
>  1 file changed, 26 insertions(+), 6 deletions(-)
>
> diff --git a/lib/kunit/assert.c b/lib/kunit/assert.c
> index 8da89043b734..9dec0551d0d0 100644
> --- a/lib/kunit/assert.c
> +++ b/lib/kunit/assert.c
> @@ -128,15 +128,35 @@ 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 (0x%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 is %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 (0x%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 is %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);
> --
> 2.43.0
>
diff mbox series

Patch

diff --git a/lib/kunit/assert.c b/lib/kunit/assert.c
index 8da89043b734..9dec0551d0d0 100644
--- a/lib/kunit/assert.c
+++ b/lib/kunit/assert.c
@@ -128,15 +128,35 @@  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 (0x%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 is %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 (0x%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 is %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);