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