xref: /vim-8.2.3635/src/testdir/test_utf8.vim (revision 2387773d)
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
23endfunc
24
25" Test for customlist completion
26func CustomComplete1(lead, line, pos)
27	return ['あ', 'い']
28endfunc
29
30func CustomComplete2(lead, line, pos)
31	return ['あたし', 'あたま', 'あたりめ']
32endfunc
33
34func CustomComplete3(lead, line, pos)
35	return ['Nこ', 'Nん', 'Nぶ']
36endfunc
37
38func Test_customlist_completion()
39  command -nargs=1 -complete=customlist,CustomComplete1 Test1 echo
40  call feedkeys(":Test1 \<C-L>\<C-B>\"\<CR>", 'itx')
41  call assert_equal('"Test1 ', getreg(':'))
42
43  command -nargs=1 -complete=customlist,CustomComplete2 Test2 echo
44  call feedkeys(":Test2 \<C-L>\<C-B>\"\<CR>", 'itx')
45  call assert_equal('"Test2 あた', getreg(':'))
46
47  command -nargs=1 -complete=customlist,CustomComplete3 Test3 echo
48  call feedkeys(":Test3 \<C-L>\<C-B>\"\<CR>", 'itx')
49  call assert_equal('"Test3 N', getreg(':'))
50
51  call garbagecollect(1)
52endfunc
53
54" Yank one 3 byte character and check the mark columns.
55func Test_getvcol()
56  new
57  call setline(1, "x\u2500x")
58  normal 0lvy
59  call assert_equal(2, col("'["))
60  call assert_equal(4, col("']"))
61  call assert_equal(2, virtcol("'["))
62  call assert_equal(2, virtcol("']"))
63endfunc
64
65func Test_list2str_str2list_utf8()
66  " One Unicode codepoint
67  let s = "\u3042\u3044"
68  let l = [0x3042, 0x3044]
69  call assert_equal(l, str2list(s, 1))
70  call assert_equal(s, list2str(l, 1))
71  if &enc ==# 'utf-8'
72    call assert_equal(str2list(s), str2list(s, 1))
73    call assert_equal(list2str(l), list2str(l, 1))
74  endif
75
76  " With composing characters
77  let s = "\u304b\u3099\u3044"
78  let l = [0x304b, 0x3099, 0x3044]
79  call assert_equal(l, str2list(s, 1))
80  call assert_equal(s, l->list2str(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  " Null list is the same as an empty list
87  call assert_equal('', list2str([]))
88  call assert_equal('', list2str(test_null_list()))
89endfunc
90
91func Test_list2str_str2list_latin1()
92  " When 'encoding' is not multi-byte can still get utf-8 string.
93  " But we need to create the utf-8 string while 'encoding' is utf-8.
94  let s = "\u3042\u3044"
95  let l = [0x3042, 0x3044]
96
97  let save_encoding = &encoding
98  set encoding=latin1
99
100  let lres = str2list(s, 1)
101  let sres = list2str(l, 1)
102
103  let &encoding = save_encoding
104  call assert_equal(l, lres)
105  call assert_equal(s, sres)
106endfunc
107
108func Test_screenchar_utf8()
109  new
110
111  " 1-cell, with composing characters
112  call setline(1, ["ABC\u0308"])
113  redraw
114  call assert_equal([0x0041], screenchars(1, 1))
115  call assert_equal([0x0042], 1->screenchars(2))
116  call assert_equal([0x0043, 0x0308], screenchars(1, 3))
117  call assert_equal("A", screenstring(1, 1))
118  call assert_equal("B", screenstring(1, 2))
119  call assert_equal("C\u0308", screenstring(1, 3))
120
121  " 2-cells, with composing characters
122  let text = "\u3042\u3044\u3046\u3099"
123  call setline(1, text)
124  redraw
125  call assert_equal([0x3042], screenchars(1, 1))
126  call assert_equal([0], screenchars(1, 2))
127  call assert_equal([0x3044], screenchars(1, 3))
128  call assert_equal([0], screenchars(1, 4))
129  call assert_equal([0x3046, 0x3099], screenchars(1, 5))
130
131  call assert_equal("\u3042", screenstring(1, 1))
132  call assert_equal("", screenstring(1, 2))
133  call assert_equal("\u3044", screenstring(1, 3))
134  call assert_equal("", screenstring(1, 4))
135  call assert_equal("\u3046\u3099", screenstring(1, 5))
136
137  call assert_equal([text . '  '], ScreenLines(1, 8))
138
139  bwipe!
140endfunc
141