0
0
mirror of https://github.com/vim/vim.git synced 2025-09-25 03:54:15 -04:00

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.
This commit is contained in:
Bram Moolenaar
2019-01-25 20:48:33 +01:00
parent 240583869a
commit 865767126e
3 changed files with 71 additions and 21 deletions

View File

@@ -9495,23 +9495,21 @@ assert_fails(typval_T *argvars)
} }
/* /*
* 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 static void
ga_concat_esc(garray_T *gap, char_u *str) ga_concat_esc(garray_T *gap, char_u *p, int clen)
{ {
char_u *p;
char_u buf[NUMBUFLEN]; char_u buf[NUMBUFLEN];
if (str == NULL) if (clen > 1)
{ {
ga_concat(gap, (char_u *)"NULL"); mch_memmove(buf, p, clen);
return; buf[clen] = NUL;
ga_concat(gap, buf);
} }
else switch (*p)
for (p = str; *p != NUL; ++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;
@@ -9532,6 +9530,52 @@ 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, char_u *str)
{
char_u *p;
char_u *s;
int c;
int clen;
char_u buf[NUMBUFLEN];
int same_len;
if (str == NULL)
{
ga_concat(gap, (char_u *)"NULL");
return;
}
for (p = str; *p != NUL; ++p)
{
same_len = 1;
s = p;
c = mb_ptr2char_adv(&s);
clen = s - p;
while (*s != NUL && c == mb_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.
*/ */
@@ -9562,11 +9606,11 @@ fill_assert_error(
ga_concat(gap, (char_u *)"Expected "); ga_concat(gap, (char_u *)"Expected ");
if (exp_str == NULL) if (exp_str == NULL)
{ {
ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0)); ga_concat_shorten_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
vim_free(tofree); vim_free(tofree);
} }
else else
ga_concat_esc(gap, exp_str); ga_concat_shorten_esc(gap, exp_str);
if (atype != ASSERT_NOTEQUAL) if (atype != ASSERT_NOTEQUAL)
{ {
if (atype == ASSERT_MATCH) if (atype == ASSERT_MATCH)
@@ -9575,7 +9619,7 @@ fill_assert_error(
ga_concat(gap, (char_u *)" does match "); ga_concat(gap, (char_u *)" does match ");
else else
ga_concat(gap, (char_u *)" but got "); ga_concat(gap, (char_u *)" but got ");
ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0)); ga_concat_shorten_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
vim_free(tofree); vim_free(tofree);
} }
} }

View File

@@ -31,6 +31,10 @@ func Test_assert_equal()
call assert_equal(1, assert_equal('bar', s)) call assert_equal(1, assert_equal('bar', s))
call assert_match("Expected 'bar' but got 'foo'", v:errors[0]) call assert_match("Expected 'bar' but got 'foo'", v:errors[0])
call remove(v:errors, 0) call remove(v:errors, 0)
call assert_equal('XxxxxxxxxxxxxxxxxxxxxxX', 'XyyyyyyyyyyyyyyyyyyyyyyyyyX')
call assert_match("Expected 'X\\\\\\[x occurs 21 times]X' but got 'X\\\\\\[y occurs 25 times]X'", v:errors[0])
call remove(v:errors, 0)
endfunc endfunc
func Test_assert_equalfile() func Test_assert_equalfile()

View File

@@ -787,6 +787,8 @@ static char *(features[]) =
static int included_patches[] = static int included_patches[] =
{ /* Add new patch number below this line */ { /* Add new patch number below this line */
/**/
819,
/**/ /**/
818, 818,
/**/ /**/