Bram Moolenaar | 85babd6 | 2016-06-21 22:59:28 +0200 | [diff] [blame] | 1 | " Tests for ruby interface |
| 2 | |
| 3 | if !has('ruby') |
| 4 | finish |
| 5 | end |
| 6 | |
Bram Moolenaar | edd6aac | 2018-07-28 17:29:19 +0200 | [diff] [blame] | 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 | |
Bram Moolenaar | 85babd6 | 2016-06-21 22:59:28 +0200 | [diff] [blame] | 14 | func Test_ruby_change_buffer() |
| 15 | call setline(line('$'), ['1 line 1']) |
| 16 | ruby Vim.command("normal /^1\n") |
| 17 | ruby $curbuf.line = "1 changed line 1" |
| 18 | call assert_equal('1 changed line 1', getline('$')) |
| 19 | endfunc |
| 20 | |
Bram Moolenaar | c593fee | 2017-01-29 23:11:25 +0100 | [diff] [blame] | 21 | func Test_rubydo() |
| 22 | " Check deleting lines does not trigger ml_get error. |
| 23 | new |
| 24 | call setline(1, ['one', 'two', 'three']) |
| 25 | rubydo Vim.command("%d_") |
| 26 | bwipe! |
| 27 | |
| 28 | " Check switching to another buffer does not trigger ml_get error. |
| 29 | new |
| 30 | let wincount = winnr('$') |
| 31 | call setline(1, ['one', 'two', 'three']) |
| 32 | rubydo Vim.command("new") |
| 33 | call assert_equal(wincount + 1, winnr('$')) |
Bram Moolenaar | edd6aac | 2018-07-28 17:29:19 +0200 | [diff] [blame] | 34 | %bwipe! |
Bram Moolenaar | c593fee | 2017-01-29 23:11:25 +0100 | [diff] [blame] | 35 | endfunc |
Bram Moolenaar | 37badc8 | 2018-01-31 20:15:30 +0100 | [diff] [blame] | 36 | |
| 37 | func Test_rubyfile() |
| 38 | " Check :rubyfile does not SEGV with Ruby level exception but just fails |
| 39 | let tempfile = tempname() . '.rb' |
| 40 | call writefile(['raise "vim!"'], tempfile) |
| 41 | call assert_fails('rubyfile ' . tempfile) |
| 42 | call delete(tempfile) |
| 43 | endfunc |
Bram Moolenaar | 5390144 | 2018-07-25 22:02:36 +0200 | [diff] [blame] | 44 | |
| 45 | func Test_set_cursor() |
| 46 | " Check that setting the cursor position works. |
| 47 | new |
| 48 | call setline(1, ['first line', 'second line']) |
| 49 | normal gg |
| 50 | rubydo $curwin.cursor = [1, 5] |
| 51 | call assert_equal([1, 6], [line('.'), col('.')]) |
Bram Moolenaar | edd6aac | 2018-07-28 17:29:19 +0200 | [diff] [blame] | 52 | call assert_equal('[1, 5]', RubyEval('$curwin.cursor')) |
Bram Moolenaar | 5390144 | 2018-07-25 22:02:36 +0200 | [diff] [blame] | 53 | |
| 54 | " Check that movement after setting cursor position keeps current column. |
| 55 | normal j |
| 56 | call assert_equal([2, 6], [line('.'), col('.')]) |
Bram Moolenaar | edd6aac | 2018-07-28 17:29:19 +0200 | [diff] [blame] | 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]) |
Bram Moolenaar | 5390144 | 2018-07-25 22:02:36 +0200 | [diff] [blame] | 366 | endfunc |