test_utf8.vim (12183B)
1 " Tests for Unicode manipulations 2 3 source check.vim 4 source view_util.vim 5 source screendump.vim 6 7 " Visual block Insert adjusts for multi-byte char 8 func Test_visual_block_insert() 9 new 10 call setline(1, ["aaa", "あああ", "bbb"]) 11 exe ":norm! gg0l\<C-V>jjIx\<Esc>" 12 call assert_equal(['axaa', ' xあああ', 'bxbb'], getline(1, '$')) 13 bwipeout! 14 endfunc 15 16 " Test for built-in functions strchars() and strcharlen() 17 func Test_strchars() 18 let inp = ["a", "あいa", "A\u20dd", "A\u20dd\u20dd", "\u20dd"] 19 let exp = [[1, 1, 1], [3, 3, 3], [2, 2, 1], [3, 3, 1], [1, 1, 1]] 20 for i in range(len(inp)) 21 call assert_equal(exp[i][0], strchars(inp[i])) 22 call assert_equal(exp[i][1], inp[i]->strchars(0)) 23 call assert_equal(exp[i][2], strchars(inp[i], 1)) 24 endfor 25 26 let exp = [1, 3, 1, 1, 1] 27 for i in range(len(inp)) 28 call assert_equal(exp[i], inp[i]->strcharlen()) 29 call assert_equal(exp[i], strcharlen(inp[i])) 30 endfor 31 32 call assert_fails("call strchars('abc', 2)", ['E1023:', 'E1023:']) 33 call assert_fails("call strchars('abc', -1)", ['E1023:', 'E1023:']) 34 call assert_fails("call strchars('abc', {})", ['E728:', 'E728:']) 35 call assert_fails("call strchars('abc', [])", ['E745:', 'E745:']) 36 endfunc 37 38 " Test for customlist completion 39 func CustomComplete1(lead, line, pos) 40 return ['あ', 'い'] 41 endfunc 42 43 func CustomComplete2(lead, line, pos) 44 return ['あたし', 'あたま', 'あたりめ'] 45 endfunc 46 47 func CustomComplete3(lead, line, pos) 48 return ['Nこ', 'Nん', 'Nぶ'] 49 endfunc 50 51 func Test_customlist_completion() 52 command -nargs=1 -complete=customlist,CustomComplete1 Test1 echo 53 call feedkeys(":Test1 \<C-L>\<C-B>\"\<CR>", 'itx') 54 call assert_equal('"Test1 ', getreg(':')) 55 56 command -nargs=1 -complete=customlist,CustomComplete2 Test2 echo 57 call feedkeys(":Test2 \<C-L>\<C-B>\"\<CR>", 'itx') 58 call assert_equal('"Test2 あた', getreg(':')) 59 60 command -nargs=1 -complete=customlist,CustomComplete3 Test3 echo 61 call feedkeys(":Test3 \<C-L>\<C-B>\"\<CR>", 'itx') 62 call assert_equal('"Test3 N', getreg(':')) 63 64 call garbagecollect(1) 65 delcommand Test1 66 delcommand Test2 67 delcommand Test3 68 endfunc 69 70 " Yank one 3 byte character and check the mark columns. 71 func Test_getvcol() 72 new 73 call setline(1, "x\u2500x") 74 normal 0lvy 75 call assert_equal(2, col("'[")) 76 call assert_equal(4, col("']")) 77 call assert_equal(2, virtcol("'[")) 78 call assert_equal(2, virtcol("']")) 79 endfunc 80 81 func Test_list2str_str2list_utf8() 82 " One Unicode codepoint 83 let s = "\u3042\u3044" 84 let l = [0x3042, 0x3044] 85 call assert_equal(l, str2list(s, 1)) 86 call assert_equal(s, list2str(l, 1)) 87 if &enc ==# 'utf-8' 88 call assert_equal(str2list(s), str2list(s, 1)) 89 call assert_equal(list2str(l), list2str(l, 1)) 90 endif 91 92 " With composing characters 93 let s = "\u304b\u3099\u3044" 94 let l = [0x304b, 0x3099, 0x3044] 95 call assert_equal(l, str2list(s, 1)) 96 call assert_equal(s, l->list2str(1)) 97 if &enc ==# 'utf-8' 98 call assert_equal(str2list(s), str2list(s, 1)) 99 call assert_equal(list2str(l), list2str(l, 1)) 100 endif 101 102 " Null list is the same as an empty list 103 call assert_equal('', list2str([])) 104 call assert_equal('', list2str(v:_null_list)) 105 endfunc 106 107 func Test_list2str_str2list_latin1() 108 " When 'encoding' is not multi-byte can still get utf-8 string. 109 " But we need to create the utf-8 string while 'encoding' is utf-8. 110 let s = "\u3042\u3044" 111 let l = [0x3042, 0x3044] 112 113 let save_encoding = &encoding 114 " set encoding=latin1 115 116 let lres = str2list(s, 1) 117 let sres = list2str(l, 1) 118 call assert_equal([65, 66, 67], str2list("ABC")) 119 120 " Try converting a list to a string in latin-1 encoding 121 call assert_equal([1, 2, 3], str2list(list2str([1, 2, 3]))) 122 123 let &encoding = save_encoding 124 call assert_equal(l, lres) 125 call assert_equal(s, sres) 126 endfunc 127 128 func Test_screenchar_utf8() 129 new 130 131 " 1-cell, with composing characters 132 call setline(1, ["ABC\u0308"]) 133 redraw 134 call assert_equal([0x0041], screenchars(1, 1)) 135 call assert_equal([0x0042], 1->screenchars(2)) 136 call assert_equal([0x0043, 0x0308], screenchars(1, 3)) 137 call assert_equal("A", screenstring(1, 1)) 138 call assert_equal("B", screenstring(1, 2)) 139 call assert_equal("C\u0308", screenstring(1, 3)) 140 141 " 1-cell, with 6 composing characters 142 set maxcombine=6 143 call setline(1, ["ABC" .. repeat("\u0308", 6)]) 144 redraw 145 call assert_equal([0x0041], screenchars(1, 1)) 146 call assert_equal([0x0042], 1->screenchars(2)) 147 " This should not use uninitialized memory 148 call assert_equal([0x0043] + repeat([0x0308], 6), screenchars(1, 3)) 149 call assert_equal("A", screenstring(1, 1)) 150 call assert_equal("B", screenstring(1, 2)) 151 call assert_equal("C" .. repeat("\u0308", 6), screenstring(1, 3)) 152 set maxcombine& 153 154 " 2-cells, with composing characters 155 let text = "\u3042\u3044\u3046\u3099" 156 call setline(1, text) 157 redraw 158 call assert_equal([0x3042], screenchars(1, 1)) 159 call assert_equal([0], screenchars(1, 2)) 160 call assert_equal([0x3044], screenchars(1, 3)) 161 call assert_equal([0], screenchars(1, 4)) 162 call assert_equal([0x3046, 0x3099], screenchars(1, 5)) 163 164 call assert_equal("\u3042", screenstring(1, 1)) 165 call assert_equal("", screenstring(1, 2)) 166 call assert_equal("\u3044", screenstring(1, 3)) 167 call assert_equal("", screenstring(1, 4)) 168 call assert_equal("\u3046\u3099", screenstring(1, 5)) 169 170 call assert_equal([text . ' '], ScreenLines(1, 8)) 171 172 bwipe! 173 endfunc 174 175 func Test_setcellwidths() 176 new 177 call setcellwidths([ 178 \ [0x1330, 0x1330, 2], 179 \ [9999, 10000, 1], 180 \ [0x1337, 0x1339, 2], 181 \]) 182 183 call assert_equal(2, strwidth("\u1330")) 184 call assert_equal(1, strwidth("\u1336")) 185 call assert_equal(2, strwidth("\u1337")) 186 call assert_equal(2, strwidth("\u1339")) 187 call assert_equal(1, strwidth("\u133a")) 188 189 for aw in ['single', 'double'] 190 exe 'set ambiwidth=' . aw 191 " Handle \u0080 to \u009F as control chars even on MS-Windows. 192 set isprint=@,161-255 193 194 call setcellwidths([]) 195 " Control chars 196 call assert_equal(4, strwidth("\u0081")) 197 call assert_equal(6, strwidth("\uFEFF")) 198 " Ambiguous width chars 199 call assert_equal((aw == 'single') ? 1 : 2, strwidth("\u00A1")) 200 call assert_equal((aw == 'single') ? 1 : 2, strwidth("\u2010")) 201 202 call setcellwidths([[0x81, 0x81, 1], [0xA1, 0xA1, 1], 203 \ [0x2010, 0x2010, 1], [0xFEFF, 0xFEFF, 1]]) 204 " Control chars 205 call assert_equal(4, strwidth("\u0081")) 206 call assert_equal(6, strwidth("\uFEFF")) 207 " Ambiguous width chars 208 call assert_equal(1, strwidth("\u00A1")) 209 call assert_equal(1, strwidth("\u2010")) 210 211 call setcellwidths([[0x81, 0x81, 2], [0xA1, 0xA1, 2], 212 \ [0x2010, 0x2010, 2], [0xFEFF, 0xFEFF, 2]]) 213 " Control chars 214 call assert_equal(4, strwidth("\u0081")) 215 call assert_equal(6, strwidth("\uFEFF")) 216 " Ambiguous width chars 217 call assert_equal(2, strwidth("\u00A1")) 218 call assert_equal(2, strwidth("\u2010")) 219 220 call setcellwidths([]) 221 call setline(1, repeat("\u2103", 10)) 222 normal! $ 223 redraw 224 call assert_equal((aw == 'single') ? 10 : 19, wincol()) 225 call setcellwidths([[0x2103, 0x2103, 1]]) 226 redraw 227 call assert_equal(10, wincol()) 228 call setcellwidths([[0x2103, 0x2103, 2]]) 229 redraw 230 call assert_equal(19, wincol()) 231 call setcellwidths([]) 232 redraw 233 call assert_equal((aw == 'single') ? 10 : 19, wincol()) 234 endfor 235 set ambiwidth& isprint& 236 237 call setcellwidths([]) 238 239 call assert_fails('call setcellwidths(1)', 'E714:') 240 241 call assert_fails('call setcellwidths([1, 2, 0])', 'E1109:') 242 243 call assert_fails('call setcellwidths([[0x101]])', 'E1110:') 244 call assert_fails('call setcellwidths([[0x101, 0x102]])', 'E1110:') 245 call assert_fails('call setcellwidths([[0x101, 0x102, 1, 4]])', 'E1110:') 246 call assert_fails('call setcellwidths([["a"]])', 'E1110:') 247 248 call assert_fails('call setcellwidths([[0x102, 0x101, 1]])', 'E1111:') 249 250 call assert_fails('call setcellwidths([[0x101, 0x102, 0]])', 'E1112:') 251 call assert_fails('call setcellwidths([[0x101, 0x102, 3]])', 'E1112:') 252 253 call assert_fails('call setcellwidths([[0x111, 0x122, 1], [0x115, 0x116, 2]])', 'E1113:') 254 call assert_fails('call setcellwidths([[0x111, 0x122, 1], [0x122, 0x123, 2]])', 'E1113:') 255 256 call assert_fails('call setcellwidths([[0x33, 0x44, 2]])', 'E1114:') 257 258 set listchars=tab:--\\u2192 fillchars=stl:\\u2501 259 call assert_fails('call setcellwidths([[0x2192, 0x2192, 2]])', 'E834:') 260 call assert_fails('call setcellwidths([[0x2501, 0x2501, 2]])', 'E835:') 261 262 call setcellwidths([[0x201c, 0x201d, 1]]) 263 set listchars& fillchars& ambiwidth=double 264 265 set listchars=nbsp:\\u201c fillchars=vert:\\u201d 266 call assert_fails('call setcellwidths([])', 'E834:') 267 set listchars& 268 call assert_fails('call setcellwidths([])', 'E835:') 269 set fillchars& 270 271 call setcellwidths([]) 272 set ambiwidth& 273 bwipe! 274 endfunc 275 276 func Test_getcellwidths() 277 call setcellwidths([]) 278 call assert_equal([], getcellwidths()) 279 280 let widthlist = [ 281 \ [0x1330, 0x1330, 2], 282 \ [9999, 10000, 1], 283 \ [0x1337, 0x1339, 2], 284 \] 285 let widthlistsorted = [ 286 \ [0x1330, 0x1330, 2], 287 \ [0x1337, 0x1339, 2], 288 \ [9999, 10000, 1], 289 \] 290 call setcellwidths(widthlist) 291 call assert_equal(widthlistsorted, getcellwidths()) 292 293 call setcellwidths([]) 294 endfunc 295 296 func Test_setcellwidths_dump() 297 CheckScreendump 298 CheckRunVimInTerminal 299 300 let lines =<< trim END 301 call setline(1, "\ue5ffDesktop") 302 END 303 call writefile(lines, 'XCellwidths', 'D') 304 let buf = RunVimInTerminal('-S XCellwidths', {'rows': 6}) 305 call VerifyScreenDump(buf, 'Test_setcellwidths_dump_1', {}) 306 307 call term_sendkeys(buf, ":call setcellwidths([[0xe5ff, 0xe5ff, 2]])\<CR>") 308 call VerifyScreenDump(buf, 'Test_setcellwidths_dump_2', {}) 309 310 call StopVimInTerminal(buf) 311 endfunc 312 313 " Test setcellwidths() on characters that are not targets of 'ambiwidth'. 314 func Test_setcellwidths_with_non_ambiwidth_character_dump() 315 CheckScreendump 316 CheckRunVimInTerminal 317 318 let lines =<< trim END 319 call setline(1, [repeat("\u279c", 60), repeat("\u279c", 60)]) 320 set ambiwidth=single 321 END 322 call writefile(lines, 'XCellwidthsWithNonAmbiwidthCharacter', 'D') 323 let buf = RunVimInTerminal('-S XCellwidthsWithNonAmbiwidthCharacter', {'rows': 6, 'cols': 50}) 324 call term_sendkeys(buf, ":call setcellwidths([[0x279c, 0x279c, 1]])\<CR>") 325 call term_sendkeys(buf, ":echo\<CR>") 326 call VerifyScreenDump(buf, 'Test_setcellwidths_with_non_ambiwidth_character_dump_1', {}) 327 328 call term_sendkeys(buf, ":call setcellwidths([[0x279c, 0x279c, 2]])\<CR>") 329 call term_sendkeys(buf, ":echo\<CR>") 330 call VerifyScreenDump(buf, 'Test_setcellwidths_with_non_ambiwidth_character_dump_2', {}) 331 332 call StopVimInTerminal(buf) 333 endfunc 334 335 " For some reason this test causes Test_customlist_completion() to fail on CI, 336 " so run it as the last test. 337 func Test_zz_ambiwidth_hl_dump() 338 CheckScreendump 339 CheckRunVimInTerminal 340 341 let lines =<< trim END 342 call setline(1, [repeat("\u2103", 60), repeat("\u2103", 60)]) 343 set ambiwidth=single cursorline list display=lastline 344 END 345 call writefile(lines, 'XAmbiwidthHl', 'D') 346 let buf = RunVimInTerminal('-S XAmbiwidthHl', {'rows': 6, 'cols': 50}) 347 call VerifyScreenDump(buf, 'Test_ambiwidth_hl_dump_1', {}) 348 349 call term_sendkeys(buf, ":set ambiwidth=double\<CR>") 350 call term_sendkeys(buf, ":echo\<CR>") 351 call VerifyScreenDump(buf, 'Test_ambiwidth_hl_dump_2', {}) 352 353 call term_sendkeys(buf, ":set ambiwidth=single\<CR>") 354 call term_sendkeys(buf, ":echo\<CR>") 355 call VerifyScreenDump(buf, 'Test_ambiwidth_hl_dump_1', {}) 356 357 call term_sendkeys(buf, ":call setcellwidths([[0x2103, 0x2103, 2]])\<CR>") 358 call term_sendkeys(buf, ":echo\<CR>") 359 call VerifyScreenDump(buf, 'Test_ambiwidth_hl_dump_2', {}) 360 361 call term_sendkeys(buf, ":call setcellwidths([[0x2103, 0x2103, 1]])\<CR>") 362 call term_sendkeys(buf, ":echo\<CR>") 363 call VerifyScreenDump(buf, 'Test_ambiwidth_hl_dump_1', {}) 364 365 call StopVimInTerminal(buf) 366 endfunc 367 368 func Test_print_overlong() 369 " Text with more composing characters than MB_MAXBYTES. 370 new 371 call setline(1, 'axxxxxxxxxxxxxxxxxxxxxxxxxxxxxx') 372 s/x/\=nr2char(1629)/g 373 print 374 bwipe! 375 endfunc 376 377 " vim: shiftwidth=2 sts=2 expandtab