1 ;;; ruby-mode-tests.el --- Test suite for ruby-mode
3 ;; Copyright (C) 2012-2013 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/>.
26 (defun ruby-should-indent (content column)
27 "Assert indentation COLUMN on the last line of CONTENT."
28 (ruby-with-temp-buffer content
30 (should (= (current-indentation) column))))
32 (defun ruby-should-indent-buffer (expected content)
33 "Assert that CONTENT turns into EXPECTED after the buffer is re-indented.
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)))))
40 (defmacro ruby-with-temp-buffer (contents &rest body)
41 (declare (indent 1) (debug t))
47 (defun ruby-test-string (s &rest args)
48 (apply 'format (replace-regexp-in-string "^[ \t]*|" "" s) args))
50 (defun ruby-assert-state (content index value &optional point)
51 "Assert syntax state values at the end of CONTENT.
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)))
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)))))
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"
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))
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))
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)))
84 (ert-deftest ruby-singleton-class-no-heredoc-font-lock ()
85 (ruby-assert-face "class<<a" 8 nil))
87 (ert-deftest ruby-heredoc-highlights-interpolations ()
88 (ruby-assert-face "s = <<EOS\n #{foo}\nEOS" 15 font-lock-variable-name-face))
90 (ert-deftest ruby-no-heredoc-inside-quotes ()
91 (ruby-assert-state "\"<<\", \"\",\nfoo" 3 nil))
93 (ert-deftest ruby-deep-indent ()
94 (let ((ruby-deep-arglist nil)
95 (ruby-deep-indent-paren '(?\( ?\{ ?\[ ?\] t)))
96 (ruby-should-indent "foo = [1,\n2" 7)
97 (ruby-should-indent "foo = {a: b,\nc: d" 7)
98 (ruby-should-indent "foo(a,\nb" 4)))
100 (ert-deftest ruby-deep-indent-disabled ()
101 (let ((ruby-deep-arglist nil)
102 (ruby-deep-indent-paren nil))
103 (ruby-should-indent "foo = [\n1" ruby-indent-level)
104 (ruby-should-indent "foo = {\na: b" ruby-indent-level)
105 (ruby-should-indent "foo(\na" ruby-indent-level)))
107 (ert-deftest ruby-indent-after-keyword-in-a-string ()
108 (ruby-should-indent "a = \"abc\nif\"\n " 0)
109 (ruby-should-indent "a = %w[abc\n def]\n " 0)
110 (ruby-should-indent "a = \"abc\n def\"\n " 0))
112 (ert-deftest ruby-regexp-doesnt-start-in-string ()
113 (ruby-assert-state "'(/', /\d+/" 3 nil))
115 (ert-deftest ruby-regexp-starts-after-string ()
116 (ruby-assert-state "'(/', /\d+/" 3 ?/ 8))
118 (ert-deftest ruby-regexp-interpolation-is-highlighted ()
119 (ruby-assert-face "/#{foobs}/" 4 font-lock-variable-name-face))
121 (ert-deftest ruby-regexp-skips-over-interpolation ()
122 (ruby-assert-state "/#{foobs.join('/')}/" 3 nil))
124 (ert-deftest ruby-regexp-continues-till-end-when-unclosed ()
125 (ruby-assert-state "/bars" 3 ?/))
127 (ert-deftest ruby-regexp-can-be-multiline ()
128 (ruby-assert-state "/bars\ntees # toots \nfoos/" 3 nil))
130 (ert-deftest ruby-slash-symbol-is-not-mistaken-for-regexp ()
131 (ruby-assert-state ":/" 3 nil))
133 (ert-deftest ruby-slash-char-literal-is-not-mistaken-for-regexp ()
134 (ruby-assert-state "?/" 3 nil))
136 (ert-deftest ruby-indent-simple ()
137 (ruby-should-indent-buffer
149 (ert-deftest ruby-indent-keyword-label ()
150 (ruby-should-indent-buffer
162 (ert-deftest ruby-indent-method-with-question-mark ()
163 (ruby-should-indent-buffer
173 (ert-deftest ruby-indent-expr-in-regexp ()
174 (ruby-should-indent-buffer
184 (ert-deftest ruby-indent-singleton-class ()
185 (ruby-should-indent-buffer
195 (ert-deftest ruby-indent-inside-heredoc-after-operator ()
196 (ruby-should-indent-buffer
202 (ert-deftest ruby-indent-inside-heredoc-after-space ()
203 (ruby-should-indent-buffer
204 "foo <<eos.gsub(' ', '*')
206 "foo <<eos.gsub(' ', '*')
209 (ert-deftest ruby-indent-array-literal ()
210 (let ((ruby-deep-indent-paren nil))
211 (ruby-should-indent-buffer
220 (ruby-should-indent-buffer
230 (ert-deftest ruby-indent-begin-end ()
231 (ruby-should-indent-buffer
241 (ert-deftest ruby-indent-array-after-paren-and-space ()
242 (ruby-should-indent-buffer
256 (ert-deftest ruby-indent-after-block-in-continued-expression ()
257 (ruby-should-indent-buffer
269 (ert-deftest ruby-indent-spread-args-in-parens ()
270 (let ((ruby-deep-indent-paren '(?\()))
271 (ruby-should-indent-buffer
281 (ert-deftest ruby-move-to-block-stops-at-indentation ()
282 (ruby-with-temp-buffer "def f\nend"
284 (ruby-move-to-block -1)
285 (should (looking-at "^def"))))
287 (ert-deftest ruby-toggle-block-to-do-end ()
288 (ruby-with-temp-buffer "foo {|b|\n}"
291 (should (string= "foo do |b|\nend" (buffer-string)))))
293 (ert-deftest ruby-toggle-block-to-brace ()
294 (let ((pairs '((16 . "foo {|b| b + 2 }")
295 (15 . "foo {|b|\n b + 2\n}"))))
298 (let ((fill-column (car pair)))
299 (insert "foo do |b|\n b + 2\nend")
303 (should (string= (cdr pair) (buffer-string))))))))
305 (ert-deftest ruby-toggle-block-to-multiline ()
306 (ruby-with-temp-buffer "foo {|b| b + 1}"
309 (should (string= "foo do |b|\n b + 1\nend" (buffer-string)))))
311 (ert-deftest ruby-recognize-symbols-starting-with-at-character ()
312 (ruby-assert-face ":@abc" 3 font-lock-constant-face))
314 (ert-deftest ruby-hash-character-not-interpolation ()
315 (ruby-assert-face "\"This is #{interpolation}\"" 15
316 font-lock-variable-name-face)
317 (ruby-assert-face "\"This is \\#{no interpolation} despite the #\""
318 15 font-lock-string-face)
319 (ruby-assert-face "\n#@comment, not ruby code" 5 font-lock-comment-face)
320 (ruby-assert-state "\n#@comment, not ruby code" 4 t)
321 (ruby-assert-face "# A comment cannot have #{an interpolation} in it"
322 30 font-lock-comment-face)
323 (ruby-assert-face "# #{comment}\n \"#{interpolation}\"" 16
324 font-lock-variable-name-face))
326 (ert-deftest ruby-interpolation-suppresses-quotes-inside ()
327 (let ((s "\"<ul><li>#{@files.join(\"</li><li>\")}</li></ul>\""))
328 (ruby-assert-state s 8 nil)
329 (ruby-assert-face s 9 font-lock-string-face)
330 (ruby-assert-face s 10 font-lock-variable-name-face)
331 (ruby-assert-face s 41 font-lock-string-face)))
333 (ert-deftest ruby-interpolation-suppresses-one-double-quote ()
334 (let ((s "\"foo#{'\"'}\""))
335 (ruby-assert-state s 8 nil)
336 (ruby-assert-face s 8 font-lock-variable-name-face)
337 (ruby-assert-face s 11 font-lock-string-face)))
339 (ert-deftest ruby-interpolation-suppresses-one-backtick ()
340 (let ((s "`as#{'`'}das`"))
341 (ruby-assert-state s 8 nil)))
343 (ert-deftest ruby-interpolation-keeps-non-quote-syntax ()
344 (let ((s "\"foo#{baz.tee}bar\""))
345 (ruby-with-temp-buffer s
346 (goto-char (point-min))
348 (font-lock-fontify-buffer)
349 (search-forward "tee")
350 (should (string= (thing-at-point 'symbol) "tee")))))
352 (ert-deftest ruby-interpolation-inside-percent-literal ()
353 (let ((s "%( #{boo} )"))
354 (ruby-assert-face s 1 font-lock-string-face)
355 (ruby-assert-face s 4 font-lock-variable-name-face)
356 (ruby-assert-face s 10 font-lock-string-face)
357 (ruby-assert-state s 8 nil)))
359 (ert-deftest ruby-interpolation-inside-percent-literal-with-paren ()
360 :expected-result :failed
361 (let ((s "%(^#{\")\"}^)"))
362 (ruby-assert-face s 3 font-lock-string-face)
363 (ruby-assert-face s 4 font-lock-variable-name-face)
364 (ruby-assert-face s 10 font-lock-string-face)
365 ;; It's confused by the closing paren in the middle.
366 (ruby-assert-state s 8 nil)))
368 (ert-deftest ruby-interpolation-inside-double-quoted-percent-literals ()
369 (ruby-assert-face "%Q{foo #@bar}" 8 font-lock-variable-name-face)
370 (ruby-assert-face "%W{foo #@bar}" 8 font-lock-variable-name-face)
371 (ruby-assert-face "%r{foo #@bar}" 8 font-lock-variable-name-face)
372 (ruby-assert-face "%x{foo #@bar}" 8 font-lock-variable-name-face))
374 (ert-deftest ruby-no-interpolation-in-single-quoted-literals ()
375 (ruby-assert-face "'foo #@bar'" 7 font-lock-string-face)
376 (ruby-assert-face "%q{foo #@bar}" 8 font-lock-string-face)
377 (ruby-assert-face "%w{foo #@bar}" 8 font-lock-string-face)
378 (ruby-assert-face "%s{foo #@bar}" 8 font-lock-string-face))
380 (ert-deftest ruby-no-unknown-percent-literals ()
381 ;; No folding of case.
382 (ruby-assert-face "%S{foo}" 4 nil)
383 (ruby-assert-face "%R{foo}" 4 nil))
385 (ert-deftest ruby-add-log-current-method-examples ()
386 (let ((pairs '(("foo" . "#foo")
388 ("self.foo" . ".foo"))))
390 (let ((name (car pair))
392 (ruby-with-temp-buffer (ruby-test-string
401 (search-backward "_")
403 (should (string= (ruby-add-log-current-method)
404 (format "M::C%s" value))))))))
406 (ert-deftest ruby-add-log-current-method-outside-of-method ()
407 (ruby-with-temp-buffer (ruby-test-string
415 (search-backward "_")
416 (should (string= (ruby-add-log-current-method)"M::C"))))
418 (ert-deftest ruby-add-log-current-method-in-singleton-class ()
419 (ruby-with-temp-buffer (ruby-test-string
427 (search-backward "_")
428 (should (string= (ruby-add-log-current-method) "C.foo"))))
430 (ert-deftest ruby-add-log-current-method-namespace-shorthand ()
431 (ruby-with-temp-buffer (ruby-test-string
437 (search-backward "_")
438 (should (string= (ruby-add-log-current-method) "C::D#foo"))))
440 (ert-deftest ruby-add-log-current-method-after-inner-class ()
441 (ruby-with-temp-buffer (ruby-test-string
451 (search-backward "_")
452 (should (string= (ruby-add-log-current-method) "M::C#foo"))))
454 (defvar ruby-block-test-example
472 (defmacro ruby-deftest-move-to-block (name &rest body)
473 `(ert-deftest ,(intern (format "ruby-move-to-block-%s" name)) ()
475 (insert ruby-block-test-example)
479 (put 'ruby-deftest-move-to-block 'lisp-indent-function 'defun)
481 (ruby-deftest-move-to-block works-on-do
484 (should (= 13 (line-number-at-pos)))
485 (ruby-beginning-of-block)
486 (should (= 11 (line-number-at-pos))))
488 (ruby-deftest-move-to-block zero-is-noop
490 (ruby-move-to-block 0)
491 (should (= 5 (line-number-at-pos))))
493 (ruby-deftest-move-to-block ok-with-three
495 (ruby-move-to-block 3)
496 (should (= 14 (line-number-at-pos))))
498 (ruby-deftest-move-to-block ok-with-minus-two
500 (ruby-move-to-block -2)
501 (should (= 2 (line-number-at-pos))))
503 (ert-deftest ruby-move-to-block-skips-percent-literal ()
504 (dolist (s (list (ruby-test-string
516 (ruby-with-temp-buffer s
519 (should (= 5 (line-number-at-pos)))
520 (ruby-beginning-of-block)
521 (should (= 1 (line-number-at-pos))))))
523 (ert-deftest ruby-move-to-block-skips-heredoc ()
524 (ruby-with-temp-buffer
527 | ActiveSupport::Deprecation.warn(<<-eowarn)
534 (should (= 6 (line-number-at-pos)))
535 (ruby-beginning-of-block)
536 (should (= 1 (line-number-at-pos)))))
538 (ert-deftest ruby-move-to-block-does-not-fold-case ()
539 (ruby-with-temp-buffer
545 (let ((case-fold-search t))
546 (ruby-beginning-of-block))
547 (should (= 1 (line-number-at-pos)))))
549 (ert-deftest ruby-move-to-block-moves-from-else-to-if ()
550 (ruby-with-temp-buffer (ruby-test-string
557 (ruby-beginning-of-block)
558 (should (= 1 (line-number-at-pos)))))
560 (ert-deftest ruby-beginning-of-defun-does-not-fold-case ()
561 (ruby-with-temp-buffer
569 (let ((case-fold-search t))
570 (beginning-of-defun))
571 (should (= 2 (line-number-at-pos)))))
573 (ert-deftest ruby-end-of-defun-skips-to-next-line-after-the-method ()
574 (ruby-with-temp-buffer
583 (should (= 5 (line-number-at-pos)))))
585 (provide 'ruby-mode-tests)
587 ;;; ruby-mode-tests.el ends here