blob: bb99cb3e60e787af1daf700cadd945affc20698b [file] [log] [blame]
Bram Moolenaar0c0590d2017-01-28 13:48:10 +01001" Tests for Unicode manipulations
Bram Moolenaar0c0590d2017-01-28 13:48:10 +01002
zeertzjq6cac7702022-01-04 18:01:21 +00003source check.vim
Bram Moolenaar2912abb2019-03-29 14:16:42 +01004source view_util.vim
Bram Moolenaar0c0590d2017-01-28 13:48:10 +01005
6" Visual block Insert adjusts for multi-byte char
7func Test_visual_block_insert()
8 new
9 call setline(1, ["aaa", "あああ", "bbb"])
10 exe ":norm! gg0l\<C-V>jjIx\<Esc>"
Bram Moolenaarfc6cceb2022-01-20 12:22:35 +000011 call assert_equal(['axaa', ' xあああ', 'bxbb'], getline(1, '$'))
Bram Moolenaar0c0590d2017-01-28 13:48:10 +010012 bwipeout!
13endfunc
14
Bram Moolenaar70ce8a12021-03-14 19:02:09 +010015" Test for built-in functions strchars() and strcharlen()
Bram Moolenaar0c0590d2017-01-28 13:48:10 +010016func Test_strchars()
17 let inp = ["a", "あいa", "A\u20dd", "A\u20dd\u20dd", "\u20dd"]
18 let exp = [[1, 1, 1], [3, 3, 3], [2, 2, 1], [3, 3, 1], [1, 1, 1]]
19 for i in range(len(inp))
20 call assert_equal(exp[i][0], strchars(inp[i]))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +020021 call assert_equal(exp[i][1], inp[i]->strchars(0))
Bram Moolenaar0c0590d2017-01-28 13:48:10 +010022 call assert_equal(exp[i][2], strchars(inp[i], 1))
23 endfor
Bram Moolenaar70ce8a12021-03-14 19:02:09 +010024
25 let exp = [1, 3, 1, 1, 1]
26 for i in range(len(inp))
27 call assert_equal(exp[i], inp[i]->strcharlen())
28 call assert_equal(exp[i], strcharlen(inp[i]))
29 endfor
30
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +020031 call assert_fails("let v=strchars('abc', [])", 'E745:')
Bram Moolenaar707be5f2020-09-06 17:13:44 +020032 call assert_fails("let v=strchars('abc', 2)", 'E1023:')
Bram Moolenaar0c0590d2017-01-28 13:48:10 +010033endfunc
34
35" Test for customlist completion
Bram Moolenaar1e115362019-01-09 23:01:02 +010036func CustomComplete1(lead, line, pos)
Bram Moolenaar0c0590d2017-01-28 13:48:10 +010037 return ['あ', 'い']
Bram Moolenaar1e115362019-01-09 23:01:02 +010038endfunc
Bram Moolenaar0c0590d2017-01-28 13:48:10 +010039
Bram Moolenaar1e115362019-01-09 23:01:02 +010040func CustomComplete2(lead, line, pos)
Bram Moolenaar0c0590d2017-01-28 13:48:10 +010041 return ['あたし', 'あたま', 'あたりめ']
Bram Moolenaar1e115362019-01-09 23:01:02 +010042endfunc
Bram Moolenaar0c0590d2017-01-28 13:48:10 +010043
Bram Moolenaar1e115362019-01-09 23:01:02 +010044func CustomComplete3(lead, line, pos)
Bram Moolenaar0c0590d2017-01-28 13:48:10 +010045 return ['Nこ', 'Nん', 'Nぶ']
Bram Moolenaar1e115362019-01-09 23:01:02 +010046endfunc
Bram Moolenaar0c0590d2017-01-28 13:48:10 +010047
48func Test_customlist_completion()
49 command -nargs=1 -complete=customlist,CustomComplete1 Test1 echo
50 call feedkeys(":Test1 \<C-L>\<C-B>\"\<CR>", 'itx')
51 call assert_equal('"Test1 ', getreg(':'))
52
53 command -nargs=1 -complete=customlist,CustomComplete2 Test2 echo
54 call feedkeys(":Test2 \<C-L>\<C-B>\"\<CR>", 'itx')
55 call assert_equal('"Test2 あた', getreg(':'))
56
57 command -nargs=1 -complete=customlist,CustomComplete3 Test3 echo
58 call feedkeys(":Test3 \<C-L>\<C-B>\"\<CR>", 'itx')
59 call assert_equal('"Test3 N', getreg(':'))
60
61 call garbagecollect(1)
62endfunc
63
64" Yank one 3 byte character and check the mark columns.
65func Test_getvcol()
66 new
67 call setline(1, "x\u2500x")
68 normal 0lvy
69 call assert_equal(2, col("'["))
70 call assert_equal(4, col("']"))
71 call assert_equal(2, virtcol("'["))
72 call assert_equal(2, virtcol("']"))
73endfunc
Bram Moolenaar2912abb2019-03-29 14:16:42 +010074
Bram Moolenaar9d401282019-04-06 13:18:12 +020075func Test_list2str_str2list_utf8()
76 " One Unicode codepoint
77 let s = "\u3042\u3044"
78 let l = [0x3042, 0x3044]
79 call assert_equal(l, str2list(s, 1))
80 call assert_equal(s, list2str(l, 1))
81 if &enc ==# 'utf-8'
82 call assert_equal(str2list(s), str2list(s, 1))
83 call assert_equal(list2str(l), list2str(l, 1))
84 endif
85
86 " With composing characters
87 let s = "\u304b\u3099\u3044"
88 let l = [0x304b, 0x3099, 0x3044]
89 call assert_equal(l, str2list(s, 1))
Bram Moolenaar02b31112019-08-31 22:16:38 +020090 call assert_equal(s, l->list2str(1))
Bram Moolenaar9d401282019-04-06 13:18:12 +020091 if &enc ==# 'utf-8'
92 call assert_equal(str2list(s), str2list(s, 1))
93 call assert_equal(list2str(l), list2str(l, 1))
94 endif
95
96 " Null list is the same as an empty list
97 call assert_equal('', list2str([]))
98 call assert_equal('', list2str(test_null_list()))
99endfunc
100
101func Test_list2str_str2list_latin1()
102 " When 'encoding' is not multi-byte can still get utf-8 string.
103 " But we need to create the utf-8 string while 'encoding' is utf-8.
104 let s = "\u3042\u3044"
105 let l = [0x3042, 0x3044]
106
107 let save_encoding = &encoding
108 set encoding=latin1
109
110 let lres = str2list(s, 1)
111 let sres = list2str(l, 1)
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100112 call assert_equal([65, 66, 67], str2list("ABC"))
Bram Moolenaar9d401282019-04-06 13:18:12 +0200113
Bram Moolenaar08f41572020-04-20 16:50:00 +0200114 " Try converting a list to a string in latin-1 encoding
115 call assert_equal([1, 2, 3], str2list(list2str([1, 2, 3])))
116
Bram Moolenaar9d401282019-04-06 13:18:12 +0200117 let &encoding = save_encoding
118 call assert_equal(l, lres)
119 call assert_equal(s, sres)
120endfunc
121
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100122func Test_screenchar_utf8()
123 new
124
125 " 1-cell, with composing characters
126 call setline(1, ["ABC\u0308"])
127 redraw
128 call assert_equal([0x0041], screenchars(1, 1))
Bram Moolenaar196b4662019-09-06 21:34:30 +0200129 call assert_equal([0x0042], 1->screenchars(2))
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100130 call assert_equal([0x0043, 0x0308], screenchars(1, 3))
131 call assert_equal("A", screenstring(1, 1))
132 call assert_equal("B", screenstring(1, 2))
133 call assert_equal("C\u0308", screenstring(1, 3))
134
135 " 2-cells, with composing characters
136 let text = "\u3042\u3044\u3046\u3099"
137 call setline(1, text)
138 redraw
139 call assert_equal([0x3042], screenchars(1, 1))
140 call assert_equal([0], screenchars(1, 2))
141 call assert_equal([0x3044], screenchars(1, 3))
142 call assert_equal([0], screenchars(1, 4))
143 call assert_equal([0x3046, 0x3099], screenchars(1, 5))
144
145 call assert_equal("\u3042", screenstring(1, 1))
146 call assert_equal("", screenstring(1, 2))
147 call assert_equal("\u3044", screenstring(1, 3))
148 call assert_equal("", screenstring(1, 4))
149 call assert_equal("\u3046\u3099", screenstring(1, 5))
150
Bram Moolenaar48aed082019-03-30 15:44:17 +0100151 call assert_equal([text . ' '], ScreenLines(1, 8))
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100152
153 bwipe!
154endfunc
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100155
Bram Moolenaar08aac3c2020-08-28 21:04:24 +0200156func Test_setcellwidths()
157 call setcellwidths([
158 \ [0x1330, 0x1330, 2],
Bram Moolenaar08aac3c2020-08-28 21:04:24 +0200159 \ [9999, 10000, 1],
Bram Moolenaarb06a6d52020-08-28 23:27:20 +0200160 \ [0x1337, 0x1339, 2],
Bram Moolenaar08aac3c2020-08-28 21:04:24 +0200161 \])
162
163 call assert_equal(2, strwidth("\u1330"))
164 call assert_equal(1, strwidth("\u1336"))
165 call assert_equal(2, strwidth("\u1337"))
166 call assert_equal(2, strwidth("\u1339"))
167 call assert_equal(1, strwidth("\u133a"))
168
169 call setcellwidths([])
170
Bram Moolenaard83392a2022-09-01 12:22:46 +0100171 call assert_fails('call setcellwidths(1)', 'E1211:')
Bram Moolenaar08aac3c2020-08-28 21:04:24 +0200172
173 call assert_fails('call setcellwidths([1, 2, 0])', 'E1109:')
174
175 call assert_fails('call setcellwidths([[0x101]])', 'E1110:')
176 call assert_fails('call setcellwidths([[0x101, 0x102]])', 'E1110:')
177 call assert_fails('call setcellwidths([[0x101, 0x102, 1, 4]])', 'E1110:')
178 call assert_fails('call setcellwidths([["a"]])', 'E1110:')
179
180 call assert_fails('call setcellwidths([[0x102, 0x101, 1]])', 'E1111:')
181
182 call assert_fails('call setcellwidths([[0x101, 0x102, 0]])', 'E1112:')
183 call assert_fails('call setcellwidths([[0x101, 0x102, 3]])', 'E1112:')
184
185 call assert_fails('call setcellwidths([[0x111, 0x122, 1], [0x115, 0x116, 2]])', 'E1113:')
186 call assert_fails('call setcellwidths([[0x111, 0x122, 1], [0x122, 0x123, 2]])', 'E1113:')
187
188 call assert_fails('call setcellwidths([[0x33, 0x44, 2]])', 'E1114:')
zeertzjq94358a12021-10-20 11:01:15 +0100189
190 set listchars=tab:--\\u2192
191 call assert_fails('call setcellwidths([[0x2192, 0x2192, 2]])', 'E834:')
192
193 set fillchars=stl:\\u2501
194 call assert_fails('call setcellwidths([[0x2501, 0x2501, 2]])', 'E835:')
195
196 set listchars&
197 set fillchars&
198 call setcellwidths([])
Bram Moolenaar08aac3c2020-08-28 21:04:24 +0200199endfunc
200
Bram Moolenaar1cbfc992020-12-02 12:37:37 +0100201func Test_print_overlong()
202 " Text with more composing characters than MB_MAXBYTES.
203 new
204 call setline(1, 'axxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
205 s/x/\=nr2char(1629)/g
206 print
207 bwipe!
208endfunc
209
zeertzjq6cac7702022-01-04 18:01:21 +0000210func Test_recording_with_select_mode_utf8()
211 call Run_test_recording_with_select_mode_utf8()
212endfunc
213
214func Run_test_recording_with_select_mode_utf8()
215 new
216
217 " No escaping
218 call feedkeys("qacc12345\<Esc>gH\<Esc>q", "tx")
219 call assert_equal("", getline(1))
220 call assert_equal("cc12345\<Esc>gH\<Esc>", @a)
221 call setline(1, 'asdf')
222 normal! @a
223 call assert_equal("", getline(1))
224
225 " is 0xE5 0x9B 0xBA where 0x9B is CSI
226 call feedkeys("qacc12345\<Esc>gH固\<Esc>q", "tx")
227 call assert_equal("固", getline(1))
228 call assert_equal("cc12345\<Esc>gH固\<Esc>", @a)
229 call setline(1, 'asdf')
230 normal! @a
231 call assert_equal("固", getline(1))
232
233 " 四 is 0xE5 0x9B 0x9B where 0x9B is CSI
234 call feedkeys("qacc12345\<Esc>gH\<Esc>q", "tx")
235 call assert_equal("", getline(1))
236 call assert_equal("cc12345\<Esc>gH\<Esc>", @a)
237 call setline(1, 'asdf')
238 normal! @a
239 call assert_equal("", getline(1))
240
241 " is 0xE5 0x80 0x92 where 0x80 is K_SPECIAL
242 call feedkeys("qacc12345\<Esc>gH倒\<Esc>q", "tx")
243 call assert_equal("倒", getline(1))
244 call assert_equal("cc12345\<Esc>gH倒\<Esc>", @a)
245 call setline(1, 'asdf')
246 normal! @a
247 call assert_equal("倒", getline(1))
248
249 bwipe!
250endfunc
251
252" This must be done as one of the last tests, because it starts the GUI, which
253" cannot be undone.
254func Test_zz_recording_with_select_mode_utf8_gui()
255 CheckCanRunGui
256
257 gui -f
258 call Run_test_recording_with_select_mode_utf8()
259endfunc
260
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100261" vim: shiftwidth=2 sts=2 expandtab