]> code.delx.au - gnu-emacs/blob - test/automated/ruby-mode-tests.el
Merge from emacs-24; up to 2012-12-29T12:57:49Z!fgallina@gnu.org
[gnu-emacs] / test / automated / ruby-mode-tests.el
1 ;;; ruby-mode-tests.el --- Test suite for ruby-mode
2
3 ;; Copyright (C) 2012-2013 Free Software Foundation, Inc.
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
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))))
31
32 (defun ruby-should-indent-buffer (expected content)
33 "Assert that CONTENT turns into EXPECTED after the buffer is re-indented.
34
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)))))
39
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
47 (defun ruby-test-string (s &rest args)
48 (apply 'format (replace-regexp-in-string "^[ \t]*|" "" s) args))
49
50 (defun ruby-assert-state (content index value &optional point)
51 "Assert syntax state values at the end of CONTENT.
52
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))))
60
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)))))
65
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))
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))
78
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)))
83
84 (ert-deftest ruby-singleton-class-no-heredoc-font-lock ()
85 (ruby-assert-face "class<<a" 8 nil))
86
87 (ert-deftest ruby-heredoc-highlights-interpolations ()
88 (ruby-assert-face "s = <<EOS\n #{foo}\nEOS" 15 font-lock-variable-name-face))
89
90 (ert-deftest ruby-no-heredoc-inside-quotes ()
91 (ruby-assert-state "\"<<\", \"\",\nfoo" 3 nil))
92
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)))
99
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)))
106
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))
111
112 (ert-deftest ruby-regexp-doesnt-start-in-string ()
113 (ruby-assert-state "'(/', /\d+/" 3 nil))
114
115 (ert-deftest ruby-regexp-starts-after-string ()
116 (ruby-assert-state "'(/', /\d+/" 3 ?/ 8))
117
118 (ert-deftest ruby-regexp-interpolation-is-highlighted ()
119 (ruby-assert-face "/#{foobs}/" 4 font-lock-variable-name-face))
120
121 (ert-deftest ruby-regexp-skips-over-interpolation ()
122 (ruby-assert-state "/#{foobs.join('/')}/" 3 nil))
123
124 (ert-deftest ruby-regexp-continues-till-end-when-unclosed ()
125 (ruby-assert-state "/bars" 3 ?/))
126
127 (ert-deftest ruby-regexp-can-be-multiline ()
128 (ruby-assert-state "/bars\ntees # toots \nfoos/" 3 nil))
129
130 (ert-deftest ruby-slash-symbol-is-not-mistaken-for-regexp ()
131 (ruby-assert-state ":/" 3 nil))
132
133 (ert-deftest ruby-slash-char-literal-is-not-mistaken-for-regexp ()
134 (ruby-assert-state "?/" 3 nil))
135
136 (ert-deftest ruby-indent-simple ()
137 (ruby-should-indent-buffer
138 "if foo
139 | bar
140 |end
141 |zot
142 |"
143 "if foo
144 |bar
145 | end
146 | zot
147 |"))
148
149 (ert-deftest ruby-indent-keyword-label ()
150 (ruby-should-indent-buffer
151 "bar(class: XXX) do
152 | foo
153 |end
154 |bar
155 |"
156 "bar(class: XXX) do
157 | foo
158 | end
159 | bar
160 |"))
161
162 (ert-deftest ruby-indent-method-with-question-mark ()
163 (ruby-should-indent-buffer
164 "if x.is_a?(XXX)
165 | foo
166 |end
167 |"
168 "if x.is_a?(XXX)
169 | foo
170 | end
171 |"))
172
173 (ert-deftest ruby-indent-expr-in-regexp ()
174 (ruby-should-indent-buffer
175 "if /#{foo}/ =~ s
176 | x = 1
177 |end
178 |"
179 "if /#{foo}/ =~ s
180 | x = 1
181 | end
182 |"))
183
184 (ert-deftest ruby-indent-singleton-class ()
185 (ruby-should-indent-buffer
186 "class<<bar
187 | foo
188 |end
189 |"
190 "class<<bar
191 |foo
192 | end
193 |"))
194
195 (ert-deftest ruby-indent-inside-heredoc-after-operator ()
196 (ruby-should-indent-buffer
197 "b=<<eos
198 | 42"
199 "b=<<eos
200 | 42"))
201
202 (ert-deftest ruby-indent-inside-heredoc-after-space ()
203 (ruby-should-indent-buffer
204 "foo <<eos.gsub(' ', '*')
205 | 42"
206 "foo <<eos.gsub(' ', '*')
207 | 42"))
208
209 (ert-deftest ruby-indent-array-literal ()
210 (let ((ruby-deep-indent-paren nil))
211 (ruby-should-indent-buffer
212 "foo = [
213 | bar
214 |]
215 |"
216 "foo = [
217 | bar
218 | ]
219 |"))
220 (ruby-should-indent-buffer
221 "foo do
222 | [bar]
223 |end
224 |"
225 "foo do
226 |[bar]
227 | end
228 |"))
229
230 (ert-deftest ruby-indent-begin-end ()
231 (ruby-should-indent-buffer
232 "begin
233 | a[b]
234 |end
235 |"
236 "begin
237 | a[b]
238 | end
239 |"))
240
241 (ert-deftest ruby-indent-array-after-paren-and-space ()
242 (ruby-should-indent-buffer
243 "class A
244 | def foo
245 | foo( [])
246 | end
247 |end
248 |"
249 "class A
250 | def foo
251 |foo( [])
252 |end
253 | end
254 |"))
255
256 (ert-deftest ruby-indent-after-block-in-continued-expression ()
257 (ruby-should-indent-buffer
258 "var =
259 | begin
260 | val
261 | end
262 |statement"
263 "var =
264 |begin
265 |val
266 |end
267 |statement"))
268
269 (ert-deftest ruby-indent-spread-args-in-parens ()
270 (let ((ruby-deep-indent-paren '(?\()))
271 (ruby-should-indent-buffer
272 "foo(1,
273 | 2,
274 | 3)
275 |"
276 "foo(1,
277 | 2,
278 | 3)
279 |")))
280
281 (ert-deftest ruby-move-to-block-stops-at-indentation ()
282 (ruby-with-temp-buffer "def f\nend"
283 (beginning-of-line)
284 (ruby-move-to-block -1)
285 (should (looking-at "^def"))))
286
287 (ert-deftest ruby-toggle-block-to-do-end ()
288 (ruby-with-temp-buffer "foo {|b|\n}"
289 (beginning-of-line)
290 (ruby-toggle-block)
291 (should (string= "foo do |b|\nend" (buffer-string)))))
292
293 (ert-deftest ruby-toggle-block-to-brace ()
294 (let ((pairs '((16 . "foo {|b| b + 2 }")
295 (15 . "foo {|b|\n b + 2\n}"))))
296 (dolist (pair pairs)
297 (with-temp-buffer
298 (let ((fill-column (car pair)))
299 (insert "foo do |b|\n b + 2\nend")
300 (ruby-mode)
301 (beginning-of-line)
302 (ruby-toggle-block)
303 (should (string= (cdr pair) (buffer-string))))))))
304
305 (ert-deftest ruby-toggle-block-to-multiline ()
306 (ruby-with-temp-buffer "foo {|b| b + 1}"
307 (beginning-of-line)
308 (ruby-toggle-block)
309 (should (string= "foo do |b|\n b + 1\nend" (buffer-string)))))
310
311 (ert-deftest ruby-recognize-symbols-starting-with-at-character ()
312 (ruby-assert-face ":@abc" 3 font-lock-constant-face))
313
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))
325
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)))
332
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)))
338
339 (ert-deftest ruby-interpolation-suppresses-one-backtick ()
340 (let ((s "`as#{'`'}das`"))
341 (ruby-assert-state s 8 nil)))
342
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))
347 (ruby-mode)
348 (font-lock-fontify-buffer)
349 (search-forward "tee")
350 (should (string= (thing-at-point 'symbol) "tee")))))
351
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)))
358
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)))
367
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))
373
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))
379
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))
384
385 (ert-deftest ruby-add-log-current-method-examples ()
386 (let ((pairs '(("foo" . "#foo")
387 ("C.foo" . ".foo")
388 ("self.foo" . ".foo"))))
389 (dolist (pair pairs)
390 (let ((name (car pair))
391 (value (cdr pair)))
392 (ruby-with-temp-buffer (ruby-test-string
393 "module M
394 | class C
395 | def %s
396 | _
397 | end
398 | end
399 |end"
400 name)
401 (search-backward "_")
402 (forward-line)
403 (should (string= (ruby-add-log-current-method)
404 (format "M::C%s" value))))))))
405
406 (ert-deftest ruby-add-log-current-method-outside-of-method ()
407 (ruby-with-temp-buffer (ruby-test-string
408 "module M
409 | class C
410 | def foo
411 | end
412 | _
413 | end
414 |end")
415 (search-backward "_")
416 (should (string= (ruby-add-log-current-method)"M::C"))))
417
418 (ert-deftest ruby-add-log-current-method-in-singleton-class ()
419 (ruby-with-temp-buffer (ruby-test-string
420 "class C
421 | class << self
422 | def foo
423 | _
424 | end
425 | end
426 |end")
427 (search-backward "_")
428 (should (string= (ruby-add-log-current-method) "C.foo"))))
429
430 (ert-deftest ruby-add-log-current-method-namespace-shorthand ()
431 (ruby-with-temp-buffer (ruby-test-string
432 "class C::D
433 | def foo
434 | _
435 | end
436 |end")
437 (search-backward "_")
438 (should (string= (ruby-add-log-current-method) "C::D#foo"))))
439
440 (ert-deftest ruby-add-log-current-method-after-inner-class ()
441 (ruby-with-temp-buffer (ruby-test-string
442 "module M
443 | class C
444 | class D
445 | end
446 | def foo
447 | _
448 | end
449 | end
450 |end")
451 (search-backward "_")
452 (should (string= (ruby-add-log-current-method) "M::C#foo"))))
453
454 (defvar ruby-block-test-example
455 (ruby-test-string
456 "class C
457 | def foo
458 | 1
459 | end
460 |
461 | def bar
462 | 2
463 | end
464 |
465 | def baz
466 |some do
467 |3
468 | end
469 | end
470 |end"))
471
472 (defmacro ruby-deftest-move-to-block (name &rest body)
473 `(ert-deftest ,(intern (format "ruby-move-to-block-%s" name)) ()
474 (with-temp-buffer
475 (insert ruby-block-test-example)
476 (ruby-mode)
477 ,@body)))
478
479 (put 'ruby-deftest-move-to-block 'lisp-indent-function 'defun)
480
481 (ruby-deftest-move-to-block works-on-do
482 (goto-line 11)
483 (ruby-end-of-block)
484 (should (= 13 (line-number-at-pos)))
485 (ruby-beginning-of-block)
486 (should (= 11 (line-number-at-pos))))
487
488 (ruby-deftest-move-to-block zero-is-noop
489 (goto-line 5)
490 (ruby-move-to-block 0)
491 (should (= 5 (line-number-at-pos))))
492
493 (ruby-deftest-move-to-block ok-with-three
494 (goto-line 2)
495 (ruby-move-to-block 3)
496 (should (= 14 (line-number-at-pos))))
497
498 (ruby-deftest-move-to-block ok-with-minus-two
499 (goto-line 10)
500 (ruby-move-to-block -2)
501 (should (= 2 (line-number-at-pos))))
502
503 (ert-deftest ruby-move-to-block-skips-percent-literal ()
504 (dolist (s (list (ruby-test-string
505 "foo do
506 | a = %%w(
507 | def yaa
508 | )
509 |end")
510 (ruby-test-string
511 "foo do
512 | a = %%w|
513 | end
514 | |
515 |end")))
516 (ruby-with-temp-buffer s
517 (goto-line 1)
518 (ruby-end-of-block)
519 (should (= 5 (line-number-at-pos)))
520 (ruby-beginning-of-block)
521 (should (= 1 (line-number-at-pos))))))
522
523 (ert-deftest ruby-move-to-block-skips-heredoc ()
524 (ruby-with-temp-buffer
525 (ruby-test-string
526 "if something_wrong?
527 | ActiveSupport::Deprecation.warn(<<-eowarn)
528 | boo hoo
529 | end
530 | eowarn
531 |end")
532 (goto-line 1)
533 (ruby-end-of-block)
534 (should (= 6 (line-number-at-pos)))
535 (ruby-beginning-of-block)
536 (should (= 1 (line-number-at-pos)))))
537
538 (ert-deftest ruby-move-to-block-does-not-fold-case ()
539 (ruby-with-temp-buffer
540 (ruby-test-string
541 "foo do
542 | Module.to_s
543 |end")
544 (end-of-buffer)
545 (let ((case-fold-search t))
546 (ruby-beginning-of-block))
547 (should (= 1 (line-number-at-pos)))))
548
549 (ert-deftest ruby-move-to-block-moves-from-else-to-if ()
550 (ruby-with-temp-buffer (ruby-test-string
551 "if true
552 | nested_block do
553 | end
554 |else
555 |end")
556 (goto-line 4)
557 (ruby-beginning-of-block)
558 (should (= 1 (line-number-at-pos)))))
559
560 (ert-deftest ruby-beginning-of-defun-does-not-fold-case ()
561 (ruby-with-temp-buffer
562 (ruby-test-string
563 "class C
564 | def bar
565 | Class.to_s
566 | end
567 |end")
568 (goto-line 4)
569 (let ((case-fold-search t))
570 (beginning-of-defun))
571 (should (= 2 (line-number-at-pos)))))
572
573 (ert-deftest ruby-end-of-defun-skips-to-next-line-after-the-method ()
574 (ruby-with-temp-buffer
575 (ruby-test-string
576 "class D
577 | def tee
578 | 'ho hum'
579 | end
580 |end")
581 (goto-line 2)
582 (end-of-defun)
583 (should (= 5 (line-number-at-pos)))))
584
585 (provide 'ruby-mode-tests)
586
587 ;;; ruby-mode-tests.el ends here