]> code.delx.au - gnu-emacs/blob - test/automated/ruby-mode-tests.el
* automated/undo-tests.el (undo-test3): Remove test that seems to
[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-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
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
109 (ert-deftest ruby-regexp-doesnt-start-in-string ()
110 (ruby-assert-state "'(/', /\d+/" 3 nil))
111
112 (ert-deftest ruby-regexp-starts-after-string ()
113 (ruby-assert-state "'(/', /\d+/" 3 ?/ 8))
114
115 (ert-deftest ruby-regexp-interpolation-is-highlighted ()
116 (ruby-assert-face "/#{foobs}/" 4 font-lock-variable-name-face))
117
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
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
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 ()
182 (ruby-should-indent-buffer
183 "class<<bar
184 | foo
185 |end
186 |"
187 "class<<bar
188 |foo
189 | end
190 |"))
191
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
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
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
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
278 (ert-deftest ruby-move-to-block-stops-at-indentation ()
279 (ruby-with-temp-buffer "def f\nend"
280 (beginning-of-line)
281 (ruby-move-to-block -1)
282 (should (looking-at "^def"))))
283
284 (ert-deftest ruby-toggle-block-to-do-end ()
285 (ruby-with-temp-buffer "foo {|b|\n}"
286 (beginning-of-line)
287 (ruby-toggle-block)
288 (should (string= "foo do |b|\nend" (buffer-string)))))
289
290 (ert-deftest ruby-toggle-block-to-brace ()
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))))))))
301
302 (ert-deftest ruby-toggle-block-to-multiline ()
303 (ruby-with-temp-buffer "foo {|b| b + 1}"
304 (beginning-of-line)
305 (ruby-toggle-block)
306 (should (string= "foo do |b|\n b + 1\nend" (buffer-string)))))
307
308 (ert-deftest ruby-recognize-symbols-starting-with-at-character ()
309 (ruby-assert-face ":@abc" 3 font-lock-constant-face))
310
311 (ert-deftest ruby-hash-character-not-interpolation ()
312 (ruby-assert-face "\"This is #{interpolation}\"" 15
313 font-lock-variable-name-face)
314 (ruby-assert-face "\"This is \\#{no interpolation} despite the #\""
315 15 font-lock-string-face)
316 (ruby-assert-face "\n#@comment, not ruby code" 5 font-lock-comment-face)
317 (ruby-assert-state "\n#@comment, not ruby code" 4 t)
318 (ruby-assert-face "# A comment cannot have #{an interpolation} in it"
319 30 font-lock-comment-face)
320 (ruby-assert-face "# #{comment}\n \"#{interpolation}\"" 16
321 font-lock-variable-name-face))
322
323 (ert-deftest ruby-interpolation-suppresses-quotes-inside ()
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
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\""))
342 (ruby-with-temp-buffer s
343 (goto-char (point-min))
344 (ruby-mode)
345 (font-lock-fontify-buffer)
346 (search-forward "tee")
347 (should (string= (thing-at-point 'symbol) "tee")))))
348
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
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)))
364
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
382 (ert-deftest ruby-add-log-current-method-examples ()
383 (let ((pairs '(("foo" . "#foo")
384 ("C.foo" . ".foo")
385 ("self.foo" . ".foo"))))
386 (dolist (pair pairs)
387 (let ((name (car pair))
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
443 | def foo
444 | _
445 | end
446 | end
447 |end")
448 (search-backward "_")
449 (should (string= (ruby-add-log-current-method) "M::C#foo"))))
450
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
463 |some do
464 |3
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)
481 (should (= 13 (line-number-at-pos)))
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)
493 (should (= 14 (line-number-at-pos))))
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
500 (ert-deftest ruby-move-to-block-skips-percent-literal ()
501 (dolist (s (list (ruby-test-string
502 "foo do
503 | a = %%w(
504 | def yaa
505 | )
506 |end")
507 (ruby-test-string
508 "foo do
509 | a = %%w|
510 | end
511 | |
512 |end")))
513 (ruby-with-temp-buffer s
514 (goto-line 1)
515 (ruby-end-of-block)
516 (should (= 5 (line-number-at-pos)))
517 (ruby-beginning-of-block)
518 (should (= 1 (line-number-at-pos))))))
519
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
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
571 (provide 'ruby-mode-tests)
572
573 ;;; ruby-mode-tests.el ends here