]> code.delx.au - gnu-emacs/blob - test/automated/ruby-mode-tests.el
Merge from emacs-24; up to 2012-12-06T01:39:03Z!monnier@iro.umontreal.ca
[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 &rest values-plist)
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 (syntax-propertize (point))
56 (while values-plist
57 (should (eq (nth (car values-plist)
58 (parse-partial-sexp (point-min) (point)))
59 (cadr values-plist)))
60 (setq values-plist (cddr values-plist)))))
61
62 (defun ruby-assert-face (content pos face)
63 (ruby-with-temp-buffer content
64 (font-lock-fontify-buffer)
65 (should (eq face (get-text-property pos 'face)))))
66
67 (ert-deftest ruby-indent-after-symbol-made-from-string-interpolation ()
68 "It can indent the line after symbol made using string interpolation."
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))
79
80 (ert-deftest ruby-heredoc-font-lock ()
81 (let ((s "foo <<eos.gsub('^ *', '')"))
82 (ruby-assert-face s 9 font-lock-string-face)
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
88 (ert-deftest ruby-deep-indent ()
89 (let ((ruby-deep-arglist nil)
90 (ruby-deep-indent-paren '(?\( ?\{ ?\[ ?\] t)))
91 (ruby-should-indent "foo = [1,\n2" 7)
92 (ruby-should-indent "foo = {a: b,\nc: d" 7)
93 (ruby-should-indent "foo(a,\nb" 4)))
94
95 (ert-deftest ruby-deep-indent-disabled ()
96 (let ((ruby-deep-arglist nil)
97 (ruby-deep-indent-paren nil))
98 (ruby-should-indent "foo = [\n1" ruby-indent-level)
99 (ruby-should-indent "foo = {\na: b" ruby-indent-level)
100 (ruby-should-indent "foo(\na" ruby-indent-level)))
101
102 (ert-deftest ruby-indent-after-keyword-in-a-string ()
103 (ruby-should-indent "a = \"abc\nif\"\n " 0)
104 (ruby-should-indent "a = %w[abc\n def]\n " 0)
105 (ruby-should-indent "a = \"abc\n def\"\n " 0))
106
107 (ert-deftest ruby-indent-simple ()
108 (ruby-should-indent-buffer
109 "if foo
110 | bar
111 |end
112 |zot
113 |"
114 "if foo
115 |bar
116 | end
117 | zot
118 |"))
119
120 (ert-deftest ruby-indent-keyword-label ()
121 (ruby-should-indent-buffer
122 "bar(class: XXX) do
123 | foo
124 |end
125 |bar
126 |"
127 "bar(class: XXX) do
128 | foo
129 | end
130 | bar
131 |"))
132
133 (ert-deftest ruby-indent-method-with-question-mark ()
134 (ruby-should-indent-buffer
135 "if x.is_a?(XXX)
136 | foo
137 |end
138 |"
139 "if x.is_a?(XXX)
140 | foo
141 | end
142 |"))
143
144 (ert-deftest ruby-indent-expr-in-regexp ()
145 (ruby-should-indent-buffer
146 "if /#{foo}/ =~ s
147 | x = 1
148 |end
149 |"
150 "if /#{foo}/ =~ s
151 | x = 1
152 | end
153 |"))
154
155 (ert-deftest ruby-indent-singleton-class ()
156 (ruby-should-indent-buffer
157 "class<<bar
158 | foo
159 |end
160 |"
161 "class<<bar
162 |foo
163 | end
164 |"))
165
166 (ert-deftest ruby-indent-inside-heredoc-after-operator ()
167 (ruby-should-indent-buffer
168 "b=<<eos
169 | 42"
170 "b=<<eos
171 | 42"))
172
173 (ert-deftest ruby-indent-inside-heredoc-after-space ()
174 (ruby-should-indent-buffer
175 "foo <<eos.gsub(' ', '*')
176 | 42"
177 "foo <<eos.gsub(' ', '*')
178 | 42"))
179
180 (ert-deftest ruby-indent-array-literal ()
181 (let ((ruby-deep-indent-paren nil))
182 (ruby-should-indent-buffer
183 "foo = [
184 | bar
185 |]
186 |"
187 "foo = [
188 | bar
189 | ]
190 |"))
191 (ruby-should-indent-buffer
192 "foo do
193 | [bar]
194 |end
195 |"
196 "foo do
197 |[bar]
198 | end
199 |"))
200
201 (ert-deftest ruby-indent-begin-end ()
202 (ruby-should-indent-buffer
203 "begin
204 | a[b]
205 |end
206 |"
207 "begin
208 | a[b]
209 | end
210 |"))
211
212 (ert-deftest ruby-indent-array-after-paren-and-space ()
213 (ruby-should-indent-buffer
214 "class A
215 | def foo
216 | foo( [])
217 | end
218 |end
219 |"
220 "class A
221 | def foo
222 |foo( [])
223 |end
224 | end
225 |"))
226
227 (ert-deftest ruby-indent-after-block-in-continued-expression ()
228 (ruby-should-indent-buffer
229 "var =
230 | begin
231 | val
232 | end
233 |statement"
234 "var =
235 |begin
236 |val
237 |end
238 |statement"))
239
240 (ert-deftest ruby-move-to-block-stops-at-indentation ()
241 (ruby-with-temp-buffer "def f\nend"
242 (beginning-of-line)
243 (ruby-move-to-block -1)
244 (should (looking-at "^def"))))
245
246 (ert-deftest ruby-toggle-block-to-do-end ()
247 (ruby-with-temp-buffer "foo {|b|\n}"
248 (beginning-of-line)
249 (ruby-toggle-block)
250 (should (string= "foo do |b|\nend" (buffer-string)))))
251
252 (ert-deftest ruby-toggle-block-to-brace ()
253 (let ((pairs '((16 . "foo {|b| b + 2 }")
254 (15 . "foo {|b|\n b + 2\n}"))))
255 (dolist (pair pairs)
256 (with-temp-buffer
257 (let ((fill-column (car pair)))
258 (insert "foo do |b|\n b + 2\nend")
259 (ruby-mode)
260 (beginning-of-line)
261 (ruby-toggle-block)
262 (should (string= (cdr pair) (buffer-string))))))))
263
264 (ert-deftest ruby-toggle-block-to-multiline ()
265 (ruby-with-temp-buffer "foo {|b| b + 1}"
266 (beginning-of-line)
267 (ruby-toggle-block)
268 (should (string= "foo do |b|\n b + 1\nend" (buffer-string)))))
269
270 (ert-deftest ruby-recognize-symbols-starting-with-at-character ()
271 (ruby-assert-face ":@abc" 3 font-lock-constant-face))
272
273 (ert-deftest ruby-hash-character-not-interpolation ()
274 (ruby-assert-face "\"This is #{interpolation}\"" 15
275 font-lock-variable-name-face)
276 (ruby-assert-face "\"This is \\#{no interpolation} despite the #\""
277 15 font-lock-string-face)
278 (ruby-assert-face "\n#@comment, not ruby code" 5 font-lock-comment-face)
279 (ruby-assert-state "\n#@comment, not ruby code" 4 t)
280 (ruby-assert-face "# A comment cannot have #{an interpolation} in it"
281 30 font-lock-comment-face)
282 (ruby-assert-face "# #{comment}\n \"#{interpolation}\"" 16
283 font-lock-variable-name-face))
284
285 (ert-deftest ruby-interpolation-suppresses-quotes-inside ()
286 (let ((s "\"<ul><li>#{@files.join(\"</li><li>\")}</li></ul>\""))
287 (ruby-assert-state s 8 nil)
288 (ruby-assert-face s 9 font-lock-string-face)
289 (ruby-assert-face s 10 font-lock-variable-name-face)
290 (ruby-assert-face s 41 font-lock-string-face)))
291
292 (ert-deftest ruby-interpolation-suppresses-one-double-quote ()
293 (let ((s "\"foo#{'\"'}\""))
294 (ruby-assert-state s 8 nil)
295 (ruby-assert-face s 8 font-lock-variable-name-face)
296 (ruby-assert-face s 11 font-lock-string-face)))
297
298 (ert-deftest ruby-interpolation-suppresses-one-backtick ()
299 (let ((s "`as#{'`'}das`"))
300 (ruby-assert-state s 8 nil)))
301
302 (ert-deftest ruby-interpolation-keeps-non-quote-syntax ()
303 (let ((s "\"foo#{baz.tee}bar\""))
304 (ruby-with-temp-buffer s
305 (goto-char (point-min))
306 (ruby-mode)
307 (font-lock-fontify-buffer)
308 (search-forward "tee")
309 (should (string= (thing-at-point 'symbol) "tee")))))
310
311 (ert-deftest ruby-interpolation-inside-percent-literal-with-paren ()
312 :expected-result :failed
313 (let ((s "%(^#{\")\"}^)"))
314 (ruby-assert-face s 3 font-lock-string-face)
315 (ruby-assert-face s 4 font-lock-variable-name-face)
316 (ruby-assert-face s 10 font-lock-string-face)
317 ;; It's confused by the closing paren in the middle.
318 (ruby-assert-state s 8 nil)))
319
320 (ert-deftest ruby-add-log-current-method-examples ()
321 (let ((pairs '(("foo" . "#foo")
322 ("C.foo" . ".foo")
323 ("self.foo" . ".foo"))))
324 (dolist (pair pairs)
325 (let ((name (car pair))
326 (value (cdr pair)))
327 (ruby-with-temp-buffer (ruby-test-string
328 "module M
329 | class C
330 | def %s
331 | _
332 | end
333 | end
334 |end"
335 name)
336 (search-backward "_")
337 (forward-line)
338 (should (string= (ruby-add-log-current-method)
339 (format "M::C%s" value))))))))
340
341 (ert-deftest ruby-add-log-current-method-outside-of-method ()
342 (ruby-with-temp-buffer (ruby-test-string
343 "module M
344 | class C
345 | def foo
346 | end
347 | _
348 | end
349 |end")
350 (search-backward "_")
351 (should (string= (ruby-add-log-current-method)"M::C"))))
352
353 (ert-deftest ruby-add-log-current-method-in-singleton-class ()
354 (ruby-with-temp-buffer (ruby-test-string
355 "class C
356 | class << self
357 | def foo
358 | _
359 | end
360 | end
361 |end")
362 (search-backward "_")
363 (should (string= (ruby-add-log-current-method) "C.foo"))))
364
365 (ert-deftest ruby-add-log-current-method-namespace-shorthand ()
366 (ruby-with-temp-buffer (ruby-test-string
367 "class C::D
368 | def foo
369 | _
370 | end
371 |end")
372 (search-backward "_")
373 (should (string= (ruby-add-log-current-method) "C::D#foo"))))
374
375 (ert-deftest ruby-add-log-current-method-after-inner-class ()
376 (ruby-with-temp-buffer (ruby-test-string
377 "module M
378 | class C
379 | class D
380 | end
381 | _
382 | end
383 |end")
384 (search-backward "_")
385 (should (string= (ruby-add-log-current-method) "M::C"))))
386
387 (defvar ruby-block-test-example
388 (ruby-test-string
389 "class C
390 | def foo
391 | 1
392 | end
393 |
394 | def bar
395 | 2
396 | end
397 |
398 | def baz
399 | some do
400 | end
401 | end
402 |end"))
403
404 (defmacro ruby-deftest-move-to-block (name &rest body)
405 `(ert-deftest ,(intern (format "ruby-move-to-block-%s" name)) ()
406 (with-temp-buffer
407 (insert ruby-block-test-example)
408 (ruby-mode)
409 ,@body)))
410
411 (put 'ruby-deftest-move-to-block 'lisp-indent-function 'defun)
412
413 (ruby-deftest-move-to-block works-on-do
414 (goto-line 11)
415 (ruby-end-of-block)
416 (should (= 12 (line-number-at-pos)))
417 (ruby-beginning-of-block)
418 (should (= 11 (line-number-at-pos))))
419
420 (ruby-deftest-move-to-block zero-is-noop
421 (goto-line 5)
422 (ruby-move-to-block 0)
423 (should (= 5 (line-number-at-pos))))
424
425 (ruby-deftest-move-to-block ok-with-three
426 (goto-line 2)
427 (ruby-move-to-block 3)
428 (should (= 13 (line-number-at-pos))))
429
430 (ruby-deftest-move-to-block ok-with-minus-two
431 (goto-line 10)
432 (ruby-move-to-block -2)
433 (should (= 2 (line-number-at-pos))))
434
435 (provide 'ruby-mode-tests)
436
437 ;;; ruby-mode-tests.el ends here