Commit | Line | Data |
---|---|---|
c28662a8 DG |
1 | ;;; ruby-mode-tests.el --- Test suite for ruby-mode |
2 | ||
ba318903 | 3 | ;; Copyright (C) 2012-2014 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 | ||
771b2fc3 | 24 | (require 'ert) |
c28662a8 DG |
25 | (require 'ruby-mode) |
26 | ||
6d8ca172 SM |
27 | (defmacro ruby-with-temp-buffer (contents &rest body) |
28 | (declare (indent 1) (debug t)) | |
29 | `(with-temp-buffer | |
30 | (insert ,contents) | |
31 | (ruby-mode) | |
32 | ,@body)) | |
33 | ||
9cd80478 | 34 | (defun ruby-should-indent (content column) |
9d2ed8a2 | 35 | "Assert indentation COLUMN on the last line of CONTENT." |
bb808526 | 36 | (ruby-with-temp-buffer content |
6d8ca172 | 37 | (indent-according-to-mode) |
9d2ed8a2 DG |
38 | (should (= (current-indentation) column)))) |
39 | ||
40 | (defun ruby-should-indent-buffer (expected content) | |
41 | "Assert that CONTENT turns into EXPECTED after the buffer is re-indented. | |
42 | ||
43 | The whitespace before and including \"|\" on each line is removed." | |
bb808526 | 44 | (ruby-with-temp-buffer (ruby-test-string content) |
5745cae6 DG |
45 | (indent-region (point-min) (point-max)) |
46 | (should (string= (ruby-test-string expected) (buffer-string))))) | |
47 | ||
48 | (defun ruby-test-string (s &rest args) | |
49 | (apply 'format (replace-regexp-in-string "^[ \t]*|" "" s) args)) | |
9cd80478 | 50 | |
ab89e9f9 | 51 | (defun ruby-assert-state (content index value &optional point) |
9cd80478 DG |
52 | "Assert syntax state values at the end of CONTENT. |
53 | ||
54 | VALUES-PLIST is a list with alternating index and value elements." | |
bb808526 | 55 | (ruby-with-temp-buffer content |
ab89e9f9 | 56 | (when point (goto-char point)) |
9cd80478 | 57 | (syntax-propertize (point)) |
ab89e9f9 DG |
58 | (should (eq (nth index |
59 | (parse-partial-sexp (point-min) (point))) | |
60 | value)))) | |
9cd80478 | 61 | |
0ba2d4b6 | 62 | (defun ruby-assert-face (content pos face) |
bb808526 | 63 | (ruby-with-temp-buffer content |
0ba2d4b6 DG |
64 | (font-lock-fontify-buffer) |
65 | (should (eq face (get-text-property pos 'face))))) | |
66 | ||
9cd80478 | 67 | (ert-deftest ruby-indent-after-symbol-made-from-string-interpolation () |
c28662a8 | 68 | "It can indent the line after symbol made using string interpolation." |
9cd80478 DG |
69 | (ruby-should-indent "def foo(suffix)\n :\"bar#{suffix}\"\n" |
70 | ruby-indent-level)) | |
71 | ||
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)) | |
75 | ||
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)) | |
c28662a8 | 79 | |
f178c32d DG |
80 | (ert-deftest ruby-heredoc-font-lock () |
81 | (let ((s "foo <<eos.gsub('^ *', '')")) | |
c62792e7 | 82 | (ruby-assert-face s 9 font-lock-string-face) |
f178c32d DG |
83 | (ruby-assert-face s 10 nil))) |
84 | ||
85 | (ert-deftest ruby-singleton-class-no-heredoc-font-lock () | |
86 | (ruby-assert-face "class<<a" 8 nil)) | |
87 | ||
1a0a0a8a DG |
88 | (ert-deftest ruby-heredoc-highlights-interpolations () |
89 | (ruby-assert-face "s = <<EOS\n #{foo}\nEOS" 15 font-lock-variable-name-face)) | |
90 | ||
6dbafa30 DG |
91 | (ert-deftest ruby-no-heredoc-inside-quotes () |
92 | (ruby-assert-state "\"<<\", \"\",\nfoo" 3 nil)) | |
93 | ||
af67e79a | 94 | (ert-deftest ruby-exit!-font-lock () |
2d161243 BB |
95 | (ruby-assert-face "exit!" 5 font-lock-builtin-face)) |
96 | ||
9d2ed8a2 DG |
97 | (ert-deftest ruby-deep-indent () |
98 | (let ((ruby-deep-arglist nil) | |
99 | (ruby-deep-indent-paren '(?\( ?\{ ?\[ ?\] t))) | |
100 | (ruby-should-indent "foo = [1,\n2" 7) | |
101 | (ruby-should-indent "foo = {a: b,\nc: d" 7) | |
102 | (ruby-should-indent "foo(a,\nb" 4))) | |
103 | ||
104 | (ert-deftest ruby-deep-indent-disabled () | |
105 | (let ((ruby-deep-arglist nil) | |
106 | (ruby-deep-indent-paren nil)) | |
107 | (ruby-should-indent "foo = [\n1" ruby-indent-level) | |
108 | (ruby-should-indent "foo = {\na: b" ruby-indent-level) | |
109 | (ruby-should-indent "foo(\na" ruby-indent-level))) | |
110 | ||
0ba2d4b6 DG |
111 | (ert-deftest ruby-indent-after-keyword-in-a-string () |
112 | (ruby-should-indent "a = \"abc\nif\"\n " 0) | |
113 | (ruby-should-indent "a = %w[abc\n def]\n " 0) | |
114 | (ruby-should-indent "a = \"abc\n def\"\n " 0)) | |
115 | ||
307d0e95 | 116 | (ert-deftest ruby-regexp-doesnt-start-in-string () |
ab89e9f9 DG |
117 | (ruby-assert-state "'(/', /\d+/" 3 nil)) |
118 | ||
119 | (ert-deftest ruby-regexp-starts-after-string () | |
120 | (ruby-assert-state "'(/', /\d+/" 3 ?/ 8)) | |
121 | ||
973d1e12 DG |
122 | (ert-deftest ruby-regexp-interpolation-is-highlighted () |
123 | (ruby-assert-face "/#{foobs}/" 4 font-lock-variable-name-face)) | |
124 | ||
1a0a0a8a DG |
125 | (ert-deftest ruby-regexp-skips-over-interpolation () |
126 | (ruby-assert-state "/#{foobs.join('/')}/" 3 nil)) | |
127 | ||
128 | (ert-deftest ruby-regexp-continues-till-end-when-unclosed () | |
129 | (ruby-assert-state "/bars" 3 ?/)) | |
130 | ||
131 | (ert-deftest ruby-regexp-can-be-multiline () | |
132 | (ruby-assert-state "/bars\ntees # toots \nfoos/" 3 nil)) | |
133 | ||
d1bbba4f DG |
134 | (ert-deftest ruby-slash-symbol-is-not-mistaken-for-regexp () |
135 | (ruby-assert-state ":/" 3 nil)) | |
136 | ||
137 | (ert-deftest ruby-slash-char-literal-is-not-mistaken-for-regexp () | |
138 | (ruby-assert-state "?/" 3 nil)) | |
139 | ||
9d2ed8a2 DG |
140 | (ert-deftest ruby-indent-simple () |
141 | (ruby-should-indent-buffer | |
142 | "if foo | |
143 | | bar | |
144 | |end | |
145 | |zot | |
146 | |" | |
147 | "if foo | |
148 | |bar | |
149 | | end | |
150 | | zot | |
151 | |")) | |
152 | ||
153 | (ert-deftest ruby-indent-keyword-label () | |
154 | (ruby-should-indent-buffer | |
155 | "bar(class: XXX) do | |
156 | | foo | |
157 | |end | |
158 | |bar | |
159 | |" | |
160 | "bar(class: XXX) do | |
161 | | foo | |
162 | | end | |
163 | | bar | |
164 | |")) | |
165 | ||
166 | (ert-deftest ruby-indent-method-with-question-mark () | |
167 | (ruby-should-indent-buffer | |
168 | "if x.is_a?(XXX) | |
169 | | foo | |
170 | |end | |
171 | |" | |
172 | "if x.is_a?(XXX) | |
173 | | foo | |
174 | | end | |
175 | |")) | |
176 | ||
177 | (ert-deftest ruby-indent-expr-in-regexp () | |
178 | (ruby-should-indent-buffer | |
179 | "if /#{foo}/ =~ s | |
180 | | x = 1 | |
181 | |end | |
182 | |" | |
183 | "if /#{foo}/ =~ s | |
184 | | x = 1 | |
185 | | end | |
186 | |")) | |
187 | ||
188 | (ert-deftest ruby-indent-singleton-class () | |
9d2ed8a2 DG |
189 | (ruby-should-indent-buffer |
190 | "class<<bar | |
191 | | foo | |
192 | |end | |
193 | |" | |
194 | "class<<bar | |
195 | |foo | |
196 | | end | |
197 | |")) | |
198 | ||
8619323f DG |
199 | (ert-deftest ruby-indent-inside-heredoc-after-operator () |
200 | (ruby-should-indent-buffer | |
201 | "b=<<eos | |
202 | | 42" | |
203 | "b=<<eos | |
204 | | 42")) | |
205 | ||
206 | (ert-deftest ruby-indent-inside-heredoc-after-space () | |
207 | (ruby-should-indent-buffer | |
208 | "foo <<eos.gsub(' ', '*') | |
209 | | 42" | |
210 | "foo <<eos.gsub(' ', '*') | |
211 | | 42")) | |
212 | ||
9d2ed8a2 DG |
213 | (ert-deftest ruby-indent-array-literal () |
214 | (let ((ruby-deep-indent-paren nil)) | |
215 | (ruby-should-indent-buffer | |
216 | "foo = [ | |
217 | | bar | |
218 | |] | |
219 | |" | |
220 | "foo = [ | |
221 | | bar | |
222 | | ] | |
223 | |")) | |
224 | (ruby-should-indent-buffer | |
225 | "foo do | |
226 | | [bar] | |
227 | |end | |
228 | |" | |
229 | "foo do | |
230 | |[bar] | |
231 | | end | |
232 | |")) | |
233 | ||
234 | (ert-deftest ruby-indent-begin-end () | |
235 | (ruby-should-indent-buffer | |
236 | "begin | |
237 | | a[b] | |
238 | |end | |
239 | |" | |
240 | "begin | |
241 | | a[b] | |
242 | | end | |
243 | |")) | |
244 | ||
245 | (ert-deftest ruby-indent-array-after-paren-and-space () | |
246 | (ruby-should-indent-buffer | |
247 | "class A | |
248 | | def foo | |
249 | | foo( []) | |
250 | | end | |
251 | |end | |
252 | |" | |
253 | "class A | |
254 | | def foo | |
255 | |foo( []) | |
256 | |end | |
257 | | end | |
258 | |")) | |
259 | ||
db590ef6 DG |
260 | (ert-deftest ruby-indent-after-block-in-continued-expression () |
261 | (ruby-should-indent-buffer | |
262 | "var = | |
263 | | begin | |
264 | | val | |
265 | | end | |
266 | |statement" | |
267 | "var = | |
268 | |begin | |
269 | |val | |
270 | |end | |
271 | |statement")) | |
272 | ||
b1625024 DG |
273 | (ert-deftest ruby-indent-spread-args-in-parens () |
274 | (let ((ruby-deep-indent-paren '(?\())) | |
275 | (ruby-should-indent-buffer | |
276 | "foo(1, | |
277 | | 2, | |
278 | | 3) | |
279 | |" | |
280 | "foo(1, | |
281 | | 2, | |
282 | | 3) | |
283 | |"))) | |
284 | ||
b520f210 DG |
285 | (ert-deftest ruby-align-to-stmt-keywords-t () |
286 | (let ((ruby-align-to-stmt-keywords t)) | |
287 | (ruby-should-indent-buffer | |
288 | "foo = if bar? | |
289 | | 1 | |
290 | |else | |
291 | | 2 | |
292 | |end | |
293 | | | |
294 | |foo || begin | |
295 | | bar | |
296 | |end | |
297 | | | |
298 | |foo || | |
299 | | begin | |
300 | | bar | |
301 | | end | |
302 | |" | |
303 | "foo = if bar? | |
304 | | 1 | |
305 | |else | |
306 | | 2 | |
307 | | end | |
308 | | | |
309 | | foo || begin | |
310 | | bar | |
311 | |end | |
312 | | | |
313 | | foo || | |
314 | | begin | |
315 | |bar | |
316 | | end | |
317 | |") | |
318 | )) | |
319 | ||
320 | (ert-deftest ruby-align-to-stmt-keywords-case () | |
321 | (let ((ruby-align-to-stmt-keywords '(case))) | |
322 | (ruby-should-indent-buffer | |
323 | "b = case a | |
324 | |when 13 | |
325 | | 6 | |
326 | |else | |
327 | | 42 | |
328 | |end" | |
329 | "b = case a | |
330 | | when 13 | |
331 | | 6 | |
332 | | else | |
333 | | 42 | |
334 | | end"))) | |
335 | ||
e2a67bd0 DG |
336 | (ert-deftest ruby-align-chained-calls () |
337 | (let ((ruby-align-chained-calls t)) | |
338 | (ruby-should-indent-buffer | |
339 | "one.two.three | |
340 | | .four | |
341 | | | |
342 | |my_array.select { |str| str.size > 5 } | |
343 | | .map { |str| str.downcase }" | |
344 | "one.two.three | |
345 | | .four | |
346 | | | |
347 | |my_array.select { |str| str.size > 5 } | |
348 | | .map { |str| str.downcase }"))) | |
349 | ||
d1e1e53d | 350 | (ert-deftest ruby-move-to-block-stops-at-indentation () |
bb808526 | 351 | (ruby-with-temp-buffer "def f\nend" |
0d9e2599 | 352 | (beginning-of-line) |
0d9e2599 | 353 | (ruby-move-to-block -1) |
d1e1e53d | 354 | (should (looking-at "^def")))) |
0d9e2599 NN |
355 | |
356 | (ert-deftest ruby-toggle-block-to-do-end () | |
bb808526 | 357 | (ruby-with-temp-buffer "foo {|b|\n}" |
c3268831 | 358 | (beginning-of-line) |
0d9e2599 | 359 | (ruby-toggle-block) |
c3268831 | 360 | (should (string= "foo do |b|\nend" (buffer-string))))) |
0d9e2599 NN |
361 | |
362 | (ert-deftest ruby-toggle-block-to-brace () | |
bae91342 DG |
363 | (let ((pairs '((17 . "foo { |b| b + 2 }") |
364 | (16 . "foo { |b|\n b + 2\n}")))) | |
32fb8162 DG |
365 | (dolist (pair pairs) |
366 | (with-temp-buffer | |
367 | (let ((fill-column (car pair))) | |
368 | (insert "foo do |b|\n b + 2\nend") | |
369 | (ruby-mode) | |
370 | (beginning-of-line) | |
371 | (ruby-toggle-block) | |
372 | (should (string= (cdr pair) (buffer-string)))))))) | |
c3268831 DG |
373 | |
374 | (ert-deftest ruby-toggle-block-to-multiline () | |
bb808526 | 375 | (ruby-with-temp-buffer "foo {|b| b + 1}" |
c3268831 | 376 | (beginning-of-line) |
0d9e2599 | 377 | (ruby-toggle-block) |
c3268831 | 378 | (should (string= "foo do |b|\n b + 1\nend" (buffer-string))))) |
0d9e2599 | 379 | |
56cd894e AS |
380 | (ert-deftest ruby-toggle-block-with-interpolation () |
381 | (ruby-with-temp-buffer "foo do\n \"#{bar}\"\nend" | |
382 | (beginning-of-line) | |
383 | (ruby-toggle-block) | |
384 | (should (string= "foo { \"#{bar}\" }" (buffer-string))))) | |
385 | ||
0ba2d4b6 | 386 | (ert-deftest ruby-recognize-symbols-starting-with-at-character () |
c62792e7 | 387 | (ruby-assert-face ":@abc" 3 font-lock-constant-face)) |
0ba2d4b6 DG |
388 | |
389 | (ert-deftest ruby-hash-character-not-interpolation () | |
390 | (ruby-assert-face "\"This is #{interpolation}\"" 15 | |
c62792e7 | 391 | font-lock-variable-name-face) |
0ba2d4b6 | 392 | (ruby-assert-face "\"This is \\#{no interpolation} despite the #\"" |
c62792e7 DG |
393 | 15 font-lock-string-face) |
394 | (ruby-assert-face "\n#@comment, not ruby code" 5 font-lock-comment-face) | |
616c6c36 | 395 | (ruby-assert-state "\n#@comment, not ruby code" 4 t) |
0ba2d4b6 | 396 | (ruby-assert-face "# A comment cannot have #{an interpolation} in it" |
c62792e7 | 397 | 30 font-lock-comment-face) |
616c6c36 | 398 | (ruby-assert-face "# #{comment}\n \"#{interpolation}\"" 16 |
c62792e7 DG |
399 | font-lock-variable-name-face)) |
400 | ||
dbb530d9 | 401 | (ert-deftest ruby-interpolation-suppresses-quotes-inside () |
c62792e7 DG |
402 | (let ((s "\"<ul><li>#{@files.join(\"</li><li>\")}</li></ul>\"")) |
403 | (ruby-assert-state s 8 nil) | |
404 | (ruby-assert-face s 9 font-lock-string-face) | |
405 | (ruby-assert-face s 10 font-lock-variable-name-face) | |
406 | (ruby-assert-face s 41 font-lock-string-face))) | |
407 | ||
dbb530d9 DG |
408 | (ert-deftest ruby-interpolation-suppresses-one-double-quote () |
409 | (let ((s "\"foo#{'\"'}\"")) | |
410 | (ruby-assert-state s 8 nil) | |
411 | (ruby-assert-face s 8 font-lock-variable-name-face) | |
412 | (ruby-assert-face s 11 font-lock-string-face))) | |
413 | ||
414 | (ert-deftest ruby-interpolation-suppresses-one-backtick () | |
415 | (let ((s "`as#{'`'}das`")) | |
416 | (ruby-assert-state s 8 nil))) | |
417 | ||
418 | (ert-deftest ruby-interpolation-keeps-non-quote-syntax () | |
419 | (let ((s "\"foo#{baz.tee}bar\"")) | |
bb808526 DG |
420 | (ruby-with-temp-buffer s |
421 | (goto-char (point-min)) | |
dbb530d9 DG |
422 | (ruby-mode) |
423 | (font-lock-fontify-buffer) | |
424 | (search-forward "tee") | |
425 | (should (string= (thing-at-point 'symbol) "tee"))))) | |
426 | ||
1a0a0a8a DG |
427 | (ert-deftest ruby-interpolation-inside-percent-literal () |
428 | (let ((s "%( #{boo} )")) | |
429 | (ruby-assert-face s 1 font-lock-string-face) | |
430 | (ruby-assert-face s 4 font-lock-variable-name-face) | |
431 | (ruby-assert-face s 10 font-lock-string-face) | |
432 | (ruby-assert-state s 8 nil))) | |
433 | ||
c62792e7 DG |
434 | (ert-deftest ruby-interpolation-inside-percent-literal-with-paren () |
435 | :expected-result :failed | |
436 | (let ((s "%(^#{\")\"}^)")) | |
437 | (ruby-assert-face s 3 font-lock-string-face) | |
438 | (ruby-assert-face s 4 font-lock-variable-name-face) | |
439 | (ruby-assert-face s 10 font-lock-string-face) | |
440 | ;; It's confused by the closing paren in the middle. | |
441 | (ruby-assert-state s 8 nil))) | |
0ba2d4b6 | 442 | |
19bb8e62 DG |
443 | (ert-deftest ruby-interpolation-inside-double-quoted-percent-literals () |
444 | (ruby-assert-face "%Q{foo #@bar}" 8 font-lock-variable-name-face) | |
445 | (ruby-assert-face "%W{foo #@bar}" 8 font-lock-variable-name-face) | |
446 | (ruby-assert-face "%r{foo #@bar}" 8 font-lock-variable-name-face) | |
447 | (ruby-assert-face "%x{foo #@bar}" 8 font-lock-variable-name-face)) | |
448 | ||
449 | (ert-deftest ruby-no-interpolation-in-single-quoted-literals () | |
450 | (ruby-assert-face "'foo #@bar'" 7 font-lock-string-face) | |
451 | (ruby-assert-face "%q{foo #@bar}" 8 font-lock-string-face) | |
452 | (ruby-assert-face "%w{foo #@bar}" 8 font-lock-string-face) | |
453 | (ruby-assert-face "%s{foo #@bar}" 8 font-lock-string-face)) | |
454 | ||
4dd3c4d5 DG |
455 | (ert-deftest ruby-interpolation-after-dollar-sign () |
456 | (ruby-assert-face "\"$#{balance}\"" 2 'font-lock-string-face) | |
457 | (ruby-assert-face "\"$#{balance}\"" 3 'font-lock-variable-name-face)) | |
458 | ||
19bb8e62 DG |
459 | (ert-deftest ruby-no-unknown-percent-literals () |
460 | ;; No folding of case. | |
461 | (ruby-assert-face "%S{foo}" 4 nil) | |
462 | (ruby-assert-face "%R{foo}" 4 nil)) | |
463 | ||
5745cae6 DG |
464 | (ert-deftest ruby-add-log-current-method-examples () |
465 | (let ((pairs '(("foo" . "#foo") | |
466 | ("C.foo" . ".foo") | |
467 | ("self.foo" . ".foo")))) | |
89eb3b0a CY |
468 | (dolist (pair pairs) |
469 | (let ((name (car pair)) | |
bb808526 DG |
470 | (value (cdr pair))) |
471 | (ruby-with-temp-buffer (ruby-test-string | |
472 | "module M | |
473 | | class C | |
474 | | def %s | |
475 | | _ | |
476 | | end | |
477 | | end | |
478 | |end" | |
479 | name) | |
480 | (search-backward "_") | |
481 | (forward-line) | |
482 | (should (string= (ruby-add-log-current-method) | |
483 | (format "M::C%s" value)))))))) | |
484 | ||
485 | (ert-deftest ruby-add-log-current-method-outside-of-method () | |
486 | (ruby-with-temp-buffer (ruby-test-string | |
487 | "module M | |
488 | | class C | |
489 | | def foo | |
490 | | end | |
491 | | _ | |
492 | | end | |
493 | |end") | |
494 | (search-backward "_") | |
495 | (should (string= (ruby-add-log-current-method)"M::C")))) | |
496 | ||
497 | (ert-deftest ruby-add-log-current-method-in-singleton-class () | |
498 | (ruby-with-temp-buffer (ruby-test-string | |
499 | "class C | |
500 | | class << self | |
501 | | def foo | |
502 | | _ | |
503 | | end | |
504 | | end | |
505 | |end") | |
506 | (search-backward "_") | |
507 | (should (string= (ruby-add-log-current-method) "C.foo")))) | |
508 | ||
509 | (ert-deftest ruby-add-log-current-method-namespace-shorthand () | |
510 | (ruby-with-temp-buffer (ruby-test-string | |
511 | "class C::D | |
512 | | def foo | |
513 | | _ | |
514 | | end | |
515 | |end") | |
516 | (search-backward "_") | |
517 | (should (string= (ruby-add-log-current-method) "C::D#foo")))) | |
518 | ||
519 | (ert-deftest ruby-add-log-current-method-after-inner-class () | |
520 | (ruby-with-temp-buffer (ruby-test-string | |
521 | "module M | |
522 | | class C | |
523 | | class D | |
524 | | end | |
71a048c1 DG |
525 | | def foo |
526 | | _ | |
527 | | end | |
bb808526 DG |
528 | | end |
529 | |end") | |
530 | (search-backward "_") | |
71a048c1 | 531 | (should (string= (ruby-add-log-current-method) "M::C#foo")))) |
5745cae6 | 532 | |
5e9419e8 DG |
533 | (defvar ruby-block-test-example |
534 | (ruby-test-string | |
535 | "class C | |
536 | | def foo | |
537 | | 1 | |
538 | | end | |
539 | | | |
540 | | def bar | |
541 | | 2 | |
542 | | end | |
543 | | | |
544 | | def baz | |
a324b8c7 DG |
545 | |some do |
546 | |3 | |
5e9419e8 DG |
547 | | end |
548 | | end | |
549 | |end")) | |
550 | ||
551 | (defmacro ruby-deftest-move-to-block (name &rest body) | |
6d8ca172 | 552 | (declare (indent defun)) |
5e9419e8 DG |
553 | `(ert-deftest ,(intern (format "ruby-move-to-block-%s" name)) () |
554 | (with-temp-buffer | |
555 | (insert ruby-block-test-example) | |
556 | (ruby-mode) | |
d2e1542a | 557 | (goto-char (point-min)) |
5e9419e8 DG |
558 | ,@body))) |
559 | ||
5e9419e8 | 560 | (ruby-deftest-move-to-block works-on-do |
d2e1542a | 561 | (forward-line 10) |
5e9419e8 | 562 | (ruby-end-of-block) |
a324b8c7 | 563 | (should (= 13 (line-number-at-pos))) |
5e9419e8 DG |
564 | (ruby-beginning-of-block) |
565 | (should (= 11 (line-number-at-pos)))) | |
566 | ||
567 | (ruby-deftest-move-to-block zero-is-noop | |
d2e1542a | 568 | (forward-line 4) |
5e9419e8 DG |
569 | (ruby-move-to-block 0) |
570 | (should (= 5 (line-number-at-pos)))) | |
571 | ||
572 | (ruby-deftest-move-to-block ok-with-three | |
d2e1542a | 573 | (forward-line 1) |
5e9419e8 | 574 | (ruby-move-to-block 3) |
a324b8c7 | 575 | (should (= 14 (line-number-at-pos)))) |
5e9419e8 DG |
576 | |
577 | (ruby-deftest-move-to-block ok-with-minus-two | |
d2e1542a | 578 | (forward-line 9) |
5e9419e8 DG |
579 | (ruby-move-to-block -2) |
580 | (should (= 2 (line-number-at-pos)))) | |
581 | ||
7132e457 DG |
582 | (ert-deftest ruby-move-to-block-skips-percent-literal () |
583 | (dolist (s (list (ruby-test-string | |
584 | "foo do | |
585 | | a = %%w( | |
53ca88c4 | 586 | | def yaa |
7132e457 DG |
587 | | ) |
588 | |end") | |
589 | (ruby-test-string | |
590 | "foo do | |
591 | | a = %%w| | |
53ca88c4 | 592 | | end |
7132e457 DG |
593 | | | |
594 | |end"))) | |
595 | (ruby-with-temp-buffer s | |
d2e1542a | 596 | (goto-char (point-min)) |
7132e457 | 597 | (ruby-end-of-block) |
53ca88c4 | 598 | (should (= 5 (line-number-at-pos))) |
7132e457 DG |
599 | (ruby-beginning-of-block) |
600 | (should (= 1 (line-number-at-pos)))))) | |
601 | ||
53ca88c4 DG |
602 | (ert-deftest ruby-move-to-block-skips-heredoc () |
603 | (ruby-with-temp-buffer | |
604 | (ruby-test-string | |
605 | "if something_wrong? | |
606 | | ActiveSupport::Deprecation.warn(<<-eowarn) | |
607 | | boo hoo | |
608 | | end | |
609 | | eowarn | |
610 | |end") | |
d2e1542a | 611 | (goto-char (point-min)) |
53ca88c4 DG |
612 | (ruby-end-of-block) |
613 | (should (= 6 (line-number-at-pos))) | |
614 | (ruby-beginning-of-block) | |
615 | (should (= 1 (line-number-at-pos))))) | |
616 | ||
fb549d64 DG |
617 | (ert-deftest ruby-move-to-block-does-not-fold-case () |
618 | (ruby-with-temp-buffer | |
619 | (ruby-test-string | |
620 | "foo do | |
621 | | Module.to_s | |
622 | |end") | |
fb549d64 DG |
623 | (let ((case-fold-search t)) |
624 | (ruby-beginning-of-block)) | |
625 | (should (= 1 (line-number-at-pos))))) | |
626 | ||
3086ca2e DG |
627 | (ert-deftest ruby-move-to-block-moves-from-else-to-if () |
628 | (ruby-with-temp-buffer (ruby-test-string | |
629 | "if true | |
630 | | nested_block do | |
631 | | end | |
632 | |else | |
633 | |end") | |
d2e1542a GM |
634 | (goto-char (point-min)) |
635 | (forward-line 3) | |
3086ca2e DG |
636 | (ruby-beginning-of-block) |
637 | (should (= 1 (line-number-at-pos))))) | |
638 | ||
fb549d64 DG |
639 | (ert-deftest ruby-beginning-of-defun-does-not-fold-case () |
640 | (ruby-with-temp-buffer | |
641 | (ruby-test-string | |
642 | "class C | |
643 | | def bar | |
644 | | Class.to_s | |
645 | | end | |
646 | |end") | |
d2e1542a GM |
647 | (goto-char (point-min)) |
648 | (forward-line 3) | |
fb549d64 DG |
649 | (let ((case-fold-search t)) |
650 | (beginning-of-defun)) | |
651 | (should (= 2 (line-number-at-pos))))) | |
652 | ||
653 | (ert-deftest ruby-end-of-defun-skips-to-next-line-after-the-method () | |
654 | (ruby-with-temp-buffer | |
655 | (ruby-test-string | |
656 | "class D | |
657 | | def tee | |
658 | | 'ho hum' | |
659 | | end | |
660 | |end") | |
d2e1542a GM |
661 | (goto-char (point-min)) |
662 | (forward-line 1) | |
fb549d64 DG |
663 | (end-of-defun) |
664 | (should (= 5 (line-number-at-pos))))) | |
665 | ||
7318480c DG |
666 | (defvar ruby-sexp-test-example |
667 | (ruby-test-string | |
668 | "class C | |
669 | | def foo | |
670 | | self.end | |
671 | | D.new.class | |
238150c8 DG |
672 | | [1, 2, 3].map do |i| |
673 | | i + 1 | |
674 | | end.sum | |
7318480c DG |
675 | | end |
676 | |end")) | |
677 | ||
678 | (ert-deftest ruby-forward-sexp-skips-method-calls-with-keyword-names () | |
679 | (ruby-with-temp-buffer ruby-sexp-test-example | |
680 | (goto-line 2) | |
681 | (ruby-forward-sexp) | |
238150c8 | 682 | (should (= 8 (line-number-at-pos))))) |
7318480c DG |
683 | |
684 | (ert-deftest ruby-backward-sexp-skips-method-calls-with-keyword-names () | |
685 | (ruby-with-temp-buffer ruby-sexp-test-example | |
238150c8 | 686 | (goto-line 8) |
7318480c DG |
687 | (end-of-line) |
688 | (ruby-backward-sexp) | |
689 | (should (= 2 (line-number-at-pos))))) | |
690 | ||
19594366 BB |
691 | (ert-deftest ruby--insert-coding-comment-ruby-style () |
692 | (with-temp-buffer | |
693 | (let ((ruby-encoding-magic-comment-style 'ruby)) | |
694 | (ruby--insert-coding-comment "utf-8") | |
695 | (should (string= "# coding: utf-8\n" (buffer-string)))))) | |
696 | ||
697 | (ert-deftest ruby--insert-coding-comment-emacs-style () | |
698 | (with-temp-buffer | |
699 | (let ((ruby-encoding-magic-comment-style 'emacs)) | |
700 | (ruby--insert-coding-comment "utf-8") | |
701 | (should (string= "# -*- coding: utf-8 -*-\n" (buffer-string)))))) | |
702 | ||
703 | (ert-deftest ruby--insert-coding-comment-custom-style () | |
704 | (with-temp-buffer | |
705 | (let ((ruby-encoding-magic-comment-style 'custom) | |
706 | (ruby-custom-encoding-magic-comment-template "# encoding: %s\n")) | |
707 | (ruby--insert-coding-comment "utf-8") | |
708 | (should (string= "# encoding: utf-8\n\n" (buffer-string)))))) | |
709 | ||
710 | ||
c28662a8 DG |
711 | (provide 'ruby-mode-tests) |
712 | ||
713 | ;;; ruby-mode-tests.el ends here |