]> granicus.if.org Git - vim/commitdiff
patch 8.1.0819: a failed assert with a long string is hard to read v8.1.0819
authorBram Moolenaar <Bram@vim.org>
Fri, 25 Jan 2019 19:48:33 +0000 (20:48 +0100)
committerBram Moolenaar <Bram@vim.org>
Fri, 25 Jan 2019 19:48:33 +0000 (20:48 +0100)
Problem:    A failed assert with a long string is hard to read.
Solution:   Shorten the assert message.

src/eval.c
src/testdir/test_assert.vim
src/version.c

index 67a006ec9525a0f6af0c9cf430e91b2854b9096e..82de63e1f8a252642e2fed005d9e68bb7bc8785f 100644 (file)
@@ -9494,15 +9494,55 @@ assert_fails(typval_T *argvars)
     return ret;
 }
 
+/*
+ * Append "p[clen]" to "gap", escaping unprintable characters.
+ * Changes NL to \n, CR to \r, etc.
+ */
+    static void
+ga_concat_esc(garray_T *gap, char_u *p, int clen)
+{
+    char_u  buf[NUMBUFLEN];
+
+    if (clen > 1)
+    {
+       mch_memmove(buf, p, clen);
+       buf[clen] = NUL;
+       ga_concat(gap, buf);
+    }
+    else switch (*p)
+    {
+       case BS: ga_concat(gap, (char_u *)"\\b"); break;
+       case ESC: ga_concat(gap, (char_u *)"\\e"); break;
+       case FF: ga_concat(gap, (char_u *)"\\f"); break;
+       case NL: ga_concat(gap, (char_u *)"\\n"); break;
+       case TAB: ga_concat(gap, (char_u *)"\\t"); break;
+       case CAR: ga_concat(gap, (char_u *)"\\r"); break;
+       case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
+       default:
+                  if (*p < ' ')
+                  {
+                      vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
+                      ga_concat(gap, buf);
+                  }
+                  else
+                      ga_append(gap, *p);
+                  break;
+    }
+}
+
 /*
  * Append "str" to "gap", escaping unprintable characters.
  * Changes NL to \n, CR to \r, etc.
  */
     static void
-ga_concat_esc(garray_T *gap, char_u *str)
+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)
     {
@@ -9511,25 +9551,29 @@ ga_concat_esc(garray_T *gap, char_u *str)
     }
 
     for (p = str; *p != NUL; ++p)
-       switch (*p)
-       {
-           case BS: ga_concat(gap, (char_u *)"\\b"); break;
-           case ESC: ga_concat(gap, (char_u *)"\\e"); break;
-           case FF: ga_concat(gap, (char_u *)"\\f"); break;
-           case NL: ga_concat(gap, (char_u *)"\\n"); break;
-           case TAB: ga_concat(gap, (char_u *)"\\t"); break;
-           case CAR: ga_concat(gap, (char_u *)"\\r"); break;
-           case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
-           default:
-               if (*p < ' ')
-               {
-                   vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
-                   ga_concat(gap, buf);
-               }
-               else
-                   ga_append(gap, *p);
-               break;
+    {
+       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);
+    }
 }
 
 /*
@@ -9562,11 +9606,11 @@ fill_assert_error(
        ga_concat(gap, (char_u *)"Expected ");
     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);
     }
     else
-       ga_concat_esc(gap, exp_str);
+       ga_concat_shorten_esc(gap, exp_str);
     if (atype != ASSERT_NOTEQUAL)
     {
        if (atype == ASSERT_MATCH)
@@ -9575,7 +9619,7 @@ fill_assert_error(
            ga_concat(gap, (char_u *)" does match ");
        else
            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);
     }
 }
index fb31478aa4e31e951ac1fd8b9b6260a268c67d26..298a6db15a891f0a1dd99841c2a85a01bd87cf4a 100644 (file)
@@ -31,6 +31,10 @@ func Test_assert_equal()
   call assert_equal(1, assert_equal('bar', s))
   call assert_match("Expected 'bar' but got 'foo'", 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
 
 func Test_assert_equalfile()
index 4beae276fd41a5eb549ac1c6b1439095b83fee16..3a75c99fc488803ce2662f795f33cf724ca28852 100644 (file)
@@ -787,6 +787,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    819,
 /**/
     818,
 /**/