blob: 991a095380411409cf8d0f667a90111d54a654e2 [file] [log] [blame]
Bram Moolenaar0c0590d2017-01-28 13:48:10 +01001" Tests for Unicode manipulations
Bram Moolenaar94722c52023-01-28 19:19:03 +00002
zeertzjq6cac7702022-01-04 18:01:21 +00003source check.vim
Bram Moolenaar2912abb2019-03-29 14:16:42 +01004source view_util.vim
Yasuhiro Matsumoto2bc849f2023-01-10 16:03:08 +00005source screendump.vim
Bram Moolenaar0c0590d2017-01-28 13:48:10 +01006
7" Visual block Insert adjusts for multi-byte char
8func Test_visual_block_insert()
9 new
10 call setline(1, ["aaa", "あああ", "bbb"])
11 exe ":norm! gg0l\<C-V>jjIx\<Esc>"
Bram Moolenaarfc6cceb2022-01-20 12:22:35 +000012 call assert_equal(['axaa', ' xあああ', 'bxbb'], getline(1, '$'))
Bram Moolenaar0c0590d2017-01-28 13:48:10 +010013 bwipeout!
14endfunc
15
Bram Moolenaar70ce8a12021-03-14 19:02:09 +010016" Test for built-in functions strchars() and strcharlen()
Bram Moolenaar0c0590d2017-01-28 13:48:10 +010017func 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]))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +020022 call assert_equal(exp[i][1], inp[i]->strchars(0))
Bram Moolenaar0c0590d2017-01-28 13:48:10 +010023 call assert_equal(exp[i][2], strchars(inp[i], 1))
24 endfor
Bram Moolenaar70ce8a12021-03-14 19:02:09 +010025
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
zeertzjq8cf51372023-05-08 15:31:38 +010032 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:'])
Bram Moolenaar0c0590d2017-01-28 13:48:10 +010036endfunc
37
38" Test for customlist completion
Bram Moolenaar1e115362019-01-09 23:01:02 +010039func CustomComplete1(lead, line, pos)
Bram Moolenaar0c0590d2017-01-28 13:48:10 +010040 return ['あ', 'い']
Bram Moolenaar1e115362019-01-09 23:01:02 +010041endfunc
Bram Moolenaar0c0590d2017-01-28 13:48:10 +010042
Bram Moolenaar1e115362019-01-09 23:01:02 +010043func CustomComplete2(lead, line, pos)
Bram Moolenaar0c0590d2017-01-28 13:48:10 +010044 return ['あたし', 'あたま', 'あたりめ']
Bram Moolenaar1e115362019-01-09 23:01:02 +010045endfunc
Bram Moolenaar0c0590d2017-01-28 13:48:10 +010046
Bram Moolenaar1e115362019-01-09 23:01:02 +010047func CustomComplete3(lead, line, pos)
Bram Moolenaar0c0590d2017-01-28 13:48:10 +010048 return ['Nこ', 'Nん', 'Nぶ']
Bram Moolenaar1e115362019-01-09 23:01:02 +010049endfunc
Bram Moolenaar0c0590d2017-01-28 13:48:10 +010050
51func 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)
zeertzjqa59e0312024-04-15 19:14:38 +020065 delcommand Test1
66 delcommand Test2
67 delcommand Test3
Bram Moolenaar0c0590d2017-01-28 13:48:10 +010068endfunc
69
70" Yank one 3 byte character and check the mark columns.
71func 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("']"))
79endfunc
Bram Moolenaar2912abb2019-03-29 14:16:42 +010080
Bram Moolenaar9d401282019-04-06 13:18:12 +020081func 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))
Bram Moolenaar02b31112019-08-31 22:16:38 +020096 call assert_equal(s, l->list2str(1))
Bram Moolenaar9d401282019-04-06 13:18:12 +020097 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(test_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.
110 let s = "\u3042\u3044"
111 let l = [0x3042, 0x3044]
112
113 let save_encoding = &encoding
114 set encoding=latin1
Bram Moolenaar94722c52023-01-28 19:19:03 +0000115
Bram Moolenaar9d401282019-04-06 13:18:12 +0200116 let lres = str2list(s, 1)
117 let sres = list2str(l, 1)
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100118 call assert_equal([65, 66, 67], str2list("ABC"))
Bram Moolenaar9d401282019-04-06 13:18:12 +0200119
Bram Moolenaar08f41572020-04-20 16:50:00 +0200120 " Try converting a list to a string in latin-1 encoding
121 call assert_equal([1, 2, 3], str2list(list2str([1, 2, 3])))
122
Bram Moolenaar9d401282019-04-06 13:18:12 +0200123 let &encoding = save_encoding
124 call assert_equal(l, lres)
125 call assert_equal(s, sres)
126endfunc
127
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100128func Test_screenchar_utf8()
129 new
130
Bram Moolenaar94722c52023-01-28 19:19:03 +0000131 " 1-cell, with composing characters
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100132 call setline(1, ["ABC\u0308"])
133 redraw
134 call assert_equal([0x0041], screenchars(1, 1))
Bram Moolenaar196b4662019-09-06 21:34:30 +0200135 call assert_equal([0x0042], 1->screenchars(2))
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100136 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
zeertzjq47eec672023-06-01 20:26:55 +0100141 " 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
Bram Moolenaar94722c52023-01-28 19:19:03 +0000154 " 2-cells, with composing characters
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100155 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
Bram Moolenaar48aed082019-03-30 15:44:17 +0100170 call assert_equal([text . ' '], ScreenLines(1, 8))
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100171
172 bwipe!
173endfunc
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100174
Bram Moolenaar08aac3c2020-08-28 21:04:24 +0200175func Test_setcellwidths()
zeertzjq05aacec2024-04-14 18:52:49 +0200176 new
Bram Moolenaar08aac3c2020-08-28 21:04:24 +0200177 call setcellwidths([
178 \ [0x1330, 0x1330, 2],
Bram Moolenaar08aac3c2020-08-28 21:04:24 +0200179 \ [9999, 10000, 1],
Bram Moolenaarb06a6d52020-08-28 23:27:20 +0200180 \ [0x1337, 0x1339, 2],
Bram Moolenaar08aac3c2020-08-28 21:04:24 +0200181 \])
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
K.Takata71933232023-01-20 16:00:55 +0000189 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"))
zeertzjq05aacec2024-04-14 18:52:49 +0200219
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())
Ken Takata539e9b52024-09-05 17:19:34 +0200231 call setcellwidths([])
232 redraw
233 call assert_equal((aw == 'single') ? 10 : 19, wincol())
K.Takata71933232023-01-20 16:00:55 +0000234 endfor
235 set ambiwidth& isprint&
236
Bram Moolenaar08aac3c2020-08-28 21:04:24 +0200237 call setcellwidths([])
238
Bram Moolenaard83392a2022-09-01 12:22:46 +0100239 call assert_fails('call setcellwidths(1)', 'E1211:')
Bram Moolenaar08aac3c2020-08-28 21:04:24 +0200240
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:')
zeertzjq94358a12021-10-20 11:01:15 +0100257
zeertzjq66f65a42024-09-06 16:24:41 +0200258 set listchars=tab:--\\u2192 fillchars=stl:\\u2501
zeertzjq94358a12021-10-20 11:01:15 +0100259 call assert_fails('call setcellwidths([[0x2192, 0x2192, 2]])', 'E834:')
zeertzjq94358a12021-10-20 11:01:15 +0100260 call assert_fails('call setcellwidths([[0x2501, 0x2501, 2]])', 'E835:')
261
zeertzjq66f65a42024-09-06 16:24:41 +0200262 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:')
zeertzjq94358a12021-10-20 11:01:15 +0100267 set listchars&
zeertzjq66f65a42024-09-06 16:24:41 +0200268 call assert_fails('call setcellwidths([])', 'E835:')
zeertzjq94358a12021-10-20 11:01:15 +0100269 set fillchars&
zeertzjq66f65a42024-09-06 16:24:41 +0200270
zeertzjq94358a12021-10-20 11:01:15 +0100271 call setcellwidths([])
zeertzjq66f65a42024-09-06 16:24:41 +0200272 set ambiwidth&
zeertzjq05aacec2024-04-14 18:52:49 +0200273 bwipe!
Bram Moolenaar08aac3c2020-08-28 21:04:24 +0200274endfunc
275
Kota Kato66bb9ae2023-01-17 18:31:56 +0000276func 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([])
294endfunc
295
Yasuhiro Matsumoto2bc849f2023-01-10 16:03:08 +0000296func Test_setcellwidths_dump()
297 CheckRunVimInTerminal
298
299 let lines =<< trim END
300 call setline(1, "\ue5ffDesktop")
301 END
302 call writefile(lines, 'XCellwidths', 'D')
303 let buf = RunVimInTerminal('-S XCellwidths', {'rows': 6})
304 call VerifyScreenDump(buf, 'Test_setcellwidths_dump_1', {})
305
306 call term_sendkeys(buf, ":call setcellwidths([[0xe5ff, 0xe5ff, 2]])\<CR>")
307 call VerifyScreenDump(buf, 'Test_setcellwidths_dump_2', {})
308
309 call StopVimInTerminal(buf)
310endfunc
311
zeertzjq094c4392024-04-18 22:09:37 +0200312" Test setcellwidths() on characters that are not targets of 'ambiwidth'.
mikoto2000e20fa592024-04-17 22:06:54 +0200313func Test_setcellwidths_with_non_ambiwidth_character_dump()
314 CheckRunVimInTerminal
315
316 let lines =<< trim END
317 call setline(1, [repeat("\u279c", 60), repeat("\u279c", 60)])
318 set ambiwidth=single
319 END
320 call writefile(lines, 'XCellwidthsWithNonAmbiwidthCharacter', 'D')
321 let buf = RunVimInTerminal('-S XCellwidthsWithNonAmbiwidthCharacter', {'rows': 6, 'cols': 50})
322 call term_sendkeys(buf, ":call setcellwidths([[0x279c, 0x279c, 1]])\<CR>")
323 call term_sendkeys(buf, ":echo\<CR>")
324 call VerifyScreenDump(buf, 'Test_setcellwidths_with_non_ambiwidth_character_dump_1', {})
325
326 call term_sendkeys(buf, ":call setcellwidths([[0x279c, 0x279c, 2]])\<CR>")
327 call term_sendkeys(buf, ":echo\<CR>")
328 call VerifyScreenDump(buf, 'Test_setcellwidths_with_non_ambiwidth_character_dump_2', {})
329
330 call StopVimInTerminal(buf)
331endfunc
332
zeertzjqa59e0312024-04-15 19:14:38 +0200333" For some reason this test causes Test_customlist_completion() to fail on CI,
334" so run it as the last test.
335func Test_zz_ambiwidth_hl_dump()
336 CheckRunVimInTerminal
337
338 let lines =<< trim END
339 call setline(1, [repeat("\u2103", 60), repeat("\u2103", 60)])
340 set ambiwidth=single cursorline list display=lastline
341 END
342 call writefile(lines, 'XAmbiwidthHl', 'D')
343 let buf = RunVimInTerminal('-S XAmbiwidthHl', {'rows': 6, 'cols': 50})
344 call VerifyScreenDump(buf, 'Test_ambiwidth_hl_dump_1', {})
345
346 call term_sendkeys(buf, ":set ambiwidth=double\<CR>")
347 call term_sendkeys(buf, ":echo\<CR>")
348 call VerifyScreenDump(buf, 'Test_ambiwidth_hl_dump_2', {})
349
350 call term_sendkeys(buf, ":set ambiwidth=single\<CR>")
351 call term_sendkeys(buf, ":echo\<CR>")
352 call VerifyScreenDump(buf, 'Test_ambiwidth_hl_dump_1', {})
353
zeertzjqa59e0312024-04-15 19:14:38 +0200354 call term_sendkeys(buf, ":call setcellwidths([[0x2103, 0x2103, 2]])\<CR>")
355 call term_sendkeys(buf, ":echo\<CR>")
356 call VerifyScreenDump(buf, 'Test_ambiwidth_hl_dump_2', {})
357
358 call term_sendkeys(buf, ":call setcellwidths([[0x2103, 0x2103, 1]])\<CR>")
359 call term_sendkeys(buf, ":echo\<CR>")
360 call VerifyScreenDump(buf, 'Test_ambiwidth_hl_dump_1', {})
zeertzjqa59e0312024-04-15 19:14:38 +0200361
362 call StopVimInTerminal(buf)
363endfunc
364
Bram Moolenaar1cbfc992020-12-02 12:37:37 +0100365func Test_print_overlong()
366 " Text with more composing characters than MB_MAXBYTES.
367 new
368 call setline(1, 'axxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
369 s/x/\=nr2char(1629)/g
370 print
371 bwipe!
372endfunc
373
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100374" vim: shiftwidth=2 sts=2 expandtab