]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 8.1.1861: only some assert functions can be used as a method v8.1.1861
authorBram Moolenaar <Bram@vim.org>
Fri, 16 Aug 2019 19:49:22 +0000 (21:49 +0200)
committerBram Moolenaar <Bram@vim.org>
Fri, 16 Aug 2019 19:49:22 +0000 (21:49 +0200)
Problem:    Only some assert functions can be used as a method.
Solution:   Allow using most assert functions as a method.

runtime/doc/testing.txt
src/evalfunc.c
src/testdir/test_assert.vim
src/version.c

index b51a1e1d8c2e45b369545ac391c616d9ef079e18..0dbe014f21d73e1792fd6f9003db8b2c67d1a1ea 100644 (file)
@@ -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}]])                   *assert_fails()*
                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}])                            *assert_false()*
                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} [, {msg}])
                |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}])                             *assert_true()*
                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:
index e7f870c6fb8bb7489f19d53480b75b649e21a261..814499331dd4659ca83450568c916a6d83ed6715 100644 (file)
@@ -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
index 900710b893fef11610c61e10a754b711c8ad870a..65a0ee41a777c59661fb078303dbd1297c90f67c 100644 (file)
@@ -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')
index cf96d640fd2b3e8c2f5b117946ef92288aeb4ace..6af44429ae12c9801520ee6b60f2c437e1dd03c7 100644 (file)
@@ -769,6 +769,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1861,
 /**/
     1860,
 /**/