blob: fa0568e597d6cb7a049e2b04a30b458e4dd7887c [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]))
20 call assert_equal(exp[i][1], strchars(inp[i], 0))
21 call assert_equal(exp[i][2], strchars(inp[i], 1))
22 endfor
23endfunc
24
25" Test for customlist completion
Bram Moolenaar1e115362019-01-09 23:01:02 +010026func CustomComplete1(lead, line, pos)
Bram Moolenaar0c0590d2017-01-28 13:48:10 +010027 return ['あ', 'い']
Bram Moolenaar1e115362019-01-09 23:01:02 +010028endfunc
Bram Moolenaar0c0590d2017-01-28 13:48:10 +010029
Bram Moolenaar1e115362019-01-09 23:01:02 +010030func CustomComplete2(lead, line, pos)
Bram Moolenaar0c0590d2017-01-28 13:48:10 +010031 return ['あたし', 'あたま', 'あたりめ']
Bram Moolenaar1e115362019-01-09 23:01:02 +010032endfunc
Bram Moolenaar0c0590d2017-01-28 13:48:10 +010033
Bram Moolenaar1e115362019-01-09 23:01:02 +010034func CustomComplete3(lead, line, pos)
Bram Moolenaar0c0590d2017-01-28 13:48:10 +010035 return ['Nこ', 'Nん', 'Nぶ']
Bram Moolenaar1e115362019-01-09 23:01:02 +010036endfunc
Bram Moolenaar0c0590d2017-01-28 13:48:10 +010037
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
Bram Moolenaar2912abb2019-03-29 14:16:42 +010064
Bram Moolenaar9d401282019-04-06 13:18:12 +020065func 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))
Bram Moolenaar02b31112019-08-31 22:16:38 +020080 call assert_equal(s, l->list2str(1))
Bram Moolenaar9d401282019-04-06 13:18:12 +020081 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
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100108func 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))
Bram Moolenaar196b4662019-09-06 21:34:30 +0200115 call assert_equal([0x0042], 1->screenchars(2))
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100116 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
Bram Moolenaar48aed082019-03-30 15:44:17 +0100137 call assert_equal([text . ' '], ScreenLines(1, 8))
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100138
139 bwipe!
140endfunc