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