mirror of
https://github.com/neovim/neovim.git
synced 2025-12-14 10:25:42 +00:00
vim-patch:8.1.0819: a failed assert with a long string is hard to read
Problem: A failed assert with a long string is hard to read.
Solution: Shorten the assert message.
865767126e
This commit is contained in:
@@ -5552,19 +5552,18 @@ void prepare_assert_error(garray_T *gap)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Append "str" to "gap", escaping unprintable characters.
|
// Append "p[clen]" to "gap", escaping unprintable characters.
|
||||||
// Changes NL to \n, CR to \r, etc.
|
// Changes NL to \n, CR to \r, etc.
|
||||||
static void ga_concat_esc(garray_T *gap, char_u *str)
|
static void ga_concat_esc(garray_T *gap, const char_u *p, int clen)
|
||||||
|
FUNC_ATTR_NONNULL_ALL
|
||||||
{
|
{
|
||||||
char_u *p;
|
|
||||||
char_u buf[NUMBUFLEN];
|
char_u buf[NUMBUFLEN];
|
||||||
|
|
||||||
if (str == NULL) {
|
if (clen > 1) {
|
||||||
ga_concat(gap, (char_u *)"NULL");
|
memmove(buf, p, clen);
|
||||||
return;
|
buf[clen] = NUL;
|
||||||
}
|
ga_concat(gap, buf);
|
||||||
|
} else {
|
||||||
for (p = str; *p != NUL; p++) {
|
|
||||||
switch (*p) {
|
switch (*p) {
|
||||||
case BS: ga_concat(gap, (char_u *)"\\b"); break;
|
case BS: ga_concat(gap, (char_u *)"\\b"); break;
|
||||||
case ESC: ga_concat(gap, (char_u *)"\\e"); break;
|
case ESC: ga_concat(gap, (char_u *)"\\e"); break;
|
||||||
@@ -5585,6 +5584,41 @@ static void ga_concat_esc(garray_T *gap, char_u *str)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Append "str" to "gap", escaping unprintable characters.
|
||||||
|
// Changes NL to \n, CR to \r, etc.
|
||||||
|
static void ga_concat_shorten_esc(garray_T *gap, const char_u *str)
|
||||||
|
FUNC_ATTR_NONNULL_ARG(1)
|
||||||
|
{
|
||||||
|
char_u buf[NUMBUFLEN];
|
||||||
|
|
||||||
|
if (str == NULL) {
|
||||||
|
ga_concat(gap, (char_u *)"NULL");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const char_u *p = str; *p != NUL; p++) {
|
||||||
|
int same_len = 1;
|
||||||
|
const char_u *s = p;
|
||||||
|
const int c = mb_ptr2char_adv(&s);
|
||||||
|
const int clen = s - p;
|
||||||
|
while (*s != NUL && c == utf_ptr2char(s)) {
|
||||||
|
same_len++;
|
||||||
|
s += clen;
|
||||||
|
}
|
||||||
|
if (same_len > 20) {
|
||||||
|
ga_concat(gap, (char_u *)"\\[");
|
||||||
|
ga_concat_esc(gap, p, clen);
|
||||||
|
ga_concat(gap, (char_u *)" occurs ");
|
||||||
|
vim_snprintf((char *)buf, NUMBUFLEN, "%d", same_len);
|
||||||
|
ga_concat(gap, buf);
|
||||||
|
ga_concat(gap, (char_u *)" times]");
|
||||||
|
p = s - 1;
|
||||||
|
} else {
|
||||||
|
ga_concat_esc(gap, p, clen);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Fill "gap" with information about an assert error.
|
// Fill "gap" with information about an assert error.
|
||||||
void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv,
|
void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv,
|
||||||
char_u *exp_str, typval_T *exp_tv,
|
char_u *exp_str, typval_T *exp_tv,
|
||||||
@@ -5609,10 +5643,10 @@ void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv,
|
|||||||
|
|
||||||
if (exp_str == NULL) {
|
if (exp_str == NULL) {
|
||||||
tofree = (char_u *)encode_tv2string(exp_tv, NULL);
|
tofree = (char_u *)encode_tv2string(exp_tv, NULL);
|
||||||
ga_concat_esc(gap, tofree);
|
ga_concat_shorten_esc(gap, tofree);
|
||||||
xfree(tofree);
|
xfree(tofree);
|
||||||
} else {
|
} else {
|
||||||
ga_concat_esc(gap, exp_str);
|
ga_concat_shorten_esc(gap, exp_str);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (atype != ASSERT_NOTEQUAL) {
|
if (atype != ASSERT_NOTEQUAL) {
|
||||||
@@ -5624,7 +5658,7 @@ void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv,
|
|||||||
ga_concat(gap, (char_u *)" but got ");
|
ga_concat(gap, (char_u *)" but got ");
|
||||||
}
|
}
|
||||||
tofree = (char_u *)encode_tv2string(got_tv, NULL);
|
tofree = (char_u *)encode_tv2string(got_tv, NULL);
|
||||||
ga_concat_esc(gap, tofree);
|
ga_concat_shorten_esc(gap, tofree);
|
||||||
xfree(tofree);
|
xfree(tofree);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -92,6 +92,11 @@ describe('assert function:', function()
|
|||||||
call('assert_equal', 'foo', 'bar', 'testing')
|
call('assert_equal', 'foo', 'bar', 'testing')
|
||||||
expected_errors({"testing: Expected 'foo' but got 'bar'"})
|
expected_errors({"testing: Expected 'foo' but got 'bar'"})
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it('should shorten a long message', function()
|
||||||
|
call ('assert_equal', 'XxxxxxxxxxxxxxxxxxxxxxX', 'XyyyyyyyyyyyyyyyyyyyyyyyyyX')
|
||||||
|
expected_errors({"Expected 'X\\[x occurs 21 times]X' but got 'X\\[y occurs 25 times]X'"})
|
||||||
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
-- assert_notequal({expected}, {actual}[, {msg}])
|
-- assert_notequal({expected}, {actual}[, {msg}])
|
||||||
|
|||||||
Reference in New Issue
Block a user