]> code.delx.au - gnu-emacs/blob - test/automated/ruby-mode-tests.el
* lisp/progmodes/ruby-mode.el (ruby-expression-expansion-re): Allow to
[gnu-emacs] / test / automated / ruby-mode-tests.el
1 ;;; ruby-mode-tests.el --- Test suite for ruby-mode
2
3 ;; Copyright (C) 2012-2013 Free Software Foundation, Inc.
4
5 ;; This file is part of GNU Emacs.
6
7 ;; GNU Emacs is free software: you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation, either version 3 of the License, or
10 ;; (at your option) any later version.
11
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
16
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
19
20 ;;; Commentary:
21
22 ;;; Code:
23
24 (require 'ruby-mode)
25
26 (defun ruby-should-indent (content column)
27 "Assert indentation COLUMN on the last line of CONTENT."
28 (ruby-with-temp-buffer content
29 (ruby-indent-line)
30 (should (= (current-indentation) column))))
31
32 (defun ruby-should-indent-buffer (expected content)
33 "Assert that CONTENT turns into EXPECTED after the buffer is re-indented.
34
35 The whitespace before and including \"|\" on each line is removed."
36 (ruby-with-temp-buffer (ruby-test-string content)
37 (indent-region (point-min) (point-max))
38 (should (string= (ruby-test-string expected) (buffer-string)))))
39
40 (defmacro ruby-with-temp-buffer (contents &rest body)
41 (declare (indent 1) (debug t))
42 `(with-temp-buffer
43 (insert ,contents)
44 (ruby-mode)
45 ,@body))
46
47 (defun ruby-test-string (s &rest args)
48 (apply 'format (replace-regexp-in-string "^[ \t]*|" "" s) args))
49
50 (defun ruby-assert-state (content index value &optional point)
51 "Assert syntax state values at the end of CONTENT.
52
53 VALUES-PLIST is a list with alternating index and value elements."
54 (ruby-with-temp-buffer content
55 (when point (goto-char point))
56 (syntax-propertize (point))
57 (should (eq (nth index
58 (parse-partial-sexp (point-min) (point)))
59 value))))
60
61 (defun ruby-assert-face (content pos face)
62 (ruby-with-temp-buffer content
63 (font-lock-fontify-buffer)
64 (should (eq face (get-text-property pos 'face)))))
65
66 (ert-deftest ruby-indent-after-symbol-made-from-string-interpolation ()
67 "It can indent the line after symbol made using string interpolation."
68 (ruby-should-indent "def foo(suffix)\n :\"bar#{suffix}\"\n"
69 ruby-indent-level))
70
71 (ert-deftest ruby-indent-after-js-style-symbol-with-block-beg-name ()
72 "JS-style hash symbol can have keyword name."
73 (ruby-should-indent "link_to \"home\", home_path, class: \"foo\"\n" 0))
74
75 (ert-deftest ruby-discern-singleton-class-from-heredoc ()
76 (ruby-assert-state "foo <<asd\n" 3 ?\n)
77 (ruby-assert-state "class <<asd\n" 3 nil))
78
79 (ert-deftest ruby-heredoc-font-lock ()
80 (let ((s "foo <<eos.gsub('^ *', '')"))
81 (ruby-assert-face s 9 font-lock-string-face)
82 (ruby-assert-face s 10 nil)))
83
84 (ert-deftest ruby-singleton-class-no-heredoc-font-lock ()
85 (ruby-assert-face "class<<a" 8 nil))
86
87 (ert-deftest ruby-heredoc-highlights-interpolations ()
88 (ruby-assert-face "s = <<EOS\n #{foo}\nEOS" 15 font-lock-variable-name-face))
89
90 (ert-deftest ruby-deep-indent ()
91 (let ((ruby-deep-arglist nil)
92 (ruby-deep-indent-paren '(?\( ?\{ ?\[ ?\] t)))
93 (ruby-should-indent "foo = [1,\n2" 7)
94 (ruby-should-indent "foo = {a: b,\nc: d" 7)
95 (ruby-should-indent "foo(a,\nb" 4)))
96
97 (ert-deftest ruby-deep-indent-disabled ()
98 (let ((ruby-deep-arglist nil)
99 (ruby-deep-indent-paren nil))
100 (ruby-should-indent "foo = [\n1" ruby-indent-level)
101 (ruby-should-indent "foo = {\na: b" ruby-indent-level)
102 (ruby-should-indent "foo(\na" ruby-indent-level)))
103
104 (ert-deftest ruby-indent-after-keyword-in-a-string ()
105 (ruby-should-indent "a = \"abc\nif\"\n " 0)
106 (ruby-should-indent "a = %w[abc\n def]\n " 0)
107 (ruby-should-indent "a = \"abc\n def\"\n " 0))
108
109 (ert-deftest ruby-regexp-doesnt-start-in-string ()
110 (ruby-assert-state "'(/', /\d+/" 3 nil))
111
112 (ert-deftest ruby-regexp-starts-after-string ()
113 (ruby-assert-state "'(/', /\d+/" 3 ?/ 8))
114
115 (ert-deftest ruby-regexp-skips-over-interpolation ()
116 (ruby-assert-state "/#{foobs.join('/')}/" 3 nil))
117
118 (ert-deftest ruby-regexp-continues-till-end-when-unclosed ()
119 (ruby-assert-state "/bars" 3 ?/))
120
121 (ert-deftest ruby-regexp-can-be-multiline ()
122 (ruby-assert-state "/bars\ntees # toots \nfoos/" 3 nil))
123
124 (ert-deftest ruby-indent-simple ()
125 (ruby-should-indent-buffer
126 "if foo
127 | bar
128 |end
129 |zot
130 |"
131 "if foo
132 |bar
133 | end
134 | zot
135 |"))
136
137 (ert-deftest ruby-indent-keyword-label ()
138 (ruby-should-indent-buffer
139 "bar(class: XXX) do
140 | foo
141 |end
142 |bar
143 |"
144 "bar(class: XXX) do
145 | foo
146 | end
147 | bar
148 |"))
149
150 (ert-deftest ruby-indent-method-with-question-mark ()
151 (ruby-should-indent-buffer
152 "if x.is_a?(XXX)
153 | foo
154 |end
155 |"
156 "if x.is_a?(XXX)
157 | foo
158 | end
159 |"))
160
161 (ert-deftest ruby-indent-expr-in-regexp ()
162 (ruby-should-indent-buffer
163 "if /#{foo}/ =~ s
164 | x = 1
165 |end
166 |"
167 "if /#{foo}/ =~ s
168 | x = 1
169 | end
170 |"))
171
172 (ert-deftest ruby-indent-singleton-class ()
173 (ruby-should-indent-buffer
174 "class<<bar
175 | foo
176 |end
177 |"
178 "class<<bar
179 |foo
180 | end
181 |"))
182
183 (ert-deftest ruby-indent-inside-heredoc-after-operator ()
184 (ruby-should-indent-buffer
185 "b=<<eos
186 | 42"
187 "b=<<eos
188 | 42"))
189
190 (ert-deftest ruby-indent-inside-heredoc-after-space ()
191 (ruby-should-indent-buffer
192 "foo <<eos.gsub(' ', '*')
193 | 42"
194 "foo <<eos.gsub(' ', '*')
195 | 42"))
196
197 (ert-deftest ruby-indent-array-literal ()
198 (let ((ruby-deep-indent-paren nil))
199 (ruby-should-indent-buffer
200 "foo = [
201 | bar
202 |]
203 |"
204 "foo = [
205 | bar
206 | ]
207 |"))
208 (ruby-should-indent-buffer
209 "foo do
210 | [bar]
211 |end
212 |"
213 "foo do
214 |[bar]
215 | end
216 |"))
217
218 (ert-deftest ruby-indent-begin-end ()
219 (ruby-should-indent-buffer
220 "begin
221 | a[b]
222 |end
223 |"
224 "begin
225 | a[b]
226 | end
227 |"))
228
229 (ert-deftest ruby-indent-array-after-paren-and-space ()
230 (ruby-should-indent-buffer
231 "class A
232 | def foo
233 | foo( [])
234 | end
235 |end
236 |"
237 "class A
238 | def foo
239 |foo( [])
240 |end
241 | end
242 |"))
243
244 (ert-deftest ruby-indent-after-block-in-continued-expression ()
245 (ruby-should-indent-buffer
246 "var =
247 | begin
248 | val
249 | end
250 |statement"
251 "var =
252 |begin
253 |val
254 |end
255 |statement"))
256
257 (ert-deftest ruby-indent-spread-args-in-parens ()
258 (let ((ruby-deep-indent-paren '(?\()))
259 (ruby-should-indent-buffer
260 "foo(1,
261 | 2,
262 | 3)
263 |"
264 "foo(1,
265 | 2,
266 | 3)
267 |")))
268
269 (ert-deftest ruby-move-to-block-stops-at-indentation ()
270 (ruby-with-temp-buffer "def f\nend"
271 (beginning-of-line)
272 (ruby-move-to-block -1)
273 (should (looking-at "^def"))))
274
275 (ert-deftest ruby-toggle-block-to-do-end ()
276 (ruby-with-temp-buffer "foo {|b|\n}"
277 (beginning-of-line)
278 (ruby-toggle-block)
279 (should (string= "foo do |b|\nend" (buffer-string)))))
280
281 (ert-deftest ruby-toggle-block-to-brace ()
282 (let ((pairs '((16 . "foo {|b| b + 2 }")
283 (15 . "foo {|b|\n b + 2\n}"))))
284 (dolist (pair pairs)
285 (with-temp-buffer
286 (let ((fill-column (car pair)))
287 (insert "foo do |b|\n b + 2\nend")
288 (ruby-mode)
289 (beginning-of-line)
290 (ruby-toggle-block)
291 (should (string= (cdr pair) (buffer-string))))))))
292
293 (ert-deftest ruby-toggle-block-to-multiline ()
294 (ruby-with-temp-buffer "foo {|b| b + 1}"
295 (beginning-of-line)
296 (ruby-toggle-block)
297 (should (string= "foo do |b|\n b + 1\nend" (buffer-string)))))
298
299 (ert-deftest ruby-recognize-symbols-starting-with-at-character ()
300 (ruby-assert-face ":@abc" 3 font-lock-constant-face))
301
302 (ert-deftest ruby-hash-character-not-interpolation ()
303 (ruby-assert-face "\"This is #{interpolation}\"" 15
304 font-lock-variable-name-face)
305 (ruby-assert-face "\"This is \\#{no interpolation} despite the #\""
306 15 font-lock-string-face)
307 (ruby-assert-face "\n#@comment, not ruby code" 5 font-lock-comment-face)
308 (ruby-assert-state "\n#@comment, not ruby code" 4 t)
309 (ruby-assert-face "# A comment cannot have #{an interpolation} in it"
310 30 font-lock-comment-face)
311 (ruby-assert-face "# #{comment}\n \"#{interpolation}\"" 16
312 font-lock-variable-name-face))
313
314 (ert-deftest ruby-interpolation-suppresses-quotes-inside ()
315 (let ((s "\"<ul><li>#{@files.join(\"</li><li>\")}</li></ul>\""))
316 (ruby-assert-state s 8 nil)
317 (ruby-assert-face s 9 font-lock-string-face)
318 (ruby-assert-face s 10 font-lock-variable-name-face)
319 (ruby-assert-face s 41 font-lock-string-face)))
320
321 (ert-deftest ruby-interpolation-suppresses-one-double-quote ()
322 (let ((s "\"foo#{'\"'}\""))
323 (ruby-assert-state s 8 nil)
324 (ruby-assert-face s 8 font-lock-variable-name-face)
325 (ruby-assert-face s 11 font-lock-string-face)))
326
327 (ert-deftest ruby-interpolation-suppresses-one-backtick ()
328 (let ((s "`as#{'`'}das`"))
329 (ruby-assert-state s 8 nil)))
330
331 (ert-deftest ruby-interpolation-keeps-non-quote-syntax ()
332 (let ((s "\"foo#{baz.tee}bar\""))
333 (ruby-with-temp-buffer s
334 (goto-char (point-min))
335 (ruby-mode)
336 (font-lock-fontify-buffer)
337 (search-forward "tee")
338 (should (string= (thing-at-point 'symbol) "tee")))))
339
340 (ert-deftest ruby-interpolation-inside-percent-literal ()
341 (let ((s "%( #{boo} )"))
342 (ruby-assert-face s 1 font-lock-string-face)
343 (ruby-assert-face s 4 font-lock-variable-name-face)
344 (ruby-assert-face s 10 font-lock-string-face)
345 (ruby-assert-state s 8 nil)))
346
347 (ert-deftest ruby-interpolation-inside-percent-literal-with-paren ()
348 :expected-result :failed
349 (let ((s "%(^#{\")\"}^)"))
350 (ruby-assert-face s 3 font-lock-string-face)
351 (ruby-assert-face s 4 font-lock-variable-name-face)
352 (ruby-assert-face s 10 font-lock-string-face)
353 ;; It's confused by the closing paren in the middle.
354 (ruby-assert-state s 8 nil)))
355
356 (ert-deftest ruby-add-log-current-method-examples ()
357 (let ((pairs '(("foo" . "#foo")
358 ("C.foo" . ".foo")
359 ("self.foo" . ".foo"))))
360 (dolist (pair pairs)
361 (let ((name (car pair))
362 (value (cdr pair)))
363 (ruby-with-temp-buffer (ruby-test-string
364 "module M
365 | class C
366 | def %s
367 | _
368 | end
369 | end
370 |end"
371 name)
372 (search-backward "_")
373 (forward-line)
374 (should (string= (ruby-add-log-current-method)
375 (format "M::C%s" value))))))))
376
377 (ert-deftest ruby-add-log-current-method-outside-of-method ()
378 (ruby-with-temp-buffer (ruby-test-string
379 "module M
380 | class C
381 | def foo
382 | end
383 | _
384 | end
385 |end")
386 (search-backward "_")
387 (should (string= (ruby-add-log-current-method)"M::C"))))
388
389 (ert-deftest ruby-add-log-current-method-in-singleton-class ()
390 (ruby-with-temp-buffer (ruby-test-string
391 "class C
392 | class << self
393 | def foo
394 | _
395 | end
396 | end
397 |end")
398 (search-backward "_")
399 (should (string= (ruby-add-log-current-method) "C.foo"))))
400
401 (ert-deftest ruby-add-log-current-method-namespace-shorthand ()
402 (ruby-with-temp-buffer (ruby-test-string
403 "class C::D
404 | def foo
405 | _
406 | end
407 |end")
408 (search-backward "_")
409 (should (string= (ruby-add-log-current-method) "C::D#foo"))))
410
411 (ert-deftest ruby-add-log-current-method-after-inner-class ()
412 (ruby-with-temp-buffer (ruby-test-string
413 "module M
414 | class C
415 | class D
416 | end
417 | def foo
418 | _
419 | end
420 | end
421 |end")
422 (search-backward "_")
423 (should (string= (ruby-add-log-current-method) "M::C#foo"))))
424
425 (defvar ruby-block-test-example
426 (ruby-test-string
427 "class C
428 | def foo
429 | 1
430 | end
431 |
432 | def bar
433 | 2
434 | end
435 |
436 | def baz
437 |some do
438 |3
439 | end
440 | end
441 |end"))
442
443 (defmacro ruby-deftest-move-to-block (name &rest body)
444 `(ert-deftest ,(intern (format "ruby-move-to-block-%s" name)) ()
445 (with-temp-buffer
446 (insert ruby-block-test-example)
447 (ruby-mode)
448 ,@body)))
449
450 (put 'ruby-deftest-move-to-block 'lisp-indent-function 'defun)
451
452 (ruby-deftest-move-to-block works-on-do
453 (goto-line 11)
454 (ruby-end-of-block)
455 (should (= 13 (line-number-at-pos)))
456 (ruby-beginning-of-block)
457 (should (= 11 (line-number-at-pos))))
458
459 (ruby-deftest-move-to-block zero-is-noop
460 (goto-line 5)
461 (ruby-move-to-block 0)
462 (should (= 5 (line-number-at-pos))))
463
464 (ruby-deftest-move-to-block ok-with-three
465 (goto-line 2)
466 (ruby-move-to-block 3)
467 (should (= 14 (line-number-at-pos))))
468
469 (ruby-deftest-move-to-block ok-with-minus-two
470 (goto-line 10)
471 (ruby-move-to-block -2)
472 (should (= 2 (line-number-at-pos))))
473
474 (ert-deftest ruby-move-to-block-skips-percent-literal ()
475 (dolist (s (list (ruby-test-string
476 "foo do
477 | a = %%w(
478 | def yaa
479 | )
480 |end")
481 (ruby-test-string
482 "foo do
483 | a = %%w|
484 | end
485 | |
486 |end")))
487 (ruby-with-temp-buffer s
488 (goto-line 1)
489 (ruby-end-of-block)
490 (should (= 5 (line-number-at-pos)))
491 (ruby-beginning-of-block)
492 (should (= 1 (line-number-at-pos))))))
493
494 (ert-deftest ruby-move-to-block-skips-heredoc ()
495 (ruby-with-temp-buffer
496 (ruby-test-string
497 "if something_wrong?
498 | ActiveSupport::Deprecation.warn(<<-eowarn)
499 | boo hoo
500 | end
501 | eowarn
502 |end")
503 (goto-line 1)
504 (ruby-end-of-block)
505 (should (= 6 (line-number-at-pos)))
506 (ruby-beginning-of-block)
507 (should (= 1 (line-number-at-pos)))))
508
509 (ert-deftest ruby-move-to-block-does-not-fold-case ()
510 (ruby-with-temp-buffer
511 (ruby-test-string
512 "foo do
513 | Module.to_s
514 |end")
515 (end-of-buffer)
516 (let ((case-fold-search t))
517 (ruby-beginning-of-block))
518 (should (= 1 (line-number-at-pos)))))
519
520 (ert-deftest ruby-beginning-of-defun-does-not-fold-case ()
521 (ruby-with-temp-buffer
522 (ruby-test-string
523 "class C
524 | def bar
525 | Class.to_s
526 | end
527 |end")
528 (goto-line 4)
529 (let ((case-fold-search t))
530 (beginning-of-defun))
531 (should (= 2 (line-number-at-pos)))))
532
533 (ert-deftest ruby-end-of-defun-skips-to-next-line-after-the-method ()
534 (ruby-with-temp-buffer
535 (ruby-test-string
536 "class D
537 | def tee
538 | 'ho hum'
539 | end
540 |end")
541 (goto-line 2)
542 (end-of-defun)
543 (should (= 5 (line-number-at-pos)))))
544
545 (provide 'ruby-mode-tests)
546
547 ;;; ruby-mode-tests.el ends here