Skip to contentSkip to conversation

kunit: Customize KUNIT_EXCEPT/KUNIT_ASSERT Expected messages

PATCH In review Vitor Massaru Iha 2 participants 3 messages Aug 19, 2020 – Aug 20, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kunit: Customize KUNIT_EXCEPT/KUNIT_ASSERT Expected messages

In some cases, to maintain the consistency of the Expected messages with the original runtime test, it is necessary to customize the Expected messages on KUnit.

As an example test_overflow conversion to KUnit (I added 1, p->s_of+1, just to fail the test).

Using KUNIT_EXPECT_EQ:

Expected _of == p->s_of+1, but
_of == 0
p->s_of+1 == 1
not ok 1 - overflow_calculation_test
ok 2 - overflow_shift_test

Using KUNIT_EXPECT_EQ_CUSTOM_MSG:

Expected 0 + 0 to not overflow (type u8)
not ok 1 - overflow_calculation_test

Which is more similar to the error message of the original test.

---
 include/kunit/assert.h |   8 ++-
 include/kunit/test.h   | 151 +++++++++++++++++++++++++++++++++++++++--
 lib/kunit/assert.c     |  34 ++++++----
 3 files changed, 172 insertions(+), 21 deletions(-)
include/kunit/assert.h +6 -2
index ad889b539ab3..b3c25b4420c8 100644
@@ -204,6 +204,7 @@ struct kunit_binary_assert {
 	long long left_value;
 	const char *right_text;
 	long long right_value;
+	bool custom_msg;
 };
 
 void kunit_binary_assert_format(const struct kunit_assert *assert,
@@ -219,6 +220,7 @@ void kunit_binary_assert_format(const struct kunit_assert *assert,
  * @left_val: The actual evaluated value of the expression in the left slot.
  * @right_str: A string representation of the expression in the right slot.
  * @right_val: The actual evaluated value of the expression in the right slot.
+ * @custom_msg_bool: Show a custom expect message instead the default message.
  *
  * Initializes a &struct kunit_binary_assert. Intended to be used in
  * KUNIT_EXPECT_* and KUNIT_ASSERT_* macros.
@@ -229,7 +231,8 @@ void kunit_binary_assert_format(const struct kunit_assert *assert,
 					left_str,			       \
 					left_val,			       \
 					right_str,			       \
-					right_val) {			       \
+					right_val,			       \
+					custom_msg_bool) {		       \
 	.assert = KUNIT_INIT_ASSERT_STRUCT(test,			       \
 					   type,			       \
 					   kunit_binary_assert_format),	       \
@@ -237,7 +240,8 @@ void kunit_binary_assert_format(const struct kunit_assert *assert,
 	.left_text = left_str,						       \
 	.left_value = left_val,						       \
 	.right_text = right_str,					       \
-	.right_value = right_val					       \
+	.right_value = right_val,					       \
+	.custom_msg = custom_msg_bool,					       \
 }
 
 /**
include/kunit/test.h +145 -6

398 lines not on this page · raw message

lib/kunit/assert.c +21 -13
index 33acdaa28a7d..202f9fdeed0e 100644
@@ -32,8 +32,14 @@ EXPORT_SYMBOL_GPL(kunit_base_assert_format);
 void kunit_assert_print_msg(const struct kunit_assert *assert,
 			    struct string_stream *stream)
 {
-	if (assert->message.fmt)
-		string_stream_add(stream, "\n%pV", &assert->message);
+	struct kunit_binary_assert *binary_assert = container_of(
+			assert, struct kunit_binary_assert, assert);
+	if (assert->message.fmt) {
+		if (binary_assert->custom_msg == false)
+			string_stream_add(stream, "\n" KUNIT_SUBSUBTEST_INDENT "%pV", &assert->message);
+		else
+			string_stream_add(stream, KUNIT_SUBTEST_INDENT "%pV", &assert->message);
+	}
 }
 EXPORT_SYMBOL_GPL(kunit_assert_print_msg);
 
@@ -92,17 +98,19 @@ void kunit_binary_assert_format(const struct kunit_assert *assert,
 			assert, struct kunit_binary_assert, assert);
 
 	kunit_base_assert_format(assert, stream);
-	string_stream_add(stream,
-			  KUNIT_SUBTEST_INDENT "Expected %s %s %s, but\n",
-			  binary_assert->left_text,
-			  binary_assert->operation,
-			  binary_assert->right_text);
-	string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %lld\n",
-			  binary_assert->left_text,
-			  binary_assert->left_value);
-	string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %lld",
-			  binary_assert->right_text,
-			  binary_assert->right_value);
+	if (binary_assert->custom_msg == false) {
+		string_stream_add(stream,
+				  KUNIT_SUBTEST_INDENT "Expected %s %s %s, but\n",
+				  binary_assert->left_text,
+				  binary_assert->operation,
+				  binary_assert->right_text);
+		string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %lld\n",
+				  binary_assert->left_text,
+				  binary_assert->left_value);
+		string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %lld",
+				  binary_assert->right_text,
+				  binary_assert->right_value);
+	}
 	kunit_assert_print_msg(assert, stream);
 }
 EXPORT_SYMBOL_GPL(kunit_binary_assert_format);

base-commit: d43c7fb05765152d4d4a39a8ef957c4ea14d8847

Signature · 1 line
2.26.2
#1 lore raw
Quoted 16 lines · Vitor Massaru Iha

In some cases, to maintain the consistency of the Expected messages with the original runtime test, it is necessary to customize the Expected messages on KUnit.

As an example test_overflow conversion to KUnit (I added 1, p->s_of+1, just to fail the test).

Using KUNIT_EXPECT_EQ:

Expected _of == p->s_of+1, but
_of == 0
p->s_of+1 == 1
not ok 1 - overflow_calculation_test
ok 2 - overflow_shift_test

Quoted 8 lines

Using KUNIT_EXPECT_EQ_CUSTOM_MSG:

Expected 0 + 0 to not overflow (type u8)
not ok 1 - overflow_calculation_test

Which is more similar to the error message of the original test.

All in all this looks pretty reasonable to me; however, is there any chance you could provide an example of its usage?

#2 lore raw
Quoted 23 lines · Brendan Higgins
Quoted 21 lines · Vitor Massaru Iha

In some cases, to maintain the consistency of the Expected messages with the original runtime test, it is necessary to customize the Expected messages on KUnit.

As an example test_overflow conversion to KUnit (I added 1, p->s_of+1, just to fail the test).

Using KUNIT_EXPECT_EQ:

Expected _of == p->s_of+1, but
_of == 0
p->s_of+1 == 1
not ok 1 - overflow_calculation_test
ok 2 - overflow_shift_test

Using KUNIT_EXPECT_EQ_CUSTOM_MSG:

Expected 0 + 0 to not overflow (type u8)
not ok 1 - overflow_calculation_test

Quoted 6 lines
Quoted 3 lines

Which is more similar to the error message of the original test.

All in all this looks pretty reasonable to me; however, is there any chance you could provide an example of its usage?

Sure I'll do that.

I am reviewing the errors reported by 0day in the overflow_kunit patch v3, and I noticed when compiling overflow_kunit with the parameters of 0day and this patch, there were some errors related to this patch that I sent.

I'll fix it too.
_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees