Auto-commit of generated files.
[emacs.git] / test / automated / ruby-mode-tests.el
blobad805f167778a569398b6a36fefa9a1ef9fd3386
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/>.
20 ;;; Commentary:
22 ;;; Code:
24 (require 'ert)
25 (require 'ruby-mode)
27 (defun ruby-should-indent (content column)
28 "Assert indentation COLUMN on the last line of CONTENT."
29 (ruby-with-temp-buffer content
30 (ruby-indent-line)
31 (should (= (current-indentation) column))))
33 (defun ruby-should-indent-buffer (expected content)
34 "Assert that CONTENT turns into EXPECTED after the buffer is re-indented.
36 The whitespace before and including \"|\" on each line is removed."
37 (ruby-with-temp-buffer (ruby-test-string content)
38 (indent-region (point-min) (point-max))
39 (should (string= (ruby-test-string expected) (buffer-string)))))
41 (defmacro ruby-with-temp-buffer (contents &rest body)
42 (declare (indent 1) (debug t))
43 `(with-temp-buffer
44 (insert ,contents)
45 (ruby-mode)
46 ,@body))
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-deep-indent ()
95 (let ((ruby-deep-arglist nil)
96 (ruby-deep-indent-paren '(?\( ?\{ ?\[ ?\] t)))
97 (ruby-should-indent "foo = [1,\n2" 7)
98 (ruby-should-indent "foo = {a: b,\nc: d" 7)
99 (ruby-should-indent "foo(a,\nb" 4)))
101 (ert-deftest ruby-deep-indent-disabled ()
102 (let ((ruby-deep-arglist nil)
103 (ruby-deep-indent-paren nil))
104 (ruby-should-indent "foo = [\n1" ruby-indent-level)
105 (ruby-should-indent "foo = {\na: b" ruby-indent-level)
106 (ruby-should-indent "foo(\na" ruby-indent-level)))
108 (ert-deftest ruby-indent-after-keyword-in-a-string ()
109 (ruby-should-indent "a = \"abc\nif\"\n " 0)
110 (ruby-should-indent "a = %w[abc\n def]\n " 0)
111 (ruby-should-indent "a = \"abc\n def\"\n " 0))
113 (ert-deftest ruby-regexp-doesnt-start-in-string ()
114 (ruby-assert-state "'(/', /\d+/" 3 nil))
116 (ert-deftest ruby-regexp-starts-after-string ()
117 (ruby-assert-state "'(/', /\d+/" 3 ?/ 8))
119 (ert-deftest ruby-regexp-interpolation-is-highlighted ()
120 (ruby-assert-face "/#{foobs}/" 4 font-lock-variable-name-face))
122 (ert-deftest ruby-regexp-skips-over-interpolation ()
123 (ruby-assert-state "/#{foobs.join('/')}/" 3 nil))
125 (ert-deftest ruby-regexp-continues-till-end-when-unclosed ()
126 (ruby-assert-state "/bars" 3 ?/))
128 (ert-deftest ruby-regexp-can-be-multiline ()
129 (ruby-assert-state "/bars\ntees # toots \nfoos/" 3 nil))
131 (ert-deftest ruby-slash-symbol-is-not-mistaken-for-regexp ()
132 (ruby-assert-state ":/" 3 nil))
134 (ert-deftest ruby-slash-char-literal-is-not-mistaken-for-regexp ()
135 (ruby-assert-state "?/" 3 nil))
137 (ert-deftest ruby-indent-simple ()
138 (ruby-should-indent-buffer
139 "if foo
140 | bar
141 |end
142 |zot
144 "if foo
145 |bar
146 | end
147 | zot
148 |"))
150 (ert-deftest ruby-indent-keyword-label ()
151 (ruby-should-indent-buffer
152 "bar(class: XXX) do
153 | foo
154 |end
155 |bar
157 "bar(class: XXX) do
158 | foo
159 | end
160 | bar
161 |"))
163 (ert-deftest ruby-indent-method-with-question-mark ()
164 (ruby-should-indent-buffer
165 "if x.is_a?(XXX)
166 | foo
167 |end
169 "if x.is_a?(XXX)
170 | foo
171 | end
172 |"))
174 (ert-deftest ruby-indent-expr-in-regexp ()
175 (ruby-should-indent-buffer
176 "if /#{foo}/ =~ s
177 | x = 1
178 |end
180 "if /#{foo}/ =~ s
181 | x = 1
182 | end
183 |"))
185 (ert-deftest ruby-indent-singleton-class ()
186 (ruby-should-indent-buffer
187 "class<<bar
188 | foo
189 |end
191 "class<<bar
192 |foo
193 | end
194 |"))
196 (ert-deftest ruby-indent-inside-heredoc-after-operator ()
197 (ruby-should-indent-buffer
198 "b=<<eos
199 | 42"
200 "b=<<eos
201 | 42"))
203 (ert-deftest ruby-indent-inside-heredoc-after-space ()
204 (ruby-should-indent-buffer
205 "foo <<eos.gsub(' ', '*')
206 | 42"
207 "foo <<eos.gsub(' ', '*')
208 | 42"))
210 (ert-deftest ruby-indent-array-literal ()
211 (let ((ruby-deep-indent-paren nil))
212 (ruby-should-indent-buffer
213 "foo = [
214 | bar
217 "foo = [
218 | bar
220 |"))
221 (ruby-should-indent-buffer
222 "foo do
223 | [bar]
224 |end
226 "foo do
227 |[bar]
228 | end
229 |"))
231 (ert-deftest ruby-indent-begin-end ()
232 (ruby-should-indent-buffer
233 "begin
234 | a[b]
235 |end
237 "begin
238 | a[b]
239 | end
240 |"))
242 (ert-deftest ruby-indent-array-after-paren-and-space ()
243 (ruby-should-indent-buffer
244 "class A
245 | def foo
246 | foo( [])
247 | end
248 |end
250 "class A
251 | def foo
252 |foo( [])
253 |end
254 | end
255 |"))
257 (ert-deftest ruby-indent-after-block-in-continued-expression ()
258 (ruby-should-indent-buffer
259 "var =
260 | begin
261 | val
262 | end
263 |statement"
264 "var =
265 |begin
266 |val
267 |end
268 |statement"))
270 (ert-deftest ruby-indent-spread-args-in-parens ()
271 (let ((ruby-deep-indent-paren '(?\()))
272 (ruby-should-indent-buffer
273 "foo(1,
274 | 2,
275 | 3)
277 "foo(1,
278 | 2,
279 | 3)
280 |")))
282 (ert-deftest ruby-move-to-block-stops-at-indentation ()
283 (ruby-with-temp-buffer "def f\nend"
284 (beginning-of-line)
285 (ruby-move-to-block -1)
286 (should (looking-at "^def"))))
288 (ert-deftest ruby-toggle-block-to-do-end ()
289 (ruby-with-temp-buffer "foo {|b|\n}"
290 (beginning-of-line)
291 (ruby-toggle-block)
292 (should (string= "foo do |b|\nend" (buffer-string)))))
294 (ert-deftest ruby-toggle-block-to-brace ()
295 (let ((pairs '((16 . "foo {|b| b + 2 }")
296 (15 . "foo {|b|\n b + 2\n}"))))
297 (dolist (pair pairs)
298 (with-temp-buffer
299 (let ((fill-column (car pair)))
300 (insert "foo do |b|\n b + 2\nend")
301 (ruby-mode)
302 (beginning-of-line)
303 (ruby-toggle-block)
304 (should (string= (cdr pair) (buffer-string))))))))
306 (ert-deftest ruby-toggle-block-to-multiline ()
307 (ruby-with-temp-buffer "foo {|b| b + 1}"
308 (beginning-of-line)
309 (ruby-toggle-block)
310 (should (string= "foo do |b|\n b + 1\nend" (buffer-string)))))
312 (ert-deftest ruby-recognize-symbols-starting-with-at-character ()
313 (ruby-assert-face ":@abc" 3 font-lock-constant-face))
315 (ert-deftest ruby-hash-character-not-interpolation ()
316 (ruby-assert-face "\"This is #{interpolation}\"" 15
317 font-lock-variable-name-face)
318 (ruby-assert-face "\"This is \\#{no interpolation} despite the #\""
319 15 font-lock-string-face)
320 (ruby-assert-face "\n#@comment, not ruby code" 5 font-lock-comment-face)
321 (ruby-assert-state "\n#@comment, not ruby code" 4 t)
322 (ruby-assert-face "# A comment cannot have #{an interpolation} in it"
323 30 font-lock-comment-face)
324 (ruby-assert-face "# #{comment}\n \"#{interpolation}\"" 16
325 font-lock-variable-name-face))
327 (ert-deftest ruby-interpolation-suppresses-quotes-inside ()
328 (let ((s "\"<ul><li>#{@files.join(\"</li><li>\")}</li></ul>\""))
329 (ruby-assert-state s 8 nil)
330 (ruby-assert-face s 9 font-lock-string-face)
331 (ruby-assert-face s 10 font-lock-variable-name-face)
332 (ruby-assert-face s 41 font-lock-string-face)))
334 (ert-deftest ruby-interpolation-suppresses-one-double-quote ()
335 (let ((s "\"foo#{'\"'}\""))
336 (ruby-assert-state s 8 nil)
337 (ruby-assert-face s 8 font-lock-variable-name-face)
338 (ruby-assert-face s 11 font-lock-string-face)))
340 (ert-deftest ruby-interpolation-suppresses-one-backtick ()
341 (let ((s "`as#{'`'}das`"))
342 (ruby-assert-state s 8 nil)))
344 (ert-deftest ruby-interpolation-keeps-non-quote-syntax ()
345 (let ((s "\"foo#{baz.tee}bar\""))
346 (ruby-with-temp-buffer s
347 (goto-char (point-min))
348 (ruby-mode)
349 (font-lock-fontify-buffer)
350 (search-forward "tee")
351 (should (string= (thing-at-point 'symbol) "tee")))))
353 (ert-deftest ruby-interpolation-inside-percent-literal ()
354 (let ((s "%( #{boo} )"))
355 (ruby-assert-face s 1 font-lock-string-face)
356 (ruby-assert-face s 4 font-lock-variable-name-face)
357 (ruby-assert-face s 10 font-lock-string-face)
358 (ruby-assert-state s 8 nil)))
360 (ert-deftest ruby-interpolation-inside-percent-literal-with-paren ()
361 :expected-result :failed
362 (let ((s "%(^#{\")\"}^)"))
363 (ruby-assert-face s 3 font-lock-string-face)
364 (ruby-assert-face s 4 font-lock-variable-name-face)
365 (ruby-assert-face s 10 font-lock-string-face)
366 ;; It's confused by the closing paren in the middle.
367 (ruby-assert-state s 8 nil)))
369 (ert-deftest ruby-interpolation-inside-double-quoted-percent-literals ()
370 (ruby-assert-face "%Q{foo #@bar}" 8 font-lock-variable-name-face)
371 (ruby-assert-face "%W{foo #@bar}" 8 font-lock-variable-name-face)
372 (ruby-assert-face "%r{foo #@bar}" 8 font-lock-variable-name-face)
373 (ruby-assert-face "%x{foo #@bar}" 8 font-lock-variable-name-face))
375 (ert-deftest ruby-no-interpolation-in-single-quoted-literals ()
376 (ruby-assert-face "'foo #@bar'" 7 font-lock-string-face)
377 (ruby-assert-face "%q{foo #@bar}" 8 font-lock-string-face)
378 (ruby-assert-face "%w{foo #@bar}" 8 font-lock-string-face)
379 (ruby-assert-face "%s{foo #@bar}" 8 font-lock-string-face))
381 (ert-deftest ruby-no-unknown-percent-literals ()
382 ;; No folding of case.
383 (ruby-assert-face "%S{foo}" 4 nil)
384 (ruby-assert-face "%R{foo}" 4 nil))
386 (ert-deftest ruby-add-log-current-method-examples ()
387 (let ((pairs '(("foo" . "#foo")
388 ("C.foo" . ".foo")
389 ("self.foo" . ".foo"))))
390 (dolist (pair pairs)
391 (let ((name (car pair))
392 (value (cdr pair)))
393 (ruby-with-temp-buffer (ruby-test-string
394 "module M
395 | class C
396 | def %s
398 | end
399 | end
400 |end"
401 name)
402 (search-backward "_")
403 (forward-line)
404 (should (string= (ruby-add-log-current-method)
405 (format "M::C%s" value))))))))
407 (ert-deftest ruby-add-log-current-method-outside-of-method ()
408 (ruby-with-temp-buffer (ruby-test-string
409 "module M
410 | class C
411 | def foo
412 | end
414 | end
415 |end")
416 (search-backward "_")
417 (should (string= (ruby-add-log-current-method)"M::C"))))
419 (ert-deftest ruby-add-log-current-method-in-singleton-class ()
420 (ruby-with-temp-buffer (ruby-test-string
421 "class C
422 | class << self
423 | def foo
425 | end
426 | end
427 |end")
428 (search-backward "_")
429 (should (string= (ruby-add-log-current-method) "C.foo"))))
431 (ert-deftest ruby-add-log-current-method-namespace-shorthand ()
432 (ruby-with-temp-buffer (ruby-test-string
433 "class C::D
434 | def foo
436 | end
437 |end")
438 (search-backward "_")
439 (should (string= (ruby-add-log-current-method) "C::D#foo"))))
441 (ert-deftest ruby-add-log-current-method-after-inner-class ()
442 (ruby-with-temp-buffer (ruby-test-string
443 "module M
444 | class C
445 | class D
446 | end
447 | def foo
449 | end
450 | end
451 |end")
452 (search-backward "_")
453 (should (string= (ruby-add-log-current-method) "M::C#foo"))))
455 (defvar ruby-block-test-example
456 (ruby-test-string
457 "class C
458 | def foo
460 | end
462 | def bar
464 | end
466 | def baz
467 |some do
469 | end
470 | end
471 |end"))
473 (defmacro ruby-deftest-move-to-block (name &rest body)
474 `(ert-deftest ,(intern (format "ruby-move-to-block-%s" name)) ()
475 (with-temp-buffer
476 (insert ruby-block-test-example)
477 (ruby-mode)
478 (goto-char (point-min))
479 ,@body)))
481 (put 'ruby-deftest-move-to-block 'lisp-indent-function 'defun)
483 (ruby-deftest-move-to-block works-on-do
484 (forward-line 10)
485 (ruby-end-of-block)
486 (should (= 13 (line-number-at-pos)))
487 (ruby-beginning-of-block)
488 (should (= 11 (line-number-at-pos))))
490 (ruby-deftest-move-to-block zero-is-noop
491 (forward-line 4)
492 (ruby-move-to-block 0)
493 (should (= 5 (line-number-at-pos))))
495 (ruby-deftest-move-to-block ok-with-three
496 (forward-line 1)
497 (ruby-move-to-block 3)
498 (should (= 14 (line-number-at-pos))))
500 (ruby-deftest-move-to-block ok-with-minus-two
501 (forward-line 9)
502 (ruby-move-to-block -2)
503 (should (= 2 (line-number-at-pos))))
505 (ert-deftest ruby-move-to-block-skips-percent-literal ()
506 (dolist (s (list (ruby-test-string
507 "foo do
508 | a = %%w(
509 | def yaa
511 |end")
512 (ruby-test-string
513 "foo do
514 | a = %%w|
515 | end
517 |end")))
518 (ruby-with-temp-buffer s
519 (goto-char (point-min))
520 (ruby-end-of-block)
521 (should (= 5 (line-number-at-pos)))
522 (ruby-beginning-of-block)
523 (should (= 1 (line-number-at-pos))))))
525 (ert-deftest ruby-move-to-block-skips-heredoc ()
526 (ruby-with-temp-buffer
527 (ruby-test-string
528 "if something_wrong?
529 | ActiveSupport::Deprecation.warn(<<-eowarn)
530 | boo hoo
531 | end
532 | eowarn
533 |end")
534 (goto-char (point-min))
535 (ruby-end-of-block)
536 (should (= 6 (line-number-at-pos)))
537 (ruby-beginning-of-block)
538 (should (= 1 (line-number-at-pos)))))
540 (ert-deftest ruby-move-to-block-does-not-fold-case ()
541 (ruby-with-temp-buffer
542 (ruby-test-string
543 "foo do
544 | Module.to_s
545 |end")
546 (let ((case-fold-search t))
547 (ruby-beginning-of-block))
548 (should (= 1 (line-number-at-pos)))))
550 (ert-deftest ruby-move-to-block-moves-from-else-to-if ()
551 (ruby-with-temp-buffer (ruby-test-string
552 "if true
553 | nested_block do
554 | end
555 |else
556 |end")
557 (goto-char (point-min))
558 (forward-line 3)
559 (ruby-beginning-of-block)
560 (should (= 1 (line-number-at-pos)))))
562 (ert-deftest ruby-beginning-of-defun-does-not-fold-case ()
563 (ruby-with-temp-buffer
564 (ruby-test-string
565 "class C
566 | def bar
567 | Class.to_s
568 | end
569 |end")
570 (goto-char (point-min))
571 (forward-line 3)
572 (let ((case-fold-search t))
573 (beginning-of-defun))
574 (should (= 2 (line-number-at-pos)))))
576 (ert-deftest ruby-end-of-defun-skips-to-next-line-after-the-method ()
577 (ruby-with-temp-buffer
578 (ruby-test-string
579 "class D
580 | def tee
581 | 'ho hum'
582 | end
583 |end")
584 (goto-char (point-min))
585 (forward-line 1)
586 (end-of-defun)
587 (should (= 5 (line-number-at-pos)))))
589 (provide 'ruby-mode-tests)
591 ;;; ruby-mode-tests.el ends here