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 | |
Bram Moolenaar | 2912abb | 2019-03-29 14:16:42 +0100 | [diff] [blame] | 3 | source view_util.vim |
Bram Moolenaar | 0c0590d | 2017-01-28 13:48:10 +0100 | [diff] [blame] | 4 | |
| 5 | " Visual block Insert adjusts for multi-byte char |
| 6 | func 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! |
| 12 | endfunc |
| 13 | |
| 14 | " Test for built-in function strchars() |
| 15 | func 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 Moolenaar | f6ed61e | 2019-09-07 19:05:09 +0200 | [diff] [blame] | 20 | call assert_equal(exp[i][1], inp[i]->strchars(0)) |
Bram Moolenaar | 0c0590d | 2017-01-28 13:48:10 +0100 | [diff] [blame] | 21 | call assert_equal(exp[i][2], strchars(inp[i], 1)) |
| 22 | endfor |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 23 | call assert_fails("let v=strchars('abc', [])", 'E745:') |
Bram Moolenaar | 0e05de4 | 2020-03-25 22:23:46 +0100 | [diff] [blame] | 24 | call assert_fails("let v=strchars('abc', 2)", 'E474:') |
Bram Moolenaar | 0c0590d | 2017-01-28 13:48:10 +0100 | [diff] [blame] | 25 | endfunc |
| 26 | |
| 27 | " Test for customlist completion |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 28 | func CustomComplete1(lead, line, pos) |
Bram Moolenaar | 0c0590d | 2017-01-28 13:48:10 +0100 | [diff] [blame] | 29 | return ['あ', 'い'] |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 30 | endfunc |
Bram Moolenaar | 0c0590d | 2017-01-28 13:48:10 +0100 | [diff] [blame] | 31 | |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 32 | func CustomComplete2(lead, line, pos) |
Bram Moolenaar | 0c0590d | 2017-01-28 13:48:10 +0100 | [diff] [blame] | 33 | return ['あたし', 'あたま', 'あたりめ'] |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 34 | endfunc |
Bram Moolenaar | 0c0590d | 2017-01-28 13:48:10 +0100 | [diff] [blame] | 35 | |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 36 | func CustomComplete3(lead, line, pos) |
Bram Moolenaar | 0c0590d | 2017-01-28 13:48:10 +0100 | [diff] [blame] | 37 | return ['Nこ', 'Nん', 'Nぶ'] |
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 | |
| 40 | func 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) |
| 54 | endfunc |
| 55 | |
| 56 | " Yank one 3 byte character and check the mark columns. |
| 57 | func 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("']")) |
| 65 | endfunc |
Bram Moolenaar | 2912abb | 2019-03-29 14:16:42 +0100 | [diff] [blame] | 66 | |
Bram Moolenaar | 9d40128 | 2019-04-06 13:18:12 +0200 | [diff] [blame] | 67 | func 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 Moolenaar | 02b3111 | 2019-08-31 22:16:38 +0200 | [diff] [blame] | 82 | call assert_equal(s, l->list2str(1)) |
Bram Moolenaar | 9d40128 | 2019-04-06 13:18:12 +0200 | [diff] [blame] | 83 | 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())) |
| 91 | endfunc |
| 92 | |
| 93 | func 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 Moolenaar | 0e05de4 | 2020-03-25 22:23:46 +0100 | [diff] [blame] | 104 | call assert_equal([65, 66, 67], str2list("ABC")) |
Bram Moolenaar | 9d40128 | 2019-04-06 13:18:12 +0200 | [diff] [blame] | 105 | |
Bram Moolenaar | 08f4157 | 2020-04-20 16:50:00 +0200 | [diff] [blame] | 106 | " Try converting a list to a string in latin-1 encoding |
| 107 | call assert_equal([1, 2, 3], str2list(list2str([1, 2, 3]))) |
| 108 | |
Bram Moolenaar | 9d40128 | 2019-04-06 13:18:12 +0200 | [diff] [blame] | 109 | let &encoding = save_encoding |
| 110 | call assert_equal(l, lres) |
| 111 | call assert_equal(s, sres) |
| 112 | endfunc |
| 113 | |
Bram Moolenaar | 2912abb | 2019-03-29 14:16:42 +0100 | [diff] [blame] | 114 | func Test_screenchar_utf8() |
| 115 | new |
| 116 | |
| 117 | " 1-cell, with composing characters |
| 118 | call setline(1, ["ABC\u0308"]) |
| 119 | redraw |
| 120 | call assert_equal([0x0041], screenchars(1, 1)) |
Bram Moolenaar | 196b466 | 2019-09-06 21:34:30 +0200 | [diff] [blame] | 121 | call assert_equal([0x0042], 1->screenchars(2)) |
Bram Moolenaar | 2912abb | 2019-03-29 14:16:42 +0100 | [diff] [blame] | 122 | call assert_equal([0x0043, 0x0308], screenchars(1, 3)) |
| 123 | call assert_equal("A", screenstring(1, 1)) |
| 124 | call assert_equal("B", screenstring(1, 2)) |
| 125 | call assert_equal("C\u0308", screenstring(1, 3)) |
| 126 | |
| 127 | " 2-cells, with composing characters |
| 128 | let text = "\u3042\u3044\u3046\u3099" |
| 129 | call setline(1, text) |
| 130 | redraw |
| 131 | call assert_equal([0x3042], screenchars(1, 1)) |
| 132 | call assert_equal([0], screenchars(1, 2)) |
| 133 | call assert_equal([0x3044], screenchars(1, 3)) |
| 134 | call assert_equal([0], screenchars(1, 4)) |
| 135 | call assert_equal([0x3046, 0x3099], screenchars(1, 5)) |
| 136 | |
| 137 | call assert_equal("\u3042", screenstring(1, 1)) |
| 138 | call assert_equal("", screenstring(1, 2)) |
| 139 | call assert_equal("\u3044", screenstring(1, 3)) |
| 140 | call assert_equal("", screenstring(1, 4)) |
| 141 | call assert_equal("\u3046\u3099", screenstring(1, 5)) |
| 142 | |
Bram Moolenaar | 48aed08 | 2019-03-30 15:44:17 +0100 | [diff] [blame] | 143 | call assert_equal([text . ' '], ScreenLines(1, 8)) |
Bram Moolenaar | 2912abb | 2019-03-29 14:16:42 +0100 | [diff] [blame] | 144 | |
| 145 | bwipe! |
| 146 | endfunc |
Bram Moolenaar | 0e05de4 | 2020-03-25 22:23:46 +0100 | [diff] [blame] | 147 | |
| 148 | " vim: shiftwidth=2 sts=2 expandtab |