1" Tests for expressions. 2 3func Test_equal() 4 let base = {} 5 func base.method() 6 return 1 7 endfunc 8 func base.other() dict 9 return 1 10 endfunc 11 let instance = copy(base) 12 call assert_true(base.method == instance.method) 13 call assert_true([base.method] == [instance.method]) 14 call assert_true(base.other == instance.other) 15 call assert_true([base.other] == [instance.other]) 16 17 call assert_false(base.method == base.other) 18 call assert_false([base.method] == [base.other]) 19 call assert_false(base.method == instance.other) 20 call assert_false([base.method] == [instance.other]) 21 22 call assert_fails('echo base.method > instance.method') 23endfunc 24 25func Test_version() 26 call assert_true(has('patch-7.4.001')) 27 call assert_true(has('patch-7.4.01')) 28 call assert_true(has('patch-7.4.1')) 29 call assert_true(has('patch-6.9.999')) 30 call assert_true(has('patch-7.1.999')) 31 call assert_true(has('patch-7.4.123')) 32 33 call assert_false(has('patch-7')) 34 call assert_false(has('patch-7.4')) 35 call assert_false(has('patch-7.4.')) 36 call assert_false(has('patch-9.1.0')) 37 call assert_false(has('patch-9.9.1')) 38endfunc 39 40func Test_dict() 41 let d = {'': 'empty', 'a': 'a', 0: 'zero'} 42 call assert_equal('empty', d['']) 43 call assert_equal('a', d['a']) 44 call assert_equal('zero', d[0]) 45 call assert_true(has_key(d, '')) 46 call assert_true(has_key(d, 'a')) 47 48 let d[''] = 'none' 49 let d['a'] = 'aaa' 50 call assert_equal('none', d['']) 51 call assert_equal('aaa', d['a']) 52endfunc 53 54func Test_strgetchar() 55 call assert_equal(char2nr('a'), strgetchar('axb', 0)) 56 call assert_equal(char2nr('x'), strgetchar('axb', 1)) 57 call assert_equal(char2nr('b'), strgetchar('axb', 2)) 58 59 call assert_equal(-1, strgetchar('axb', -1)) 60 call assert_equal(-1, strgetchar('axb', 3)) 61 call assert_equal(-1, strgetchar('', 0)) 62endfunc 63 64func Test_strcharpart() 65 call assert_equal('a', strcharpart('axb', 0, 1)) 66 call assert_equal('x', strcharpart('axb', 1, 1)) 67 call assert_equal('b', strcharpart('axb', 2, 1)) 68 call assert_equal('xb', strcharpart('axb', 1)) 69 70 call assert_equal('', strcharpart('axb', 1, 0)) 71 call assert_equal('', strcharpart('axb', 1, -1)) 72 call assert_equal('', strcharpart('axb', -1, 1)) 73 call assert_equal('', strcharpart('axb', -2, 2)) 74 75 call assert_equal('a', strcharpart('axb', -1, 2)) 76endfunc 77 78func Test_getreg_empty_list() 79 call assert_equal('', getreg('x')) 80 call assert_equal([], getreg('x', 1, 1)) 81 let x = getreg('x', 1, 1) 82 let y = x 83 call add(x, 'foo') 84 call assert_equal(['foo'], y) 85endfunc 86 87func Test_loop_over_null_list() 88 let null_list = test_null_list() 89 for i in null_list 90 call assert_true(0, 'should not get here') 91 endfor 92endfunc 93 94func Test_compare_null_dict() 95 call assert_fails('let x = test_null_dict()[10]') 96 call assert_equal({}, {}) 97 call assert_equal(test_null_dict(), test_null_dict()) 98 call assert_notequal({}, test_null_dict()) 99endfunc 100 101func Test_set_reg_null_list() 102 call setreg('x', test_null_list()) 103endfunc 104 105func Test_special_char() 106 " The failure is only visible using valgrind. 107 call assert_fails('echo "\<C-">') 108endfunc 109 110func Test_option_value() 111 " boolean 112 set bri 113 call assert_equal(1, &bri) 114 set nobri 115 call assert_equal(0, &bri) 116 117 " number 118 set ts=1 119 call assert_equal(1, &ts) 120 set ts=8 121 call assert_equal(8, &ts) 122 123 " string 124 exe "set cedit=\<Esc>" 125 call assert_equal("\<Esc>", &cedit) 126 set cpo= 127 call assert_equal("", &cpo) 128 set cpo=abcdefgi 129 call assert_equal("abcdefgi", &cpo) 130 set cpo&vim 131endfunc 132 133function Test_printf_64bit() 134 if has('num64') 135 call assert_equal("123456789012345", printf('%d', 123456789012345)) 136 endif 137endfunc 138 139function Test_printf_spec_s() 140 " number 141 call assert_equal("1234567890", printf('%s', 1234567890)) 142 143 " string 144 call assert_equal("abcdefgi", printf('%s', "abcdefgi")) 145 146 " float 147 if has('float') 148 call assert_equal("1.23", printf('%s', 1.23)) 149 endif 150 151 " list 152 let value = [1, 'two', ['three', 4]] 153 call assert_equal(string(value), printf('%s', value)) 154 155 " dict 156 let value = {'key1' : 'value1', 'key2' : ['list', 'value'], 'key3' : {'dict' : 'value'}} 157 call assert_equal(string(value), printf('%s', value)) 158 159 " funcref 160 call assert_equal('printf', printf('%s', function('printf'))) 161 162 " partial 163 call assert_equal(string(function('printf', ['%s'])), printf('%s', function('printf', ['%s']))) 164endfunc 165 166function Test_printf_spec_b() 167 call assert_equal("0", printf('%b', 0)) 168 call assert_equal("00001100", printf('%08b', 12)) 169 call assert_equal("11111111", printf('%08b', 0xff)) 170 call assert_equal(" 1111011", printf('%10b', 123)) 171 call assert_equal("0001111011", printf('%010b', 123)) 172 call assert_equal(" 0b1111011", printf('%#10b', 123)) 173 call assert_equal("0B01111011", printf('%#010B', 123)) 174 call assert_equal("1001001100101100000001011010010", printf('%b', 1234567890)) 175 if has('num64') 176 call assert_equal("11100000100100010000110000011011101111101111001", printf('%b', 123456789012345)) 177 call assert_equal("1111111111111111111111111111111111111111111111111111111111111111", printf('%b', -1)) 178 else 179 call assert_equal("11111111111111111111111111111111", printf('%b', -1)) 180 endif 181endfunc 182 183func Test_substitute_expr() 184 let g:val = 'XXX' 185 call assert_equal('XXX', substitute('yyy', 'y*', '\=g:val', '')) 186 call assert_equal('XXX', substitute('yyy', 'y*', {-> g:val}, '')) 187 call assert_equal("-\u1b \uf2-", substitute("-%1b %f2-", '%\(\x\x\)', 188 \ '\=nr2char("0x" . submatch(1))', 'g')) 189 call assert_equal("-\u1b \uf2-", substitute("-%1b %f2-", '%\(\x\x\)', 190 \ {-> nr2char("0x" . submatch(1))}, 'g')) 191 192 call assert_equal('231', substitute('123', '\(.\)\(.\)\(.\)', 193 \ {-> submatch(2) . submatch(3) . submatch(1)}, '')) 194 195 func Recurse() 196 return substitute('yyy', 'y*', {-> g:val}, '') 197 endfunc 198 call assert_equal('--', substitute('xxx', 'x*', {-> '-' . Recurse() . '-'}, '')) 199endfunc 200 201func Test_invalid_submatch() 202 " This was causing invalid memory access in Vim-7.4.2232 and older 203 call assert_fails("call substitute('x', '.', {-> submatch(10)}, '')", 'E935:') 204endfunc 205 206func Test_substitute_expr_arg() 207 call assert_equal('123456789-123456789=', substitute('123456789', 208 \ '\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)', 209 \ {m -> m[0] . '-' . m[1] . m[2] . m[3] . m[4] . m[5] . m[6] . m[7] . m[8] . m[9] . '='}, '')) 210 211 call assert_equal('123456-123456=789', substitute('123456789', 212 \ '\(.\)\(.\)\(.\)\(a*\)\(n*\)\(.\)\(.\)\(.\)\(x*\)', 213 \ {m -> m[0] . '-' . m[1] . m[2] . m[3] . m[4] . m[5] . m[6] . m[7] . m[8] . m[9] . '='}, '')) 214 215 call assert_equal('123456789-123456789x=', substitute('123456789', 216 \ '\(.\)\(.\)\(.*\)', 217 \ {m -> m[0] . '-' . m[1] . m[2] . m[3] . 'x' . m[4] . m[5] . m[6] . m[7] . m[8] . m[9] . '='}, '')) 218 219 call assert_fails("call substitute('xxx', '.', {m -> string(add(m, 'x'))}, '')", 'E742:') 220 call assert_fails("call substitute('xxx', '.', {m -> string(insert(m, 'x'))}, '')", 'E742:') 221 call assert_fails("call substitute('xxx', '.', {m -> string(extend(m, ['x']))}, '')", 'E742:') 222 call assert_fails("call substitute('xxx', '.', {m -> string(remove(m, 1))}, '')", 'E742:') 223endfunc 224 225func Test_function_with_funcref() 226 let s:f = function('type') 227 let s:fref = function(s:f) 228 call assert_equal(v:t_string, s:fref('x')) 229 call assert_fails("call function('s:f')", 'E700:') 230endfunc 231 232func Test_funcref() 233 func! One() 234 return 1 235 endfunc 236 let OneByName = function('One') 237 let OneByRef = funcref('One') 238 func! One() 239 return 2 240 endfunc 241 call assert_equal(2, OneByName()) 242 call assert_equal(1, OneByRef()) 243 let OneByRef = funcref('One') 244 call assert_equal(2, OneByRef()) 245 call assert_fails('echo funcref("{")', 'E475:') 246endfunc 247 248func Test_setmatches() 249 hi def link 1 Comment 250 hi def link 2 PreProc 251 let set = [{"group": 1, "pattern": 2, "id": 3, "priority": 4}] 252 let exp = [{"group": '1', "pattern": '2', "id": 3, "priority": 4}] 253 if has('conceal') 254 let set[0]['conceal'] = 5 255 let exp[0]['conceal'] = '5' 256 endif 257 call setmatches(set) 258 call assert_equal(exp, getmatches()) 259endfunc 260