]> code.delx.au - gnu-emacs/blob - test/automated/ruby-mode-tests.el
28c2a2a070d88a7ab02ba7ee8dc67136450425b6
[gnu-emacs] / 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-string))))))
44
45 (defun ruby-assert-state (content &rest values-plist)
46 "Assert syntax state values at the end of CONTENT.
47
48 VALUES-PLIST is a list with alternating index and value elements."
49 (with-temp-buffer
50 (insert content)
51 (ruby-mode)
52 (syntax-propertize (point))
53 (while values-plist
54 (should (eq (nth (car values-plist)
55 (parse-partial-sexp (point-min) (point)))
56 (cadr values-plist)))
57 (setq values-plist (cddr values-plist)))))
58
59 (defun ruby-assert-face (content pos face)
60 (with-temp-buffer
61 (insert content)
62 (ruby-mode)
63 (font-lock-fontify-buffer)
64 (should (eq face (get-text-property pos 'face)))))
65
66 (ert-deftest ruby-indent-after-symbol-made-from-string-interpolation ()
67 "It can indent the line after symbol made using string interpolation."
68 (ruby-should-indent "def foo(suffix)\n :\"bar#{suffix}\"\n"
69 ruby-indent-level))
70
71 (ert-deftest ruby-indent-after-js-style-symbol-with-block-beg-name ()
72 "JS-style hash symbol can have keyword name."
73 (ruby-should-indent "link_to \"home\", home_path, class: \"foo\"\n" 0))
74
75 (ert-deftest ruby-discern-singleton-class-from-heredoc ()
76 (ruby-assert-state "foo <<asd\n" 3 ?\n)
77 (ruby-assert-state "class <<asd\n" 3 nil))
78
79 (ert-deftest ruby-deep-indent ()
80 (let ((ruby-deep-arglist nil)
81 (ruby-deep-indent-paren '(?\( ?\{ ?\[ ?\] t)))
82 (ruby-should-indent "foo = [1,\n2" 7)
83 (ruby-should-indent "foo = {a: b,\nc: d" 7)
84 (ruby-should-indent "foo(a,\nb" 4)))
85
86 (ert-deftest ruby-deep-indent-disabled ()
87 (let ((ruby-deep-arglist nil)
88 (ruby-deep-indent-paren nil))
89 (ruby-should-indent "foo = [\n1" ruby-indent-level)
90 (ruby-should-indent "foo = {\na: b" ruby-indent-level)
91 (ruby-should-indent "foo(\na" ruby-indent-level)))
92
93 (ert-deftest ruby-indent-after-keyword-in-a-string ()
94 (ruby-should-indent "a = \"abc\nif\"\n " 0)
95 (ruby-should-indent "a = %w[abc\n def]\n " 0)
96 (ruby-should-indent "a = \"abc\n def\"\n " 0))
97
98 (ert-deftest ruby-indent-simple ()
99 (ruby-should-indent-buffer
100 "if foo
101 | bar
102 |end
103 |zot
104 |"
105 "if foo
106 |bar
107 | end
108 | zot
109 |"))
110
111 (ert-deftest ruby-indent-keyword-label ()
112 (ruby-should-indent-buffer
113 "bar(class: XXX) do
114 | foo
115 |end
116 |bar
117 |"
118 "bar(class: XXX) do
119 | foo
120 | end
121 | bar
122 |"))
123
124 (ert-deftest ruby-indent-method-with-question-mark ()
125 (ruby-should-indent-buffer
126 "if x.is_a?(XXX)
127 | foo
128 |end
129 |"
130 "if x.is_a?(XXX)
131 | foo
132 | end
133 |"))
134
135 (ert-deftest ruby-indent-expr-in-regexp ()
136 (ruby-should-indent-buffer
137 "if /#{foo}/ =~ s
138 | x = 1
139 |end
140 |"
141 "if /#{foo}/ =~ s
142 | x = 1
143 | end
144 |"))
145
146 (ert-deftest ruby-indent-singleton-class ()
147 :expected-result :failed ; Doesn't work yet, when no space before "<<".
148 (ruby-should-indent-buffer
149 "class<<bar
150 | foo
151 |end
152 |"
153 "class<<bar
154 |foo
155 | end
156 |"))
157
158 (ert-deftest ruby-indent-array-literal ()
159 (let ((ruby-deep-indent-paren nil))
160 (ruby-should-indent-buffer
161 "foo = [
162 | bar
163 |]
164 |"
165 "foo = [
166 | bar
167 | ]
168 |"))
169 (ruby-should-indent-buffer
170 "foo do
171 | [bar]
172 |end
173 |"
174 "foo do
175 |[bar]
176 | end
177 |"))
178
179 (ert-deftest ruby-indent-begin-end ()
180 (ruby-should-indent-buffer
181 "begin
182 | a[b]
183 |end
184 |"
185 "begin
186 | a[b]
187 | end
188 |"))
189
190 (ert-deftest ruby-indent-array-after-paren-and-space ()
191 (ruby-should-indent-buffer
192 "class A
193 | def foo
194 | foo( [])
195 | end
196 |end
197 |"
198 "class A
199 | def foo
200 |foo( [])
201 |end
202 | end
203 |"))
204
205 (ert-deftest ruby-indent-after-block-in-continued-expression ()
206 (ruby-should-indent-buffer
207 "var =
208 | begin
209 | val
210 | end
211 |statement"
212 "var =
213 |begin
214 |val
215 |end
216 |statement"))
217
218 (ert-deftest ruby-move-to-block-stops-at-indentation ()
219 (with-temp-buffer
220 (insert "def f\nend")
221 (beginning-of-line)
222 (ruby-mode)
223 (ruby-move-to-block -1)
224 (should (looking-at "^def"))))
225
226 (ert-deftest ruby-toggle-block-to-do-end ()
227 (with-temp-buffer
228 (insert "foo {|b|\n}")
229 (ruby-mode)
230 (beginning-of-line)
231 (ruby-toggle-block)
232 (should (string= "foo do |b|\nend" (buffer-string)))))
233
234 (ert-deftest ruby-toggle-block-to-brace ()
235 (let ((pairs '((16 . "foo {|b| b + 2 }")
236 (15 . "foo {|b|\n b + 2\n}"))))
237 (dolist (pair pairs)
238 (with-temp-buffer
239 (let ((fill-column (car pair)))
240 (insert "foo do |b|\n b + 2\nend")
241 (ruby-mode)
242 (beginning-of-line)
243 (ruby-toggle-block)
244 (should (string= (cdr pair) (buffer-string))))))))
245
246 (ert-deftest ruby-toggle-block-to-multiline ()
247 (with-temp-buffer
248 (insert "foo {|b| b + 1}")
249 (ruby-mode)
250 (beginning-of-line)
251 (ruby-toggle-block)
252 (should (string= "foo do |b|\n b + 1\nend" (buffer-string)))))
253
254 (ert-deftest ruby-recognize-symbols-starting-with-at-character ()
255 (ruby-assert-face ":@abc" 3 'font-lock-constant-face))
256
257 (ert-deftest ruby-hash-character-not-interpolation ()
258 (ruby-assert-face "\"This is #{interpolation}\"" 15
259 'font-lock-variable-name-face)
260 (ruby-assert-face "\"This is \\#{no interpolation} despite the #\""
261 15 'font-lock-string-face)
262 (ruby-assert-face "\n#@comment, not ruby code" 5 'font-lock-comment-face)
263 (ruby-assert-state "\n#@comment, not ruby code" 4 t)
264 (ruby-assert-face "# A comment cannot have #{an interpolation} in it"
265 30 'font-lock-comment-face)
266 (ruby-assert-face "# #{comment}\n \"#{interpolation}\"" 16
267 'font-lock-variable-name-face))
268
269 (provide 'ruby-mode-tests)
270
271 ;;; ruby-mode-tests.el ends here