* lisp/progmodes/ruby-mode.el (ruby-match-expression-expansion): Only
[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 (cl-flet ((fix-indent (s) (replace-regexp-in-string "^[ \t]*|" "" s)))
40 (insert (fix-indent content))
41 (ruby-mode)
42 (indent-region (point-min) (point-max))
43 (should (string= (fix-indent expected) (buffer-substring-no-properties
44 (point-min) (point-max)))))))
45
46 (defun ruby-assert-state (content &rest values-plist)
47 "Assert syntax state values at the end of CONTENT.
48
49 VALUES-PLIST is a list with alternating index and value elements."
50 (with-temp-buffer
51 (insert content)
52 (ruby-mode)
53 (syntax-propertize (point))
54 (while values-plist
55 (should (eq (nth (car values-plist)
56 (parse-partial-sexp (point-min) (point)))
57 (cadr values-plist)))
58 (setq values-plist (cddr values-plist)))))
59
60 (defun ruby-assert-face (content pos face)
61 (with-temp-buffer
62 (insert content)
63 (ruby-mode)
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-deep-indent ()
81 (let ((ruby-deep-arglist nil)
82 (ruby-deep-indent-paren '(?\( ?\{ ?\[ ?\] t)))
83 (ruby-should-indent "foo = [1,\n2" 7)
84 (ruby-should-indent "foo = {a: b,\nc: d" 7)
85 (ruby-should-indent "foo(a,\nb" 4)))
86
87 (ert-deftest ruby-deep-indent-disabled ()
88 (let ((ruby-deep-arglist nil)
89 (ruby-deep-indent-paren nil))
90 (ruby-should-indent "foo = [\n1" ruby-indent-level)
91 (ruby-should-indent "foo = {\na: b" ruby-indent-level)
92 (ruby-should-indent "foo(\na" ruby-indent-level)))
93
94 (ert-deftest ruby-indent-after-keyword-in-a-string ()
95 (ruby-should-indent "a = \"abc\nif\"\n " 0)
96 (ruby-should-indent "a = %w[abc\n def]\n " 0)
97 (ruby-should-indent "a = \"abc\n def\"\n " 0))
98
99 (ert-deftest ruby-indent-simple ()
100 (ruby-should-indent-buffer
101 "if foo
102 | bar
103 |end
104 |zot
105 |"
106 "if foo
107 |bar
108 | end
109 | zot
110 |"))
111
112 (ert-deftest ruby-indent-keyword-label ()
113 (ruby-should-indent-buffer
114 "bar(class: XXX) do
115 | foo
116 |end
117 |bar
118 |"
119 "bar(class: XXX) do
120 | foo
121 | end
122 | bar
123 |"))
124
125 (ert-deftest ruby-indent-method-with-question-mark ()
126 (ruby-should-indent-buffer
127 "if x.is_a?(XXX)
128 | foo
129 |end
130 |"
131 "if x.is_a?(XXX)
132 | foo
133 | end
134 |"))
135
136 (ert-deftest ruby-indent-expr-in-regexp ()
137 (ruby-should-indent-buffer
138 "if /#{foo}/ =~ s
139 | x = 1
140 |end
141 |"
142 "if /#{foo}/ =~ s
143 | x = 1
144 | end
145 |"))
146
147 (ert-deftest ruby-indent-singleton-class ()
148 :expected-result :failed ; Doesn't work yet, when no space before "<<".
149 (ruby-should-indent-buffer
150 "class<<bar
151 | foo
152 |end
153 |"
154 "class<<bar
155 |foo
156 | end
157 |"))
158
159 (ert-deftest ruby-indent-array-literal ()
160 (let ((ruby-deep-indent-paren nil))
161 (ruby-should-indent-buffer
162 "foo = [
163 | bar
164 |]
165 |"
166 "foo = [
167 | bar
168 | ]
169 |"))
170 (ruby-should-indent-buffer
171 "foo do
172 | [bar]
173 |end
174 |"
175 "foo do
176 |[bar]
177 | end
178 |"))
179
180 (ert-deftest ruby-indent-begin-end ()
181 (ruby-should-indent-buffer
182 "begin
183 | a[b]
184 |end
185 |"
186 "begin
187 | a[b]
188 | end
189 |"))
190
191 (ert-deftest ruby-indent-array-after-paren-and-space ()
192 (ruby-should-indent-buffer
193 "class A
194 | def foo
195 | foo( [])
196 | end
197 |end
198 |"
199 "class A
200 | def foo
201 |foo( [])
202 |end
203 | end
204 |"))
205
206 (ert-deftest ruby-move-to-block-stops-at-opening ()
207 (with-temp-buffer
208 (insert "def f\nend")
209 (beginning-of-line)
210 (ruby-mode)
211 (ruby-move-to-block -1)
212 (should (looking-at "f$"))))
213
214 (ert-deftest ruby-toggle-block-to-do-end ()
215 (with-temp-buffer
216 (insert "foo {|b|\n}\n")
217 (ruby-mode)
218 (search-backward "{")
219 (ruby-toggle-block)
220 (should (string= "foo do |b|\nend\n" (buffer-substring-no-properties
221 (point-min) (point-max))))))
222
223 (ert-deftest ruby-toggle-block-to-brace ()
224 (with-temp-buffer
225 (insert "foo do |b|\nend\n")
226 (ruby-mode)
227 (search-backward "do")
228 (ruby-toggle-block)
229 (should (string= "foo {|b|\n}\n" (buffer-substring-no-properties
230 (point-min) (point-max))))))
231
232 (ert-deftest ruby-recognize-symbols-starting-with-at-character ()
233 (ruby-assert-face ":@abc" 3 'font-lock-constant-face))
234
235 (ert-deftest ruby-hash-character-not-interpolation ()
236 (ruby-assert-face "\"This is #{interpolation}\"" 15
237 'font-lock-variable-name-face)
238 (ruby-assert-face "\"This is \\#{no interpolation} despite the #\""
239 15 'font-lock-string-face)
240 (ruby-assert-face "\n#@comment, not ruby code" 5 'font-lock-comment-face)
241 (ruby-assert-state "\n#@comment, not ruby code" 4 t)
242 (ruby-assert-face "# A comment cannot have #{an interpolation} in it"
243 30 'font-lock-comment-face)
244 (ruby-assert-face "# #{comment}\n \"#{interpolation}\"" 16
245 'font-lock-variable-name-face))
246
247 (provide 'ruby-mode-tests)
248
249 ;;; ruby-mode-tests.el ends here