vim-patch:7.4.944

Problem:    Writing tests for Vim script is hard.
Solution:   Add assertEqual(), assertFalse() and assertTrue() functions.  Add
            the v:errors variable.  Add the runtest script. Add a first new
            style test script.

43345546ae
This commit is contained in:
watiko
2015-12-13 12:19:54 +09:00
parent 50db0312f9
commit 593df501b3
8 changed files with 262 additions and 5 deletions

View File

@@ -1,4 +1,4 @@
*eval.txt* For Vim version 7.4. Last change: 2015 Jan 29 *eval.txt* For Vim version 7.4. Last change: 2015 Nov 29
VIM REFERENCE MANUAL by Bram Moolenaar VIM REFERENCE MANUAL by Bram Moolenaar
@@ -1375,6 +1375,15 @@ v:errmsg Last given error message. It's allowed to set this variable.
: ... handle error : ... handle error
< "errmsg" also works, for backwards compatibility. < "errmsg" also works, for backwards compatibility.
*v:errors* *errors-variable*
v:errors Errors found by assert functions, such as |assertTrue()|.
This is a list of strings.
The assert functions append an item when an assert fails.
To remove old results make it empty: >
:let v:errors = []
< If v:errors is set to anything but a list it is made an empty
list by the assert function.
*v:exception* *exception-variable* *v:exception* *exception-variable*
v:exception The value of the exception most recently caught and not v:exception The value of the exception most recently caught and not
finished. See also |v:throwpoint| and |throw-variables|. finished. See also |v:throwpoint| and |throw-variables|.
@@ -1732,6 +1741,9 @@ arglistid( [{winnr}, [ {tabnr}]])
Number argument list id Number argument list id
argv( {nr}) String {nr} entry of the argument list argv( {nr}) String {nr} entry of the argument list
argv( ) List the argument list argv( ) List the argument list
assertEqual( {exp}, {act}) none assert that {exp} equals {act}
assertFalse( {actual}) none assert that {actual} is false
assertTrue( {actual}) none assert that {actual} is true
asin( {expr}) Float arc sine of {expr} asin( {expr}) Float arc sine of {expr}
atan( {expr}) Float arc tangent of {expr} atan( {expr}) Float arc tangent of {expr}
atan2( {expr}, {expr}) Float arc tangent of {expr1} / {expr2} atan2( {expr}, {expr}) Float arc tangent of {expr1} / {expr2}
@@ -2161,6 +2173,31 @@ argv([{nr}]) The result is the {nr}th file in the argument list of the
< Without the {nr} argument a |List| with the whole |arglist| is < Without the {nr} argument a |List| with the whole |arglist| is
returned. returned.
*assertEqual()*
assertEqual({expected}, {actual})
When {expected} and {actual} are not equal an error message is
added to |v:errors|.
There is no automatic conversion, the String "4" is different
from the Number 4. And the number 4 is different from the
Float 4.0. The value of 'ignorecase' is not used here, case
always matters.
Example: >
assertEqual('foo', 'bar')
< Will result in a string to be added to |v:errors|:
test.vim line 12: Expected 'foo' but got 'bar' ~
assertFalse({actual}) *assertFalse()*
When {actual} is not false an error message is added to
|v:errors|, like with |assertEqual()|..
A value is false when it is zero. When "{actual}" is not a
number the assert fails.
assertTrue({actual}) *assertTrue()*
When {actual} is not true an error message is added to
|v:errors|, like with |assertEqual()|..
A value is true when it is a non-zeron number. When {actual}
is not a number the assert fails.
asin({expr}) *asin()* asin({expr}) *asin()*
Return the arc sine of {expr} measured in radians, as a |Float| Return the arc sine of {expr} measured in radians, as a |Float|
in the range of [-pi/2, pi/2]. in the range of [-pi/2, pi/2].

View File

@@ -376,6 +376,7 @@ static struct vimvar {
{VV_NAME("option_new", VAR_STRING), VV_RO}, {VV_NAME("option_new", VAR_STRING), VV_RO},
{VV_NAME("option_old", VAR_STRING), VV_RO}, {VV_NAME("option_old", VAR_STRING), VV_RO},
{VV_NAME("option_type", VAR_STRING), VV_RO}, {VV_NAME("option_type", VAR_STRING), VV_RO},
{VV_NAME("errors", VAR_LIST), 0},
{VV_NAME("msgpack_types", VAR_DICT), VV_RO}, {VV_NAME("msgpack_types", VAR_DICT), VV_RO},
}; };
@@ -7099,7 +7100,10 @@ static struct fst {
{"argidx", 0, 0, f_argidx}, {"argidx", 0, 0, f_argidx},
{"arglistid", 0, 2, f_arglistid}, {"arglistid", 0, 2, f_arglistid},
{"argv", 0, 1, f_argv}, {"argv", 0, 1, f_argv},
{"asin", 1, 1, f_asin}, /* WJMc */ {"asin", 1, 1, f_asin}, // WJMc
{"assertEqual", 2, 3, f_assertEqual},
{"assertFalse", 1, 2, f_assertFalse},
{"assertTrue", 1, 2, f_assertTrue},
{"atan", 1, 1, f_atan}, {"atan", 1, 1, f_atan},
{"atan2", 2, 2, f_atan2}, {"atan2", 2, 2, f_atan2},
{"browse", 4, 4, f_browse}, {"browse", 4, 4, f_browse},
@@ -7981,6 +7985,86 @@ static void f_argv(typval_T *argvars, typval_T *rettv)
} }
} }
// Add an assert error to v:errors.
static void assertError(garray_T *gap)
{
struct vimvar *vp = &vimvars[VV_ERRORS];
if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL) {
// Make sure v:errors is a list.
set_vim_var_list(VV_ERRORS, list_alloc());
}
list_append_string(vimvars[VV_ERRORS].vv_list,
gap->ga_data, gap->ga_len);
}
static void prepareForAssertError(garray_T *gap)
{
char buf[NUMBUFLEN];
ga_init(gap, 1, 100);
ga_concat(gap, sourcing_name);
vim_snprintf(buf, ARRAY_SIZE(buf), " line %ld", (long)sourcing_lnum);
ga_concat(gap, (char_u *)buf);
}
// "assertEqual(expected, actual[, msg])" function
static void f_assertEqual(typval_T *argvars, typval_T *rettv)
{
garray_T ga;
char_u *tofree;
if (!tv_equal(&argvars[0], &argvars[1], false, false)) {
prepareForAssertError(&ga);
ga_concat(&ga, (char_u *)": Expected ");
tofree = (char_u *) tv2string(&argvars[0], NULL);
ga_concat(&ga, tofree);
xfree(tofree);
ga_concat(&ga, (char_u *)" but got ");
tofree = (char_u *) tv2string(&argvars[1], NULL);
ga_concat(&ga, tofree);
xfree(tofree);
assertError(&ga);
ga_clear(&ga);
}
}
static void assertBool(typval_T *argvars, bool isTrue)
{
int error = (int)false;
garray_T ga;
char_u *tofree;
if (argvars[0].v_type != VAR_NUMBER ||
(get_tv_number_chk(&argvars[0], &error) == 0) == isTrue || error) {
prepareForAssertError(&ga);
ga_concat(&ga, (char_u *)": Expected ");
if (isTrue) {
ga_concat(&ga, (char_u *)"True ");
} else {
ga_concat(&ga, (char_u *)"False ");
}
ga_concat(&ga, (char_u *)"but got ");
tofree = (char_u *) tv2string(&argvars[0], NULL);
ga_concat(&ga, tofree);
xfree(tofree);
assertError(&ga);
ga_clear(&ga);
}
}
// "assertFalse(actual[, msg])" function
static void f_assertFalse(typval_T *argvars, typval_T *rettv)
{
assertBool(argvars, false);
}
// "assertTrue(actual[, msg])" function
static void f_assertTrue(typval_T *argvars, typval_T *rettv)
{
assertBool(argvars, true);
}
/* /*
* "asin()" function * "asin()" function
*/ */

View File

@@ -111,6 +111,7 @@ enum {
VV_OPTION_NEW, VV_OPTION_NEW,
VV_OPTION_OLD, VV_OPTION_OLD,
VV_OPTION_TYPE, VV_OPTION_TYPE,
VV_ERRORS,
VV_MSGPACK_TYPES, VV_MSGPACK_TYPES,
VV_LEN, /* number of v: vars */ VV_LEN, /* number of v: vars */
}; };

View File

@@ -174,6 +174,7 @@ char_u* ga_concat_strings(const garray_T *gap) FUNC_ATTR_NONNULL_RET
} }
/// Concatenate a string to a growarray which contains characters. /// Concatenate a string to a growarray which contains characters.
/// When "s" is NULL does not do anything.
/// ///
/// WARNING: /// WARNING:
/// - Does NOT copy the NUL at the end! /// - Does NOT copy the NUL at the end!
@@ -183,6 +184,10 @@ char_u* ga_concat_strings(const garray_T *gap) FUNC_ATTR_NONNULL_RET
/// @param s /// @param s
void ga_concat(garray_T *gap, const char_u *restrict s) void ga_concat(garray_T *gap, const char_u *restrict s)
{ {
if (s == NULL) {
return;
}
int len = (int)strlen((char *) s); int len = (int)strlen((char *) s);
if (len) { if (len) {
ga_grow(gap, len); ga_grow(gap, len);

View File

@@ -31,6 +31,8 @@ SCRIPTS := test_eval.out \
test_command_count.out \ test_command_count.out \
test_cdo.out \ test_cdo.out \
NEW_TESTS = test_assert.res
SCRIPTS_GUI := test16.out SCRIPTS_GUI := test16.out
@@ -62,9 +64,9 @@ ifdef TESTNUM
SCRIPTS := test$(TESTNUM).out SCRIPTS := test$(TESTNUM).out
endif endif
nongui: nolog $(SCRIPTS) report nongui: nolog $(SCRIPTS) newtests report
gui: nolog $(SCRIPTS) $(SCRIPTS_GUI) report gui: nolog $(SCRIPTS) $(SCRIPTS_GUI) newtests report
.gdbinit: .gdbinit:
echo 'set $$_exitcode = -1\nrun\nif $$_exitcode != -1\n quit\nend' > .gdbinit echo 'set $$_exitcode = -1\nrun\nif $$_exitcode != -1\n quit\nend' > .gdbinit
@@ -91,6 +93,7 @@ RUN_VIM := VIMRUNTIME=$(SCRIPTSOURCE); export VIMRUNTIME; $(TOOL) $(VIMPROG)
clean: clean:
-rm -rf *.out \ -rm -rf *.out \
*.failed \ *.failed \
*.res \
*.rej \ *.rej \
*.orig \ *.orig \
test.log \ test.log \
@@ -147,3 +150,14 @@ test49.out: test49.vim
nolog: nolog:
-rm -f test.log -rm -f test.log
# New style of tests uses Vim script with assert calls. These are easier
# to write and a lot easier to read and debug.
# Limitation: Only works with the +eval feature.
RUN_VIMTEST = VIMRUNTIME=$(SCRIPTSOURCE); export VIMRUNTIME; $(VALGRIND) $(VIMPROG) -u unix.vim -U NONE --noplugin
newtests: $(NEW_TESTS)
%.res: %.vim .gdbinit
$(RUN_VIMTEST) -u runtest.vim $*.vim

View File

@@ -0,0 +1,97 @@
" This script is sourced while editing the .vim file with the tests.
" When the script is successful the .res file will be created.
" Errors are appended to the test.log file.
"
" The test script may contain anything, only functions that start with
" "Test_" are special. These will be invoked and should contain assert
" functions. See test_assert.vim for an example.
"
" It is possible to source other files that contain "Test_" functions. This
" can speed up testing, since Vim does not need to restart. But be careful
" that the tests do not interfere with each other.
"
" If an error cannot be detected properly with an assert function add the
" error to the v:errors list:
" call add(v:errors, 'test foo failed: Cannot find xyz')
"
" If preparation for each Test_ function is needed, define a SetUp function.
" It will be called before each Test_ function.
"
" If cleanup after each Test_ function is needed, define a TearDown function.
" It will be called after each Test_ function.
" Without the +eval feature we can't run these tests, bail out.
if 0
quit!
endif
" Check that the screen size is at least 24 x 80 characters.
if &lines < 24 || &columns < 80
let error = 'Screen size too small! Tests require at least 24 lines with 80 characters'
echoerr error
split test.log
$put =error
w
cquit
endif
" Source the test script. First grab the file name, in case the script
" navigates away.
let testname = expand('%')
source %
" Locate Test_ functions and execute them.
redir @q
function /^Test_
redir END
let tests = split(substitute(@q, 'function \(\k*()\)', '\1', 'g'))
let done = 0
let fail = 0
let errors = []
for test in tests
if exists("*SetUp")
call SetUp()
endif
let done += 1
try
exe 'call ' . test
catch
let fail += 1
call add(v:errors, 'Caught exception in ' . test . ': ' . v:exception . ' @ ' . v:throwpoint)
endtry
if len(v:errors) > 0
let fail += 1
call add(errors, 'Found errors in ' . test . ':')
call extend(errors, v:errors)
let v:errors = []
endif
if exists("*TearDown")
call TearDown()
endif
endfor
if fail == 0
" Success, create the .res file so that make knows it's done.
split %:r.res
write
endif
if len(errors) > 0
" Append errors to test.log
split test.log
call append(line('$'), '')
call append(line('$'), 'From ' . testname . ':')
call append(line('$'), errors)
write
endif
echo 'Executed ' . done . (done > 1 ? ' tests': ' test')
if fail > 0
echo fail . ' FAILED'
endif
qall!

View File

@@ -0,0 +1,19 @@
" Test that the methods used for testing work.
func Test_assert_false()
call assert_false(0)
endfunc
func Test_assert_true()
call assert_true(1)
call assert_true(123)
endfunc
func Test_assert_equal()
let s = 'foo'
call assert_equal('foo', s)
let n = 4
call assert_equal(4, n)
let l = [1, 2, 3]
call assert_equal([1, 2, 3], l)
endfunc

View File

@@ -125,7 +125,7 @@ static int included_patches[] = {
// 947, // 947,
// 946, // 946,
// 945, // 945,
// 944, 944,
// 943, // 943,
// 942, // 942,
// 941, // 941,