Merge from emacs-24; up to 2012-11-15T23:31:37Z!dancol@dancol.org
[bpt/emacs.git] / test / automated / ruby-mode-tests.el
1 ;;; ruby-mode-tests.el --- Test suite for ruby-mode
2
3 ;; Copyright (C) 2012 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 (with-temp-buffer
29 (insert content)
30 (ruby-mode)
31 (ruby-indent-line)
32 (should (= (current-indentation) column))))
33
34 (defun ruby-should-indent-buffer (expected content)
35 "Assert that CONTENT turns into EXPECTED after the buffer is re-indented.
36
37 The whitespace before and including \"|\" on each line is removed."
38 (with-temp-buffer
39 (insert (ruby-test-string content))
40 (ruby-mode)
41 (indent-region (point-min) (point-max))
42 (should (string= (ruby-test-string expected) (buffer-string)))))
43
44 (defun ruby-test-string (s &rest args)
45 (apply 'format (replace-regexp-in-string "^[ \t]*|" "" s) args))
46
47 (defun ruby-assert-state (content &rest values-plist)
48 "Assert syntax state values at the end of CONTENT.
49
50 VALUES-PLIST is a list with alternating index and value elements."
51 (with-temp-buffer
52 (insert content)
53 (ruby-mode)
54 (syntax-propertize (point))
55 (while values-plist
56 (should (eq (nth (car values-plist)
57 (parse-partial-sexp (point-min) (point)))
58 (cadr values-plist)))
59 (setq values-plist (cddr values-plist)))))
60
61 (defun ruby-assert-face (content pos face)
62 (with-temp-buffer
63 (insert content)
64 (ruby-mode)
65 (font-lock-fontify-buffer)
66 (should (eq face (get-text-property pos 'face)))))
67
68 (ert-deftest ruby-indent-after-symbol-made-from-string-interpolation ()
69 "It can indent the line after symbol made using string interpolation."
70 (ruby-should-indent "def foo(suffix)\n :\"bar#{suffix}\"\n"
71 ruby-indent-level))
72
73 (ert-deftest ruby-indent-after-js-style-symbol-with-block-beg-name ()
74 "JS-style hash symbol can have keyword name."
75 (ruby-should-indent "link_to \"home\", home_path, class: \"foo\"\n" 0))
76
77 (ert-deftest ruby-discern-singleton-class-from-heredoc ()
78 (ruby-assert-state "foo <<asd\n" 3 ?\n)
79 (ruby-assert-state "class <<asd\n" 3 nil))
80
81 (ert-deftest ruby-heredoc-font-lock ()
82 (let ((s "foo <<eos.gsub('^ *', '')"))
83 (ruby-assert-face s 9 font-lock-string-face)
84 (ruby-assert-face s 10 nil)))
85
86 (ert-deftest ruby-singleton-class-no-heredoc-font-lock ()
87 (ruby-assert-face "class<<a" 8 nil))
88
89 (ert-deftest ruby-deep-indent ()
90 (let ((ruby-deep-arglist nil)
91 (ruby-deep-indent-paren '(?\( ?\{ ?\[ ?\] t)))
92 (ruby-should-indent "foo = [1,\n2" 7)
93 (ruby-should-indent "foo = {a: b,\nc: d" 7)
94 (ruby-should-indent "foo(a,\nb" 4)))
95
96 (ert-deftest ruby-deep-indent-disabled ()
97 (let ((ruby-deep-arglist nil)
98 (ruby-deep-indent-paren nil))
99 (ruby-should-indent "foo = [\n1" ruby-indent-level)
100 (ruby-should-indent "foo = {\na: b" ruby-indent-level)
101 (ruby-should-indent "foo(\na" ruby-indent-level)))
102
103 (ert-deftest ruby-indent-after-keyword-in-a-string ()
104 (ruby-should-indent "a = \"abc\nif\"\n " 0)
105 (ruby-should-indent "a = %w[abc\n def]\n " 0)
106 (ruby-should-indent "a = \"abc\n def\"\n " 0))
107
108 (ert-deftest ruby-indent-simple ()
109 (ruby-should-indent-buffer
110 "if foo
111 | bar
112 |end
113 |zot
114 |"
115 "if foo
116 |bar
117 | end
118 | zot
119 |"))
120
121 (ert-deftest ruby-indent-keyword-label ()
122 (ruby-should-indent-buffer
123 "bar(class: XXX) do
124 | foo
125 |end
126 |bar
127 |"
128 "bar(class: XXX) do
129 | foo
130 | end
131 | bar
132 |"))
133
134 (ert-deftest ruby-indent-method-with-question-mark ()
135 (ruby-should-indent-buffer
136 "if x.is_a?(XXX)
137 | foo
138 |end
139 |"
140 "if x.is_a?(XXX)
141 | foo
142 | end
143 |"))
144
145 (ert-deftest ruby-indent-expr-in-regexp ()
146 (ruby-should-indent-buffer
147 "if /#{foo}/ =~ s
148 | x = 1
149 |end
150 |"
151 "if /#{foo}/ =~ s
152 | x = 1
153 | end
154 |"))
155
156 (ert-deftest ruby-indent-singleton-class ()
157 (ruby-should-indent-buffer
158 "class<<bar
159 | foo
160 |end
161 |"
162 "class<<bar
163 |foo
164 | end
165 |"))
166
167 (ert-deftest ruby-indent-inside-heredoc-after-operator ()
168 (ruby-should-indent-buffer
169 "b=<<eos
170 | 42"
171 "b=<<eos
172 | 42"))
173
174 (ert-deftest ruby-indent-inside-heredoc-after-space ()
175 (ruby-should-indent-buffer
176 "foo <<eos.gsub(' ', '*')
177 | 42"
178 "foo <<eos.gsub(' ', '*')
179 | 42"))
180
181 (ert-deftest ruby-indent-array-literal ()
182 (let ((ruby-deep-indent-paren nil))
183 (ruby-should-indent-buffer
184 "foo = [
185 | bar
186 |]
187 |"
188 "foo = [
189 | bar
190 | ]
191 |"))
192 (ruby-should-indent-buffer
193 "foo do
194 | [bar]
195 |end
196 |"
197 "foo do
198 |[bar]
199 | end
200 |"))
201
202 (ert-deftest ruby-indent-begin-end ()
203 (ruby-should-indent-buffer
204 "begin
205 | a[b]
206 |end
207 |"
208 "begin
209 | a[b]
210 | end
211 |"))
212
213 (ert-deftest ruby-indent-array-after-paren-and-space ()
214 (ruby-should-indent-buffer
215 "class A
216 | def foo
217 | foo( [])
218 | end
219 |end
220 |"
221 "class A
222 | def foo
223 |foo( [])
224 |end
225 | end
226 |"))
227
228 (ert-deftest ruby-move-to-block-stops-at-indentation ()
229 (with-temp-buffer
230 (insert "def f\nend")
231 (beginning-of-line)
232 (ruby-mode)
233 (ruby-move-to-block -1)
234 (should (looking-at "^def"))))
235
236 (ert-deftest ruby-toggle-block-to-do-end ()
237 (with-temp-buffer
238 (insert "foo {|b|\n}")
239 (ruby-mode)
240 (beginning-of-line)
241 (ruby-toggle-block)
242 (should (string= "foo do |b|\nend" (buffer-string)))))
243
244 (ert-deftest ruby-toggle-block-to-brace ()
245 (let ((pairs '((16 . "foo {|b| b + 2 }")
246 (15 . "foo {|b|\n b + 2\n}"))))
247 (dolist (pair pairs)
248 (with-temp-buffer
249 (let ((fill-column (car pair)))
250 (insert "foo do |b|\n b + 2\nend")
251 (ruby-mode)
252 (beginning-of-line)
253 (ruby-toggle-block)
254 (should (string= (cdr pair) (buffer-string))))))))
255
256 (ert-deftest ruby-toggle-block-to-multiline ()
257 (with-temp-buffer
258 (insert "foo {|b| b + 1}")
259 (ruby-mode)
260 (beginning-of-line)
261 (ruby-toggle-block)
262 (should (string= "foo do |b|\n b + 1\nend" (buffer-string)))))
263
264 (ert-deftest ruby-recognize-symbols-starting-with-at-character ()
265 (ruby-assert-face ":@abc" 3 font-lock-constant-face))
266
267 (ert-deftest ruby-hash-character-not-interpolation ()
268 (ruby-assert-face "\"This is #{interpolation}\"" 15
269 font-lock-variable-name-face)
270 (ruby-assert-face "\"This is \\#{no interpolation} despite the #\""
271 15 font-lock-string-face)
272 (ruby-assert-face "\n#@comment, not ruby code" 5 font-lock-comment-face)
273 (ruby-assert-state "\n#@comment, not ruby code" 4 t)
274 (ruby-assert-face "# A comment cannot have #{an interpolation} in it"
275 30 font-lock-comment-face)
276 (ruby-assert-face "# #{comment}\n \"#{interpolation}\"" 16
277 font-lock-variable-name-face))
278
279 (ert-deftest ruby-interpolation-suppresses-syntax-inside ()
280 (let ((s "\"<ul><li>#{@files.join(\"</li><li>\")}</li></ul>\""))
281 (ruby-assert-state s 8 nil)
282 (ruby-assert-face s 9 font-lock-string-face)
283 (ruby-assert-face s 10 font-lock-variable-name-face)
284 (ruby-assert-face s 41 font-lock-string-face)))
285
286 (ert-deftest ruby-interpolation-inside-percent-literal-with-paren ()
287 :expected-result :failed
288 (let ((s "%(^#{\")\"}^)"))
289 (ruby-assert-face s 3 font-lock-string-face)
290 (ruby-assert-face s 4 font-lock-variable-name-face)
291 (ruby-assert-face s 10 font-lock-string-face)
292 ;; It's confused by the closing paren in the middle.
293 (ruby-assert-state s 8 nil)))
294
295 (ert-deftest ruby-add-log-current-method-examples ()
296 (let ((pairs '(("foo" . "#foo")
297 ("C.foo" . ".foo")
298 ("self.foo" . ".foo"))))
299 (loop for (name . value) in pairs
300 do (with-temp-buffer
301 (insert (ruby-test-string
302 "module M
303 | class C
304 | def %s
305 | end
306 | end
307 |end"
308 name))
309 (ruby-mode)
310 (search-backward "def")
311 (forward-line)
312 (should (string= (ruby-add-log-current-method)
313 (format "M::C%s" value)))))))
314
315 (defvar ruby-block-test-example
316 (ruby-test-string
317 "class C
318 | def foo
319 | 1
320 | end
321 |
322 | def bar
323 | 2
324 | end
325 |
326 | def baz
327 | some do
328 | end
329 | end
330 |end"))
331
332 (defmacro ruby-deftest-move-to-block (name &rest body)
333 `(ert-deftest ,(intern (format "ruby-move-to-block-%s" name)) ()
334 (with-temp-buffer
335 (insert ruby-block-test-example)
336 (ruby-mode)
337 ,@body)))
338
339 (put 'ruby-deftest-move-to-block 'lisp-indent-function 'defun)
340
341 (ruby-deftest-move-to-block works-on-do
342 (goto-line 11)
343 (ruby-end-of-block)
344 (should (= 12 (line-number-at-pos)))
345 (ruby-beginning-of-block)
346 (should (= 11 (line-number-at-pos))))
347
348 (ruby-deftest-move-to-block zero-is-noop
349 (goto-line 5)
350 (ruby-move-to-block 0)
351 (should (= 5 (line-number-at-pos))))
352
353 (ruby-deftest-move-to-block ok-with-three
354 (goto-line 2)
355 (ruby-move-to-block 3)
356 (should (= 13 (line-number-at-pos))))
357
358 (ruby-deftest-move-to-block ok-with-minus-two
359 (goto-line 10)
360 (ruby-move-to-block -2)
361 (should (= 2 (line-number-at-pos))))
362
363 (provide 'ruby-mode-tests)
364
365 ;;; ruby-mode-tests.el ends here