/* vi:set ts=8 sts=4 sw=4 noet:
*
* VIM - Vi IMproved by Bram Moolenaar
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/
/*
* testing.c: Support for tests.
*/
#include "vim.h"
#if defined(FEAT_EVAL) || defined(PROTO)
/*
* Prepare "gap" for an assert error and add the sourcing position.
*/
static void
prepare_assert_error(garray_T *gap)
{
char buf[NUMBUFLEN];
ga_init2(gap, 1, 100);
if (sourcing_name != NULL)
{
ga_concat(gap, sourcing_name);
if (sourcing_lnum > 0)
ga_concat(gap, (char_u *)" ");
}
if (sourcing_lnum > 0)
{
sprintf(buf, "line %ld", (long)sourcing_lnum);
ga_concat(gap, (char_u *)buf);
}
if (sourcing_name != NULL || sourcing_lnum > 0)
ga_concat(gap, (char_u *)": ");
}
/*
* 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_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.
*/
static void
fill_assert_error(
garray_T *gap,
typval_T *opt_msg_tv,
char_u *exp_str,
typval_T *exp_tv,
typval_T *got_tv,
assert_type_T atype)
{
char_u numbuf[NUMBUFLEN];
char_u *tofree;
if (opt_msg_tv->v_type != VAR_UNKNOWN)
{
ga_concat(gap, echo_string(opt_msg_tv, &tofree, numbuf, 0));
vim_free(tofree);
ga_concat(gap, (char_u *)": ");
}
if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
ga_concat(gap, (char_u *)"Pattern ");
else if (atype == ASSERT_NOTEQUAL