test_utf8.vim 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. " Tests for Unicode manipulations
  2. source check.vim
  3. source view_util.vim
  4. source screendump.vim
  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. " Test for built-in functions strchars() and strcharlen()
  14. func Test_strchars()
  15. let inp = ["a", "あいa", "A\u20dd", "A\u20dd\u20dd", "\u20dd"]
  16. let exp = [[1, 1, 1], [3, 3, 3], [2, 2, 1], [3, 3, 1], [1, 1, 1]]
  17. for i in range(len(inp))
  18. call assert_equal(exp[i][0], strchars(inp[i]))
  19. call assert_equal(exp[i][1], inp[i]->strchars(0))
  20. call assert_equal(exp[i][2], strchars(inp[i], 1))
  21. endfor
  22. let exp = [1, 3, 1, 1, 1]
  23. for i in range(len(inp))
  24. call assert_equal(exp[i], inp[i]->strcharlen())
  25. call assert_equal(exp[i], strcharlen(inp[i]))
  26. endfor
  27. call assert_fails("call strchars('abc', 2)", ['E1023:', 'E1023:'])
  28. call assert_fails("call strchars('abc', -1)", ['E1023:', 'E1023:'])
  29. call assert_fails("call strchars('abc', {})", ['E728:', 'E728:'])
  30. call assert_fails("call strchars('abc', [])", ['E745:', 'E745:'])
  31. endfunc
  32. " Test for customlist completion
  33. func CustomComplete1(lead, line, pos)
  34. return ['あ', 'い']
  35. endfunc
  36. func CustomComplete2(lead, line, pos)
  37. return ['あたし', 'あたま', 'あたりめ']
  38. endfunc
  39. func CustomComplete3(lead, line, pos)
  40. return ['Nこ', 'Nん', 'Nぶ']
  41. endfunc
  42. func Test_customlist_completion()
  43. command -nargs=1 -complete=customlist,CustomComplete1 Test1 echo
  44. call feedkeys(":Test1 \<C-L>\<C-B>\"\<CR>", 'itx')
  45. call assert_equal('"Test1 ', getreg(':'))
  46. command -nargs=1 -complete=customlist,CustomComplete2 Test2 echo
  47. call feedkeys(":Test2 \<C-L>\<C-B>\"\<CR>", 'itx')
  48. call assert_equal('"Test2 あた', getreg(':'))
  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. call garbagecollect(1)
  53. delcommand Test1
  54. delcommand Test2
  55. delcommand Test3
  56. endfunc
  57. " Yank one 3 byte character and check the mark columns.
  58. func Test_getvcol()
  59. new
  60. call setline(1, "x\u2500x")
  61. normal 0lvy
  62. call assert_equal(2, col("'["))
  63. call assert_equal(4, col("']"))
  64. call assert_equal(2, virtcol("'["))
  65. call assert_equal(2, virtcol("']"))
  66. endfunc
  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. " With composing characters
  78. let s = "\u304b\u3099\u3044"
  79. let l = [0x304b, 0x3099, 0x3044]
  80. call assert_equal(l, str2list(s, 1))
  81. call assert_equal(s, l->list2str(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. " Null list is the same as an empty list
  87. call assert_equal('', list2str([]))
  88. call assert_equal('', list2str(v:_null_list))
  89. endfunc
  90. func Test_list2str_str2list_latin1()
  91. " When 'encoding' is not multi-byte can still get utf-8 string.
  92. " But we need to create the utf-8 string while 'encoding' is utf-8.
  93. let s = "\u3042\u3044"
  94. let l = [0x3042, 0x3044]
  95. let save_encoding = &encoding
  96. " set encoding=latin1
  97. let lres = str2list(s, 1)
  98. let sres = list2str(l, 1)
  99. call assert_equal([65, 66, 67], str2list("ABC"))
  100. " Try converting a list to a string in latin-1 encoding
  101. call assert_equal([1, 2, 3], str2list(list2str([1, 2, 3])))
  102. let &encoding = save_encoding
  103. call assert_equal(l, lres)
  104. call assert_equal(s, sres)
  105. endfunc
  106. func Test_screenchar_utf8()
  107. new
  108. " 1-cell, with composing characters
  109. call setline(1, ["ABC\u0308"])
  110. redraw
  111. call assert_equal([0x0041], screenchars(1, 1))
  112. call assert_equal([0x0042], 1->screenchars(2))
  113. call assert_equal([0x0043, 0x0308], screenchars(1, 3))
  114. call assert_equal("A", screenstring(1, 1))
  115. call assert_equal("B", screenstring(1, 2))
  116. call assert_equal("C\u0308", screenstring(1, 3))
  117. " 1-cell, with 6 composing characters
  118. set maxcombine=6
  119. call setline(1, ["ABC" .. repeat("\u0308", 6)])
  120. redraw
  121. call assert_equal([0x0041], screenchars(1, 1))
  122. call assert_equal([0x0042], 1->screenchars(2))
  123. " This should not use uninitialized memory
  124. call assert_equal([0x0043] + repeat([0x0308], 6), screenchars(1, 3))
  125. call assert_equal("A", screenstring(1, 1))
  126. call assert_equal("B", screenstring(1, 2))
  127. call assert_equal("C" .. repeat("\u0308", 6), screenstring(1, 3))
  128. set maxcombine&
  129. " 2-cells, with composing characters
  130. let text = "\u3042\u3044\u3046\u3099"
  131. call setline(1, text)
  132. redraw
  133. call assert_equal([0x3042], screenchars(1, 1))
  134. call assert_equal([0], screenchars(1, 2))
  135. call assert_equal([0x3044], screenchars(1, 3))
  136. call assert_equal([0], screenchars(1, 4))
  137. call assert_equal([0x3046, 0x3099], screenchars(1, 5))
  138. call assert_equal("\u3042", screenstring(1, 1))
  139. call assert_equal("", screenstring(1, 2))
  140. call assert_equal("\u3044", screenstring(1, 3))
  141. call assert_equal("", screenstring(1, 4))
  142. call assert_equal("\u3046\u3099", screenstring(1, 5))
  143. call assert_equal([text . ' '], ScreenLines(1, 8))
  144. bwipe!
  145. endfunc
  146. func Test_setcellwidths()
  147. new
  148. call setcellwidths([
  149. \ [0x1330, 0x1330, 2],
  150. \ [9999, 10000, 1],
  151. \ [0x1337, 0x1339, 2],
  152. \])
  153. call assert_equal(2, strwidth("\u1330"))
  154. call assert_equal(1, strwidth("\u1336"))
  155. call assert_equal(2, strwidth("\u1337"))
  156. call assert_equal(2, strwidth("\u1339"))
  157. call assert_equal(1, strwidth("\u133a"))
  158. for aw in ['single', 'double']
  159. exe 'set ambiwidth=' . aw
  160. " Handle \u0080 to \u009F as control chars even on MS-Windows.
  161. set isprint=@,161-255
  162. call setcellwidths([])
  163. " Control chars
  164. call assert_equal(4, strwidth("\u0081"))
  165. call assert_equal(6, strwidth("\uFEFF"))
  166. " Ambiguous width chars
  167. call assert_equal((aw == 'single') ? 1 : 2, strwidth("\u00A1"))
  168. call assert_equal((aw == 'single') ? 1 : 2, strwidth("\u2010"))
  169. call setcellwidths([[0x81, 0x81, 1], [0xA1, 0xA1, 1],
  170. \ [0x2010, 0x2010, 1], [0xFEFF, 0xFEFF, 1]])
  171. " Control chars
  172. call assert_equal(4, strwidth("\u0081"))
  173. call assert_equal(6, strwidth("\uFEFF"))
  174. " Ambiguous width chars
  175. call assert_equal(1, strwidth("\u00A1"))
  176. call assert_equal(1, strwidth("\u2010"))
  177. call setcellwidths([[0x81, 0x81, 2], [0xA1, 0xA1, 2],
  178. \ [0x2010, 0x2010, 2], [0xFEFF, 0xFEFF, 2]])
  179. " Control chars
  180. call assert_equal(4, strwidth("\u0081"))
  181. call assert_equal(6, strwidth("\uFEFF"))
  182. " Ambiguous width chars
  183. call assert_equal(2, strwidth("\u00A1"))
  184. call assert_equal(2, strwidth("\u2010"))
  185. call setcellwidths([])
  186. call setline(1, repeat("\u2103", 10))
  187. normal! $
  188. redraw
  189. call assert_equal((aw == 'single') ? 10 : 19, wincol())
  190. call setcellwidths([[0x2103, 0x2103, 1]])
  191. redraw
  192. call assert_equal(10, wincol())
  193. call setcellwidths([[0x2103, 0x2103, 2]])
  194. redraw
  195. call assert_equal(19, wincol())
  196. call setcellwidths([])
  197. redraw
  198. call assert_equal((aw == 'single') ? 10 : 19, wincol())
  199. endfor
  200. set ambiwidth& isprint&
  201. call setcellwidths([])
  202. call assert_fails('call setcellwidths(1)', 'E714:')
  203. call assert_fails('call setcellwidths([1, 2, 0])', 'E1109:')
  204. call assert_fails('call setcellwidths([[0x101]])', 'E1110:')
  205. call assert_fails('call setcellwidths([[0x101, 0x102]])', 'E1110:')
  206. call assert_fails('call setcellwidths([[0x101, 0x102, 1, 4]])', 'E1110:')
  207. call assert_fails('call setcellwidths([["a"]])', 'E1110:')
  208. call assert_fails('call setcellwidths([[0x102, 0x101, 1]])', 'E1111:')
  209. call assert_fails('call setcellwidths([[0x101, 0x102, 0]])', 'E1112:')
  210. call assert_fails('call setcellwidths([[0x101, 0x102, 3]])', 'E1112:')
  211. call assert_fails('call setcellwidths([[0x111, 0x122, 1], [0x115, 0x116, 2]])', 'E1113:')
  212. call assert_fails('call setcellwidths([[0x111, 0x122, 1], [0x122, 0x123, 2]])', 'E1113:')
  213. call assert_fails('call setcellwidths([[0x33, 0x44, 2]])', 'E1114:')
  214. set listchars=tab:--\\u2192 fillchars=stl:\\u2501
  215. call assert_fails('call setcellwidths([[0x2192, 0x2192, 2]])', 'E834:')
  216. call assert_fails('call setcellwidths([[0x2501, 0x2501, 2]])', 'E835:')
  217. call setcellwidths([[0x201c, 0x201d, 1]])
  218. set listchars& fillchars& ambiwidth=double
  219. set listchars=nbsp:\\u201c fillchars=vert:\\u201d
  220. call assert_fails('call setcellwidths([])', 'E834:')
  221. set listchars&
  222. call assert_fails('call setcellwidths([])', 'E835:')
  223. set fillchars&
  224. call setcellwidths([])
  225. set ambiwidth&
  226. bwipe!
  227. endfunc
  228. func Test_getcellwidths()
  229. call setcellwidths([])
  230. call assert_equal([], getcellwidths())
  231. let widthlist = [
  232. \ [0x1330, 0x1330, 2],
  233. \ [9999, 10000, 1],
  234. \ [0x1337, 0x1339, 2],
  235. \]
  236. let widthlistsorted = [
  237. \ [0x1330, 0x1330, 2],
  238. \ [0x1337, 0x1339, 2],
  239. \ [9999, 10000, 1],
  240. \]
  241. call setcellwidths(widthlist)
  242. call assert_equal(widthlistsorted, getcellwidths())
  243. call setcellwidths([])
  244. endfunc
  245. func Test_setcellwidths_dump()
  246. CheckRunVimInTerminal
  247. let lines =<< trim END
  248. call setline(1, "\ue5ffDesktop")
  249. END
  250. call writefile(lines, 'XCellwidths', 'D')
  251. let buf = RunVimInTerminal('-S XCellwidths', {'rows': 6})
  252. call VerifyScreenDump(buf, 'Test_setcellwidths_dump_1', {})
  253. call term_sendkeys(buf, ":call setcellwidths([[0xe5ff, 0xe5ff, 2]])\<CR>")
  254. call VerifyScreenDump(buf, 'Test_setcellwidths_dump_2', {})
  255. call StopVimInTerminal(buf)
  256. endfunc
  257. " Test setcellwidths() on characters that are not targets of 'ambiwidth'.
  258. func Test_setcellwidths_with_non_ambiwidth_character_dump()
  259. CheckRunVimInTerminal
  260. let lines =<< trim END
  261. call setline(1, [repeat("\u279c", 60), repeat("\u279c", 60)])
  262. set ambiwidth=single
  263. END
  264. call writefile(lines, 'XCellwidthsWithNonAmbiwidthCharacter', 'D')
  265. let buf = RunVimInTerminal('-S XCellwidthsWithNonAmbiwidthCharacter', {'rows': 6, 'cols': 50})
  266. call term_sendkeys(buf, ":call setcellwidths([[0x279c, 0x279c, 1]])\<CR>")
  267. call term_sendkeys(buf, ":echo\<CR>")
  268. call VerifyScreenDump(buf, 'Test_setcellwidths_with_non_ambiwidth_character_dump_1', {})
  269. call term_sendkeys(buf, ":call setcellwidths([[0x279c, 0x279c, 2]])\<CR>")
  270. call term_sendkeys(buf, ":echo\<CR>")
  271. call VerifyScreenDump(buf, 'Test_setcellwidths_with_non_ambiwidth_character_dump_2', {})
  272. call StopVimInTerminal(buf)
  273. endfunc
  274. " For some reason this test causes Test_customlist_completion() to fail on CI,
  275. " so run it as the last test.
  276. func Test_zz_ambiwidth_hl_dump()
  277. CheckRunVimInTerminal
  278. let lines =<< trim END
  279. call setline(1, [repeat("\u2103", 60), repeat("\u2103", 60)])
  280. set ambiwidth=single cursorline list display=lastline
  281. END
  282. call writefile(lines, 'XAmbiwidthHl', 'D')
  283. let buf = RunVimInTerminal('-S XAmbiwidthHl', {'rows': 6, 'cols': 50})
  284. call VerifyScreenDump(buf, 'Test_ambiwidth_hl_dump_1', {})
  285. call term_sendkeys(buf, ":set ambiwidth=double\<CR>")
  286. call term_sendkeys(buf, ":echo\<CR>")
  287. call VerifyScreenDump(buf, 'Test_ambiwidth_hl_dump_2', {})
  288. call term_sendkeys(buf, ":set ambiwidth=single\<CR>")
  289. call term_sendkeys(buf, ":echo\<CR>")
  290. call VerifyScreenDump(buf, 'Test_ambiwidth_hl_dump_1', {})
  291. call term_sendkeys(buf, ":call setcellwidths([[0x2103, 0x2103, 2]])\<CR>")
  292. call term_sendkeys(buf, ":echo\<CR>")
  293. call VerifyScreenDump(buf, 'Test_ambiwidth_hl_dump_2', {})
  294. call term_sendkeys(buf, ":call setcellwidths([[0x2103, 0x2103, 1]])\<CR>")
  295. call term_sendkeys(buf, ":echo\<CR>")
  296. call VerifyScreenDump(buf, 'Test_ambiwidth_hl_dump_1', {})
  297. call StopVimInTerminal(buf)
  298. endfunc
  299. func Test_print_overlong()
  300. " Text with more composing characters than MB_MAXBYTES.
  301. new
  302. call setline(1, 'axxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
  303. s/x/\=nr2char(1629)/g
  304. print
  305. bwipe!
  306. endfunc
  307. " vim: shiftwidth=2 sts=2 expandtab