blob: 610566fd6504d98ccbaa87ed4881333df2c60ddf [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)
65endfunc
66
67" Yank one 3 byte character and check the mark columns.
68func Test_getvcol()
69 new
70 call setline(1, "x\u2500x")
71 normal 0lvy
72 call assert_equal(2, col("'["))
73 call assert_equal(4, col("']"))
74 call assert_equal(2, virtcol("'["))
75 call assert_equal(2, virtcol("']"))
76endfunc
Bram Moolenaar2912abb2019-03-29 14:16:42 +010077
Bram Moolenaar9d401282019-04-06 13:18:12 +020078func Test_list2str_str2list_utf8()
79 " One Unicode codepoint
80 let s = "\u3042\u3044"
81 let l = [0x3042, 0x3044]
82 call assert_equal(l, str2list(s, 1))
83 call assert_equal(s, list2str(l, 1))
84 if &enc ==# 'utf-8'
85 call assert_equal(str2list(s), str2list(s, 1))
86 call assert_equal(list2str(l), list2str(l, 1))
87 endif
88
89 " With composing characters
90 let s = "\u304b\u3099\u3044"
91 let l = [0x304b, 0x3099, 0x3044]
92 call assert_equal(l, str2list(s, 1))
Bram Moolenaar02b31112019-08-31 22:16:38 +020093 call assert_equal(s, l->list2str(1))
Bram Moolenaar9d401282019-04-06 13:18:12 +020094 if &enc ==# 'utf-8'
95 call assert_equal(str2list(s), str2list(s, 1))
96 call assert_equal(list2str(l), list2str(l, 1))
97 endif
98
99 " Null list is the same as an empty list
100 call assert_equal('', list2str([]))
101 call assert_equal('', list2str(test_null_list()))
102endfunc
103
104func Test_list2str_str2list_latin1()
105 " When 'encoding' is not multi-byte can still get utf-8 string.
106 " But we need to create the utf-8 string while 'encoding' is utf-8.
107 let s = "\u3042\u3044"
108 let l = [0x3042, 0x3044]
109
110 let save_encoding = &encoding
111 set encoding=latin1
Bram Moolenaar94722c52023-01-28 19:19:03 +0000112
Bram Moolenaar9d401282019-04-06 13:18:12 +0200113 let lres = str2list(s, 1)
114 let sres = list2str(l, 1)
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100115 call assert_equal([65, 66, 67], str2list("ABC"))
Bram Moolenaar9d401282019-04-06 13:18:12 +0200116
Bram Moolenaar08f41572020-04-20 16:50:00 +0200117 " Try converting a list to a string in latin-1 encoding
118 call assert_equal([1, 2, 3], str2list(list2str([1, 2, 3])))
119
Bram Moolenaar9d401282019-04-06 13:18:12 +0200120 let &encoding = save_encoding
121 call assert_equal(l, lres)
122 call assert_equal(s, sres)
123endfunc
124
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100125func Test_screenchar_utf8()
126 new
127
Bram Moolenaar94722c52023-01-28 19:19:03 +0000128 " 1-cell, with composing characters
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100129 call setline(1, ["ABC\u0308"])
130 redraw
131 call assert_equal([0x0041], screenchars(1, 1))
Bram Moolenaar196b4662019-09-06 21:34:30 +0200132 call assert_equal([0x0042], 1->screenchars(2))
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100133 call assert_equal([0x0043, 0x0308], screenchars(1, 3))
134 call assert_equal("A", screenstring(1, 1))
135 call assert_equal("B", screenstring(1, 2))
136 call assert_equal("C\u0308", screenstring(1, 3))
137
Bram Moolenaar94722c52023-01-28 19:19:03 +0000138 " 2-cells, with composing characters
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100139 let text = "\u3042\u3044\u3046\u3099"
140 call setline(1, text)
141 redraw
142 call assert_equal([0x3042], screenchars(1, 1))
143 call assert_equal([0], screenchars(1, 2))
144 call assert_equal([0x3044], screenchars(1, 3))
145 call assert_equal([0], screenchars(1, 4))
146 call assert_equal([0x3046, 0x3099], screenchars(1, 5))
147
148 call assert_equal("\u3042", screenstring(1, 1))
149 call assert_equal("", screenstring(1, 2))
150 call assert_equal("\u3044", screenstring(1, 3))
151 call assert_equal("", screenstring(1, 4))
152 call assert_equal("\u3046\u3099", screenstring(1, 5))
153
Bram Moolenaar48aed082019-03-30 15:44:17 +0100154 call assert_equal([text . ' '], ScreenLines(1, 8))
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100155
156 bwipe!
157endfunc
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100158
Bram Moolenaar08aac3c2020-08-28 21:04:24 +0200159func Test_setcellwidths()
160 call setcellwidths([
161 \ [0x1330, 0x1330, 2],
Bram Moolenaar08aac3c2020-08-28 21:04:24 +0200162 \ [9999, 10000, 1],
Bram Moolenaarb06a6d52020-08-28 23:27:20 +0200163 \ [0x1337, 0x1339, 2],
Bram Moolenaar08aac3c2020-08-28 21:04:24 +0200164 \])
165
166 call assert_equal(2, strwidth("\u1330"))
167 call assert_equal(1, strwidth("\u1336"))
168 call assert_equal(2, strwidth("\u1337"))
169 call assert_equal(2, strwidth("\u1339"))
170 call assert_equal(1, strwidth("\u133a"))
171
K.Takata71933232023-01-20 16:00:55 +0000172 for aw in ['single', 'double']
173 exe 'set ambiwidth=' . aw
174 " Handle \u0080 to \u009F as control chars even on MS-Windows.
175 set isprint=@,161-255
176
177 call setcellwidths([])
178 " Control chars
179 call assert_equal(4, strwidth("\u0081"))
180 call assert_equal(6, strwidth("\uFEFF"))
181 " Ambiguous width chars
182 call assert_equal((aw == 'single') ? 1 : 2, strwidth("\u00A1"))
183 call assert_equal((aw == 'single') ? 1 : 2, strwidth("\u2010"))
184
185 call setcellwidths([[0x81, 0x81, 1], [0xA1, 0xA1, 1],
186 \ [0x2010, 0x2010, 1], [0xFEFF, 0xFEFF, 1]])
187 " Control chars
188 call assert_equal(4, strwidth("\u0081"))
189 call assert_equal(6, strwidth("\uFEFF"))
190 " Ambiguous width chars
191 call assert_equal(1, strwidth("\u00A1"))
192 call assert_equal(1, strwidth("\u2010"))
193
194 call setcellwidths([[0x81, 0x81, 2], [0xA1, 0xA1, 2],
195 \ [0x2010, 0x2010, 2], [0xFEFF, 0xFEFF, 2]])
196 " Control chars
197 call assert_equal(4, strwidth("\u0081"))
198 call assert_equal(6, strwidth("\uFEFF"))
199 " Ambiguous width chars
200 call assert_equal(2, strwidth("\u00A1"))
201 call assert_equal(2, strwidth("\u2010"))
202 endfor
203 set ambiwidth& isprint&
204
Bram Moolenaar08aac3c2020-08-28 21:04:24 +0200205 call setcellwidths([])
206
Bram Moolenaard83392a2022-09-01 12:22:46 +0100207 call assert_fails('call setcellwidths(1)', 'E1211:')
Bram Moolenaar08aac3c2020-08-28 21:04:24 +0200208
209 call assert_fails('call setcellwidths([1, 2, 0])', 'E1109:')
210
211 call assert_fails('call setcellwidths([[0x101]])', 'E1110:')
212 call assert_fails('call setcellwidths([[0x101, 0x102]])', 'E1110:')
213 call assert_fails('call setcellwidths([[0x101, 0x102, 1, 4]])', 'E1110:')
214 call assert_fails('call setcellwidths([["a"]])', 'E1110:')
215
216 call assert_fails('call setcellwidths([[0x102, 0x101, 1]])', 'E1111:')
217
218 call assert_fails('call setcellwidths([[0x101, 0x102, 0]])', 'E1112:')
219 call assert_fails('call setcellwidths([[0x101, 0x102, 3]])', 'E1112:')
220
221 call assert_fails('call setcellwidths([[0x111, 0x122, 1], [0x115, 0x116, 2]])', 'E1113:')
222 call assert_fails('call setcellwidths([[0x111, 0x122, 1], [0x122, 0x123, 2]])', 'E1113:')
223
224 call assert_fails('call setcellwidths([[0x33, 0x44, 2]])', 'E1114:')
zeertzjq94358a12021-10-20 11:01:15 +0100225
226 set listchars=tab:--\\u2192
227 call assert_fails('call setcellwidths([[0x2192, 0x2192, 2]])', 'E834:')
228
229 set fillchars=stl:\\u2501
230 call assert_fails('call setcellwidths([[0x2501, 0x2501, 2]])', 'E835:')
231
232 set listchars&
233 set fillchars&
234 call setcellwidths([])
Bram Moolenaar08aac3c2020-08-28 21:04:24 +0200235endfunc
236
Kota Kato66bb9ae2023-01-17 18:31:56 +0000237func Test_getcellwidths()
238 call setcellwidths([])
239 call assert_equal([], getcellwidths())
240
241 let widthlist = [
242 \ [0x1330, 0x1330, 2],
243 \ [9999, 10000, 1],
244 \ [0x1337, 0x1339, 2],
245 \]
246 let widthlistsorted = [
247 \ [0x1330, 0x1330, 2],
248 \ [0x1337, 0x1339, 2],
249 \ [9999, 10000, 1],
250 \]
251 call setcellwidths(widthlist)
252 call assert_equal(widthlistsorted, getcellwidths())
253
254 call setcellwidths([])
255endfunc
256
Yasuhiro Matsumoto2bc849f2023-01-10 16:03:08 +0000257func Test_setcellwidths_dump()
258 CheckRunVimInTerminal
259
260 let lines =<< trim END
261 call setline(1, "\ue5ffDesktop")
262 END
263 call writefile(lines, 'XCellwidths', 'D')
264 let buf = RunVimInTerminal('-S XCellwidths', {'rows': 6})
265 call VerifyScreenDump(buf, 'Test_setcellwidths_dump_1', {})
266
267 call term_sendkeys(buf, ":call setcellwidths([[0xe5ff, 0xe5ff, 2]])\<CR>")
268 call VerifyScreenDump(buf, 'Test_setcellwidths_dump_2', {})
269
270 call StopVimInTerminal(buf)
271endfunc
272
Bram Moolenaar1cbfc992020-12-02 12:37:37 +0100273func Test_print_overlong()
274 " Text with more composing characters than MB_MAXBYTES.
275 new
276 call setline(1, 'axxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
277 s/x/\=nr2char(1629)/g
278 print
279 bwipe!
280endfunc
281
zeertzjq6cac7702022-01-04 18:01:21 +0000282func Test_recording_with_select_mode_utf8()
283 call Run_test_recording_with_select_mode_utf8()
284endfunc
285
286func Run_test_recording_with_select_mode_utf8()
287 new
288
289 " No escaping
290 call feedkeys("qacc12345\<Esc>gH哦\<Esc>q", "tx")
291 call assert_equal("哦", getline(1))
292 call assert_equal("cc12345\<Esc>gH哦\<Esc>", @a)
293 call setline(1, 'asdf')
294 normal! @a
295 call assert_equal("哦", getline(1))
296
297 " 固 is 0xE5 0x9B 0xBA where 0x9B is CSI
298 call feedkeys("qacc12345\<Esc>gH\<Esc>q", "tx")
299 call assert_equal("", getline(1))
300 call assert_equal("cc12345\<Esc>gH\<Esc>", @a)
301 call setline(1, 'asdf')
302 normal! @a
303 call assert_equal("", getline(1))
304
305 " is 0xE5 0x9B 0x9B where 0x9B is CSI
306 call feedkeys("qacc12345\<Esc>gH四\<Esc>q", "tx")
307 call assert_equal("四", getline(1))
308 call assert_equal("cc12345\<Esc>gH四\<Esc>", @a)
309 call setline(1, 'asdf')
310 normal! @a
311 call assert_equal("四", getline(1))
312
313 " 倒 is 0xE5 0x80 0x92 where 0x80 is K_SPECIAL
314 call feedkeys("qacc12345\<Esc>gH\<Esc>q", "tx")
315 call assert_equal("", getline(1))
316 call assert_equal("cc12345\<Esc>gH\<Esc>", @a)
317 call setline(1, 'asdf')
318 normal! @a
319 call assert_equal("", getline(1))
320
321 bwipe!
322endfunc
323
324" This must be done as one of the last tests, because it starts the GUI, which
325" cannot be undone.
326func Test_zz_recording_with_select_mode_utf8_gui()
327 CheckCanRunGui
328
329 gui -f
330 call Run_test_recording_with_select_mode_utf8()
331endfunc
332
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100333" vim: shiftwidth=2 sts=2 expandtab