blob: 0f46240db5aba1fa036781fec050c137c9be6ca2 [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())
K.Takata71933232023-01-20 16:00:55 +0000231 endfor
232 set ambiwidth& isprint&
233
Bram Moolenaar08aac3c2020-08-28 21:04:24 +0200234 call setcellwidths([])
235
Bram Moolenaard83392a2022-09-01 12:22:46 +0100236 call assert_fails('call setcellwidths(1)', 'E1211:')
Bram Moolenaar08aac3c2020-08-28 21:04:24 +0200237
238 call assert_fails('call setcellwidths([1, 2, 0])', 'E1109:')
239
240 call assert_fails('call setcellwidths([[0x101]])', 'E1110:')
241 call assert_fails('call setcellwidths([[0x101, 0x102]])', 'E1110:')
242 call assert_fails('call setcellwidths([[0x101, 0x102, 1, 4]])', 'E1110:')
243 call assert_fails('call setcellwidths([["a"]])', 'E1110:')
244
245 call assert_fails('call setcellwidths([[0x102, 0x101, 1]])', 'E1111:')
246
247 call assert_fails('call setcellwidths([[0x101, 0x102, 0]])', 'E1112:')
248 call assert_fails('call setcellwidths([[0x101, 0x102, 3]])', 'E1112:')
249
250 call assert_fails('call setcellwidths([[0x111, 0x122, 1], [0x115, 0x116, 2]])', 'E1113:')
251 call assert_fails('call setcellwidths([[0x111, 0x122, 1], [0x122, 0x123, 2]])', 'E1113:')
252
253 call assert_fails('call setcellwidths([[0x33, 0x44, 2]])', 'E1114:')
zeertzjq94358a12021-10-20 11:01:15 +0100254
255 set listchars=tab:--\\u2192
256 call assert_fails('call setcellwidths([[0x2192, 0x2192, 2]])', 'E834:')
257
258 set fillchars=stl:\\u2501
259 call assert_fails('call setcellwidths([[0x2501, 0x2501, 2]])', 'E835:')
260
261 set listchars&
262 set fillchars&
263 call setcellwidths([])
zeertzjq05aacec2024-04-14 18:52:49 +0200264 bwipe!
Bram Moolenaar08aac3c2020-08-28 21:04:24 +0200265endfunc
266
Kota Kato66bb9ae2023-01-17 18:31:56 +0000267func Test_getcellwidths()
268 call setcellwidths([])
269 call assert_equal([], getcellwidths())
270
271 let widthlist = [
272 \ [0x1330, 0x1330, 2],
273 \ [9999, 10000, 1],
274 \ [0x1337, 0x1339, 2],
275 \]
276 let widthlistsorted = [
277 \ [0x1330, 0x1330, 2],
278 \ [0x1337, 0x1339, 2],
279 \ [9999, 10000, 1],
280 \]
281 call setcellwidths(widthlist)
282 call assert_equal(widthlistsorted, getcellwidths())
283
284 call setcellwidths([])
285endfunc
286
Yasuhiro Matsumoto2bc849f2023-01-10 16:03:08 +0000287func Test_setcellwidths_dump()
288 CheckRunVimInTerminal
289
290 let lines =<< trim END
291 call setline(1, "\ue5ffDesktop")
292 END
293 call writefile(lines, 'XCellwidths', 'D')
294 let buf = RunVimInTerminal('-S XCellwidths', {'rows': 6})
295 call VerifyScreenDump(buf, 'Test_setcellwidths_dump_1', {})
296
297 call term_sendkeys(buf, ":call setcellwidths([[0xe5ff, 0xe5ff, 2]])\<CR>")
298 call VerifyScreenDump(buf, 'Test_setcellwidths_dump_2', {})
299
300 call StopVimInTerminal(buf)
301endfunc
302
zeertzjq094c4392024-04-18 22:09:37 +0200303" Test setcellwidths() on characters that are not targets of 'ambiwidth'.
mikoto2000e20fa592024-04-17 22:06:54 +0200304func Test_setcellwidths_with_non_ambiwidth_character_dump()
305 CheckRunVimInTerminal
306
307 let lines =<< trim END
308 call setline(1, [repeat("\u279c", 60), repeat("\u279c", 60)])
309 set ambiwidth=single
310 END
311 call writefile(lines, 'XCellwidthsWithNonAmbiwidthCharacter', 'D')
312 let buf = RunVimInTerminal('-S XCellwidthsWithNonAmbiwidthCharacter', {'rows': 6, 'cols': 50})
313 call term_sendkeys(buf, ":call setcellwidths([[0x279c, 0x279c, 1]])\<CR>")
314 call term_sendkeys(buf, ":echo\<CR>")
315 call VerifyScreenDump(buf, 'Test_setcellwidths_with_non_ambiwidth_character_dump_1', {})
316
317 call term_sendkeys(buf, ":call setcellwidths([[0x279c, 0x279c, 2]])\<CR>")
318 call term_sendkeys(buf, ":echo\<CR>")
319 call VerifyScreenDump(buf, 'Test_setcellwidths_with_non_ambiwidth_character_dump_2', {})
320
321 call StopVimInTerminal(buf)
322endfunc
323
zeertzjqa59e0312024-04-15 19:14:38 +0200324" For some reason this test causes Test_customlist_completion() to fail on CI,
325" so run it as the last test.
326func Test_zz_ambiwidth_hl_dump()
327 CheckRunVimInTerminal
328
329 let lines =<< trim END
330 call setline(1, [repeat("\u2103", 60), repeat("\u2103", 60)])
331 set ambiwidth=single cursorline list display=lastline
332 END
333 call writefile(lines, 'XAmbiwidthHl', 'D')
334 let buf = RunVimInTerminal('-S XAmbiwidthHl', {'rows': 6, 'cols': 50})
335 call VerifyScreenDump(buf, 'Test_ambiwidth_hl_dump_1', {})
336
337 call term_sendkeys(buf, ":set ambiwidth=double\<CR>")
338 call term_sendkeys(buf, ":echo\<CR>")
339 call VerifyScreenDump(buf, 'Test_ambiwidth_hl_dump_2', {})
340
341 call term_sendkeys(buf, ":set ambiwidth=single\<CR>")
342 call term_sendkeys(buf, ":echo\<CR>")
343 call VerifyScreenDump(buf, 'Test_ambiwidth_hl_dump_1', {})
344
zeertzjqa59e0312024-04-15 19:14:38 +0200345 call term_sendkeys(buf, ":call setcellwidths([[0x2103, 0x2103, 2]])\<CR>")
346 call term_sendkeys(buf, ":echo\<CR>")
347 call VerifyScreenDump(buf, 'Test_ambiwidth_hl_dump_2', {})
348
349 call term_sendkeys(buf, ":call setcellwidths([[0x2103, 0x2103, 1]])\<CR>")
350 call term_sendkeys(buf, ":echo\<CR>")
351 call VerifyScreenDump(buf, 'Test_ambiwidth_hl_dump_1', {})
zeertzjqa59e0312024-04-15 19:14:38 +0200352
353 call StopVimInTerminal(buf)
354endfunc
355
Bram Moolenaar1cbfc992020-12-02 12:37:37 +0100356func Test_print_overlong()
357 " Text with more composing characters than MB_MAXBYTES.
358 new
359 call setline(1, 'axxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
360 s/x/\=nr2char(1629)/g
361 print
362 bwipe!
363endfunc
364
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100365" vim: shiftwidth=2 sts=2 expandtab