xref: /vim-8.2.3635/src/testdir/test_utf8.vim (revision 125ed274)
1" Tests for Unicode manipulations
2
3source view_util.vim
4
5" Visual block Insert adjusts for multi-byte char
6func Test_visual_block_insert()
7  new
8  call setline(1, ["aaa", "あああ", "bbb"])
9  exe ":norm! gg0l\<C-V>jjIx\<Esc>"
10  call assert_equal(['axaa', 'xあああ', 'bxbb'], getline(1, '$'))
11  bwipeout!
12endfunc
13
14" Test for built-in functions strchars() and strcharlen()
15func Test_strchars()
16  let inp = ["a", "あいa", "A\u20dd", "A\u20dd\u20dd", "\u20dd"]
17  let exp = [[1, 1, 1], [3, 3, 3], [2, 2, 1], [3, 3, 1], [1, 1, 1]]
18  for i in range(len(inp))
19    call assert_equal(exp[i][0], strchars(inp[i]))
20    call assert_equal(exp[i][1], inp[i]->strchars(0))
21    call assert_equal(exp[i][2], strchars(inp[i], 1))
22  endfor
23
24  let exp = [1, 3, 1, 1, 1]
25  for i in range(len(inp))
26    call assert_equal(exp[i], inp[i]->strcharlen())
27    call assert_equal(exp[i], strcharlen(inp[i]))
28  endfor
29
30  call assert_fails("let v=strchars('abc', [])", 'E745:')
31  call assert_fails("let v=strchars('abc', 2)", 'E1023:')
32endfunc
33
34" Test for customlist completion
35func CustomComplete1(lead, line, pos)
36	return ['あ', 'い']
37endfunc
38
39func CustomComplete2(lead, line, pos)
40	return ['あたし', 'あたま', 'あたりめ']
41endfunc
42
43func CustomComplete3(lead, line, pos)
44	return ['Nこ', 'Nん', 'Nぶ']
45endfunc
46
47func Test_customlist_completion()
48  command -nargs=1 -complete=customlist,CustomComplete1 Test1 echo
49  call feedkeys(":Test1 \<C-L>\<C-B>\"\<CR>", 'itx')
50  call assert_equal('"Test1 ', getreg(':'))
51
52  command -nargs=1 -complete=customlist,CustomComplete2 Test2 echo
53  call feedkeys(":Test2 \<C-L>\<C-B>\"\<CR>", 'itx')
54  call assert_equal('"Test2 あた', getreg(':'))
55
56  command -nargs=1 -complete=customlist,CustomComplete3 Test3 echo
57  call feedkeys(":Test3 \<C-L>\<C-B>\"\<CR>", 'itx')
58  call assert_equal('"Test3 N', getreg(':'))
59
60  call garbagecollect(1)
61endfunc
62
63" Yank one 3 byte character and check the mark columns.
64func Test_getvcol()
65  new
66  call setline(1, "x\u2500x")
67  normal 0lvy
68  call assert_equal(2, col("'["))
69  call assert_equal(4, col("']"))
70  call assert_equal(2, virtcol("'["))
71  call assert_equal(2, virtcol("']"))
72endfunc
73
74func Test_list2str_str2list_utf8()
75  " One Unicode codepoint
76  let s = "\u3042\u3044"
77  let l = [0x3042, 0x3044]
78  call assert_equal(l, str2list(s, 1))
79  call assert_equal(s, list2str(l, 1))
80  if &enc ==# 'utf-8'
81    call assert_equal(str2list(s), str2list(s, 1))
82    call assert_equal(list2str(l), list2str(l, 1))
83  endif
84
85  " With composing characters
86  let s = "\u304b\u3099\u3044"
87  let l = [0x304b, 0x3099, 0x3044]
88  call assert_equal(l, str2list(s, 1))
89  call assert_equal(s, l->list2str(1))
90  if &enc ==# 'utf-8'
91    call assert_equal(str2list(s), str2list(s, 1))
92    call assert_equal(list2str(l), list2str(l, 1))
93  endif
94
95  " Null list is the same as an empty list
96  call assert_equal('', list2str([]))
97  call assert_equal('', list2str(test_null_list()))
98endfunc
99
100func Test_list2str_str2list_latin1()
101  " When 'encoding' is not multi-byte can still get utf-8 string.
102  " But we need to create the utf-8 string while 'encoding' is utf-8.
103  let s = "\u3042\u3044"
104  let l = [0x3042, 0x3044]
105
106  let save_encoding = &encoding
107  set encoding=latin1
108
109  let lres = str2list(s, 1)
110  let sres = list2str(l, 1)
111  call assert_equal([65, 66, 67], str2list("ABC"))
112
113  " Try converting a list to a string in latin-1 encoding
114  call assert_equal([1, 2, 3], str2list(list2str([1, 2, 3])))
115
116  let &encoding = save_encoding
117  call assert_equal(l, lres)
118  call assert_equal(s, sres)
119endfunc
120
121func Test_screenchar_utf8()
122  new
123
124  " 1-cell, with composing characters
125  call setline(1, ["ABC\u0308"])
126  redraw
127  call assert_equal([0x0041], screenchars(1, 1))
128  call assert_equal([0x0042], 1->screenchars(2))
129  call assert_equal([0x0043, 0x0308], screenchars(1, 3))
130  call assert_equal("A", screenstring(1, 1))
131  call assert_equal("B", screenstring(1, 2))
132  call assert_equal("C\u0308", screenstring(1, 3))
133
134  " 2-cells, with composing characters
135  let text = "\u3042\u3044\u3046\u3099"
136  call setline(1, text)
137  redraw
138  call assert_equal([0x3042], screenchars(1, 1))
139  call assert_equal([0], screenchars(1, 2))
140  call assert_equal([0x3044], screenchars(1, 3))
141  call assert_equal([0], screenchars(1, 4))
142  call assert_equal([0x3046, 0x3099], screenchars(1, 5))
143
144  call assert_equal("\u3042", screenstring(1, 1))
145  call assert_equal("", screenstring(1, 2))
146  call assert_equal("\u3044", screenstring(1, 3))
147  call assert_equal("", screenstring(1, 4))
148  call assert_equal("\u3046\u3099", screenstring(1, 5))
149
150  call assert_equal([text . '  '], ScreenLines(1, 8))
151
152  bwipe!
153endfunc
154
155func Test_setcellwidths()
156  call setcellwidths([
157        \ [0x1330, 0x1330, 2],
158        \ [9999, 10000, 1],
159        \ [0x1337, 0x1339, 2],
160        \])
161
162  call assert_equal(2, strwidth("\u1330"))
163  call assert_equal(1, strwidth("\u1336"))
164  call assert_equal(2, strwidth("\u1337"))
165  call assert_equal(2, strwidth("\u1339"))
166  call assert_equal(1, strwidth("\u133a"))
167
168  call setcellwidths([])
169
170  call assert_fails('call setcellwidths(1)', 'E714:')
171
172  call assert_fails('call setcellwidths([1, 2, 0])', 'E1109:')
173
174  call assert_fails('call setcellwidths([[0x101]])', 'E1110:')
175  call assert_fails('call setcellwidths([[0x101, 0x102]])', 'E1110:')
176  call assert_fails('call setcellwidths([[0x101, 0x102, 1, 4]])', 'E1110:')
177  call assert_fails('call setcellwidths([["a"]])', 'E1110:')
178
179  call assert_fails('call setcellwidths([[0x102, 0x101, 1]])', 'E1111:')
180
181  call assert_fails('call setcellwidths([[0x101, 0x102, 0]])', 'E1112:')
182  call assert_fails('call setcellwidths([[0x101, 0x102, 3]])', 'E1112:')
183
184  call assert_fails('call setcellwidths([[0x111, 0x122, 1], [0x115, 0x116, 2]])', 'E1113:')
185  call assert_fails('call setcellwidths([[0x111, 0x122, 1], [0x122, 0x123, 2]])', 'E1113:')
186
187  call assert_fails('call setcellwidths([[0x33, 0x44, 2]])', 'E1114:')
188endfunc
189
190func Test_print_overlong()
191  " Text with more composing characters than MB_MAXBYTES.
192  new
193  call setline(1, 'axxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
194  s/x/\=nr2char(1629)/g
195  print
196  bwipe!
197endfunc
198
199" vim: shiftwidth=2 sts=2 expandtab
200