blob: 16bf60d9e00460bc554e1e6e8ea9bcf946d98e5a [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
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +020032 call assert_fails("let v=strchars('abc', [])", 'E745:')
Bram Moolenaar707be5f2020-09-06 17:13:44 +020033 call assert_fails("let v=strchars('abc', 2)", 'E1023:')
Bram Moolenaar0c0590d2017-01-28 13:48:10 +010034endfunc
35
36" Test for customlist completion
Bram Moolenaar1e115362019-01-09 23:01:02 +010037func CustomComplete1(lead, line, pos)
Bram Moolenaar0c0590d2017-01-28 13:48:10 +010038 return ['あ', 'い']
Bram Moolenaar1e115362019-01-09 23:01:02 +010039endfunc
Bram Moolenaar0c0590d2017-01-28 13:48:10 +010040
Bram Moolenaar1e115362019-01-09 23:01:02 +010041func CustomComplete2(lead, line, pos)
Bram Moolenaar0c0590d2017-01-28 13:48:10 +010042 return ['あたし', 'あたま', 'あたりめ']
Bram Moolenaar1e115362019-01-09 23:01:02 +010043endfunc
Bram Moolenaar0c0590d2017-01-28 13:48:10 +010044
Bram Moolenaar1e115362019-01-09 23:01:02 +010045func CustomComplete3(lead, line, pos)
Bram Moolenaar0c0590d2017-01-28 13:48:10 +010046 return ['Nこ', 'Nん', 'Nぶ']
Bram Moolenaar1e115362019-01-09 23:01:02 +010047endfunc
Bram Moolenaar0c0590d2017-01-28 13:48:10 +010048
49func Test_customlist_completion()
50 command -nargs=1 -complete=customlist,CustomComplete1 Test1 echo
51 call feedkeys(":Test1 \<C-L>\<C-B>\"\<CR>", 'itx')
52 call assert_equal('"Test1 ', getreg(':'))
53
54 command -nargs=1 -complete=customlist,CustomComplete2 Test2 echo
55 call feedkeys(":Test2 \<C-L>\<C-B>\"\<CR>", 'itx')
56 call assert_equal('"Test2 あた', getreg(':'))
57
58 command -nargs=1 -complete=customlist,CustomComplete3 Test3 echo
59 call feedkeys(":Test3 \<C-L>\<C-B>\"\<CR>", 'itx')
60 call assert_equal('"Test3 N', getreg(':'))
61
62 call garbagecollect(1)
63endfunc
64
65" Yank one 3 byte character and check the mark columns.
66func Test_getvcol()
67 new
68 call setline(1, "x\u2500x")
69 normal 0lvy
70 call assert_equal(2, col("'["))
71 call assert_equal(4, col("']"))
72 call assert_equal(2, virtcol("'["))
73 call assert_equal(2, virtcol("']"))
74endfunc
Bram Moolenaar2912abb2019-03-29 14:16:42 +010075
Bram Moolenaar9d401282019-04-06 13:18:12 +020076func Test_list2str_str2list_utf8()
77 " One Unicode codepoint
78 let s = "\u3042\u3044"
79 let l = [0x3042, 0x3044]
80 call assert_equal(l, str2list(s, 1))
81 call assert_equal(s, list2str(l, 1))
82 if &enc ==# 'utf-8'
83 call assert_equal(str2list(s), str2list(s, 1))
84 call assert_equal(list2str(l), list2str(l, 1))
85 endif
86
87 " With composing characters
88 let s = "\u304b\u3099\u3044"
89 let l = [0x304b, 0x3099, 0x3044]
90 call assert_equal(l, str2list(s, 1))
Bram Moolenaar02b31112019-08-31 22:16:38 +020091 call assert_equal(s, l->list2str(1))
Bram Moolenaar9d401282019-04-06 13:18:12 +020092 if &enc ==# 'utf-8'
93 call assert_equal(str2list(s), str2list(s, 1))
94 call assert_equal(list2str(l), list2str(l, 1))
95 endif
96
97 " Null list is the same as an empty list
98 call assert_equal('', list2str([]))
99 call assert_equal('', list2str(test_null_list()))
100endfunc
101
102func Test_list2str_str2list_latin1()
103 " When 'encoding' is not multi-byte can still get utf-8 string.
104 " But we need to create the utf-8 string while 'encoding' is utf-8.
105 let s = "\u3042\u3044"
106 let l = [0x3042, 0x3044]
107
108 let save_encoding = &encoding
109 set encoding=latin1
Bram Moolenaar94722c52023-01-28 19:19:03 +0000110
Bram Moolenaar9d401282019-04-06 13:18:12 +0200111 let lres = str2list(s, 1)
112 let sres = list2str(l, 1)
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100113 call assert_equal([65, 66, 67], str2list("ABC"))
Bram Moolenaar9d401282019-04-06 13:18:12 +0200114
Bram Moolenaar08f41572020-04-20 16:50:00 +0200115 " Try converting a list to a string in latin-1 encoding
116 call assert_equal([1, 2, 3], str2list(list2str([1, 2, 3])))
117
Bram Moolenaar9d401282019-04-06 13:18:12 +0200118 let &encoding = save_encoding
119 call assert_equal(l, lres)
120 call assert_equal(s, sres)
121endfunc
122
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100123func Test_screenchar_utf8()
124 new
125
Bram Moolenaar94722c52023-01-28 19:19:03 +0000126 " 1-cell, with composing characters
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100127 call setline(1, ["ABC\u0308"])
128 redraw
129 call assert_equal([0x0041], screenchars(1, 1))
Bram Moolenaar196b4662019-09-06 21:34:30 +0200130 call assert_equal([0x0042], 1->screenchars(2))
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100131 call assert_equal([0x0043, 0x0308], screenchars(1, 3))
132 call assert_equal("A", screenstring(1, 1))
133 call assert_equal("B", screenstring(1, 2))
134 call assert_equal("C\u0308", screenstring(1, 3))
135
Bram Moolenaar94722c52023-01-28 19:19:03 +0000136 " 2-cells, with composing characters
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100137 let text = "\u3042\u3044\u3046\u3099"
138 call setline(1, text)
139 redraw
140 call assert_equal([0x3042], screenchars(1, 1))
141 call assert_equal([0], screenchars(1, 2))
142 call assert_equal([0x3044], screenchars(1, 3))
143 call assert_equal([0], screenchars(1, 4))
144 call assert_equal([0x3046, 0x3099], screenchars(1, 5))
145
146 call assert_equal("\u3042", screenstring(1, 1))
147 call assert_equal("", screenstring(1, 2))
148 call assert_equal("\u3044", screenstring(1, 3))
149 call assert_equal("", screenstring(1, 4))
150 call assert_equal("\u3046\u3099", screenstring(1, 5))
151
Bram Moolenaar48aed082019-03-30 15:44:17 +0100152 call assert_equal([text . ' '], ScreenLines(1, 8))
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100153
154 bwipe!
155endfunc
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100156
Bram Moolenaar08aac3c2020-08-28 21:04:24 +0200157func Test_setcellwidths()
158 call setcellwidths([
159 \ [0x1330, 0x1330, 2],
Bram Moolenaar08aac3c2020-08-28 21:04:24 +0200160 \ [9999, 10000, 1],
Bram Moolenaarb06a6d52020-08-28 23:27:20 +0200161 \ [0x1337, 0x1339, 2],
Bram Moolenaar08aac3c2020-08-28 21:04:24 +0200162 \])
163
164 call assert_equal(2, strwidth("\u1330"))
165 call assert_equal(1, strwidth("\u1336"))
166 call assert_equal(2, strwidth("\u1337"))
167 call assert_equal(2, strwidth("\u1339"))
168 call assert_equal(1, strwidth("\u133a"))
169
K.Takata71933232023-01-20 16:00:55 +0000170 for aw in ['single', 'double']
171 exe 'set ambiwidth=' . aw
172 " Handle \u0080 to \u009F as control chars even on MS-Windows.
173 set isprint=@,161-255
174
175 call setcellwidths([])
176 " Control chars
177 call assert_equal(4, strwidth("\u0081"))
178 call assert_equal(6, strwidth("\uFEFF"))
179 " Ambiguous width chars
180 call assert_equal((aw == 'single') ? 1 : 2, strwidth("\u00A1"))
181 call assert_equal((aw == 'single') ? 1 : 2, strwidth("\u2010"))
182
183 call setcellwidths([[0x81, 0x81, 1], [0xA1, 0xA1, 1],
184 \ [0x2010, 0x2010, 1], [0xFEFF, 0xFEFF, 1]])
185 " Control chars
186 call assert_equal(4, strwidth("\u0081"))
187 call assert_equal(6, strwidth("\uFEFF"))
188 " Ambiguous width chars
189 call assert_equal(1, strwidth("\u00A1"))
190 call assert_equal(1, strwidth("\u2010"))
191
192 call setcellwidths([[0x81, 0x81, 2], [0xA1, 0xA1, 2],
193 \ [0x2010, 0x2010, 2], [0xFEFF, 0xFEFF, 2]])
194 " Control chars
195 call assert_equal(4, strwidth("\u0081"))
196 call assert_equal(6, strwidth("\uFEFF"))
197 " Ambiguous width chars
198 call assert_equal(2, strwidth("\u00A1"))
199 call assert_equal(2, strwidth("\u2010"))
200 endfor
201 set ambiwidth& isprint&
202
Bram Moolenaar08aac3c2020-08-28 21:04:24 +0200203 call setcellwidths([])
204
Bram Moolenaard83392a2022-09-01 12:22:46 +0100205 call assert_fails('call setcellwidths(1)', 'E1211:')
Bram Moolenaar08aac3c2020-08-28 21:04:24 +0200206
207 call assert_fails('call setcellwidths([1, 2, 0])', 'E1109:')
208
209 call assert_fails('call setcellwidths([[0x101]])', 'E1110:')
210 call assert_fails('call setcellwidths([[0x101, 0x102]])', 'E1110:')
211 call assert_fails('call setcellwidths([[0x101, 0x102, 1, 4]])', 'E1110:')
212 call assert_fails('call setcellwidths([["a"]])', 'E1110:')
213
214 call assert_fails('call setcellwidths([[0x102, 0x101, 1]])', 'E1111:')
215
216 call assert_fails('call setcellwidths([[0x101, 0x102, 0]])', 'E1112:')
217 call assert_fails('call setcellwidths([[0x101, 0x102, 3]])', 'E1112:')
218
219 call assert_fails('call setcellwidths([[0x111, 0x122, 1], [0x115, 0x116, 2]])', 'E1113:')
220 call assert_fails('call setcellwidths([[0x111, 0x122, 1], [0x122, 0x123, 2]])', 'E1113:')
221
222 call assert_fails('call setcellwidths([[0x33, 0x44, 2]])', 'E1114:')
zeertzjq94358a12021-10-20 11:01:15 +0100223
224 set listchars=tab:--\\u2192
225 call assert_fails('call setcellwidths([[0x2192, 0x2192, 2]])', 'E834:')
226
227 set fillchars=stl:\\u2501
228 call assert_fails('call setcellwidths([[0x2501, 0x2501, 2]])', 'E835:')
229
230 set listchars&
231 set fillchars&
232 call setcellwidths([])
Bram Moolenaar08aac3c2020-08-28 21:04:24 +0200233endfunc
234
Kota Kato66bb9ae2023-01-17 18:31:56 +0000235func Test_getcellwidths()
236 call setcellwidths([])
237 call assert_equal([], getcellwidths())
238
239 let widthlist = [
240 \ [0x1330, 0x1330, 2],
241 \ [9999, 10000, 1],
242 \ [0x1337, 0x1339, 2],
243 \]
244 let widthlistsorted = [
245 \ [0x1330, 0x1330, 2],
246 \ [0x1337, 0x1339, 2],
247 \ [9999, 10000, 1],
248 \]
249 call setcellwidths(widthlist)
250 call assert_equal(widthlistsorted, getcellwidths())
251
252 call setcellwidths([])
253endfunc
254
Yasuhiro Matsumoto2bc849f2023-01-10 16:03:08 +0000255func Test_setcellwidths_dump()
256 CheckRunVimInTerminal
257
258 let lines =<< trim END
259 call setline(1, "\ue5ffDesktop")
260 END
261 call writefile(lines, 'XCellwidths', 'D')
262 let buf = RunVimInTerminal('-S XCellwidths', {'rows': 6})
263 call VerifyScreenDump(buf, 'Test_setcellwidths_dump_1', {})
264
265 call term_sendkeys(buf, ":call setcellwidths([[0xe5ff, 0xe5ff, 2]])\<CR>")
266 call VerifyScreenDump(buf, 'Test_setcellwidths_dump_2', {})
267
268 call StopVimInTerminal(buf)
269endfunc
270
Bram Moolenaar1cbfc992020-12-02 12:37:37 +0100271func Test_print_overlong()
272 " Text with more composing characters than MB_MAXBYTES.
273 new
274 call setline(1, 'axxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
275 s/x/\=nr2char(1629)/g
276 print
277 bwipe!
278endfunc
279
zeertzjq6cac7702022-01-04 18:01:21 +0000280func Test_recording_with_select_mode_utf8()
281 call Run_test_recording_with_select_mode_utf8()
282endfunc
283
284func Run_test_recording_with_select_mode_utf8()
285 new
286
287 " No escaping
288 call feedkeys("qacc12345\<Esc>gH哦\<Esc>q", "tx")
289 call assert_equal("哦", getline(1))
290 call assert_equal("cc12345\<Esc>gH哦\<Esc>", @a)
291 call setline(1, 'asdf')
292 normal! @a
293 call assert_equal("哦", getline(1))
294
295 " 固 is 0xE5 0x9B 0xBA where 0x9B is CSI
296 call feedkeys("qacc12345\<Esc>gH\<Esc>q", "tx")
297 call assert_equal("", getline(1))
298 call assert_equal("cc12345\<Esc>gH\<Esc>", @a)
299 call setline(1, 'asdf')
300 normal! @a
301 call assert_equal("", getline(1))
302
303 " is 0xE5 0x9B 0x9B where 0x9B is CSI
304 call feedkeys("qacc12345\<Esc>gH四\<Esc>q", "tx")
305 call assert_equal("四", getline(1))
306 call assert_equal("cc12345\<Esc>gH四\<Esc>", @a)
307 call setline(1, 'asdf')
308 normal! @a
309 call assert_equal("四", getline(1))
310
311 " 倒 is 0xE5 0x80 0x92 where 0x80 is K_SPECIAL
312 call feedkeys("qacc12345\<Esc>gH\<Esc>q", "tx")
313 call assert_equal("", getline(1))
314 call assert_equal("cc12345\<Esc>gH\<Esc>", @a)
315 call setline(1, 'asdf')
316 normal! @a
317 call assert_equal("", getline(1))
318
319 bwipe!
320endfunc
321
322" This must be done as one of the last tests, because it starts the GUI, which
323" cannot be undone.
324func Test_zz_recording_with_select_mode_utf8_gui()
325 CheckCanRunGui
326
327 gui -f
328 call Run_test_recording_with_select_mode_utf8()
329endfunc
330
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100331" vim: shiftwidth=2 sts=2 expandtab