* test/automated/package-x-test.el: Change the commentary.
[bpt/emacs.git] / test / automated / ruby-mode-tests.el
CommitLineData
c28662a8
DG
1;;; ruby-mode-tests.el --- Test suite for ruby-mode
2
ab422c4d 3;; Copyright (C) 2012-2013 Free Software Foundation, Inc.
c28662a8
DG
4
5;; This file is part of GNU Emacs.
6
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.
11
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.
16
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/>.
19
20;;; Commentary:
21
22;;; Code:
23
24(require 'ruby-mode)
25
9cd80478 26(defun ruby-should-indent (content column)
9d2ed8a2 27 "Assert indentation COLUMN on the last line of CONTENT."
bb808526 28 (ruby-with-temp-buffer content
9cd80478 29 (ruby-indent-line)
9d2ed8a2
DG
30 (should (= (current-indentation) column))))
31
32(defun ruby-should-indent-buffer (expected content)
33 "Assert that CONTENT turns into EXPECTED after the buffer is re-indented.
34
35The whitespace before and including \"|\" on each line is removed."
bb808526 36 (ruby-with-temp-buffer (ruby-test-string content)
5745cae6
DG
37 (indent-region (point-min) (point-max))
38 (should (string= (ruby-test-string expected) (buffer-string)))))
39
bb808526
DG
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))
46
5745cae6
DG
47(defun ruby-test-string (s &rest args)
48 (apply 'format (replace-regexp-in-string "^[ \t]*|" "" s) args))
9cd80478 49
ab89e9f9 50(defun ruby-assert-state (content index value &optional point)
9cd80478
DG
51 "Assert syntax state values at the end of CONTENT.
52
53VALUES-PLIST is a list with alternating index and value elements."
bb808526 54 (ruby-with-temp-buffer content
ab89e9f9 55 (when point (goto-char point))
9cd80478 56 (syntax-propertize (point))
ab89e9f9
DG
57 (should (eq (nth index
58 (parse-partial-sexp (point-min) (point)))
59 value))))
9cd80478 60
0ba2d4b6 61(defun ruby-assert-face (content pos face)
bb808526 62 (ruby-with-temp-buffer content
0ba2d4b6
DG
63 (font-lock-fontify-buffer)
64 (should (eq face (get-text-property pos 'face)))))
65
9cd80478 66(ert-deftest ruby-indent-after-symbol-made-from-string-interpolation ()
c28662a8 67 "It can indent the line after symbol made using string interpolation."
9cd80478
DG
68 (ruby-should-indent "def foo(suffix)\n :\"bar#{suffix}\"\n"
69 ruby-indent-level))
70
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))
74
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))
c28662a8 78
f178c32d
DG
79(ert-deftest ruby-heredoc-font-lock ()
80 (let ((s "foo <<eos.gsub('^ *', '')"))
c62792e7 81 (ruby-assert-face s 9 font-lock-string-face)
f178c32d
DG
82 (ruby-assert-face s 10 nil)))
83
84(ert-deftest ruby-singleton-class-no-heredoc-font-lock ()
85 (ruby-assert-face "class<<a" 8 nil))
86
1a0a0a8a
DG
87(ert-deftest ruby-heredoc-highlights-interpolations ()
88 (ruby-assert-face "s = <<EOS\n #{foo}\nEOS" 15 font-lock-variable-name-face))
89
9d2ed8a2
DG
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)))
96
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)))
103
0ba2d4b6
DG
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))
108
307d0e95 109(ert-deftest ruby-regexp-doesnt-start-in-string ()
ab89e9f9
DG
110 (ruby-assert-state "'(/', /\d+/" 3 nil))
111
112(ert-deftest ruby-regexp-starts-after-string ()
113 (ruby-assert-state "'(/', /\d+/" 3 ?/ 8))
114
973d1e12
DG
115(ert-deftest ruby-regexp-interpolation-is-highlighted ()
116 (ruby-assert-face "/#{foobs}/" 4 font-lock-variable-name-face))
117
1a0a0a8a
DG
118(ert-deftest ruby-regexp-skips-over-interpolation ()
119 (ruby-assert-state "/#{foobs.join('/')}/" 3 nil))
120
121(ert-deftest ruby-regexp-continues-till-end-when-unclosed ()
122 (ruby-assert-state "/bars" 3 ?/))
123
124(ert-deftest ruby-regexp-can-be-multiline ()
125 (ruby-assert-state "/bars\ntees # toots \nfoos/" 3 nil))
126
d1bbba4f
DG
127(ert-deftest ruby-slash-symbol-is-not-mistaken-for-regexp ()
128 (ruby-assert-state ":/" 3 nil))
129
130(ert-deftest ruby-slash-char-literal-is-not-mistaken-for-regexp ()
131 (ruby-assert-state "?/" 3 nil))
132
9d2ed8a2
DG
133(ert-deftest ruby-indent-simple ()
134 (ruby-should-indent-buffer
135 "if foo
136 | bar
137 |end
138 |zot
139 |"
140 "if foo
141 |bar
142 | end
143 | zot
144 |"))
145
146(ert-deftest ruby-indent-keyword-label ()
147 (ruby-should-indent-buffer
148 "bar(class: XXX) do
149 | foo
150 |end
151 |bar
152 |"
153 "bar(class: XXX) do
154 | foo
155 | end
156 | bar
157 |"))
158
159(ert-deftest ruby-indent-method-with-question-mark ()
160 (ruby-should-indent-buffer
161 "if x.is_a?(XXX)
162 | foo
163 |end
164 |"
165 "if x.is_a?(XXX)
166 | foo
167 | end
168 |"))
169
170(ert-deftest ruby-indent-expr-in-regexp ()
171 (ruby-should-indent-buffer
172 "if /#{foo}/ =~ s
173 | x = 1
174 |end
175 |"
176 "if /#{foo}/ =~ s
177 | x = 1
178 | end
179 |"))
180
181(ert-deftest ruby-indent-singleton-class ()
9d2ed8a2
DG
182 (ruby-should-indent-buffer
183 "class<<bar
184 | foo
185 |end
186 |"
187 "class<<bar
188 |foo
189 | end
190 |"))
191
8619323f
DG
192(ert-deftest ruby-indent-inside-heredoc-after-operator ()
193 (ruby-should-indent-buffer
194 "b=<<eos
195 | 42"
196 "b=<<eos
197 | 42"))
198
199(ert-deftest ruby-indent-inside-heredoc-after-space ()
200 (ruby-should-indent-buffer
201 "foo <<eos.gsub(' ', '*')
202 | 42"
203 "foo <<eos.gsub(' ', '*')
204 | 42"))
205
9d2ed8a2
DG
206(ert-deftest ruby-indent-array-literal ()
207 (let ((ruby-deep-indent-paren nil))
208 (ruby-should-indent-buffer
209 "foo = [
210 | bar
211 |]
212 |"
213 "foo = [
214 | bar
215 | ]
216 |"))
217 (ruby-should-indent-buffer
218 "foo do
219 | [bar]
220 |end
221 |"
222 "foo do
223 |[bar]
224 | end
225 |"))
226
227(ert-deftest ruby-indent-begin-end ()
228 (ruby-should-indent-buffer
229 "begin
230 | a[b]
231 |end
232 |"
233 "begin
234 | a[b]
235 | end
236 |"))
237
238(ert-deftest ruby-indent-array-after-paren-and-space ()
239 (ruby-should-indent-buffer
240 "class A
241 | def foo
242 | foo( [])
243 | end
244 |end
245 |"
246 "class A
247 | def foo
248 |foo( [])
249 |end
250 | end
251 |"))
252
db590ef6
DG
253(ert-deftest ruby-indent-after-block-in-continued-expression ()
254 (ruby-should-indent-buffer
255 "var =
256 | begin
257 | val
258 | end
259 |statement"
260 "var =
261 |begin
262 |val
263 |end
264 |statement"))
265
b1625024
DG
266(ert-deftest ruby-indent-spread-args-in-parens ()
267 (let ((ruby-deep-indent-paren '(?\()))
268 (ruby-should-indent-buffer
269 "foo(1,
270 | 2,
271 | 3)
272 |"
273 "foo(1,
274 | 2,
275 | 3)
276 |")))
277
d1e1e53d 278(ert-deftest ruby-move-to-block-stops-at-indentation ()
bb808526 279 (ruby-with-temp-buffer "def f\nend"
0d9e2599 280 (beginning-of-line)
0d9e2599 281 (ruby-move-to-block -1)
d1e1e53d 282 (should (looking-at "^def"))))
0d9e2599
NN
283
284(ert-deftest ruby-toggle-block-to-do-end ()
bb808526 285 (ruby-with-temp-buffer "foo {|b|\n}"
c3268831 286 (beginning-of-line)
0d9e2599 287 (ruby-toggle-block)
c3268831 288 (should (string= "foo do |b|\nend" (buffer-string)))))
0d9e2599
NN
289
290(ert-deftest ruby-toggle-block-to-brace ()
32fb8162
DG
291 (let ((pairs '((16 . "foo {|b| b + 2 }")
292 (15 . "foo {|b|\n b + 2\n}"))))
293 (dolist (pair pairs)
294 (with-temp-buffer
295 (let ((fill-column (car pair)))
296 (insert "foo do |b|\n b + 2\nend")
297 (ruby-mode)
298 (beginning-of-line)
299 (ruby-toggle-block)
300 (should (string= (cdr pair) (buffer-string))))))))
c3268831
DG
301
302(ert-deftest ruby-toggle-block-to-multiline ()
bb808526 303 (ruby-with-temp-buffer "foo {|b| b + 1}"
c3268831 304 (beginning-of-line)
0d9e2599 305 (ruby-toggle-block)
c3268831 306 (should (string= "foo do |b|\n b + 1\nend" (buffer-string)))))
0d9e2599 307
0ba2d4b6 308(ert-deftest ruby-recognize-symbols-starting-with-at-character ()
c62792e7 309 (ruby-assert-face ":@abc" 3 font-lock-constant-face))
0ba2d4b6
DG
310
311(ert-deftest ruby-hash-character-not-interpolation ()
312 (ruby-assert-face "\"This is #{interpolation}\"" 15
c62792e7 313 font-lock-variable-name-face)
0ba2d4b6 314 (ruby-assert-face "\"This is \\#{no interpolation} despite the #\""
c62792e7
DG
315 15 font-lock-string-face)
316 (ruby-assert-face "\n#@comment, not ruby code" 5 font-lock-comment-face)
616c6c36 317 (ruby-assert-state "\n#@comment, not ruby code" 4 t)
0ba2d4b6 318 (ruby-assert-face "# A comment cannot have #{an interpolation} in it"
c62792e7 319 30 font-lock-comment-face)
616c6c36 320 (ruby-assert-face "# #{comment}\n \"#{interpolation}\"" 16
c62792e7
DG
321 font-lock-variable-name-face))
322
dbb530d9 323(ert-deftest ruby-interpolation-suppresses-quotes-inside ()
c62792e7
DG
324 (let ((s "\"<ul><li>#{@files.join(\"</li><li>\")}</li></ul>\""))
325 (ruby-assert-state s 8 nil)
326 (ruby-assert-face s 9 font-lock-string-face)
327 (ruby-assert-face s 10 font-lock-variable-name-face)
328 (ruby-assert-face s 41 font-lock-string-face)))
329
dbb530d9
DG
330(ert-deftest ruby-interpolation-suppresses-one-double-quote ()
331 (let ((s "\"foo#{'\"'}\""))
332 (ruby-assert-state s 8 nil)
333 (ruby-assert-face s 8 font-lock-variable-name-face)
334 (ruby-assert-face s 11 font-lock-string-face)))
335
336(ert-deftest ruby-interpolation-suppresses-one-backtick ()
337 (let ((s "`as#{'`'}das`"))
338 (ruby-assert-state s 8 nil)))
339
340(ert-deftest ruby-interpolation-keeps-non-quote-syntax ()
341 (let ((s "\"foo#{baz.tee}bar\""))
bb808526
DG
342 (ruby-with-temp-buffer s
343 (goto-char (point-min))
dbb530d9
DG
344 (ruby-mode)
345 (font-lock-fontify-buffer)
346 (search-forward "tee")
347 (should (string= (thing-at-point 'symbol) "tee")))))
348
1a0a0a8a
DG
349(ert-deftest ruby-interpolation-inside-percent-literal ()
350 (let ((s "%( #{boo} )"))
351 (ruby-assert-face s 1 font-lock-string-face)
352 (ruby-assert-face s 4 font-lock-variable-name-face)
353 (ruby-assert-face s 10 font-lock-string-face)
354 (ruby-assert-state s 8 nil)))
355
c62792e7
DG
356(ert-deftest ruby-interpolation-inside-percent-literal-with-paren ()
357 :expected-result :failed
358 (let ((s "%(^#{\")\"}^)"))
359 (ruby-assert-face s 3 font-lock-string-face)
360 (ruby-assert-face s 4 font-lock-variable-name-face)
361 (ruby-assert-face s 10 font-lock-string-face)
362 ;; It's confused by the closing paren in the middle.
363 (ruby-assert-state s 8 nil)))
0ba2d4b6 364
19bb8e62
DG
365(ert-deftest ruby-interpolation-inside-double-quoted-percent-literals ()
366 (ruby-assert-face "%Q{foo #@bar}" 8 font-lock-variable-name-face)
367 (ruby-assert-face "%W{foo #@bar}" 8 font-lock-variable-name-face)
368 (ruby-assert-face "%r{foo #@bar}" 8 font-lock-variable-name-face)
369 (ruby-assert-face "%x{foo #@bar}" 8 font-lock-variable-name-face))
370
371(ert-deftest ruby-no-interpolation-in-single-quoted-literals ()
372 (ruby-assert-face "'foo #@bar'" 7 font-lock-string-face)
373 (ruby-assert-face "%q{foo #@bar}" 8 font-lock-string-face)
374 (ruby-assert-face "%w{foo #@bar}" 8 font-lock-string-face)
375 (ruby-assert-face "%s{foo #@bar}" 8 font-lock-string-face))
376
377(ert-deftest ruby-no-unknown-percent-literals ()
378 ;; No folding of case.
379 (ruby-assert-face "%S{foo}" 4 nil)
380 (ruby-assert-face "%R{foo}" 4 nil))
381
5745cae6
DG
382(ert-deftest ruby-add-log-current-method-examples ()
383 (let ((pairs '(("foo" . "#foo")
384 ("C.foo" . ".foo")
385 ("self.foo" . ".foo"))))
89eb3b0a
CY
386 (dolist (pair pairs)
387 (let ((name (car pair))
bb808526
DG
388 (value (cdr pair)))
389 (ruby-with-temp-buffer (ruby-test-string
390 "module M
391 | class C
392 | def %s
393 | _
394 | end
395 | end
396 |end"
397 name)
398 (search-backward "_")
399 (forward-line)
400 (should (string= (ruby-add-log-current-method)
401 (format "M::C%s" value))))))))
402
403(ert-deftest ruby-add-log-current-method-outside-of-method ()
404 (ruby-with-temp-buffer (ruby-test-string
405 "module M
406 | class C
407 | def foo
408 | end
409 | _
410 | end
411 |end")
412 (search-backward "_")
413 (should (string= (ruby-add-log-current-method)"M::C"))))
414
415(ert-deftest ruby-add-log-current-method-in-singleton-class ()
416 (ruby-with-temp-buffer (ruby-test-string
417 "class C
418 | class << self
419 | def foo
420 | _
421 | end
422 | end
423 |end")
424 (search-backward "_")
425 (should (string= (ruby-add-log-current-method) "C.foo"))))
426
427(ert-deftest ruby-add-log-current-method-namespace-shorthand ()
428 (ruby-with-temp-buffer (ruby-test-string
429 "class C::D
430 | def foo
431 | _
432 | end
433 |end")
434 (search-backward "_")
435 (should (string= (ruby-add-log-current-method) "C::D#foo"))))
436
437(ert-deftest ruby-add-log-current-method-after-inner-class ()
438 (ruby-with-temp-buffer (ruby-test-string
439 "module M
440 | class C
441 | class D
442 | end
71a048c1
DG
443 | def foo
444 | _
445 | end
bb808526
DG
446 | end
447 |end")
448 (search-backward "_")
71a048c1 449 (should (string= (ruby-add-log-current-method) "M::C#foo"))))
5745cae6 450
5e9419e8
DG
451(defvar ruby-block-test-example
452 (ruby-test-string
453 "class C
454 | def foo
455 | 1
456 | end
457 |
458 | def bar
459 | 2
460 | end
461 |
462 | def baz
a324b8c7
DG
463 |some do
464 |3
5e9419e8
DG
465 | end
466 | end
467 |end"))
468
469(defmacro ruby-deftest-move-to-block (name &rest body)
470 `(ert-deftest ,(intern (format "ruby-move-to-block-%s" name)) ()
471 (with-temp-buffer
472 (insert ruby-block-test-example)
473 (ruby-mode)
474 ,@body)))
475
476(put 'ruby-deftest-move-to-block 'lisp-indent-function 'defun)
477
478(ruby-deftest-move-to-block works-on-do
479 (goto-line 11)
480 (ruby-end-of-block)
a324b8c7 481 (should (= 13 (line-number-at-pos)))
5e9419e8
DG
482 (ruby-beginning-of-block)
483 (should (= 11 (line-number-at-pos))))
484
485(ruby-deftest-move-to-block zero-is-noop
486 (goto-line 5)
487 (ruby-move-to-block 0)
488 (should (= 5 (line-number-at-pos))))
489
490(ruby-deftest-move-to-block ok-with-three
491 (goto-line 2)
492 (ruby-move-to-block 3)
a324b8c7 493 (should (= 14 (line-number-at-pos))))
5e9419e8
DG
494
495(ruby-deftest-move-to-block ok-with-minus-two
496 (goto-line 10)
497 (ruby-move-to-block -2)
498 (should (= 2 (line-number-at-pos))))
499
7132e457
DG
500(ert-deftest ruby-move-to-block-skips-percent-literal ()
501 (dolist (s (list (ruby-test-string
502 "foo do
503 | a = %%w(
53ca88c4 504 | def yaa
7132e457
DG
505 | )
506 |end")
507 (ruby-test-string
508 "foo do
509 | a = %%w|
53ca88c4 510 | end
7132e457
DG
511 | |
512 |end")))
513 (ruby-with-temp-buffer s
514 (goto-line 1)
515 (ruby-end-of-block)
53ca88c4 516 (should (= 5 (line-number-at-pos)))
7132e457
DG
517 (ruby-beginning-of-block)
518 (should (= 1 (line-number-at-pos))))))
519
53ca88c4
DG
520(ert-deftest ruby-move-to-block-skips-heredoc ()
521 (ruby-with-temp-buffer
522 (ruby-test-string
523 "if something_wrong?
524 | ActiveSupport::Deprecation.warn(<<-eowarn)
525 | boo hoo
526 | end
527 | eowarn
528 |end")
529 (goto-line 1)
530 (ruby-end-of-block)
531 (should (= 6 (line-number-at-pos)))
532 (ruby-beginning-of-block)
533 (should (= 1 (line-number-at-pos)))))
534
fb549d64
DG
535(ert-deftest ruby-move-to-block-does-not-fold-case ()
536 (ruby-with-temp-buffer
537 (ruby-test-string
538 "foo do
539 | Module.to_s
540 |end")
541 (end-of-buffer)
542 (let ((case-fold-search t))
543 (ruby-beginning-of-block))
544 (should (= 1 (line-number-at-pos)))))
545
546(ert-deftest ruby-beginning-of-defun-does-not-fold-case ()
547 (ruby-with-temp-buffer
548 (ruby-test-string
549 "class C
550 | def bar
551 | Class.to_s
552 | end
553 |end")
554 (goto-line 4)
555 (let ((case-fold-search t))
556 (beginning-of-defun))
557 (should (= 2 (line-number-at-pos)))))
558
559(ert-deftest ruby-end-of-defun-skips-to-next-line-after-the-method ()
560 (ruby-with-temp-buffer
561 (ruby-test-string
562 "class D
563 | def tee
564 | 'ho hum'
565 | end
566 |end")
567 (goto-line 2)
568 (end-of-defun)
569 (should (= 5 (line-number-at-pos)))))
570
c28662a8
DG
571(provide 'ruby-mode-tests)
572
573;;; ruby-mode-tests.el ends here