Skip to content

Commit 8657671

Browse files
committed
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.
1 parent 2405838 commit 8657671

3 files changed

Lines changed: 72 additions & 22 deletions

File tree

src/eval.c

Lines changed: 66 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9494,15 +9494,55 @@ assert_fails(typval_T *argvars)
94949494
return ret;
94959495
}
94969496

9497+
/*
9498+
* Append "p[clen]" to "gap", escaping unprintable characters.
9499+
* Changes NL to \n, CR to \r, etc.
9500+
*/
9501+
static void
9502+
ga_concat_esc(garray_T *gap, char_u *p, int clen)
9503+
{
9504+
char_u buf[NUMBUFLEN];
9505+
9506+
if (clen > 1)
9507+
{
9508+
mch_memmove(buf, p, clen);
9509+
buf[clen] = NUL;
9510+
ga_concat(gap, buf);
9511+
}
9512+
else switch (*p)
9513+
{
9514+
case BS: ga_concat(gap, (char_u *)"\\b"); break;
9515+
case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9516+
case FF: ga_concat(gap, (char_u *)"\\f"); break;
9517+
case NL: ga_concat(gap, (char_u *)"\\n"); break;
9518+
case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9519+
case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9520+
case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9521+
default:
9522+
if (*p < ' ')
9523+
{
9524+
vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9525+
ga_concat(gap, buf);
9526+
}
9527+
else
9528+
ga_append(gap, *p);
9529+
break;
9530+
}
9531+
}
9532+
94979533
/*
94989534
* Append "str" to "gap", escaping unprintable characters.
94999535
* Changes NL to \n, CR to \r, etc.
95009536
*/
95019537
static void
9502-
ga_concat_esc(garray_T *gap, char_u *str)
9538+
ga_concat_shorten_esc(garray_T *gap, char_u *str)
95039539
{
95049540
char_u *p;
9541+
char_u *s;
9542+
int c;
9543+
int clen;
95059544
char_u buf[NUMBUFLEN];
9545+
int same_len;
95069546

95079547
if (str == NULL)
95089548
{
@@ -9511,25 +9551,29 @@ ga_concat_esc(garray_T *gap, char_u *str)
95119551
}
95129552

95139553
for (p = str; *p != NUL; ++p)
9514-
switch (*p)
9515-
{
9516-
case BS: ga_concat(gap, (char_u *)"\\b"); break;
9517-
case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9518-
case FF: ga_concat(gap, (char_u *)"\\f"); break;
9519-
case NL: ga_concat(gap, (char_u *)"\\n"); break;
9520-
case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9521-
case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9522-
case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9523-
default:
9524-
if (*p < ' ')
9525-
{
9526-
vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9527-
ga_concat(gap, buf);
9528-
}
9529-
else
9530-
ga_append(gap, *p);
9531-
break;
9554+
{
9555+
same_len = 1;
9556+
s = p;
9557+
c = mb_ptr2char_adv(&s);
9558+
clen = s - p;
9559+
while (*s != NUL && c == mb_ptr2char(s))
9560+
{
9561+
++same_len;
9562+
s += clen;
9563+
}
9564+
if (same_len > 20)
9565+
{
9566+
ga_concat(gap, (char_u *)"\\[");
9567+
ga_concat_esc(gap, p, clen);
9568+
ga_concat(gap, (char_u *)" occurs ");
9569+
vim_snprintf((char *)buf, NUMBUFLEN, "%d", same_len);
9570+
ga_concat(gap, buf);
9571+
ga_concat(gap, (char_u *)" times]");
9572+
p = s - 1;
95329573
}
9574+
else
9575+
ga_concat_esc(gap, p, clen);
9576+
}
95339577
}
95349578

95359579
/*
@@ -9562,11 +9606,11 @@ fill_assert_error(
95629606
ga_concat(gap, (char_u *)"Expected ");
95639607
if (exp_str == NULL)
95649608
{
9565-
ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
9609+
ga_concat_shorten_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
95669610
vim_free(tofree);
95679611
}
95689612
else
9569-
ga_concat_esc(gap, exp_str);
9613+
ga_concat_shorten_esc(gap, exp_str);
95709614
if (atype != ASSERT_NOTEQUAL)
95719615
{
95729616
if (atype == ASSERT_MATCH)
@@ -9575,7 +9619,7 @@ fill_assert_error(
95759619
ga_concat(gap, (char_u *)" does match ");
95769620
else
95779621
ga_concat(gap, (char_u *)" but got ");
9578-
ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
9622+
ga_concat_shorten_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
95799623
vim_free(tofree);
95809624
}
95819625
}

src/testdir/test_assert.vim

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ func Test_assert_equal()
3131
call assert_equal(1, assert_equal('bar', s))
3232
call assert_match("Expected 'bar' but got 'foo'", v:errors[0])
3333
call remove(v:errors, 0)
34+
35+
call assert_equal('XxxxxxxxxxxxxxxxxxxxxxX', 'XyyyyyyyyyyyyyyyyyyyyyyyyyX')
36+
call assert_match("Expected 'X\\\\\\[x occurs 21 times]X' but got 'X\\\\\\[y occurs 25 times]X'", v:errors[0])
37+
call remove(v:errors, 0)
3438
endfunc
3539

3640
func Test_assert_equalfile()

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,8 @@ static char *(features[]) =
787787

788788
static int included_patches[] =
789789
{ /* Add new patch number below this line */
790+
/**/
791+
819,
790792
/**/
791793
818,
792794
/**/

0 commit comments

Comments
 (0)