Move fix for cygw32 icon issue from emacs-24 branch to trunk as Stefan Monnier requests
[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 (dolist (pair pairs)
300 (let ((name (car pair))
301 (value (cdr pair)))
302 (with-temp-buffer
303 (insert (ruby-test-string
304 "module M
305 | class C
306 | def %s
307 | end
308 | end
309 |end"
310 name))
311 (ruby-mode)
312 (search-backward "def")
313 (forward-line)
314 (should (string= (ruby-add-log-current-method)
315 (format "M::C%s" value))))))))
316
317 (defvar ruby-block-test-example
318 (ruby-test-string
319 "class C
320 | def foo
321 | 1
322 | end
323 |
324 | def bar
325 | 2
326 | end
327 |
328 | def baz
329 | some do
330 | end
331 | end
332 |end"))
333
334 (defmacro ruby-deftest-move-to-block (name &rest body)
335 `(ert-deftest ,(intern (format "ruby-move-to-block-%s" name)) ()
336 (with-temp-buffer
337 (insert ruby-block-test-example)
338 (ruby-mode)
339 ,@body)))
340
341 (put 'ruby-deftest-move-to-block 'lisp-indent-function 'defun)
342
343 (ruby-deftest-move-to-block works-on-do
344 (goto-line 11)
345 (ruby-end-of-block)
346 (should (= 12 (line-number-at-pos)))
347 (ruby-beginning-of-block)
348 (should (= 11 (line-number-at-pos))))
349
350 (ruby-deftest-move-to-block zero-is-noop
351 (goto-line 5)
352 (ruby-move-to-block 0)
353 (should (= 5 (line-number-at-pos))))
354
355 (ruby-deftest-move-to-block ok-with-three
356 (goto-line 2)
357 (ruby-move-to-block 3)
358 (should (= 13 (line-number-at-pos))))
359
360 (ruby-deftest-move-to-block ok-with-minus-two
361 (goto-line 10)
362 (ruby-move-to-block -2)
363 (should (= 2 (line-number-at-pos))))
364
365 (provide 'ruby-mode-tests)
366
367 ;;; ruby-mode-tests.el ends here