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