# HG changeset patch # User Bram Moolenaar # Date 1565985604 -7200 # Node ID 68ea27d26d5becfa0cc4b734e5c41556841f5313 # Parent 145b465ffd333f243dc49af9a24c4aa54b8d5d45 patch 8.1.1861: only some assert functions can be used as a method commit https://github.com/vim/vim/commit/24278d2407dfbc8d93eb36593cdd006ff5d86f94 Author: Bram Moolenaar Date: Fri Aug 16 21:49:22 2019 +0200 patch 8.1.1861: only some assert functions can be used as a method Problem: Only some assert functions can be used as a method. Solution: Allow using most assert functions as a method. diff --git a/runtime/doc/testing.txt b/runtime/doc/testing.txt --- a/runtime/doc/testing.txt +++ b/runtime/doc/testing.txt @@ -206,6 +206,9 @@ assert_beeps({cmd}) *assert_beeps()* NOT produce a beep or visual bell. Also see |assert_fails()| and |assert-return|. + Can also be used as a |method|: > + GetCmd()->assert_beeps() +< *assert_equal()* assert_equal({expected}, {actual} [, {msg}]) When {expected} and {actual} are not equal an error message is @@ -255,6 +258,9 @@ assert_fails({cmd} [, {error} [, {msg}]] Note that beeping is not considered an error, and some failing commands only beep. Use |assert_beeps()| for those. + Can also be used as a |method|: > + GetCmd()->assert_fails('E99:') + assert_false({actual} [, {msg}]) *assert_false()* When {actual} is not false an error message is added to |v:errors|, like with |assert_equal()|. @@ -264,6 +270,9 @@ assert_false({actual} [, {msg}]) *ass When {msg} is omitted an error in the form "Expected False but got {actual}" is produced. + Can also be used as a |method|: > + GetResult()->assert_false() + assert_inrange({lower}, {upper}, {actual} [, {msg}]) *assert_inrange()* This asserts number and |Float| values. When {actual} is lower than {lower} or higher than {upper} an error message is added @@ -292,6 +301,9 @@ assert_match({pattern}, {actual} [, {msg < Will result in a string to be added to |v:errors|: test.vim line 12: Pattern '^f.*o$' does not match 'foobar' ~ + Can also be used as a |method|: > + getFile()->assert_match('foo.*') +< *assert_notequal()* assert_notequal({expected}, {actual} [, {msg}]) The opposite of `assert_equal()`: add an error message to @@ -307,6 +319,9 @@ assert_notmatch({pattern}, {actual} [, { |v:errors| when {pattern} matches {actual}. Also see |assert-return|. + Can also be used as a |method|: > + getFile()->assert_notmatch('bar.*') + assert_report({msg}) *assert_report()* Report a test failure directly, using {msg}. Always returns one. @@ -320,5 +335,8 @@ assert_true({actual} [, {msg}]) *asse When {msg} is omitted an error in the form "Expected True but got {actual}" is produced. + Can also be used as a |method|: > + GetResult()->assert_true() +< vim:tw=78:ts=8:noet:ft=help:norl: diff --git a/src/evalfunc.c b/src/evalfunc.c --- a/src/evalfunc.c +++ b/src/evalfunc.c @@ -415,6 +415,7 @@ typedef struct // values for f_argtype; zero means it cannot be used as a method #define FEARG_1 1 // base is the first argument #define FEARG_2 2 // base is the second argument +#define FEARG_3 3 // base is the third argument #define FEARG_LAST 9 // base is the last argument static funcentry_T global_functions[] = @@ -434,18 +435,18 @@ static funcentry_T global_functions[] = #ifdef FEAT_FLOAT {"asin", 1, 1, 0, f_asin}, // WJMc #endif - {"assert_beeps", 1, 2, 0, f_assert_beeps}, + {"assert_beeps", 1, 2, FEARG_1, f_assert_beeps}, {"assert_equal", 2, 3, FEARG_2, f_assert_equal}, {"assert_equalfile", 2, 2, 0, f_assert_equalfile}, {"assert_exception", 1, 2, 0, f_assert_exception}, - {"assert_fails", 1, 3, 0, f_assert_fails}, - {"assert_false", 1, 2, 0, f_assert_false}, - {"assert_inrange", 3, 4, 0, f_assert_inrange}, - {"assert_match", 2, 3, 0, f_assert_match}, + {"assert_fails", 1, 3, FEARG_1, f_assert_fails}, + {"assert_false", 1, 2, FEARG_1, f_assert_false}, + {"assert_inrange", 3, 4, FEARG_3, f_assert_inrange}, + {"assert_match", 2, 3, FEARG_2, f_assert_match}, {"assert_notequal", 2, 3, FEARG_2, f_assert_notequal}, - {"assert_notmatch", 2, 3, 0, f_assert_notmatch}, + {"assert_notmatch", 2, 3, FEARG_2, f_assert_notmatch}, {"assert_report", 1, 1, 0, f_assert_report}, - {"assert_true", 1, 2, 0, f_assert_true}, + {"assert_true", 1, 2, FEARG_1, f_assert_true}, #ifdef FEAT_FLOAT {"atan", 1, 1, 0, f_atan}, {"atan2", 2, 2, 0, f_atan2}, @@ -1134,6 +1135,15 @@ call_internal_method( for (i = 1; i < argcount; ++i) argv[i + 1] = argvars[i]; } + else if (global_functions[fi].f_argtype == FEARG_3) + { + // base value goes third + argv[0] = argvars[0]; + argv[1] = argvars[1]; + argv[2] = *basetv; + for (i = 2; i < argcount; ++i) + argv[i + 1] = argvars[i]; + } else { // FEARG_1: base value goes first diff --git a/src/testdir/test_assert.vim b/src/testdir/test_assert.vim --- a/src/testdir/test_assert.vim +++ b/src/testdir/test_assert.vim @@ -3,20 +3,30 @@ func Test_assert_false() call assert_equal(0, assert_false(0)) call assert_equal(0, assert_false(v:false)) + call assert_equal(0, v:false->assert_false()) call assert_equal(1, assert_false(123)) call assert_match("Expected False but got 123", v:errors[0]) call remove(v:errors, 0) + + call assert_equal(1, 123->assert_false()) + call assert_match("Expected False but got 123", v:errors[0]) + call remove(v:errors, 0) endfunc func Test_assert_true() call assert_equal(0, assert_true(1)) call assert_equal(0, assert_true(123)) call assert_equal(0, assert_true(v:true)) + call assert_equal(0, v:true->assert_true()) call assert_equal(1, assert_true(0)) call assert_match("Expected True but got 0", v:errors[0]) call remove(v:errors, 0) + + call assert_equal(1, 0->assert_true()) + call assert_match("Expected True but got 0", v:errors[0]) + call remove(v:errors, 0) endfunc func Test_assert_equal() @@ -141,6 +151,10 @@ func Test_match() call assert_equal(1, assert_match('bar.*foo', 'foobar', 'wrong')) call assert_match('wrong', v:errors[0]) call remove(v:errors, 0) + + call assert_equal(1, 'foobar'->assert_match('bar.*foo', 'wrong')) + call assert_match('wrong', v:errors[0]) + call remove(v:errors, 0) endfunc func Test_notmatch() @@ -150,6 +164,10 @@ func Test_notmatch() call assert_equal(1, assert_notmatch('foo', 'foobar')) call assert_match("Pattern 'foo' does match 'foobar'", v:errors[0]) call remove(v:errors, 0) + + call assert_equal(1, 'foobar'->assert_notmatch('foo')) + call assert_match("Pattern 'foo' does match 'foobar'", v:errors[0]) + call remove(v:errors, 0) endfunc func Test_assert_fail_fails() @@ -164,6 +182,10 @@ func Test_assert_fail_fails() call assert_equal(1, assert_fails('echo', '', 'echo command')) call assert_match("command did not fail: echo command", v:errors[0]) call remove(v:errors, 0) + + call assert_equal(1, 'echo'->assert_fails('', 'echo command')) + call assert_match("command did not fail: echo command", v:errors[0]) + call remove(v:errors, 0) endfunc func Test_assert_fails_in_try_block() @@ -179,6 +201,12 @@ func Test_assert_beeps() call assert_equal(1, assert_beeps('normal 0')) call assert_match("command did not beep: normal 0", v:errors[0]) call remove(v:errors, 0) + + call assert_equal(0, 'normal h'->assert_beeps()) + call assert_equal(1, 'normal 0'->assert_beeps()) + call assert_match("command did not beep: normal 0", v:errors[0]) + call remove(v:errors, 0) + bwipe endfunc @@ -195,6 +223,12 @@ func Test_assert_inrange() call assert_match("Expected range 5 - 7, but got 8", v:errors[0]) call remove(v:errors, 0) + call assert_equal(0, 5->assert_inrange(5, 7)) + call assert_equal(0, 7->assert_inrange(5, 7)) + call assert_equal(1, 8->assert_inrange(5, 7)) + call assert_match("Expected range 5 - 7, but got 8", v:errors[0]) + call remove(v:errors, 0) + call assert_fails('call assert_inrange(1, 1)', 'E119:') if has('float') diff --git a/src/version.c b/src/version.c --- a/src/version.c +++ b/src/version.c @@ -770,6 +770,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ /**/ + 1861, +/**/ 1860, /**/ 1859,