Skip to content

Commit edd6aac

Browse files
committed
patch 8.1.0221: not enough testing for the Ruby interface
Problem: Not enough testing for the Ruby interface. Solution: Add more tests. (Dominique Pelle, closes #3252)
1 parent d84b26a commit edd6aac

File tree

3 files changed

+322
-33
lines changed

3 files changed

+322
-33
lines changed

runtime/doc/if_ruby.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ self[{n}] Returns the buffer object for the number {n}. The first number
145145

146146
Methods:
147147

148-
name Returns the name of the buffer.
148+
name Returns the full name of the buffer.
149149
number Returns the number of the buffer.
150150
count Returns the number of lines.
151151
length Returns the number of lines.
@@ -181,6 +181,7 @@ height = {n} Sets the window height to {n}.
181181
width Returns the width of the window.
182182
width = {n} Sets the window width to {n}.
183183
cursor Returns a [row, col] array for the cursor position.
184+
First line number is 1 and first column number is 0.
184185
cursor = [{row}, {col}]
185186
Sets the cursor position to {row} and {col}.
186187

src/testdir/test_ruby.vim

Lines changed: 318 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,20 @@ if !has('ruby')
44
finish
55
end
66

7+
" Helper function as there is no builtin rubyeval() function similar
8+
" to perleval, luaevel() or pyeval().
9+
func RubyEval(ruby_expr)
10+
let s = split(execute('ruby print ' . a:ruby_expr), "\n")
11+
return (len(s) == 0) ? '' : s[-1]
12+
endfunc
13+
714
func Test_ruby_change_buffer()
815
call setline(line('$'), ['1 line 1'])
916
ruby Vim.command("normal /^1\n")
1017
ruby $curbuf.line = "1 changed line 1"
1118
call assert_equal('1 changed line 1', getline('$'))
1219
endfunc
1320

14-
func 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"))
22-
EOF
23-
normal j
24-
.rubydo $_ = $_.gsub(/\n/, '/')
25-
call assert_equal('abc/def', getline('$'))
26-
endfunc
27-
28-
func 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"))
34-
endfunc
35-
36-
func 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"))
42-
endfunc
43-
4421
func Test_rubydo()
4522
" Check deleting lines does not trigger ml_get error.
4623
new
@@ -54,8 +31,7 @@ func Test_rubydo()
5431
call setline(1, ['one', 'two', 'three'])
5532
rubydo Vim.command("new")
5633
call assert_equal(wincount + 1, winnr('$'))
57-
bwipe!
58-
bwipe!
34+
%bwipe!
5935
endfunc
6036

6137
func Test_rubyfile()
@@ -73,8 +49,318 @@ func Test_set_cursor()
7349
normal gg
7450
rubydo $curwin.cursor = [1, 5]
7551
call assert_equal([1, 6], [line('.'), col('.')])
52+
call assert_equal('[1, 5]', RubyEval('$curwin.cursor'))
7653

7754
" Check that movement after setting cursor position keeps current column.
7855
normal j
7956
call assert_equal([2, 6], [line('.'), col('.')])
57+
call assert_equal('[2, 5]', RubyEval('$curwin.cursor'))
58+
59+
call assert_fails('ruby $curwin.cursor = [1]',
60+
\ 'ArgumentError: array length must be 2')
61+
bwipe!
62+
endfunc
63+
64+
" Test buffer.count and buffer.length (number of lines in buffer)
65+
func Test_buffer_count()
66+
new
67+
call setline(1, ['one', 'two', 'three'])
68+
call assert_equal('3', RubyEval('$curbuf.count'))
69+
call assert_equal('3', RubyEval('$curbuf.length'))
70+
bwipe!
71+
endfunc
72+
73+
" Test buffer.name (buffer name)
74+
func Test_buffer_name()
75+
new Xfoo
76+
call assert_equal(expand('%:p'), RubyEval('$curbuf.name'))
77+
bwipe
78+
call assert_equal('', RubyEval('$curbuf.name'))
79+
endfunc
80+
81+
" Test buffer.number (number of the buffer).
82+
func Test_buffer_number()
83+
new
84+
call assert_equal(string(bufnr('%')), RubyEval('$curbuf.number'))
85+
new
86+
call assert_equal(string(bufnr('%')), RubyEval('$curbuf.number'))
87+
88+
%bwipe
89+
endfunc
90+
91+
" Test buffer.delete({n}) (delete line {n})
92+
func Test_buffer_delete()
93+
new
94+
call setline(1, ['one', 'two', 'three'])
95+
ruby $curbuf.delete(2)
96+
call assert_equal(['one', 'three'], getline(1, '$'))
97+
98+
call assert_fails('ruby $curbuf.delete(0)', 'IndexError: line number 0 out of range')
99+
call assert_fails('ruby $curbuf.delete(3)', 'IndexError: line number 3 out of range')
100+
101+
bwipe!
102+
endfunc
103+
104+
" Test buffer.append({str}, str) (append line {str} after line {n})
105+
func Test_buffer_append()
106+
new
107+
ruby $curbuf.append(0, 'one')
108+
ruby $curbuf.append(1, 'three')
109+
ruby $curbuf.append(1, 'two')
110+
ruby $curbuf.append(4, 'four')
111+
112+
call assert_equal(['one', 'two', 'three', '', 'four'], getline(1, '$'))
113+
114+
call assert_fails('ruby $curbuf.append(-1, "x")',
115+
\ 'IndexError: line number -1 out of range')
116+
call assert_fails('ruby $curbuf.append(6, "x")',
117+
\ 'IndexError: line number 6 out of range')
118+
119+
bwipe!
120+
endfunc
121+
122+
" Test buffer.line (get or set the current line)
123+
func Test_buffer_line()
124+
new
125+
call setline(1, ['one', 'two', 'three'])
126+
2
127+
call assert_equal('two', RubyEval('$curbuf.line'))
128+
129+
ruby $curbuf.line = 'TWO'
130+
call assert_equal(['one', 'TWO', 'three'], getline(1, '$'))
131+
132+
bwipe!
133+
endfunc
134+
135+
" Test buffer.line_number (get current line number)
136+
func Test_buffer_line_number()
137+
new
138+
call setline(1, ['one', 'two', 'three'])
139+
2
140+
call assert_equal('2', RubyEval('$curbuf.line_number'))
141+
142+
bwipe!
143+
endfunc
144+
145+
func Test_buffer_get()
146+
new
147+
call setline(1, ['one', 'two'])
148+
call assert_equal('one', RubyEval('$curbuf[1]'))
149+
call assert_equal('two', RubyEval('$curbuf[2]'))
150+
151+
call assert_fails('ruby $curbuf[0]',
152+
\ 'IndexError: line number 0 out of range')
153+
call assert_fails('ruby $curbuf[3]',
154+
\ 'IndexError: line number 3 out of range')
155+
156+
bwipe!
157+
endfunc
158+
159+
func Test_buffer_set()
160+
new
161+
call setline(1, ['one', 'two'])
162+
ruby $curbuf[2] = 'TWO'
163+
ruby $curbuf[1] = 'ONE'
164+
165+
call assert_fails('ruby $curbuf[0] = "ZERO"',
166+
\ 'IndexError: line number 0 out of range')
167+
call assert_fails('ruby $curbuf[3] = "THREE"',
168+
\ 'IndexError: line number 3 out of range')
169+
bwipe!
170+
endfunc
171+
172+
" Test window.width (get or set window height).
173+
func Test_window_height()
174+
new
175+
176+
" Test setting window height
177+
ruby $curwin.height = 2
178+
call assert_equal(2, winheight(0))
179+
180+
" Test getting window height
181+
call assert_equal('2', RubyEval('$curwin.height'))
182+
183+
bwipe
184+
endfunc
185+
186+
" Test window.width (get or set window width).
187+
func Test_window_width()
188+
vnew
189+
190+
" Test setting window width
191+
ruby $curwin.width = 2
192+
call assert_equal(2, winwidth(0))
193+
194+
" Test getting window width
195+
call assert_equal('2', RubyEval('$curwin.width'))
196+
197+
bwipe
198+
endfunc
199+
200+
" Test window.buffer (get buffer object of a window object).
201+
func Test_window_buffer()
202+
new Xfoo1
203+
new Xfoo2
204+
ruby $b2 = $curwin.buffer
205+
ruby $w2 = $curwin
206+
wincmd j
207+
ruby $b1 = $curwin.buffer
208+
ruby $w1 = $curwin
209+
210+
call assert_equal(RubyEval('$b1'), RubyEval('$w1.buffer'))
211+
call assert_equal(RubyEval('$b2'), RubyEval('$w2.buffer'))
212+
call assert_equal(string(bufnr('Xfoo1')), RubyEval('$w1.buffer.number'))
213+
call assert_equal(string(bufnr('Xfoo2')), RubyEval('$w2.buffer.number'))
214+
215+
ruby $b1, $w1, $b2, $w2 = nil
216+
%bwipe
217+
endfunc
218+
219+
" Test Vim::Window.current (get current window object)
220+
func Test_Vim_window_current()
221+
let cw = RubyEval('$curwin')
222+
call assert_equal(cw, RubyEval('Vim::Window.current'))
223+
call assert_match('^#<Vim::Window:0x\x\+>$', cw)
224+
endfunc
225+
226+
" Test Vim::Window.count (number of windows)
227+
func Test_Vim_window_count()
228+
new Xfoo1
229+
new Xfoo2
230+
split
231+
call assert_equal('4', RubyEval('Vim::Window.count'))
232+
%bwipe
233+
call assert_equal('1', RubyEval('Vim::Window.count'))
234+
endfunc
235+
236+
" Test Vim::Window[n] (get window object of window n)
237+
func Test_Vim_window_get()
238+
new Xfoo1
239+
new Xfoo2
240+
call assert_match('Xfoo2$', RubyEval('Vim::Window[0].buffer.name'))
241+
wincmd j
242+
call assert_match('Xfoo1$', RubyEval('Vim::Window[1].buffer.name'))
243+
wincmd j
244+
call assert_equal('', RubyEval('Vim::Window[2].buffer.name'))
245+
%bwipe
246+
endfunc
247+
248+
" Test Vim::Buffer.current (return the buffer object of current buffer)
249+
func Test_Vim_buffer_current()
250+
let cb = RubyEval('$curbuf')
251+
call assert_equal(cb, RubyEval('Vim::Buffer.current'))
252+
call assert_match('^#<Vim::Buffer:0x\x\+>$', cb)
253+
endfunc
254+
255+
" Test Vim::Buffer:.count (return the number of buffers)
256+
func Test_Vim_buffer_count()
257+
new Xfoo1
258+
new Xfoo2
259+
call assert_equal('3', RubyEval('Vim::Buffer.count'))
260+
%bwipe
261+
call assert_equal('1', RubyEval('Vim::Buffer.count'))
262+
endfunc
263+
264+
" Test Vim::buffer[n] (return the buffer object of buffer number n)
265+
func Test_Vim_buffer_get()
266+
new Xfoo1
267+
new Xfoo2
268+
269+
" Index of Vim::Buffer[n] goes from 0 to the number of buffers.
270+
call assert_equal('', RubyEval('Vim::Buffer[0].name'))
271+
call assert_match('Xfoo1$', RubyEval('Vim::Buffer[1].name'))
272+
call assert_match('Xfoo2$', RubyEval('Vim::Buffer[2].name'))
273+
call assert_fails('ruby print Vim::Buffer[3].name',
274+
\ "NoMethodError: undefined method `name' for nil:NilClass")
275+
%bwipe
276+
endfunc
277+
278+
" Test Vim::command({cmd}) (execute a Ex command))
279+
" Test Vim::command({cmd})
280+
func Test_Vim_command()
281+
new
282+
call setline(1, ['one', 'two', 'three', 'four'])
283+
ruby Vim::command('2,3d')
284+
call assert_equal(['one', 'four'], getline(1, '$'))
285+
bwipe!
286+
endfunc
287+
288+
" Test Vim::set_option (set a vim option)
289+
func Test_Vim_set_option()
290+
call assert_equal(0, &number)
291+
ruby Vim::set_option('number')
292+
call assert_equal(1, &number)
293+
ruby Vim::set_option('nonumber')
294+
call assert_equal(0, &number)
295+
endfunc
296+
297+
func Test_Vim_evaluate()
298+
call assert_equal('123', RubyEval('Vim::evaluate("123")'))
299+
" Vim::evaluate("123").class gives Integer or Fixnum depending
300+
" on versions of Ruby.
301+
call assert_match('^Integer\|Fixnum$', RubyEval('Vim::evaluate("123").class'))
302+
303+
call assert_equal('1.23', RubyEval('Vim::evaluate("1.23")'))
304+
call assert_equal('Float', RubyEval('Vim::evaluate("1.23").class'))
305+
306+
call assert_equal('foo', RubyEval('Vim::evaluate("\"foo\"")'))
307+
call assert_equal('String', RubyEval('Vim::evaluate("\"foo\"").class'))
308+
309+
call assert_equal('[1, 2]', RubyEval('Vim::evaluate("[1, 2]")'))
310+
call assert_equal('Array', RubyEval('Vim::evaluate("[1, 2]").class'))
311+
312+
call assert_equal('{"1"=>2}', RubyEval('Vim::evaluate("{1:2}")'))
313+
call assert_equal('Hash', RubyEval('Vim::evaluate("{1:2}").class'))
314+
315+
call assert_equal('', RubyEval('Vim::evaluate("v:null")'))
316+
call assert_equal('NilClass', RubyEval('Vim::evaluate("v:null").class'))
317+
318+
call assert_equal('', RubyEval('Vim::evaluate("v:none")'))
319+
call assert_equal('NilClass', RubyEval('Vim::evaluate("v:none").class'))
320+
321+
call assert_equal('true', RubyEval('Vim::evaluate("v:true")'))
322+
call assert_equal('TrueClass', RubyEval('Vim::evaluate("v:true").class'))
323+
call assert_equal('false', RubyEval('Vim::evaluate("v:false")'))
324+
call assert_equal('FalseClass',RubyEval('Vim::evaluate("v:false").class'))
325+
endfunc
326+
327+
func Test_Vim_evaluate_list()
328+
call setline(line('$'), ['2 line 2'])
329+
ruby Vim.command("normal /^2\n")
330+
let l = ["abc", "def"]
331+
ruby << EOF
332+
curline = $curbuf.line_number
333+
l = Vim.evaluate("l");
334+
$curbuf.append(curline, l.join("\n"))
335+
EOF
336+
normal j
337+
.rubydo $_ = $_.gsub(/\n/, '/')
338+
call assert_equal('abc/def', getline('$'))
339+
endfunc
340+
341+
func Test_Vim_evaluate_dict()
342+
let d = {'a': 'foo', 'b': 123}
343+
redir => l:out
344+
ruby d = Vim.evaluate("d"); print d
345+
redir END
346+
call assert_equal(['{"a"=>"foo", "b"=>123}'], split(l:out, "\n"))
347+
endfunc
348+
349+
" Test Vim::message({msg}) (display message {msg})
350+
func Test_Vim_message()
351+
ruby Vim::message('A message')
352+
let messages = split(execute('message'), "\n")
353+
call assert_equal('A message', messages[-1])
354+
endfunc
355+
356+
func Test_print()
357+
ruby print "Hello World!"
358+
let messages = split(execute('message'), "\n")
359+
call assert_equal('Hello World!', messages[-1])
360+
endfunc
361+
362+
func Test_p()
363+
ruby p 'Just a test'
364+
let messages = split(execute('message'), "\n")
365+
call assert_equal('"Just a test"', messages[-1])
80366
endfunc

0 commit comments

Comments
 (0)