* lisp/progmodes/ruby-mode.el (ruby-font-lock-keywords): Never
[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-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-heredoc-font-lock ()
80 (let ((s "foo <<eos.gsub('^ *', '')"))
81 (ruby-assert-face s 9 'font-lock-string-face)
82 (ruby-assert-face s 10 nil)))
83
84 (ert-deftest ruby-singleton-class-no-heredoc-font-lock ()
85 (ruby-assert-face "class<<a" 8 nil))
86
87 (ert-deftest ruby-deep-indent ()
88 (let ((ruby-deep-arglist nil)
89 (ruby-deep-indent-paren '(?\( ?\{ ?\[ ?\] t)))
90 (ruby-should-indent "foo = [1,\n2" 7)
91 (ruby-should-indent "foo = {a: b,\nc: d" 7)
92 (ruby-should-indent "foo(a,\nb" 4)))
93
94 (ert-deftest ruby-deep-indent-disabled ()
95 (let ((ruby-deep-arglist nil)
96 (ruby-deep-indent-paren nil))
97 (ruby-should-indent "foo = [\n1" ruby-indent-level)
98 (ruby-should-indent "foo = {\na: b" ruby-indent-level)
99 (ruby-should-indent "foo(\na" ruby-indent-level)))
100
101 (ert-deftest ruby-indent-after-keyword-in-a-string ()
102 (ruby-should-indent "a = \"abc\nif\"\n " 0)
103 (ruby-should-indent "a = %w[abc\n def]\n " 0)
104 (ruby-should-indent "a = \"abc\n def\"\n " 0))
105
106 (ert-deftest ruby-indent-simple ()
107 (ruby-should-indent-buffer
108 "if foo
109 | bar
110 |end
111 |zot
112 |"
113 "if foo
114 |bar
115 | end
116 | zot
117 |"))
118
119 (ert-deftest ruby-indent-keyword-label ()
120 (ruby-should-indent-buffer
121 "bar(class: XXX) do
122 | foo
123 |end
124 |bar
125 |"
126 "bar(class: XXX) do
127 | foo
128 | end
129 | bar
130 |"))
131
132 (ert-deftest ruby-indent-method-with-question-mark ()
133 (ruby-should-indent-buffer
134 "if x.is_a?(XXX)
135 | foo
136 |end
137 |"
138 "if x.is_a?(XXX)
139 | foo
140 | end
141 |"))
142
143 (ert-deftest ruby-indent-expr-in-regexp ()
144 (ruby-should-indent-buffer
145 "if /#{foo}/ =~ s
146 | x = 1
147 |end
148 |"
149 "if /#{foo}/ =~ s
150 | x = 1
151 | end
152 |"))
153
154 (ert-deftest ruby-indent-singleton-class ()
155 :expected-result :failed ; Doesn't work yet, when no space before "<<".
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-array-literal ()
167 (let ((ruby-deep-indent-paren nil))
168 (ruby-should-indent-buffer
169 "foo = [
170 | bar
171 |]
172 |"
173 "foo = [
174 | bar
175 | ]
176 |"))
177 (ruby-should-indent-buffer
178 "foo do
179 | [bar]
180 |end
181 |"
182 "foo do
183 |[bar]
184 | end
185 |"))
186
187 (ert-deftest ruby-indent-begin-end ()
188 (ruby-should-indent-buffer
189 "begin
190 | a[b]
191 |end
192 |"
193 "begin
194 | a[b]
195 | end
196 |"))
197
198 (ert-deftest ruby-indent-array-after-paren-and-space ()
199 (ruby-should-indent-buffer
200 "class A
201 | def foo
202 | foo( [])
203 | end
204 |end
205 |"
206 "class A
207 | def foo
208 |foo( [])
209 |end
210 | end
211 |"))
212
213 (ert-deftest ruby-move-to-block-stops-at-indentation ()
214 (with-temp-buffer
215 (insert "def f\nend")
216 (beginning-of-line)
217 (ruby-mode)
218 (ruby-move-to-block -1)
219 (should (looking-at "^def"))))
220
221 (ert-deftest ruby-toggle-block-to-do-end ()
222 (with-temp-buffer
223 (insert "foo {|b|\n}")
224 (ruby-mode)
225 (beginning-of-line)
226 (ruby-toggle-block)
227 (should (string= "foo do |b|\nend" (buffer-string)))))
228
229 (ert-deftest ruby-toggle-block-to-brace ()
230 (let ((pairs '((16 . "foo {|b| b + 2 }")
231 (15 . "foo {|b|\n b + 2\n}"))))
232 (dolist (pair pairs)
233 (with-temp-buffer
234 (let ((fill-column (car pair)))
235 (insert "foo do |b|\n b + 2\nend")
236 (ruby-mode)
237 (beginning-of-line)
238 (ruby-toggle-block)
239 (should (string= (cdr pair) (buffer-string))))))))
240
241 (ert-deftest ruby-toggle-block-to-multiline ()
242 (with-temp-buffer
243 (insert "foo {|b| b + 1}")
244 (ruby-mode)
245 (beginning-of-line)
246 (ruby-toggle-block)
247 (should (string= "foo do |b|\n b + 1\nend" (buffer-string)))))
248
249 (ert-deftest ruby-recognize-symbols-starting-with-at-character ()
250 (ruby-assert-face ":@abc" 3 'font-lock-constant-face))
251
252 (ert-deftest ruby-hash-character-not-interpolation ()
253 (ruby-assert-face "\"This is #{interpolation}\"" 15
254 'font-lock-variable-name-face)
255 (ruby-assert-face "\"This is \\#{no interpolation} despite the #\""
256 15 'font-lock-string-face)
257 (ruby-assert-face "\n#@comment, not ruby code" 5 'font-lock-comment-face)
258 (ruby-assert-state "\n#@comment, not ruby code" 4 t)
259 (ruby-assert-face "# A comment cannot have #{an interpolation} in it"
260 30 'font-lock-comment-face)
261 (ruby-assert-face "# #{comment}\n \"#{interpolation}\"" 16
262 'font-lock-variable-name-face))
263
264 (provide 'ruby-mode-tests)
265
266 ;;; ruby-mode-tests.el ends here