blob: fa6f76a6107aa37c4752f349bc17cc5e9b1aa7bb [file] [log] [blame]
Bram Moolenaar0c0590d2017-01-28 13:48:10 +01001" Tests for Unicode manipulations
Bram Moolenaar0c0590d2017-01-28 13:48:10 +01002
Bram Moolenaar2912abb2019-03-29 14:16:42 +01003source view_util.vim
Bram Moolenaar0c0590d2017-01-28 13:48:10 +01004
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]))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +020020 call assert_equal(exp[i][1], inp[i]->strchars(0))
Bram Moolenaar0c0590d2017-01-28 13:48:10 +010021 call assert_equal(exp[i][2], strchars(inp[i], 1))
22 endfor
Bram Moolenaar0e05de42020-03-25 22:23:46 +010023 call assert_fails("let v=strchars('abc', [])", 'E474:')
24 call assert_fails("let v=strchars('abc', 2)", 'E474:')
Bram Moolenaar0c0590d2017-01-28 13:48:10 +010025endfunc
26
27" Test for customlist completion
Bram Moolenaar1e115362019-01-09 23:01:02 +010028func CustomComplete1(lead, line, pos)
Bram Moolenaar0c0590d2017-01-28 13:48:10 +010029 return ['あ', 'い']
Bram Moolenaar1e115362019-01-09 23:01:02 +010030endfunc
Bram Moolenaar0c0590d2017-01-28 13:48:10 +010031
Bram Moolenaar1e115362019-01-09 23:01:02 +010032func CustomComplete2(lead, line, pos)
Bram Moolenaar0c0590d2017-01-28 13:48:10 +010033 return ['あたし', 'あたま', 'あたりめ']
Bram Moolenaar1e115362019-01-09 23:01:02 +010034endfunc
Bram Moolenaar0c0590d2017-01-28 13:48:10 +010035
Bram Moolenaar1e115362019-01-09 23:01:02 +010036func CustomComplete3(lead, line, pos)
Bram Moolenaar0c0590d2017-01-28 13:48:10 +010037 return ['Nこ', 'Nん', 'Nぶ']
Bram Moolenaar1e115362019-01-09 23:01:02 +010038endfunc
Bram Moolenaar0c0590d2017-01-28 13:48:10 +010039
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
Bram Moolenaar2912abb2019-03-29 14:16:42 +010066
Bram Moolenaar9d401282019-04-06 13:18:12 +020067func 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))
Bram Moolenaar02b31112019-08-31 22:16:38 +020082 call assert_equal(s, l->list2str(1))
Bram Moolenaar9d401282019-04-06 13:18:12 +020083 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)
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100104 call assert_equal([65, 66, 67], str2list("ABC"))
Bram Moolenaar9d401282019-04-06 13:18:12 +0200105
106 let &encoding = save_encoding
107 call assert_equal(l, lres)
108 call assert_equal(s, sres)
109endfunc
110
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100111func Test_screenchar_utf8()
112 new
113
114 " 1-cell, with composing characters
115 call setline(1, ["ABC\u0308"])
116 redraw
117 call assert_equal([0x0041], screenchars(1, 1))
Bram Moolenaar196b4662019-09-06 21:34:30 +0200118 call assert_equal([0x0042], 1->screenchars(2))
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100119 call assert_equal([0x0043, 0x0308], screenchars(1, 3))
120 call assert_equal("A", screenstring(1, 1))
121 call assert_equal("B", screenstring(1, 2))
122 call assert_equal("C\u0308", screenstring(1, 3))
123
124 " 2-cells, with composing characters
125 let text = "\u3042\u3044\u3046\u3099"
126 call setline(1, text)
127 redraw
128 call assert_equal([0x3042], screenchars(1, 1))
129 call assert_equal([0], screenchars(1, 2))
130 call assert_equal([0x3044], screenchars(1, 3))
131 call assert_equal([0], screenchars(1, 4))
132 call assert_equal([0x3046, 0x3099], screenchars(1, 5))
133
134 call assert_equal("\u3042", screenstring(1, 1))
135 call assert_equal("", screenstring(1, 2))
136 call assert_equal("\u3044", screenstring(1, 3))
137 call assert_equal("", screenstring(1, 4))
138 call assert_equal("\u3046\u3099", screenstring(1, 5))
139
Bram Moolenaar48aed082019-03-30 15:44:17 +0100140 call assert_equal([text . ' '], ScreenLines(1, 8))
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100141
142 bwipe!
143endfunc
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100144
145" vim: shiftwidth=2 sts=2 expandtab