Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / test / automated / ruby-mode-tests.el
blobaa8032bb8709fad3a75cb703a46dbf68241b1416
1 ;;; ruby-mode-tests.el --- Test suite for ruby-mode
3 ;; Copyright (C) 2012-2014 Free Software Foundation, Inc.
5 ;; This file is part of GNU Emacs.
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.
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.
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/>.
20 ;;; Commentary:
22 ;;; Code:
24 (require 'ert)
25 (require 'ruby-mode)
27 (defmacro ruby-with-temp-buffer (contents &rest body)
28 (declare (indent 1) (debug t))
29 `(with-temp-buffer
30 (insert ,contents)
31 (ruby-mode)
32 ,@body))
34 (defun ruby-should-indent (content column)
35 "Assert indentation COLUMN on the last line of CONTENT."
36 (ruby-with-temp-buffer content
37 (indent-according-to-mode)
38 (should (= (current-indentation) column))))
40 (defun ruby-should-indent-buffer (expected content)
41 "Assert that CONTENT turns into EXPECTED after the buffer is re-indented.
43 The whitespace before and including \"|\" on each line is removed."
44 (ruby-with-temp-buffer (ruby-test-string content)
45 (indent-region (point-min) (point-max))
46 (should (string= (ruby-test-string expected) (buffer-string)))))
48 (defun ruby-test-string (s &rest args)
49 (apply 'format (replace-regexp-in-string "^[ \t]*|" "" s) args))
51 (defun ruby-assert-state (content index value &optional point)
52 "Assert syntax state values at the end of CONTENT.
54 VALUES-PLIST is a list with alternating index and value elements."
55 (ruby-with-temp-buffer content
56 (when point (goto-char point))
57 (syntax-propertize (point))
58 (should (eq (nth index
59 (parse-partial-sexp (point-min) (point)))
60 value))))
62 (defun ruby-assert-face (content pos face)
63 (ruby-with-temp-buffer content
64 (font-lock-fontify-buffer)
65 (should (eq face (get-text-property pos 'face)))))
67 (ert-deftest ruby-indent-after-symbol-made-from-string-interpolation ()
68 "It can indent the line after symbol made using string interpolation."
69 (ruby-should-indent "def foo(suffix)\n :\"bar#{suffix}\"\n"
70 ruby-indent-level))
72 (ert-deftest ruby-indent-after-js-style-symbol-with-block-beg-name ()
73 "JS-style hash symbol can have keyword name."
74 (ruby-should-indent "link_to \"home\", home_path, class: \"foo\"\n" 0))
76 (ert-deftest ruby-discern-singleton-class-from-heredoc ()
77 (ruby-assert-state "foo <<asd\n" 3 ?\n)
78 (ruby-assert-state "class <<asd\n" 3 nil))
80 (ert-deftest ruby-heredoc-font-lock ()
81 (let ((s "foo <<eos.gsub('^ *', '')"))
82 (ruby-assert-face s 9 font-lock-string-face)
83 (ruby-assert-face s 10 nil)))
85 (ert-deftest ruby-singleton-class-no-heredoc-font-lock ()
86 (ruby-assert-face "class<<a" 8 nil))
88 (ert-deftest ruby-heredoc-highlights-interpolations ()
89 (ruby-assert-face "s = <<EOS\n #{foo}\nEOS" 15 font-lock-variable-name-face))
91 (ert-deftest ruby-no-heredoc-inside-quotes ()
92 (ruby-assert-state "\"<<\", \"\",\nfoo" 3 nil))
94 (ert-deftest ruby-exit!-font-lock ()
95 (ruby-assert-face "exit!" 5 font-lock-builtin-face))
97 (ert-deftest ruby-deep-indent ()
98 (let ((ruby-deep-arglist nil)
99 (ruby-deep-indent-paren '(?\( ?\{ ?\[ ?\] t)))
100 (ruby-should-indent "foo = [1,\n2" 7)
101 (ruby-should-indent "foo = {a: b,\nc: d" 7)
102 (ruby-should-indent "foo(a,\nb" 4)))
104 (ert-deftest ruby-deep-indent-disabled ()
105 (let ((ruby-deep-arglist nil)
106 (ruby-deep-indent-paren nil))
107 (ruby-should-indent "foo = [\n1" ruby-indent-level)
108 (ruby-should-indent "foo = {\na: b" ruby-indent-level)
109 (ruby-should-indent "foo(\na" ruby-indent-level)))
111 (ert-deftest ruby-indent-after-keyword-in-a-string ()
112 (ruby-should-indent "a = \"abc\nif\"\n " 0)
113 (ruby-should-indent "a = %w[abc\n def]\n " 0)
114 (ruby-should-indent "a = \"abc\n def\"\n " 0))
116 (ert-deftest ruby-regexp-doesnt-start-in-string ()
117 (ruby-assert-state "'(/', /\d+/" 3 nil))
119 (ert-deftest ruby-regexp-starts-after-string ()
120 (ruby-assert-state "'(/', /\d+/" 3 ?/ 8))
122 (ert-deftest ruby-regexp-interpolation-is-highlighted ()
123 (ruby-assert-face "/#{foobs}/" 4 font-lock-variable-name-face))
125 (ert-deftest ruby-regexp-skips-over-interpolation ()
126 (ruby-assert-state "/#{foobs.join('/')}/" 3 nil))
128 (ert-deftest ruby-regexp-continues-till-end-when-unclosed ()
129 (ruby-assert-state "/bars" 3 ?/))
131 (ert-deftest ruby-regexp-can-be-multiline ()
132 (ruby-assert-state "/bars\ntees # toots \nfoos/" 3 nil))
134 (ert-deftest ruby-slash-symbol-is-not-mistaken-for-regexp ()
135 (ruby-assert-state ":/" 3 nil))
137 (ert-deftest ruby-slash-char-literal-is-not-mistaken-for-regexp ()
138 (ruby-assert-state "?/" 3 nil))
140 (ert-deftest ruby-indent-simple ()
141 (ruby-should-indent-buffer
142 "if foo
143 | bar
144 |end
145 |zot
147 "if foo
148 |bar
149 | end
150 | zot
151 |"))
153 (ert-deftest ruby-indent-keyword-label ()
154 (ruby-should-indent-buffer
155 "bar(class: XXX) do
156 | foo
157 |end
158 |bar
160 "bar(class: XXX) do
161 | foo
162 | end
163 | bar
164 |"))
166 (ert-deftest ruby-indent-method-with-question-mark ()
167 (ruby-should-indent-buffer
168 "if x.is_a?(XXX)
169 | foo
170 |end
172 "if x.is_a?(XXX)
173 | foo
174 | end
175 |"))
177 (ert-deftest ruby-indent-expr-in-regexp ()
178 (ruby-should-indent-buffer
179 "if /#{foo}/ =~ s
180 | x = 1
181 |end
183 "if /#{foo}/ =~ s
184 | x = 1
185 | end
186 |"))
188 (ert-deftest ruby-indent-singleton-class ()
189 (ruby-should-indent-buffer
190 "class<<bar
191 | foo
192 |end
194 "class<<bar
195 |foo
196 | end
197 |"))
199 (ert-deftest ruby-indent-inside-heredoc-after-operator ()
200 (ruby-should-indent-buffer
201 "b=<<eos
202 | 42"
203 "b=<<eos
204 | 42"))
206 (ert-deftest ruby-indent-inside-heredoc-after-space ()
207 (ruby-should-indent-buffer
208 "foo <<eos.gsub(' ', '*')
209 | 42"
210 "foo <<eos.gsub(' ', '*')
211 | 42"))
213 (ert-deftest ruby-indent-array-literal ()
214 (let ((ruby-deep-indent-paren nil))
215 (ruby-should-indent-buffer
216 "foo = [
217 | bar
220 "foo = [
221 | bar
223 |"))
224 (ruby-should-indent-buffer
225 "foo do
226 | [bar]
227 |end
229 "foo do
230 |[bar]
231 | end
232 |"))
234 (ert-deftest ruby-indent-begin-end ()
235 (ruby-should-indent-buffer
236 "begin
237 | a[b]
238 |end
240 "begin
241 | a[b]
242 | end
243 |"))
245 (ert-deftest ruby-indent-array-after-paren-and-space ()
246 (ruby-should-indent-buffer
247 "class A
248 | def foo
249 | foo( [])
250 | end
251 |end
253 "class A
254 | def foo
255 |foo( [])
256 |end
257 | end
258 |"))
260 (ert-deftest ruby-indent-after-block-in-continued-expression ()
261 (ruby-should-indent-buffer
262 "var =
263 | begin
264 | val
265 | end
266 |statement"
267 "var =
268 |begin
269 |val
270 |end
271 |statement"))
273 (ert-deftest ruby-indent-spread-args-in-parens ()
274 (let ((ruby-deep-indent-paren '(?\()))
275 (ruby-should-indent-buffer
276 "foo(1,
277 | 2,
278 | 3)
280 "foo(1,
281 | 2,
282 | 3)
283 |")))
285 (ert-deftest ruby-align-to-stmt-keywords-t ()
286 (let ((ruby-align-to-stmt-keywords t))
287 (ruby-should-indent-buffer
288 "foo = if bar?
290 |else
292 |end
294 |foo || begin
295 | bar
296 |end
298 |foo ||
299 | begin
300 | bar
301 | end
303 "foo = if bar?
305 |else
307 | end
309 | foo || begin
310 | bar
311 |end
313 | foo ||
314 | begin
315 |bar
316 | end
320 (ert-deftest ruby-align-to-stmt-keywords-case ()
321 (let ((ruby-align-to-stmt-keywords '(case)))
322 (ruby-should-indent-buffer
323 "b = case a
324 |when 13
326 |else
327 | 42
328 |end"
329 "b = case a
330 | when 13
332 | else
333 | 42
334 | end")))
336 (ert-deftest ruby-move-to-block-stops-at-indentation ()
337 (ruby-with-temp-buffer "def f\nend"
338 (beginning-of-line)
339 (ruby-move-to-block -1)
340 (should (looking-at "^def"))))
342 (ert-deftest ruby-toggle-block-to-do-end ()
343 (ruby-with-temp-buffer "foo {|b|\n}"
344 (beginning-of-line)
345 (ruby-toggle-block)
346 (should (string= "foo do |b|\nend" (buffer-string)))))
348 (ert-deftest ruby-toggle-block-to-brace ()
349 (let ((pairs '((17 . "foo { |b| b + 2 }")
350 (16 . "foo { |b|\n b + 2\n}"))))
351 (dolist (pair pairs)
352 (with-temp-buffer
353 (let ((fill-column (car pair)))
354 (insert "foo do |b|\n b + 2\nend")
355 (ruby-mode)
356 (beginning-of-line)
357 (ruby-toggle-block)
358 (should (string= (cdr pair) (buffer-string))))))))
360 (ert-deftest ruby-toggle-block-to-multiline ()
361 (ruby-with-temp-buffer "foo {|b| b + 1}"
362 (beginning-of-line)
363 (ruby-toggle-block)
364 (should (string= "foo do |b|\n b + 1\nend" (buffer-string)))))
366 (ert-deftest ruby-toggle-block-with-interpolation ()
367 (ruby-with-temp-buffer "foo do\n \"#{bar}\"\nend"
368 (beginning-of-line)
369 (ruby-toggle-block)
370 (should (string= "foo { \"#{bar}\" }" (buffer-string)))))
372 (ert-deftest ruby-recognize-symbols-starting-with-at-character ()
373 (ruby-assert-face ":@abc" 3 font-lock-constant-face))
375 (ert-deftest ruby-hash-character-not-interpolation ()
376 (ruby-assert-face "\"This is #{interpolation}\"" 15
377 font-lock-variable-name-face)
378 (ruby-assert-face "\"This is \\#{no interpolation} despite the #\""
379 15 font-lock-string-face)
380 (ruby-assert-face "\n#@comment, not ruby code" 5 font-lock-comment-face)
381 (ruby-assert-state "\n#@comment, not ruby code" 4 t)
382 (ruby-assert-face "# A comment cannot have #{an interpolation} in it"
383 30 font-lock-comment-face)
384 (ruby-assert-face "# #{comment}\n \"#{interpolation}\"" 16
385 font-lock-variable-name-face))
387 (ert-deftest ruby-interpolation-suppresses-quotes-inside ()
388 (let ((s "\"<ul><li>#{@files.join(\"</li><li>\")}</li></ul>\""))
389 (ruby-assert-state s 8 nil)
390 (ruby-assert-face s 9 font-lock-string-face)
391 (ruby-assert-face s 10 font-lock-variable-name-face)
392 (ruby-assert-face s 41 font-lock-string-face)))
394 (ert-deftest ruby-interpolation-suppresses-one-double-quote ()
395 (let ((s "\"foo#{'\"'}\""))
396 (ruby-assert-state s 8 nil)
397 (ruby-assert-face s 8 font-lock-variable-name-face)
398 (ruby-assert-face s 11 font-lock-string-face)))
400 (ert-deftest ruby-interpolation-suppresses-one-backtick ()
401 (let ((s "`as#{'`'}das`"))
402 (ruby-assert-state s 8 nil)))
404 (ert-deftest ruby-interpolation-keeps-non-quote-syntax ()
405 (let ((s "\"foo#{baz.tee}bar\""))
406 (ruby-with-temp-buffer s
407 (goto-char (point-min))
408 (ruby-mode)
409 (font-lock-fontify-buffer)
410 (search-forward "tee")
411 (should (string= (thing-at-point 'symbol) "tee")))))
413 (ert-deftest ruby-interpolation-inside-percent-literal ()
414 (let ((s "%( #{boo} )"))
415 (ruby-assert-face s 1 font-lock-string-face)
416 (ruby-assert-face s 4 font-lock-variable-name-face)
417 (ruby-assert-face s 10 font-lock-string-face)
418 (ruby-assert-state s 8 nil)))
420 (ert-deftest ruby-interpolation-inside-percent-literal-with-paren ()
421 :expected-result :failed
422 (let ((s "%(^#{\")\"}^)"))
423 (ruby-assert-face s 3 font-lock-string-face)
424 (ruby-assert-face s 4 font-lock-variable-name-face)
425 (ruby-assert-face s 10 font-lock-string-face)
426 ;; It's confused by the closing paren in the middle.
427 (ruby-assert-state s 8 nil)))
429 (ert-deftest ruby-interpolation-inside-double-quoted-percent-literals ()
430 (ruby-assert-face "%Q{foo #@bar}" 8 font-lock-variable-name-face)
431 (ruby-assert-face "%W{foo #@bar}" 8 font-lock-variable-name-face)
432 (ruby-assert-face "%r{foo #@bar}" 8 font-lock-variable-name-face)
433 (ruby-assert-face "%x{foo #@bar}" 8 font-lock-variable-name-face))
435 (ert-deftest ruby-no-interpolation-in-single-quoted-literals ()
436 (ruby-assert-face "'foo #@bar'" 7 font-lock-string-face)
437 (ruby-assert-face "%q{foo #@bar}" 8 font-lock-string-face)
438 (ruby-assert-face "%w{foo #@bar}" 8 font-lock-string-face)
439 (ruby-assert-face "%s{foo #@bar}" 8 font-lock-string-face))
441 (ert-deftest ruby-no-unknown-percent-literals ()
442 ;; No folding of case.
443 (ruby-assert-face "%S{foo}" 4 nil)
444 (ruby-assert-face "%R{foo}" 4 nil))
446 (ert-deftest ruby-add-log-current-method-examples ()
447 (let ((pairs '(("foo" . "#foo")
448 ("C.foo" . ".foo")
449 ("self.foo" . ".foo"))))
450 (dolist (pair pairs)
451 (let ((name (car pair))
452 (value (cdr pair)))
453 (ruby-with-temp-buffer (ruby-test-string
454 "module M
455 | class C
456 | def %s
458 | end
459 | end
460 |end"
461 name)
462 (search-backward "_")
463 (forward-line)
464 (should (string= (ruby-add-log-current-method)
465 (format "M::C%s" value))))))))
467 (ert-deftest ruby-add-log-current-method-outside-of-method ()
468 (ruby-with-temp-buffer (ruby-test-string
469 "module M
470 | class C
471 | def foo
472 | end
474 | end
475 |end")
476 (search-backward "_")
477 (should (string= (ruby-add-log-current-method)"M::C"))))
479 (ert-deftest ruby-add-log-current-method-in-singleton-class ()
480 (ruby-with-temp-buffer (ruby-test-string
481 "class C
482 | class << self
483 | def foo
485 | end
486 | end
487 |end")
488 (search-backward "_")
489 (should (string= (ruby-add-log-current-method) "C.foo"))))
491 (ert-deftest ruby-add-log-current-method-namespace-shorthand ()
492 (ruby-with-temp-buffer (ruby-test-string
493 "class C::D
494 | def foo
496 | end
497 |end")
498 (search-backward "_")
499 (should (string= (ruby-add-log-current-method) "C::D#foo"))))
501 (ert-deftest ruby-add-log-current-method-after-inner-class ()
502 (ruby-with-temp-buffer (ruby-test-string
503 "module M
504 | class C
505 | class D
506 | end
507 | def foo
509 | end
510 | end
511 |end")
512 (search-backward "_")
513 (should (string= (ruby-add-log-current-method) "M::C#foo"))))
515 (defvar ruby-block-test-example
516 (ruby-test-string
517 "class C
518 | def foo
520 | end
522 | def bar
524 | end
526 | def baz
527 |some do
529 | end
530 | end
531 |end"))
533 (defmacro ruby-deftest-move-to-block (name &rest body)
534 (declare (indent defun))
535 `(ert-deftest ,(intern (format "ruby-move-to-block-%s" name)) ()
536 (with-temp-buffer
537 (insert ruby-block-test-example)
538 (ruby-mode)
539 (goto-char (point-min))
540 ,@body)))
542 (ruby-deftest-move-to-block works-on-do
543 (forward-line 10)
544 (ruby-end-of-block)
545 (should (= 13 (line-number-at-pos)))
546 (ruby-beginning-of-block)
547 (should (= 11 (line-number-at-pos))))
549 (ruby-deftest-move-to-block zero-is-noop
550 (forward-line 4)
551 (ruby-move-to-block 0)
552 (should (= 5 (line-number-at-pos))))
554 (ruby-deftest-move-to-block ok-with-three
555 (forward-line 1)
556 (ruby-move-to-block 3)
557 (should (= 14 (line-number-at-pos))))
559 (ruby-deftest-move-to-block ok-with-minus-two
560 (forward-line 9)
561 (ruby-move-to-block -2)
562 (should (= 2 (line-number-at-pos))))
564 (ert-deftest ruby-move-to-block-skips-percent-literal ()
565 (dolist (s (list (ruby-test-string
566 "foo do
567 | a = %%w(
568 | def yaa
570 |end")
571 (ruby-test-string
572 "foo do
573 | a = %%w|
574 | end
576 |end")))
577 (ruby-with-temp-buffer s
578 (goto-char (point-min))
579 (ruby-end-of-block)
580 (should (= 5 (line-number-at-pos)))
581 (ruby-beginning-of-block)
582 (should (= 1 (line-number-at-pos))))))
584 (ert-deftest ruby-move-to-block-skips-heredoc ()
585 (ruby-with-temp-buffer
586 (ruby-test-string
587 "if something_wrong?
588 | ActiveSupport::Deprecation.warn(<<-eowarn)
589 | boo hoo
590 | end
591 | eowarn
592 |end")
593 (goto-char (point-min))
594 (ruby-end-of-block)
595 (should (= 6 (line-number-at-pos)))
596 (ruby-beginning-of-block)
597 (should (= 1 (line-number-at-pos)))))
599 (ert-deftest ruby-move-to-block-does-not-fold-case ()
600 (ruby-with-temp-buffer
601 (ruby-test-string
602 "foo do
603 | Module.to_s
604 |end")
605 (let ((case-fold-search t))
606 (ruby-beginning-of-block))
607 (should (= 1 (line-number-at-pos)))))
609 (ert-deftest ruby-move-to-block-moves-from-else-to-if ()
610 (ruby-with-temp-buffer (ruby-test-string
611 "if true
612 | nested_block do
613 | end
614 |else
615 |end")
616 (goto-char (point-min))
617 (forward-line 3)
618 (ruby-beginning-of-block)
619 (should (= 1 (line-number-at-pos)))))
621 (ert-deftest ruby-beginning-of-defun-does-not-fold-case ()
622 (ruby-with-temp-buffer
623 (ruby-test-string
624 "class C
625 | def bar
626 | Class.to_s
627 | end
628 |end")
629 (goto-char (point-min))
630 (forward-line 3)
631 (let ((case-fold-search t))
632 (beginning-of-defun))
633 (should (= 2 (line-number-at-pos)))))
635 (ert-deftest ruby-end-of-defun-skips-to-next-line-after-the-method ()
636 (ruby-with-temp-buffer
637 (ruby-test-string
638 "class D
639 | def tee
640 | 'ho hum'
641 | end
642 |end")
643 (goto-char (point-min))
644 (forward-line 1)
645 (end-of-defun)
646 (should (= 5 (line-number-at-pos)))))
648 (defvar ruby-sexp-test-example
649 (ruby-test-string
650 "class C
651 | def foo
652 | self.end
653 | D.new.class
654 | [1, 2, 3].map do |i|
655 | i + 1
656 | end.sum
657 | end
658 |end"))
660 (ert-deftest ruby-forward-sexp-skips-method-calls-with-keyword-names ()
661 (ruby-with-temp-buffer ruby-sexp-test-example
662 (goto-line 2)
663 (ruby-forward-sexp)
664 (should (= 8 (line-number-at-pos)))))
666 (ert-deftest ruby-backward-sexp-skips-method-calls-with-keyword-names ()
667 (ruby-with-temp-buffer ruby-sexp-test-example
668 (goto-line 8)
669 (end-of-line)
670 (ruby-backward-sexp)
671 (should (= 2 (line-number-at-pos)))))
673 (ert-deftest ruby--insert-coding-comment-ruby-style ()
674 (with-temp-buffer
675 (let ((ruby-encoding-magic-comment-style 'ruby))
676 (ruby--insert-coding-comment "utf-8")
677 (should (string= "# coding: utf-8\n" (buffer-string))))))
679 (ert-deftest ruby--insert-coding-comment-emacs-style ()
680 (with-temp-buffer
681 (let ((ruby-encoding-magic-comment-style 'emacs))
682 (ruby--insert-coding-comment "utf-8")
683 (should (string= "# -*- coding: utf-8 -*-\n" (buffer-string))))))
685 (ert-deftest ruby--insert-coding-comment-custom-style ()
686 (with-temp-buffer
687 (let ((ruby-encoding-magic-comment-style 'custom)
688 (ruby-custom-encoding-magic-comment-template "# encoding: %s\n"))
689 (ruby--insert-coding-comment "utf-8")
690 (should (string= "# encoding: utf-8\n\n" (buffer-string))))))
693 (provide 'ruby-mode-tests)
695 ;;; ruby-mode-tests.el ends here