1 ;;; ruby-mode-tests.el --- Test suite for ruby-mode
3 ;; Copyright (C) 2012-2016 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/>.
27 (defmacro ruby-with-temp-buffer (contents &rest body)
28 (declare (indent 1) (debug t))
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)))
62 (defun ruby-assert-face (content pos face)
63 (ruby-with-temp-buffer content
64 (font-lock-ensure nil nil)
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"
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-no-heredoc-left-shift ()
95 ;; We can't really detect the left shift operator (like in similar
96 ;; cases, it depends on the type of foo), so we just require for <<
97 ;; to be preceded by a character from a known set.
98 (ruby-assert-state "foo(a<<b)" 3 nil))
100 (ert-deftest ruby-no-heredoc-class-self ()
101 (ruby-assert-state "class <<self\nend" 3 nil))
103 (ert-deftest ruby-exit!-font-lock ()
104 (ruby-assert-face "exit!" 5 font-lock-builtin-face))
106 (ert-deftest ruby-deep-indent ()
107 (let ((ruby-deep-arglist nil)
108 (ruby-deep-indent-paren '(?\( ?\{ ?\[ ?\] t)))
109 (ruby-should-indent "foo = [1,\n2" 7)
110 (ruby-should-indent "foo = {a: b,\nc: d" 7)
111 (ruby-should-indent "foo(a,\nb" 4)))
113 (ert-deftest ruby-deep-indent-disabled ()
114 (let ((ruby-deep-arglist nil)
115 (ruby-deep-indent-paren nil))
116 (ruby-should-indent "foo = [\n1" ruby-indent-level)
117 (ruby-should-indent "foo = {\na: b" ruby-indent-level)
118 (ruby-should-indent "foo(\na" ruby-indent-level)))
120 (ert-deftest ruby-indent-after-keyword-in-a-string ()
121 (ruby-should-indent "a = \"abc\nif\"\n " 0)
122 (ruby-should-indent "a = %w[abc\n def]\n " 0)
123 (ruby-should-indent "a = \"abc\n def\"\n " 0))
125 (ert-deftest ruby-regexp-doesnt-start-in-string ()
126 (ruby-assert-state "'(/', /\d+/" 3 nil))
128 (ert-deftest ruby-regexp-starts-after-string ()
129 (ruby-assert-state "'(/', /\d+/" 3 ?/ 8))
131 (ert-deftest ruby-regexp-interpolation-is-highlighted ()
132 (ruby-assert-face "/#{foobs}/" 4 font-lock-variable-name-face))
134 (ert-deftest ruby-regexp-skips-over-interpolation ()
135 (ruby-assert-state "/#{foobs.join('/')}/" 3 nil))
137 (ert-deftest ruby-regexp-continues-till-end-when-unclosed ()
138 (ruby-assert-state "/bars" 3 ?/))
140 (ert-deftest ruby-regexp-can-be-multiline ()
141 (ruby-assert-state "/bars\ntees # toots \nfoos/" 3 nil))
143 (ert-deftest ruby-slash-symbol-is-not-mistaken-for-regexp ()
144 (ruby-assert-state ":/" 3 nil))
146 (ert-deftest ruby-slash-char-literal-is-not-mistaken-for-regexp ()
147 (ruby-assert-state "?/" 3 nil))
149 (ert-deftest ruby-indent-simple ()
150 (ruby-should-indent-buffer
162 (ert-deftest ruby-indent-keyword-label ()
163 (ruby-should-indent-buffer
175 (ert-deftest ruby-indent-method-with-question-mark ()
176 (ruby-should-indent-buffer
186 (ert-deftest ruby-indent-expr-in-regexp ()
187 (ruby-should-indent-buffer
197 (ert-deftest ruby-indent-singleton-class ()
198 (ruby-should-indent-buffer
208 (ert-deftest ruby-indent-inside-heredoc-after-operator ()
209 (ruby-should-indent-buffer
215 (ert-deftest ruby-indent-inside-heredoc-after-space ()
216 (ruby-should-indent-buffer
217 "foo <<eos.gsub(' ', '*')
219 "foo <<eos.gsub(' ', '*')
222 (ert-deftest ruby-indent-array-literal ()
223 (let ((ruby-deep-indent-paren nil))
224 (ruby-should-indent-buffer
233 (ruby-should-indent-buffer
243 (ert-deftest ruby-indent-begin-end ()
244 (ruby-should-indent-buffer
254 (ert-deftest ruby-indent-array-after-paren-and-space ()
255 (ruby-should-indent-buffer
269 (ert-deftest ruby-indent-after-block-in-continued-expression ()
270 (ruby-should-indent-buffer
282 (ert-deftest ruby-indent-spread-args-in-parens ()
283 (let ((ruby-deep-indent-paren '(?\()))
284 (ruby-should-indent-buffer
294 (ert-deftest ruby-align-to-stmt-keywords-t ()
295 (let ((ruby-align-to-stmt-keywords t))
296 (ruby-should-indent-buffer
329 (ert-deftest ruby-align-to-stmt-keywords-case ()
330 (let ((ruby-align-to-stmt-keywords '(case)))
331 (ruby-should-indent-buffer
345 (ert-deftest ruby-align-chained-calls ()
346 (let ((ruby-align-chained-calls t))
347 (ruby-should-indent-buffer
351 |my_array.select { |str| str.size > 5 }
352 | .map { |str| str.downcase }"
356 |my_array.select { |str| str.size > 5 }
357 | .map { |str| str.downcase }")))
359 (ert-deftest ruby-move-to-block-stops-at-indentation ()
360 (ruby-with-temp-buffer "def f\nend"
362 (ruby-move-to-block -1)
363 (should (looking-at "^def"))))
365 (ert-deftest ruby-toggle-block-to-do-end ()
366 (ruby-with-temp-buffer "foo {|b|\n}"
369 (should (string= "foo do |b|\nend" (buffer-string)))))
371 (ert-deftest ruby-toggle-block-to-brace ()
372 (let ((pairs '((17 . "foo { |b| b + 2 }")
373 (16 . "foo { |b|\n b + 2\n}"))))
376 (let ((fill-column (car pair)))
377 (insert "foo do |b|\n b + 2\nend")
381 (should (string= (cdr pair) (buffer-string))))))))
383 (ert-deftest ruby-toggle-block-to-multiline ()
384 (ruby-with-temp-buffer "foo {|b| b + 1}"
387 (should (string= "foo do |b|\n b + 1\nend" (buffer-string)))))
389 (ert-deftest ruby-toggle-block-with-interpolation ()
390 (ruby-with-temp-buffer "foo do\n \"#{bar}\"\nend"
393 (should (string= "foo { \"#{bar}\" }" (buffer-string)))))
395 (ert-deftest ruby-recognize-symbols-starting-with-at-character ()
396 (ruby-assert-face ":@abc" 3 font-lock-constant-face))
398 (ert-deftest ruby-hash-character-not-interpolation ()
399 (ruby-assert-face "\"This is #{interpolation}\"" 15
400 font-lock-variable-name-face)
401 (ruby-assert-face "\"This is \\#{no interpolation} despite the #\""
402 15 font-lock-string-face)
403 (ruby-assert-face "\n#@comment, not ruby code" 5 font-lock-comment-face)
404 (ruby-assert-state "\n#@comment, not ruby code" 4 t)
405 (ruby-assert-face "# A comment cannot have #{an interpolation} in it"
406 30 font-lock-comment-face)
407 (ruby-assert-face "# #{comment}\n \"#{interpolation}\"" 16
408 font-lock-variable-name-face))
410 (ert-deftest ruby-interpolation-suppresses-quotes-inside ()
411 (let ((s "\"<ul><li>#{@files.join(\"</li><li>\")}</li></ul>\""))
412 (ruby-assert-state s 8 nil)
413 (ruby-assert-face s 9 font-lock-string-face)
414 (ruby-assert-face s 10 font-lock-variable-name-face)
415 (ruby-assert-face s 41 font-lock-string-face)))
417 (ert-deftest ruby-interpolation-suppresses-one-double-quote ()
418 (let ((s "\"foo#{'\"'}\""))
419 (ruby-assert-state s 8 nil)
420 (ruby-assert-face s 8 font-lock-variable-name-face)
421 (ruby-assert-face s 11 font-lock-string-face)))
423 (ert-deftest ruby-interpolation-suppresses-one-backtick ()
424 (let ((s "`as#{'`'}das`"))
425 (ruby-assert-state s 8 nil)))
427 (ert-deftest ruby-interpolation-keeps-non-quote-syntax ()
428 (let ((s "\"foo#{baz.tee}bar\""))
429 (ruby-with-temp-buffer s
430 (goto-char (point-min))
432 (syntax-propertize (point-max))
433 (search-forward "tee")
434 (should (string= (thing-at-point 'symbol) "tee")))))
436 (ert-deftest ruby-interpolation-inside-percent-literal ()
437 (let ((s "%( #{boo} )"))
438 (ruby-assert-face s 1 font-lock-string-face)
439 (ruby-assert-face s 4 font-lock-variable-name-face)
440 (ruby-assert-face s 10 font-lock-string-face)
441 (ruby-assert-state s 8 nil)))
443 (ert-deftest ruby-interpolation-inside-percent-literal-with-paren ()
444 :expected-result :failed
445 (let ((s "%(^#{\")\"}^)"))
446 (ruby-assert-face s 3 font-lock-string-face)
447 (ruby-assert-face s 4 font-lock-variable-name-face)
448 (ruby-assert-face s 10 font-lock-string-face)
449 ;; It's confused by the closing paren in the middle.
450 (ruby-assert-state s 8 nil)))
452 (ert-deftest ruby-interpolation-inside-another-interpolation ()
453 :expected-result :failed
454 (let ((s "\"#{[a, b, c].map { |v| \"#{v}\" }.join}\""))
455 (ruby-assert-face s 1 font-lock-string-face)
456 (ruby-assert-face s 2 font-lock-variable-name-face)
457 (ruby-assert-face s 38 font-lock-string-face)
458 (ruby-assert-state s 8 nil)))
460 (ert-deftest ruby-interpolation-inside-double-quoted-percent-literals ()
461 (ruby-assert-face "%Q{foo #@bar}" 8 font-lock-variable-name-face)
462 (ruby-assert-face "%W{foo #@bar}" 8 font-lock-variable-name-face)
463 (ruby-assert-face "%r{foo #@bar}" 8 font-lock-variable-name-face)
464 (ruby-assert-face "%x{foo #@bar}" 8 font-lock-variable-name-face))
466 (ert-deftest ruby-no-interpolation-in-single-quoted-literals ()
467 (ruby-assert-face "'foo #@bar'" 7 font-lock-string-face)
468 (ruby-assert-face "%q{foo #@bar}" 8 font-lock-string-face)
469 (ruby-assert-face "%w{foo #@bar}" 8 font-lock-string-face)
470 (ruby-assert-face "%s{foo #@bar}" 8 font-lock-string-face))
472 (ert-deftest ruby-interpolation-after-dollar-sign ()
473 (ruby-assert-face "\"$#{balance}\"" 2 'font-lock-string-face)
474 (ruby-assert-face "\"$#{balance}\"" 3 'font-lock-variable-name-face))
476 (ert-deftest ruby-no-unknown-percent-literals ()
477 ;; No folding of case.
478 (ruby-assert-face "%S{foo}" 4 nil)
479 (ruby-assert-face "%R{foo}" 4 nil))
481 (ert-deftest ruby-no-nested-percent-literals ()
482 (ruby-with-temp-buffer "a = %w[b %()]"
483 (syntax-propertize (point))
484 (should (null (nth 8 (syntax-ppss))))
485 (should (eq t (nth 3 (syntax-ppss (1- (point-max))))))
486 (search-backward "[")
487 (should (eq t (nth 3 (syntax-ppss))))))
489 (ert-deftest ruby-add-log-current-method-examples ()
490 (let ((pairs '(("foo" . "#foo")
492 ("self.foo" . ".foo"))))
494 (let ((name (car pair))
496 (ruby-with-temp-buffer (ruby-test-string
505 (search-backward "_")
507 (should (string= (ruby-add-log-current-method)
508 (format "M::C%s" value))))))))
510 (ert-deftest ruby-add-log-current-method-outside-of-method ()
511 (ruby-with-temp-buffer (ruby-test-string
519 (search-backward "_")
520 (should (string= (ruby-add-log-current-method)"M::C"))))
522 (ert-deftest ruby-add-log-current-method-in-singleton-class ()
523 (ruby-with-temp-buffer (ruby-test-string
531 (search-backward "_")
532 (should (string= (ruby-add-log-current-method) "C.foo"))))
534 (ert-deftest ruby-add-log-current-method-namespace-shorthand ()
535 (ruby-with-temp-buffer (ruby-test-string
541 (search-backward "_")
542 (should (string= (ruby-add-log-current-method) "C::D#foo"))))
544 (ert-deftest ruby-add-log-current-method-after-inner-class ()
545 (ruby-with-temp-buffer (ruby-test-string
555 (search-backward "_")
556 (should (string= (ruby-add-log-current-method) "M::C#foo"))))
558 (defvar ruby-block-test-example
576 (defmacro ruby-deftest-move-to-block (name &rest body)
577 (declare (indent defun))
578 `(ert-deftest ,(intern (format "ruby-move-to-block-%s" name)) ()
580 (insert ruby-block-test-example)
582 (goto-char (point-min))
585 (ruby-deftest-move-to-block works-on-do
588 (should (= 13 (line-number-at-pos)))
589 (ruby-beginning-of-block)
590 (should (= 11 (line-number-at-pos))))
592 (ruby-deftest-move-to-block zero-is-noop
594 (ruby-move-to-block 0)
595 (should (= 5 (line-number-at-pos))))
597 (ruby-deftest-move-to-block ok-with-three
599 (ruby-move-to-block 3)
600 (should (= 14 (line-number-at-pos))))
602 (ruby-deftest-move-to-block ok-with-minus-two
604 (ruby-move-to-block -2)
605 (should (= 2 (line-number-at-pos))))
607 (ert-deftest ruby-move-to-block-skips-percent-literal ()
608 (dolist (s (list (ruby-test-string
620 (ruby-with-temp-buffer s
621 (goto-char (point-min))
623 (should (= 5 (line-number-at-pos)))
624 (ruby-beginning-of-block)
625 (should (= 1 (line-number-at-pos))))))
627 (ert-deftest ruby-move-to-block-skips-heredoc ()
628 (ruby-with-temp-buffer
631 | ActiveSupport::Deprecation.warn(<<-eowarn)
636 (goto-char (point-min))
638 (should (= 6 (line-number-at-pos)))
639 (ruby-beginning-of-block)
640 (should (= 1 (line-number-at-pos)))))
642 (ert-deftest ruby-move-to-block-does-not-fold-case ()
643 (ruby-with-temp-buffer
648 (let ((case-fold-search t))
649 (ruby-beginning-of-block))
650 (should (= 1 (line-number-at-pos)))))
652 (ert-deftest ruby-move-to-block-moves-from-else-to-if ()
653 (ruby-with-temp-buffer (ruby-test-string
659 (goto-char (point-min))
661 (ruby-beginning-of-block)
662 (should (= 1 (line-number-at-pos)))))
664 (ert-deftest ruby-beginning-of-defun-does-not-fold-case ()
665 (ruby-with-temp-buffer
672 (goto-char (point-min))
674 (let ((case-fold-search t))
675 (beginning-of-defun))
676 (should (= 2 (line-number-at-pos)))))
678 (ert-deftest ruby-end-of-defun-skips-to-next-line-after-the-method ()
679 (ruby-with-temp-buffer
686 (goto-char (point-min))
689 (should (= 5 (line-number-at-pos)))))
691 (defvar ruby-sexp-test-example
697 | [1, 2, 3].map do |i|
703 (ert-deftest ruby-forward-sexp-skips-method-calls-with-keyword-names ()
704 (ruby-with-temp-buffer ruby-sexp-test-example
707 (should (= 8 (line-number-at-pos)))))
709 (ert-deftest ruby-backward-sexp-skips-method-calls-with-keyword-names ()
710 (ruby-with-temp-buffer ruby-sexp-test-example
714 (should (= 2 (line-number-at-pos)))))
716 (ert-deftest ruby--insert-coding-comment-ruby-style ()
718 (let ((ruby-encoding-magic-comment-style 'ruby))
719 (ruby--insert-coding-comment "utf-8")
720 (should (string= "# coding: utf-8\n" (buffer-string))))))
722 (ert-deftest ruby--insert-coding-comment-emacs-style ()
724 (let ((ruby-encoding-magic-comment-style 'emacs))
725 (ruby--insert-coding-comment "utf-8")
726 (should (string= "# -*- coding: utf-8 -*-\n" (buffer-string))))))
728 (ert-deftest ruby--insert-coding-comment-custom-style ()
730 (let ((ruby-encoding-magic-comment-style 'custom)
731 (ruby-custom-encoding-magic-comment-template "# encoding: %s\n"))
732 (ruby--insert-coding-comment "utf-8")
733 (should (string= "# encoding: utf-8\n\n" (buffer-string))))))
736 (provide 'ruby-mode-tests)
738 ;;; ruby-mode-tests.el ends here