Skip to content

Commit acd9762

Browse files
dlatypovshuahkh
authored andcommitted
kunit: make KUNIT_EXPECT_STREQ() quote values, don't print literals
Before: > Expected str == "world", but > str == hello > "world" == world After: > Expected str == "world", but > str == "hello" <we don't need to tell the user that "world" == "world"> Note: like the literal ellision for integers, this doesn't handle the case of KUNIT_EXPECT_STREQ(test, "hello", "world") since we don't expect it to realistically happen in checked in tests. (If you really wanted a test to fail, KUNIT_FAIL("msg") exists) In that case, you'd get: > Expected "hello" == "world", but <output for next failure> Signed-off-by: Daniel Latypov <[email protected]> Reviewed-by: Brendan Higgins <[email protected]> Signed-off-by: Shuah Khan <[email protected]>
1 parent 2f9f21c commit acd9762

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

lib/kunit/assert.c

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,22 @@ void kunit_binary_ptr_assert_format(const struct kunit_assert *assert,
163163
}
164164
EXPORT_SYMBOL_GPL(kunit_binary_ptr_assert_format);
165165

166+
/* Checks if KUNIT_EXPECT_STREQ() args were string literals.
167+
* Note: `text` will have ""s where as `value` will not.
168+
*/
169+
static bool is_str_literal(const char *text, const char *value)
170+
{
171+
int len;
172+
173+
len = strlen(text);
174+
if (len < 2)
175+
return false;
176+
if (text[0] != '\"' || text[len - 1] != '\"')
177+
return false;
178+
179+
return strncmp(text + 1, value, len - 2) == 0;
180+
}
181+
166182
void kunit_binary_str_assert_format(const struct kunit_assert *assert,
167183
struct string_stream *stream)
168184
{
@@ -177,12 +193,14 @@ void kunit_binary_str_assert_format(const struct kunit_assert *assert,
177193
binary_assert->left_text,
178194
binary_assert->operation,
179195
binary_assert->right_text);
180-
string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %s\n",
181-
binary_assert->left_text,
182-
binary_assert->left_value);
183-
string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %s",
184-
binary_assert->right_text,
185-
binary_assert->right_value);
196+
if (!is_str_literal(binary_assert->left_text, binary_assert->left_value))
197+
string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == \"%s\"\n",
198+
binary_assert->left_text,
199+
binary_assert->left_value);
200+
if (!is_str_literal(binary_assert->right_text, binary_assert->right_value))
201+
string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == \"%s\"",
202+
binary_assert->right_text,
203+
binary_assert->right_value);
186204
kunit_assert_print_msg(assert, stream);
187205
}
188206
EXPORT_SYMBOL_GPL(kunit_binary_str_assert_format);

0 commit comments

Comments
 (0)