1" Test binding arguments to a Funcref. 2 3func MyFunc(arg1, arg2, arg3) 4 return a:arg1 . '/' . a:arg2 . '/' . a:arg3 5endfunc 6 7func MySort(up, one, two) 8 if a:one == a:two 9 return 0 10 endif 11 if a:up 12 return a:one > a:two ? 1 : -1 13 endif 14 return a:one < a:two ? 1 : -1 15endfunc 16 17func Test_partial_args() 18 let Cb = function('MyFunc', ["foo", "bar"]) 19 20 call Cb("zzz") 21 call assert_equal("foo/bar/xxx", Cb("xxx")) 22 call assert_equal("foo/bar/yyy", call(Cb, ["yyy"])) 23 let Cb2 = function(Cb) 24 call assert_equal("foo/bar/zzz", Cb2("zzz")) 25 let Cb3 = function(Cb, ["www"]) 26 call assert_equal("foo/bar/www", Cb3()) 27 28 let Cb = function('MyFunc', []) 29 call assert_equal("a/b/c", Cb("a", "b", "c")) 30 let Cb2 = function(Cb, []) 31 call assert_equal("a/b/d", Cb2("a", "b", "d")) 32 let Cb3 = function(Cb, ["a", "b"]) 33 call assert_equal("a/b/e", Cb3("e")) 34 35 let Sort = function('MySort', [1]) 36 call assert_equal([1, 2, 3], sort([3, 1, 2], Sort)) 37 let Sort = function('MySort', [0]) 38 call assert_equal([3, 2, 1], sort([3, 1, 2], Sort)) 39endfunc 40 41func MyDictFunc(arg1, arg2) dict 42 return self.name . '/' . a:arg1 . '/' . a:arg2 43endfunc 44 45func Test_partial_dict() 46 let dict = {'name': 'hello'} 47 let Cb = function('MyDictFunc', ["foo", "bar"], dict) 48 call assert_equal("hello/foo/bar", Cb()) 49 call assert_fails('Cb("xxx")', 'E492:') 50 51 let Cb = function('MyDictFunc', ["foo"], dict) 52 call assert_equal("hello/foo/xxx", Cb("xxx")) 53 call assert_fails('Cb()', 'E492:') 54 55 let Cb = function('MyDictFunc', [], dict) 56 call assert_equal("hello/ttt/xxx", Cb("ttt", "xxx")) 57 call assert_fails('Cb("yyy")', 'E492:') 58 59 let Cb = function('MyDictFunc', dict) 60 call assert_equal("hello/xxx/yyy", Cb("xxx", "yyy")) 61 call assert_fails('Cb("fff")', 'E492:') 62 63 let dict = {"tr": function('tr', ['hello', 'h', 'H'])} 64 call assert_equal("Hello", dict.tr()) 65endfunc 66 67func Test_partial_implicit() 68 let dict = {'name': 'foo'} 69 func dict.MyFunc(arg) dict 70 return self.name . '/' . a:arg 71 endfunc 72 73 call assert_equal('foo/bar', dict.MyFunc('bar')) 74 75 call assert_fails('let func = dict.MyFunc', 'E704:') 76 let Func = dict.MyFunc 77 call assert_equal('foo/aaa', Func('aaa')) 78 79 let Func = function(dict.MyFunc, ['bbb']) 80 call assert_equal('foo/bbb', Func()) 81endfunc 82 83fun InnerCall(funcref) 84 return a:funcref 85endfu 86 87fun OuterCall() 88 let opt = { 'func' : function('sin') } 89 call InnerCall(opt.func) 90endfu 91 92func Test_function_in_dict() 93 call OuterCall() 94endfunc 95 96function! s:cache_clear() dict 97 return self.name 98endfunction 99 100func Test_script_function_in_dict() 101 let s:obj = {'name': 'foo'} 102 let s:obj2 = {'name': 'bar'} 103 104 let s:obj['clear'] = function('s:cache_clear') 105 106 call assert_equal('foo', s:obj.clear()) 107 let F = s:obj.clear 108 call assert_equal('foo', F()) 109 call assert_equal('foo', call(s:obj.clear, [], s:obj)) 110 call assert_equal('bar', call(s:obj.clear, [], s:obj2)) 111 112 let s:obj2['clear'] = function('s:cache_clear') 113 call assert_equal('bar', s:obj2.clear()) 114 let B = s:obj2.clear 115 call assert_equal('bar', B()) 116endfunc 117 118function! s:cache_arg(arg) dict 119 let s:result = self.name . '/' . a:arg 120 return s:result 121endfunction 122 123func Test_script_function_in_dict_arg() 124 let s:obj = {'name': 'foo'} 125 let s:obj['clear'] = function('s:cache_arg') 126 127 call assert_equal('foo/bar', s:obj.clear('bar')) 128 let F = s:obj.clear 129 let s:result = '' 130 call assert_equal('foo/bar', F('bar')) 131 call assert_equal('foo/bar', s:result) 132 133 let s:obj['clear'] = function('s:cache_arg', ['bar']) 134 call assert_equal('foo/bar', s:obj.clear()) 135 let s:result = '' 136 call s:obj.clear() 137 call assert_equal('foo/bar', s:result) 138 139 let F = s:obj.clear 140 call assert_equal('foo/bar', F()) 141 let s:result = '' 142 call F() 143 call assert_equal('foo/bar', s:result) 144 145 call assert_equal('foo/bar', call(s:obj.clear, [], s:obj)) 146endfunc 147 148func Test_partial_exists() 149 let F = function('MyFunc') 150 call assert_true(exists('*F')) 151 let lF = [F] 152 call assert_true(exists('*lF[0]')) 153 154 let F = function('MyFunc', ['arg']) 155 call assert_true(exists('*F')) 156 let lF = [F] 157 call assert_true(exists('*lF[0]')) 158endfunc 159 160func Test_partial_string() 161 let F = function('MyFunc') 162 call assert_equal("function('MyFunc')", string(F)) 163 let F = function('MyFunc', ['foo']) 164 call assert_equal("function('MyFunc', ['foo'])", string(F)) 165 let F = function('MyFunc', ['foo', 'bar']) 166 call assert_equal("function('MyFunc', ['foo', 'bar'])", string(F)) 167 let d = {'one': 1} 168 let F = function('MyFunc', d) 169 call assert_equal("function('MyFunc', {'one': 1})", string(F)) 170 let F = function('MyFunc', ['foo'], d) 171 call assert_equal("function('MyFunc', ['foo'], {'one': 1})", string(F)) 172endfunc 173