test_perl.vim 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. " Tests for Perl interface
  2. source check.vim
  3. CheckFeature perl
  4. CheckNotMSWindows
  5. " FIXME: RunTest don't see any error when Perl abort...
  6. perl $SIG{__WARN__} = sub { die "Unexpected warnings from perl: @_" };
  7. func Test_change_buffer()
  8. call setline(line('$'), ['1 line 1'])
  9. perl VIM::DoCommand("normal /^1\n")
  10. perl $curline = VIM::Eval("line('.')")
  11. perl $curbuf->Set($curline, "1 changed line 1")
  12. call assert_equal('1 changed line 1', getline('$'))
  13. endfunc
  14. func Test_evaluate_list()
  15. call setline(line('$'), ['2 line 2'])
  16. perl VIM::DoCommand("normal /^2\n")
  17. perl $curline = VIM::Eval("line('.')")
  18. let l = ["abc", "def"]
  19. perl << EOF
  20. $l = VIM::Eval("l");
  21. $curbuf->Append($curline, $l);
  22. EOF
  23. normal j
  24. .perldo s|\n|/|g
  25. " call assert_equal('abc/def/', getline('$'))
  26. call assert_equal('def', getline('$'))
  27. endfunc
  28. funct Test_VIM_Blob()
  29. call assert_equal('0z', perleval('VIM::Blob("")'))
  30. call assert_equal('0z31326162', 'VIM::Blob("12ab")'->perleval())
  31. call assert_equal('0z00010203', perleval('VIM::Blob("\x00\x01\x02\x03")'))
  32. call assert_equal('0z8081FEFF', perleval('VIM::Blob("\x80\x81\xfe\xff")'))
  33. endfunc
  34. func Test_buffer_Delete()
  35. new
  36. call setline(1, ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'])
  37. perl $curbuf->Delete(7)
  38. perl $curbuf->Delete(2, 5)
  39. perl $curbuf->Delete(10)
  40. call assert_equal(['a', 'f', 'h'], getline(1, '$'))
  41. bwipe!
  42. endfunc
  43. func Test_buffer_Append()
  44. new
  45. perl $curbuf->Append(1, '1')
  46. perl $curbuf->Append(2, '2', '3', '4')
  47. perl @l = ('5' ..'7')
  48. perl $curbuf->Append(0, @l)
  49. call assert_equal(['5', '6', '7', '', '1', '2', '3', '4'], getline(1, '$'))
  50. bwipe!
  51. endfunc
  52. func Test_buffer_Set()
  53. new
  54. call setline(1, ['1', '2', '3', '4', '5'])
  55. perl $curbuf->Set(2, 'a', 'b', 'c')
  56. perl $curbuf->Set(4, 'A', 'B', 'C')
  57. call assert_equal(['1', 'a', 'b', 'A', 'B'], getline(1, '$'))
  58. bwipe!
  59. endfunc
  60. func Test_buffer_Get()
  61. new
  62. call setline(1, ['1', '2', '3', '4'])
  63. call assert_equal('2:3', perleval('join(":", $curbuf->Get(2, 3))'))
  64. bwipe!
  65. endfunc
  66. func Test_buffer_Count()
  67. new
  68. call setline(1, ['a', 'b', 'c'])
  69. call assert_equal(3, perleval('$curbuf->Count()'))
  70. bwipe!
  71. endfunc
  72. func Test_buffer_Name()
  73. new
  74. call assert_equal('', perleval('$curbuf->Name()'))
  75. bwipe!
  76. new Xfoo
  77. call assert_equal('Xfoo', perleval('$curbuf->Name()'))
  78. bwipe!
  79. endfunc
  80. func Test_buffer_Number()
  81. call assert_equal(bufnr('%'), perleval('$curbuf->Number()'))
  82. endfunc
  83. func Test_window_Cursor()
  84. throw 'skipped: flaky '
  85. new
  86. call setline(1, ['line1', 'line2'])
  87. perl $curwin->Cursor(2, 3)
  88. call assert_equal('2:3', perleval('join(":", $curwin->Cursor())'))
  89. " Col is numbered from 0 in Perl, and from 1 in Vim script.
  90. call assert_equal([0, 2, 4, 0], getpos('.'))
  91. bwipe!
  92. endfunc
  93. func Test_window_SetHeight()
  94. throw 'skipped: flaky '
  95. new
  96. perl $curwin->SetHeight(2)
  97. call assert_equal(2, winheight(0))
  98. bwipe!
  99. endfunc
  100. func Test_VIM_Windows()
  101. new
  102. " VIM::Windows() without argument in scalar and list context.
  103. perl $winnr = VIM::Windows()
  104. perl @winlist = VIM::Windows()
  105. perl $curbuf->Append(0, $winnr, scalar(@winlist))
  106. call assert_equal(['2', '2', ''], getline(1, '$'))
  107. " VIM::Windows() with window number argument.
  108. perl (VIM::Windows(VIM::Eval('winnr()')))[0]->Buffer()->Set(1, 'bar')
  109. call assert_equal('bar', getline(1))
  110. bwipe!
  111. endfunc
  112. func Test_VIM_Buffers()
  113. new Xbar
  114. " VIM::Buffers() without argument in scalar and list context.
  115. perl $nbuf = VIM::Buffers()
  116. perl @buflist = VIM::Buffers()
  117. " VIM::Buffers() with argument.
  118. perl $curbuf = (VIM::Buffers('Xbar'))[0]
  119. perl $curbuf->Append(0, $nbuf, scalar(@buflist))
  120. call assert_equal(['2', '2', ''], getline(1, '$'))
  121. bwipe!
  122. endfunc
  123. func <SID>catch_peval(expr)
  124. try
  125. call perleval(a:expr)
  126. catch
  127. return v:exception
  128. endtry
  129. call assert_report('no exception for `perleval("'.a:expr.'")`')
  130. return ''
  131. endfunc
  132. func Test_perleval()
  133. call assert_false(perleval('undef'))
  134. " scalar
  135. call assert_equal(0, perleval('0'))
  136. call assert_equal(2, perleval('2'))
  137. call assert_equal(-2, perleval('-2'))
  138. if has('float')
  139. call assert_equal(2.5, perleval('2.5'))
  140. else
  141. call assert_equal(2, perleval('2.5'))
  142. end
  143. " sandbox call assert_equal(2, perleval('2'))
  144. call assert_equal('abc', perleval('"abc"'))
  145. " call assert_equal("abc\ndef", perleval('"abc\0def"'))
  146. " ref
  147. call assert_equal([], perleval('[]'))
  148. call assert_equal(['word', 42, [42],{}], perleval('["word", 42, [42], {}]'))
  149. call assert_equal({}, perleval('{}'))
  150. call assert_equal({'foo': 'bar'}, perleval('{foo => "bar"}'))
  151. perl our %h; our @a;
  152. let a = perleval('[\(%h, %h, @a, @a)]')
  153. " call assert_true((a[0] is a[1]))
  154. call assert_equal(a[0], a[1])
  155. " call assert_true((a[2] is a[3]))
  156. call assert_equal(a[2], a[3])
  157. perl undef %h; undef @a;
  158. " call assert_true(<SID>catch_peval('{"" , 0}') =~ 'Malformed key Dictionary')
  159. " call assert_true(<SID>catch_peval('{"\0" , 0}') =~ 'Malformed key Dictionary')
  160. " call assert_true(<SID>catch_peval('{"foo\0bar" , 0}') =~ 'Malformed key Dictionary')
  161. call assert_equal('*VIM', perleval('"*VIM"'))
  162. " call assert_true(perleval('\\0') =~ 'SCALAR(0x\x\+)')
  163. endfunc
  164. func Test_perldo()
  165. sp __TEST__
  166. exe 'read ' g:testname
  167. perldo s/perl/vieux_chameau/g
  168. 1
  169. call assert_false(search('\Cperl'))
  170. bw!
  171. " Check deleting lines does not trigger ml_get error.
  172. new
  173. call setline(1, ['one', 'two', 'three'])
  174. perldo VIM::DoCommand("%d_")
  175. bwipe!
  176. " Check switching to another buffer does not trigger ml_get error.
  177. new
  178. let wincount = winnr('$')
  179. call setline(1, ['one', 'two', 'three'])
  180. perldo VIM::DoCommand("new")
  181. call assert_equal(wincount + 1, winnr('$'))
  182. bwipe!
  183. bwipe!
  184. endfunc
  185. func Test_VIM_package()
  186. perl VIM::DoCommand('let l:var = "foo"')
  187. call assert_equal(l:var, 'foo')
  188. set noet
  189. perl VIM::SetOption('et')
  190. call assert_true(&et)
  191. endfunc
  192. func Test_stdio()
  193. throw 'skipped: TODO: '
  194. redir =>l:out
  195. perl << trim EOF
  196. VIM::Msg("&VIM::Msg");
  197. print "STDOUT";
  198. print STDERR "STDERR";
  199. EOF
  200. redir END
  201. call assert_equal(['&VIM::Msg', 'STDOUT', 'STDERR'], split(l:out, "\n"))
  202. endfunc
  203. " Run first to get a clean namespace
  204. func Test_000_SvREFCNT()
  205. throw 'skipped: TODO: '
  206. for i in range(8)
  207. exec 'new X'.i
  208. endfor
  209. new t
  210. perl <<--perl
  211. #line 5 "Test_000_SvREFCNT()"
  212. my ($b, $w);
  213. my $num = 0;
  214. for ( 0 .. 100 ) {
  215. if ( ++$num >= 8 ) { $num = 0 }
  216. VIM::DoCommand("buffer X$num");
  217. $b = $curbuf;
  218. }
  219. VIM::DoCommand("buffer t");
  220. $b = $curbuf for 0 .. 100;
  221. $w = $curwin for 0 .. 100;
  222. () = VIM::Buffers for 0 .. 100;
  223. () = VIM::Windows for 0 .. 100;
  224. VIM::DoCommand('bw! t');
  225. if (exists &Internals::SvREFCNT) {
  226. my $cb = Internals::SvREFCNT($$b);
  227. my $cw = Internals::SvREFCNT($$w);
  228. VIM::Eval("assert_equal(2, $cb, 'T1')");
  229. VIM::Eval("assert_equal(2, $cw, 'T2')");
  230. my $strongref;
  231. foreach ( VIM::Buffers, VIM::Windows ) {
  232. VIM::DoCommand("%bw!");
  233. my $c = Internals::SvREFCNT($_);
  234. VIM::Eval("assert_equal(2, $c, 'T3')");
  235. $c = Internals::SvREFCNT($$_);
  236. next if $c == 2 && !$strongref++;
  237. VIM::Eval("assert_equal(1, $c, 'T4')");
  238. }
  239. $cb = Internals::SvREFCNT($$curbuf);
  240. $cw = Internals::SvREFCNT($$curwin);
  241. VIM::Eval("assert_equal(3, $cb, 'T5')");
  242. VIM::Eval("assert_equal(3, $cw, 'T6')");
  243. }
  244. VIM::Eval("assert_false($$b)");
  245. VIM::Eval("assert_false($$w)");
  246. --perl
  247. %bw!
  248. endfunc
  249. func Test_set_cursor()
  250. " Check that setting the cursor position works.
  251. new
  252. call setline(1, ['first line', 'second line'])
  253. normal gg
  254. perldo $curwin->Cursor(1, 5)
  255. call assert_equal([1, 6], [line('.'), col('.')])
  256. " Check that movement after setting cursor position keeps current column.
  257. normal j
  258. call assert_equal([2, 6], [line('.'), col('.')])
  259. endfunc
  260. " Test for various heredoc syntax
  261. func Test_perl_heredoc()
  262. perl << END
  263. VIM::DoCommand('let s = "A"')
  264. END
  265. perl <<
  266. VIM::DoCommand('let s ..= "B"')
  267. .
  268. perl << trim END
  269. VIM::DoCommand('let s ..= "C"')
  270. END
  271. perl << trim
  272. VIM::DoCommand('let s ..= "D"')
  273. .
  274. perl << trim eof
  275. VIM::DoCommand('let s ..= "E"')
  276. eof
  277. call assert_equal('ABCDE', s)
  278. endfunc
  279. " vim: shiftwidth=2 sts=2 expandtab