Bram Moolenaar | 0c0590d | 2017-01-28 13:48:10 +0100 | [diff] [blame] | 1 | " Tests for Unicode manipulations |
Bram Moolenaar | 0c0590d | 2017-01-28 13:48:10 +0100 | [diff] [blame] | 2 | |
zeertzjq | 6cac770 | 2022-01-04 18:01:21 +0000 | [diff] [blame] | 3 | source check.vim |
Bram Moolenaar | 2912abb | 2019-03-29 14:16:42 +0100 | [diff] [blame] | 4 | source view_util.vim |
Bram Moolenaar | 0c0590d | 2017-01-28 13:48:10 +0100 | [diff] [blame] | 5 | |
| 6 | " Visual block Insert adjusts for multi-byte char |
| 7 | func Test_visual_block_insert() |
| 8 | new |
| 9 | call setline(1, ["aaa", "あああ", "bbb"]) |
| 10 | exe ":norm! gg0l\<C-V>jjIx\<Esc>" |
Bram Moolenaar | fc6cceb | 2022-01-20 12:22:35 +0000 | [diff] [blame] | 11 | call assert_equal(['axaa', ' xあああ', 'bxbb'], getline(1, '$')) |
Bram Moolenaar | 0c0590d | 2017-01-28 13:48:10 +0100 | [diff] [blame] | 12 | bwipeout! |
| 13 | endfunc |
| 14 | |
Bram Moolenaar | 70ce8a1 | 2021-03-14 19:02:09 +0100 | [diff] [blame] | 15 | " Test for built-in functions strchars() and strcharlen() |
Bram Moolenaar | 0c0590d | 2017-01-28 13:48:10 +0100 | [diff] [blame] | 16 | func Test_strchars() |
| 17 | let inp = ["a", "あいa", "A\u20dd", "A\u20dd\u20dd", "\u20dd"] |
| 18 | let exp = [[1, 1, 1], [3, 3, 3], [2, 2, 1], [3, 3, 1], [1, 1, 1]] |
| 19 | for i in range(len(inp)) |
| 20 | call assert_equal(exp[i][0], strchars(inp[i])) |
Bram Moolenaar | f6ed61e | 2019-09-07 19:05:09 +0200 | [diff] [blame] | 21 | call assert_equal(exp[i][1], inp[i]->strchars(0)) |
Bram Moolenaar | 0c0590d | 2017-01-28 13:48:10 +0100 | [diff] [blame] | 22 | call assert_equal(exp[i][2], strchars(inp[i], 1)) |
| 23 | endfor |
Bram Moolenaar | 70ce8a1 | 2021-03-14 19:02:09 +0100 | [diff] [blame] | 24 | |
| 25 | let exp = [1, 3, 1, 1, 1] |
| 26 | for i in range(len(inp)) |
| 27 | call assert_equal(exp[i], inp[i]->strcharlen()) |
| 28 | call assert_equal(exp[i], strcharlen(inp[i])) |
| 29 | endfor |
| 30 | |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 31 | call assert_fails("let v=strchars('abc', [])", 'E745:') |
Bram Moolenaar | 707be5f | 2020-09-06 17:13:44 +0200 | [diff] [blame] | 32 | call assert_fails("let v=strchars('abc', 2)", 'E1023:') |
Bram Moolenaar | 0c0590d | 2017-01-28 13:48:10 +0100 | [diff] [blame] | 33 | endfunc |
| 34 | |
| 35 | " Test for customlist completion |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 36 | func CustomComplete1(lead, line, pos) |
Bram Moolenaar | 0c0590d | 2017-01-28 13:48:10 +0100 | [diff] [blame] | 37 | return ['あ', 'い'] |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 38 | endfunc |
Bram Moolenaar | 0c0590d | 2017-01-28 13:48:10 +0100 | [diff] [blame] | 39 | |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 40 | func CustomComplete2(lead, line, pos) |
Bram Moolenaar | 0c0590d | 2017-01-28 13:48:10 +0100 | [diff] [blame] | 41 | return ['あたし', 'あたま', 'あたりめ'] |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 42 | endfunc |
Bram Moolenaar | 0c0590d | 2017-01-28 13:48:10 +0100 | [diff] [blame] | 43 | |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 44 | func CustomComplete3(lead, line, pos) |
Bram Moolenaar | 0c0590d | 2017-01-28 13:48:10 +0100 | [diff] [blame] | 45 | return ['Nこ', 'Nん', 'Nぶ'] |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 46 | endfunc |
Bram Moolenaar | 0c0590d | 2017-01-28 13:48:10 +0100 | [diff] [blame] | 47 | |
| 48 | func Test_customlist_completion() |
| 49 | command -nargs=1 -complete=customlist,CustomComplete1 Test1 echo |
| 50 | call feedkeys(":Test1 \<C-L>\<C-B>\"\<CR>", 'itx') |
| 51 | call assert_equal('"Test1 ', getreg(':')) |
| 52 | |
| 53 | command -nargs=1 -complete=customlist,CustomComplete2 Test2 echo |
| 54 | call feedkeys(":Test2 \<C-L>\<C-B>\"\<CR>", 'itx') |
| 55 | call assert_equal('"Test2 あた', getreg(':')) |
| 56 | |
| 57 | command -nargs=1 -complete=customlist,CustomComplete3 Test3 echo |
| 58 | call feedkeys(":Test3 \<C-L>\<C-B>\"\<CR>", 'itx') |
| 59 | call assert_equal('"Test3 N', getreg(':')) |
| 60 | |
| 61 | call garbagecollect(1) |
| 62 | endfunc |
| 63 | |
| 64 | " Yank one 3 byte character and check the mark columns. |
| 65 | func Test_getvcol() |
| 66 | new |
| 67 | call setline(1, "x\u2500x") |
| 68 | normal 0lvy |
| 69 | call assert_equal(2, col("'[")) |
| 70 | call assert_equal(4, col("']")) |
| 71 | call assert_equal(2, virtcol("'[")) |
| 72 | call assert_equal(2, virtcol("']")) |
| 73 | endfunc |
Bram Moolenaar | 2912abb | 2019-03-29 14:16:42 +0100 | [diff] [blame] | 74 | |
Bram Moolenaar | 9d40128 | 2019-04-06 13:18:12 +0200 | [diff] [blame] | 75 | func Test_list2str_str2list_utf8() |
| 76 | " One Unicode codepoint |
| 77 | let s = "\u3042\u3044" |
| 78 | let l = [0x3042, 0x3044] |
| 79 | call assert_equal(l, str2list(s, 1)) |
| 80 | call assert_equal(s, list2str(l, 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 | " With composing characters |
| 87 | let s = "\u304b\u3099\u3044" |
| 88 | let l = [0x304b, 0x3099, 0x3044] |
| 89 | call assert_equal(l, str2list(s, 1)) |
Bram Moolenaar | 02b3111 | 2019-08-31 22:16:38 +0200 | [diff] [blame] | 90 | call assert_equal(s, l->list2str(1)) |
Bram Moolenaar | 9d40128 | 2019-04-06 13:18:12 +0200 | [diff] [blame] | 91 | if &enc ==# 'utf-8' |
| 92 | call assert_equal(str2list(s), str2list(s, 1)) |
| 93 | call assert_equal(list2str(l), list2str(l, 1)) |
| 94 | endif |
| 95 | |
| 96 | " Null list is the same as an empty list |
| 97 | call assert_equal('', list2str([])) |
| 98 | call assert_equal('', list2str(test_null_list())) |
| 99 | endfunc |
| 100 | |
| 101 | func Test_list2str_str2list_latin1() |
| 102 | " When 'encoding' is not multi-byte can still get utf-8 string. |
| 103 | " But we need to create the utf-8 string while 'encoding' is utf-8. |
| 104 | let s = "\u3042\u3044" |
| 105 | let l = [0x3042, 0x3044] |
| 106 | |
| 107 | let save_encoding = &encoding |
| 108 | set encoding=latin1 |
| 109 | |
| 110 | let lres = str2list(s, 1) |
| 111 | let sres = list2str(l, 1) |
Bram Moolenaar | 0e05de4 | 2020-03-25 22:23:46 +0100 | [diff] [blame] | 112 | call assert_equal([65, 66, 67], str2list("ABC")) |
Bram Moolenaar | 9d40128 | 2019-04-06 13:18:12 +0200 | [diff] [blame] | 113 | |
Bram Moolenaar | 08f4157 | 2020-04-20 16:50:00 +0200 | [diff] [blame] | 114 | " Try converting a list to a string in latin-1 encoding |
| 115 | call assert_equal([1, 2, 3], str2list(list2str([1, 2, 3]))) |
| 116 | |
Bram Moolenaar | 9d40128 | 2019-04-06 13:18:12 +0200 | [diff] [blame] | 117 | let &encoding = save_encoding |
| 118 | call assert_equal(l, lres) |
| 119 | call assert_equal(s, sres) |
| 120 | endfunc |
| 121 | |
Bram Moolenaar | 2912abb | 2019-03-29 14:16:42 +0100 | [diff] [blame] | 122 | func Test_screenchar_utf8() |
| 123 | new |
| 124 | |
| 125 | " 1-cell, with composing characters |
| 126 | call setline(1, ["ABC\u0308"]) |
| 127 | redraw |
| 128 | call assert_equal([0x0041], screenchars(1, 1)) |
Bram Moolenaar | 196b466 | 2019-09-06 21:34:30 +0200 | [diff] [blame] | 129 | call assert_equal([0x0042], 1->screenchars(2)) |
Bram Moolenaar | 2912abb | 2019-03-29 14:16:42 +0100 | [diff] [blame] | 130 | call assert_equal([0x0043, 0x0308], screenchars(1, 3)) |
| 131 | call assert_equal("A", screenstring(1, 1)) |
| 132 | call assert_equal("B", screenstring(1, 2)) |
| 133 | call assert_equal("C\u0308", screenstring(1, 3)) |
| 134 | |
| 135 | " 2-cells, with composing characters |
| 136 | let text = "\u3042\u3044\u3046\u3099" |
| 137 | call setline(1, text) |
| 138 | redraw |
| 139 | call assert_equal([0x3042], screenchars(1, 1)) |
| 140 | call assert_equal([0], screenchars(1, 2)) |
| 141 | call assert_equal([0x3044], screenchars(1, 3)) |
| 142 | call assert_equal([0], screenchars(1, 4)) |
| 143 | call assert_equal([0x3046, 0x3099], screenchars(1, 5)) |
| 144 | |
| 145 | call assert_equal("\u3042", screenstring(1, 1)) |
| 146 | call assert_equal("", screenstring(1, 2)) |
| 147 | call assert_equal("\u3044", screenstring(1, 3)) |
| 148 | call assert_equal("", screenstring(1, 4)) |
| 149 | call assert_equal("\u3046\u3099", screenstring(1, 5)) |
| 150 | |
Bram Moolenaar | 48aed08 | 2019-03-30 15:44:17 +0100 | [diff] [blame] | 151 | call assert_equal([text . ' '], ScreenLines(1, 8)) |
Bram Moolenaar | 2912abb | 2019-03-29 14:16:42 +0100 | [diff] [blame] | 152 | |
| 153 | bwipe! |
| 154 | endfunc |
Bram Moolenaar | 0e05de4 | 2020-03-25 22:23:46 +0100 | [diff] [blame] | 155 | |
Bram Moolenaar | 08aac3c | 2020-08-28 21:04:24 +0200 | [diff] [blame] | 156 | func Test_setcellwidths() |
| 157 | call setcellwidths([ |
| 158 | \ [0x1330, 0x1330, 2], |
Bram Moolenaar | 08aac3c | 2020-08-28 21:04:24 +0200 | [diff] [blame] | 159 | \ [9999, 10000, 1], |
Bram Moolenaar | b06a6d5 | 2020-08-28 23:27:20 +0200 | [diff] [blame] | 160 | \ [0x1337, 0x1339, 2], |
Bram Moolenaar | 08aac3c | 2020-08-28 21:04:24 +0200 | [diff] [blame] | 161 | \]) |
| 162 | |
| 163 | call assert_equal(2, strwidth("\u1330")) |
| 164 | call assert_equal(1, strwidth("\u1336")) |
| 165 | call assert_equal(2, strwidth("\u1337")) |
| 166 | call assert_equal(2, strwidth("\u1339")) |
| 167 | call assert_equal(1, strwidth("\u133a")) |
| 168 | |
| 169 | call setcellwidths([]) |
| 170 | |
Bram Moolenaar | d83392a | 2022-09-01 12:22:46 +0100 | [diff] [blame] | 171 | call assert_fails('call setcellwidths(1)', 'E1211:') |
Bram Moolenaar | 08aac3c | 2020-08-28 21:04:24 +0200 | [diff] [blame] | 172 | |
| 173 | call assert_fails('call setcellwidths([1, 2, 0])', 'E1109:') |
| 174 | |
| 175 | call assert_fails('call setcellwidths([[0x101]])', 'E1110:') |
| 176 | call assert_fails('call setcellwidths([[0x101, 0x102]])', 'E1110:') |
| 177 | call assert_fails('call setcellwidths([[0x101, 0x102, 1, 4]])', 'E1110:') |
| 178 | call assert_fails('call setcellwidths([["a"]])', 'E1110:') |
| 179 | |
| 180 | call assert_fails('call setcellwidths([[0x102, 0x101, 1]])', 'E1111:') |
| 181 | |
| 182 | call assert_fails('call setcellwidths([[0x101, 0x102, 0]])', 'E1112:') |
| 183 | call assert_fails('call setcellwidths([[0x101, 0x102, 3]])', 'E1112:') |
| 184 | |
| 185 | call assert_fails('call setcellwidths([[0x111, 0x122, 1], [0x115, 0x116, 2]])', 'E1113:') |
| 186 | call assert_fails('call setcellwidths([[0x111, 0x122, 1], [0x122, 0x123, 2]])', 'E1113:') |
| 187 | |
| 188 | call assert_fails('call setcellwidths([[0x33, 0x44, 2]])', 'E1114:') |
zeertzjq | 94358a1 | 2021-10-20 11:01:15 +0100 | [diff] [blame] | 189 | |
| 190 | set listchars=tab:--\\u2192 |
| 191 | call assert_fails('call setcellwidths([[0x2192, 0x2192, 2]])', 'E834:') |
| 192 | |
| 193 | set fillchars=stl:\\u2501 |
| 194 | call assert_fails('call setcellwidths([[0x2501, 0x2501, 2]])', 'E835:') |
| 195 | |
| 196 | set listchars& |
| 197 | set fillchars& |
| 198 | call setcellwidths([]) |
Bram Moolenaar | 08aac3c | 2020-08-28 21:04:24 +0200 | [diff] [blame] | 199 | endfunc |
| 200 | |
Bram Moolenaar | 1cbfc99 | 2020-12-02 12:37:37 +0100 | [diff] [blame] | 201 | func Test_print_overlong() |
| 202 | " Text with more composing characters than MB_MAXBYTES. |
| 203 | new |
| 204 | call setline(1, 'axxxxxxxxxxxxxxxxxxxxxxxxxxxxxx') |
| 205 | s/x/\=nr2char(1629)/g |
| 206 | print |
| 207 | bwipe! |
| 208 | endfunc |
| 209 | |
zeertzjq | 6cac770 | 2022-01-04 18:01:21 +0000 | [diff] [blame] | 210 | func Test_recording_with_select_mode_utf8() |
| 211 | call Run_test_recording_with_select_mode_utf8() |
| 212 | endfunc |
| 213 | |
| 214 | func Run_test_recording_with_select_mode_utf8() |
| 215 | new |
| 216 | |
| 217 | " No escaping |
| 218 | call feedkeys("qacc12345\<Esc>gH哦\<Esc>q", "tx") |
| 219 | call assert_equal("哦", getline(1)) |
| 220 | call assert_equal("cc12345\<Esc>gH哦\<Esc>", @a) |
| 221 | call setline(1, 'asdf') |
| 222 | normal! @a |
| 223 | call assert_equal("哦", getline(1)) |
| 224 | |
| 225 | " 固 is 0xE5 0x9B 0xBA where 0x9B is CSI |
| 226 | call feedkeys("qacc12345\<Esc>gH固\<Esc>q", "tx") |
| 227 | call assert_equal("固", getline(1)) |
| 228 | call assert_equal("cc12345\<Esc>gH固\<Esc>", @a) |
| 229 | call setline(1, 'asdf') |
| 230 | normal! @a |
| 231 | call assert_equal("固", getline(1)) |
| 232 | |
| 233 | " 四 is 0xE5 0x9B 0x9B where 0x9B is CSI |
| 234 | call feedkeys("qacc12345\<Esc>gH四\<Esc>q", "tx") |
| 235 | call assert_equal("四", getline(1)) |
| 236 | call assert_equal("cc12345\<Esc>gH四\<Esc>", @a) |
| 237 | call setline(1, 'asdf') |
| 238 | normal! @a |
| 239 | call assert_equal("四", getline(1)) |
| 240 | |
| 241 | " 倒 is 0xE5 0x80 0x92 where 0x80 is K_SPECIAL |
| 242 | call feedkeys("qacc12345\<Esc>gH倒\<Esc>q", "tx") |
| 243 | call assert_equal("倒", getline(1)) |
| 244 | call assert_equal("cc12345\<Esc>gH倒\<Esc>", @a) |
| 245 | call setline(1, 'asdf') |
| 246 | normal! @a |
| 247 | call assert_equal("倒", getline(1)) |
| 248 | |
| 249 | bwipe! |
| 250 | endfunc |
| 251 | |
| 252 | " This must be done as one of the last tests, because it starts the GUI, which |
| 253 | " cannot be undone. |
| 254 | func Test_zz_recording_with_select_mode_utf8_gui() |
| 255 | CheckCanRunGui |
| 256 | |
| 257 | gui -f |
| 258 | call Run_test_recording_with_select_mode_utf8() |
| 259 | endfunc |
| 260 | |
Bram Moolenaar | 0e05de4 | 2020-03-25 22:23:46 +0100 | [diff] [blame] | 261 | " vim: shiftwidth=2 sts=2 expandtab |