xref: /vim-8.2.3635/src/testdir/test_ruby.vim (revision d84b26a0)
1" Tests for ruby interface
2
3if !has('ruby')
4  finish
5end
6
7func Test_ruby_change_buffer()
8  call setline(line('$'), ['1 line 1'])
9  ruby Vim.command("normal /^1\n")
10  ruby $curbuf.line = "1 changed line 1"
11  call assert_equal('1 changed line 1', getline('$'))
12endfunc
13
14func Test_ruby_evaluate_list()
15  call setline(line('$'), ['2 line 2'])
16  ruby Vim.command("normal /^2\n")
17  let l = ["abc", "def"]
18  ruby << EOF
19  curline = $curbuf.line_number
20  l = Vim.evaluate("l");
21  $curbuf.append(curline, l.join("\n"))
22EOF
23  normal j
24  .rubydo $_ = $_.gsub(/\n/, '/')
25  call assert_equal('abc/def', getline('$'))
26endfunc
27
28func Test_ruby_evaluate_dict()
29  let d = {'a': 'foo', 'b': 123}
30  redir => l:out
31  ruby d = Vim.evaluate("d"); print d
32  redir END
33  call assert_equal(['{"a"=>"foo", "b"=>123}'], split(l:out, "\n"))
34endfunc
35
36func Test_ruby_evaluate_special_var()
37  let l = [v:true, v:false, v:null, v:none]
38  redir => l:out
39  ruby d = Vim.evaluate("l"); print d
40  redir END
41  call assert_equal(['[true, false, nil, nil]'], split(l:out, "\n"))
42endfunc
43
44func Test_rubydo()
45  " Check deleting lines does not trigger ml_get error.
46  new
47  call setline(1, ['one', 'two', 'three'])
48  rubydo Vim.command("%d_")
49  bwipe!
50
51  " Check switching to another buffer does not trigger ml_get error.
52  new
53  let wincount = winnr('$')
54  call setline(1, ['one', 'two', 'three'])
55  rubydo Vim.command("new")
56  call assert_equal(wincount + 1, winnr('$'))
57  bwipe!
58  bwipe!
59endfunc
60
61func Test_rubyfile()
62  " Check :rubyfile does not SEGV with Ruby level exception but just fails
63  let tempfile = tempname() . '.rb'
64  call writefile(['raise "vim!"'], tempfile)
65  call assert_fails('rubyfile ' . tempfile)
66  call delete(tempfile)
67endfunc
68
69func Test_set_cursor()
70  " Check that setting the cursor position works.
71  new
72  call setline(1, ['first line', 'second line'])
73  normal gg
74  rubydo $curwin.cursor = [1, 5]
75  call assert_equal([1, 6], [line('.'), col('.')])
76
77  " Check that movement after setting cursor position keeps current column.
78  normal j
79  call assert_equal([2, 6], [line('.'), col('.')])
80endfunc
81