fix a few latent issues in the thread patch
[emacs.git] / test / automated / ruby-mode-tests.el
blob77dd62821f74efb9cc523cd7a79d7d90033017a2
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 'ruby-mode)
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))))
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))
42 `(with-temp-buffer
43 (insert ,contents)
44 (ruby-mode)
45 ,@body))
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)))
59 value))))
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"
69 ruby-indent-level))
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-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)))
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)))
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))
109 (ert-deftest ruby-regexp-doesnt-start-in-string ()
110 (ruby-assert-state "'(/', /\d+/" 3 nil))
112 (ert-deftest ruby-regexp-starts-after-string ()
113 (ruby-assert-state "'(/', /\d+/" 3 ?/ 8))
115 (ert-deftest ruby-regexp-interpolation-is-highlighted ()
116 (ruby-assert-face "/#{foobs}/" 4 font-lock-variable-name-face))
118 (ert-deftest ruby-regexp-skips-over-interpolation ()
119 (ruby-assert-state "/#{foobs.join('/')}/" 3 nil))
121 (ert-deftest ruby-regexp-continues-till-end-when-unclosed ()
122 (ruby-assert-state "/bars" 3 ?/))
124 (ert-deftest ruby-regexp-can-be-multiline ()
125 (ruby-assert-state "/bars\ntees # toots \nfoos/" 3 nil))
127 (ert-deftest ruby-indent-simple ()
128 (ruby-should-indent-buffer
129 "if foo
130 | bar
131 |end
132 |zot
134 "if foo
135 |bar
136 | end
137 | zot
138 |"))
140 (ert-deftest ruby-indent-keyword-label ()
141 (ruby-should-indent-buffer
142 "bar(class: XXX) do
143 | foo
144 |end
145 |bar
147 "bar(class: XXX) do
148 | foo
149 | end
150 | bar
151 |"))
153 (ert-deftest ruby-indent-method-with-question-mark ()
154 (ruby-should-indent-buffer
155 "if x.is_a?(XXX)
156 | foo
157 |end
159 "if x.is_a?(XXX)
160 | foo
161 | end
162 |"))
164 (ert-deftest ruby-indent-expr-in-regexp ()
165 (ruby-should-indent-buffer
166 "if /#{foo}/ =~ s
167 | x = 1
168 |end
170 "if /#{foo}/ =~ s
171 | x = 1
172 | end
173 |"))
175 (ert-deftest ruby-indent-singleton-class ()
176 (ruby-should-indent-buffer
177 "class<<bar
178 | foo
179 |end
181 "class<<bar
182 |foo
183 | end
184 |"))
186 (ert-deftest ruby-indent-inside-heredoc-after-operator ()
187 (ruby-should-indent-buffer
188 "b=<<eos
189 | 42"
190 "b=<<eos
191 | 42"))
193 (ert-deftest ruby-indent-inside-heredoc-after-space ()
194 (ruby-should-indent-buffer
195 "foo <<eos.gsub(' ', '*')
196 | 42"
197 "foo <<eos.gsub(' ', '*')
198 | 42"))
200 (ert-deftest ruby-indent-array-literal ()
201 (let ((ruby-deep-indent-paren nil))
202 (ruby-should-indent-buffer
203 "foo = [
204 | bar
207 "foo = [
208 | bar
210 |"))
211 (ruby-should-indent-buffer
212 "foo do
213 | [bar]
214 |end
216 "foo do
217 |[bar]
218 | end
219 |"))
221 (ert-deftest ruby-indent-begin-end ()
222 (ruby-should-indent-buffer
223 "begin
224 | a[b]
225 |end
227 "begin
228 | a[b]
229 | end
230 |"))
232 (ert-deftest ruby-indent-array-after-paren-and-space ()
233 (ruby-should-indent-buffer
234 "class A
235 | def foo
236 | foo( [])
237 | end
238 |end
240 "class A
241 | def foo
242 |foo( [])
243 |end
244 | end
245 |"))
247 (ert-deftest ruby-indent-after-block-in-continued-expression ()
248 (ruby-should-indent-buffer
249 "var =
250 | begin
251 | val
252 | end
253 |statement"
254 "var =
255 |begin
256 |val
257 |end
258 |statement"))
260 (ert-deftest ruby-indent-spread-args-in-parens ()
261 (let ((ruby-deep-indent-paren '(?\()))
262 (ruby-should-indent-buffer
263 "foo(1,
264 | 2,
265 | 3)
267 "foo(1,
268 | 2,
269 | 3)
270 |")))
272 (ert-deftest ruby-move-to-block-stops-at-indentation ()
273 (ruby-with-temp-buffer "def f\nend"
274 (beginning-of-line)
275 (ruby-move-to-block -1)
276 (should (looking-at "^def"))))
278 (ert-deftest ruby-toggle-block-to-do-end ()
279 (ruby-with-temp-buffer "foo {|b|\n}"
280 (beginning-of-line)
281 (ruby-toggle-block)
282 (should (string= "foo do |b|\nend" (buffer-string)))))
284 (ert-deftest ruby-toggle-block-to-brace ()
285 (let ((pairs '((16 . "foo {|b| b + 2 }")
286 (15 . "foo {|b|\n b + 2\n}"))))
287 (dolist (pair pairs)
288 (with-temp-buffer
289 (let ((fill-column (car pair)))
290 (insert "foo do |b|\n b + 2\nend")
291 (ruby-mode)
292 (beginning-of-line)
293 (ruby-toggle-block)
294 (should (string= (cdr pair) (buffer-string))))))))
296 (ert-deftest ruby-toggle-block-to-multiline ()
297 (ruby-with-temp-buffer "foo {|b| b + 1}"
298 (beginning-of-line)
299 (ruby-toggle-block)
300 (should (string= "foo do |b|\n b + 1\nend" (buffer-string)))))
302 (ert-deftest ruby-recognize-symbols-starting-with-at-character ()
303 (ruby-assert-face ":@abc" 3 font-lock-constant-face))
305 (ert-deftest ruby-hash-character-not-interpolation ()
306 (ruby-assert-face "\"This is #{interpolation}\"" 15
307 font-lock-variable-name-face)
308 (ruby-assert-face "\"This is \\#{no interpolation} despite the #\""
309 15 font-lock-string-face)
310 (ruby-assert-face "\n#@comment, not ruby code" 5 font-lock-comment-face)
311 (ruby-assert-state "\n#@comment, not ruby code" 4 t)
312 (ruby-assert-face "# A comment cannot have #{an interpolation} in it"
313 30 font-lock-comment-face)
314 (ruby-assert-face "# #{comment}\n \"#{interpolation}\"" 16
315 font-lock-variable-name-face))
317 (ert-deftest ruby-interpolation-suppresses-quotes-inside ()
318 (let ((s "\"<ul><li>#{@files.join(\"</li><li>\")}</li></ul>\""))
319 (ruby-assert-state s 8 nil)
320 (ruby-assert-face s 9 font-lock-string-face)
321 (ruby-assert-face s 10 font-lock-variable-name-face)
322 (ruby-assert-face s 41 font-lock-string-face)))
324 (ert-deftest ruby-interpolation-suppresses-one-double-quote ()
325 (let ((s "\"foo#{'\"'}\""))
326 (ruby-assert-state s 8 nil)
327 (ruby-assert-face s 8 font-lock-variable-name-face)
328 (ruby-assert-face s 11 font-lock-string-face)))
330 (ert-deftest ruby-interpolation-suppresses-one-backtick ()
331 (let ((s "`as#{'`'}das`"))
332 (ruby-assert-state s 8 nil)))
334 (ert-deftest ruby-interpolation-keeps-non-quote-syntax ()
335 (let ((s "\"foo#{baz.tee}bar\""))
336 (ruby-with-temp-buffer s
337 (goto-char (point-min))
338 (ruby-mode)
339 (font-lock-fontify-buffer)
340 (search-forward "tee")
341 (should (string= (thing-at-point 'symbol) "tee")))))
343 (ert-deftest ruby-interpolation-inside-percent-literal ()
344 (let ((s "%( #{boo} )"))
345 (ruby-assert-face s 1 font-lock-string-face)
346 (ruby-assert-face s 4 font-lock-variable-name-face)
347 (ruby-assert-face s 10 font-lock-string-face)
348 (ruby-assert-state s 8 nil)))
350 (ert-deftest ruby-interpolation-inside-percent-literal-with-paren ()
351 :expected-result :failed
352 (let ((s "%(^#{\")\"}^)"))
353 (ruby-assert-face s 3 font-lock-string-face)
354 (ruby-assert-face s 4 font-lock-variable-name-face)
355 (ruby-assert-face s 10 font-lock-string-face)
356 ;; It's confused by the closing paren in the middle.
357 (ruby-assert-state s 8 nil)))
359 (ert-deftest ruby-interpolation-inside-double-quoted-percent-literals ()
360 (ruby-assert-face "%Q{foo #@bar}" 8 font-lock-variable-name-face)
361 (ruby-assert-face "%W{foo #@bar}" 8 font-lock-variable-name-face)
362 (ruby-assert-face "%r{foo #@bar}" 8 font-lock-variable-name-face)
363 (ruby-assert-face "%x{foo #@bar}" 8 font-lock-variable-name-face))
365 (ert-deftest ruby-no-interpolation-in-single-quoted-literals ()
366 (ruby-assert-face "'foo #@bar'" 7 font-lock-string-face)
367 (ruby-assert-face "%q{foo #@bar}" 8 font-lock-string-face)
368 (ruby-assert-face "%w{foo #@bar}" 8 font-lock-string-face)
369 (ruby-assert-face "%s{foo #@bar}" 8 font-lock-string-face))
371 (ert-deftest ruby-no-unknown-percent-literals ()
372 ;; No folding of case.
373 (ruby-assert-face "%S{foo}" 4 nil)
374 (ruby-assert-face "%R{foo}" 4 nil))
376 (ert-deftest ruby-add-log-current-method-examples ()
377 (let ((pairs '(("foo" . "#foo")
378 ("C.foo" . ".foo")
379 ("self.foo" . ".foo"))))
380 (dolist (pair pairs)
381 (let ((name (car pair))
382 (value (cdr pair)))
383 (ruby-with-temp-buffer (ruby-test-string
384 "module M
385 | class C
386 | def %s
388 | end
389 | end
390 |end"
391 name)
392 (search-backward "_")
393 (forward-line)
394 (should (string= (ruby-add-log-current-method)
395 (format "M::C%s" value))))))))
397 (ert-deftest ruby-add-log-current-method-outside-of-method ()
398 (ruby-with-temp-buffer (ruby-test-string
399 "module M
400 | class C
401 | def foo
402 | end
404 | end
405 |end")
406 (search-backward "_")
407 (should (string= (ruby-add-log-current-method)"M::C"))))
409 (ert-deftest ruby-add-log-current-method-in-singleton-class ()
410 (ruby-with-temp-buffer (ruby-test-string
411 "class C
412 | class << self
413 | def foo
415 | end
416 | end
417 |end")
418 (search-backward "_")
419 (should (string= (ruby-add-log-current-method) "C.foo"))))
421 (ert-deftest ruby-add-log-current-method-namespace-shorthand ()
422 (ruby-with-temp-buffer (ruby-test-string
423 "class C::D
424 | def foo
426 | end
427 |end")
428 (search-backward "_")
429 (should (string= (ruby-add-log-current-method) "C::D#foo"))))
431 (ert-deftest ruby-add-log-current-method-after-inner-class ()
432 (ruby-with-temp-buffer (ruby-test-string
433 "module M
434 | class C
435 | class D
436 | end
437 | def foo
439 | end
440 | end
441 |end")
442 (search-backward "_")
443 (should (string= (ruby-add-log-current-method) "M::C#foo"))))
445 (defvar ruby-block-test-example
446 (ruby-test-string
447 "class C
448 | def foo
450 | end
452 | def bar
454 | end
456 | def baz
457 |some do
459 | end
460 | end
461 |end"))
463 (defmacro ruby-deftest-move-to-block (name &rest body)
464 `(ert-deftest ,(intern (format "ruby-move-to-block-%s" name)) ()
465 (with-temp-buffer
466 (insert ruby-block-test-example)
467 (ruby-mode)
468 ,@body)))
470 (put 'ruby-deftest-move-to-block 'lisp-indent-function 'defun)
472 (ruby-deftest-move-to-block works-on-do
473 (goto-line 11)
474 (ruby-end-of-block)
475 (should (= 13 (line-number-at-pos)))
476 (ruby-beginning-of-block)
477 (should (= 11 (line-number-at-pos))))
479 (ruby-deftest-move-to-block zero-is-noop
480 (goto-line 5)
481 (ruby-move-to-block 0)
482 (should (= 5 (line-number-at-pos))))
484 (ruby-deftest-move-to-block ok-with-three
485 (goto-line 2)
486 (ruby-move-to-block 3)
487 (should (= 14 (line-number-at-pos))))
489 (ruby-deftest-move-to-block ok-with-minus-two
490 (goto-line 10)
491 (ruby-move-to-block -2)
492 (should (= 2 (line-number-at-pos))))
494 (ert-deftest ruby-move-to-block-skips-percent-literal ()
495 (dolist (s (list (ruby-test-string
496 "foo do
497 | a = %%w(
498 | def yaa
500 |end")
501 (ruby-test-string
502 "foo do
503 | a = %%w|
504 | end
506 |end")))
507 (ruby-with-temp-buffer s
508 (goto-line 1)
509 (ruby-end-of-block)
510 (should (= 5 (line-number-at-pos)))
511 (ruby-beginning-of-block)
512 (should (= 1 (line-number-at-pos))))))
514 (ert-deftest ruby-move-to-block-skips-heredoc ()
515 (ruby-with-temp-buffer
516 (ruby-test-string
517 "if something_wrong?
518 | ActiveSupport::Deprecation.warn(<<-eowarn)
519 | boo hoo
520 | end
521 | eowarn
522 |end")
523 (goto-line 1)
524 (ruby-end-of-block)
525 (should (= 6 (line-number-at-pos)))
526 (ruby-beginning-of-block)
527 (should (= 1 (line-number-at-pos)))))
529 (ert-deftest ruby-move-to-block-does-not-fold-case ()
530 (ruby-with-temp-buffer
531 (ruby-test-string
532 "foo do
533 | Module.to_s
534 |end")
535 (end-of-buffer)
536 (let ((case-fold-search t))
537 (ruby-beginning-of-block))
538 (should (= 1 (line-number-at-pos)))))
540 (ert-deftest ruby-beginning-of-defun-does-not-fold-case ()
541 (ruby-with-temp-buffer
542 (ruby-test-string
543 "class C
544 | def bar
545 | Class.to_s
546 | end
547 |end")
548 (goto-line 4)
549 (let ((case-fold-search t))
550 (beginning-of-defun))
551 (should (= 2 (line-number-at-pos)))))
553 (ert-deftest ruby-end-of-defun-skips-to-next-line-after-the-method ()
554 (ruby-with-temp-buffer
555 (ruby-test-string
556 "class D
557 | def tee
558 | 'ho hum'
559 | end
560 |end")
561 (goto-line 2)
562 (end-of-defun)
563 (should (= 5 (line-number-at-pos)))))
565 (provide 'ruby-mode-tests)
567 ;;; ruby-mode-tests.el ends here