Merge from upstream ruby-mode.el
[emacs.git] / test / automated / ruby-mode-tests.el
blobdf51aa0d15a65f9602e28282a2f3ce60db85b290
1 ;;; ruby-mode-tests.el --- Test suite for ruby-mode
3 ;; Copyright (C) 2012 Free Software Foundation, Inc.
5 ;; This file is part of GNU Emacs.
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.
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.
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/>.
20 ;;; Commentary:
22 ;;; Code:
24 (require 'ruby-mode)
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))))
34 (defun ruby-should-indent-buffer (expected content)
35 "Assert that CONTENT turns into EXPECTED after the buffer is re-indented.
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)))))))
46 (defun ruby-assert-state (content &rest values-plist)
47 "Assert syntax state values at the end of CONTENT.
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)))))
60 (ert-deftest ruby-indent-after-symbol-made-from-string-interpolation ()
61 "It can indent the line after symbol made using string interpolation."
62 (ruby-should-indent "def foo(suffix)\n :\"bar#{suffix}\"\n"
63 ruby-indent-level))
65 (ert-deftest ruby-indent-after-js-style-symbol-with-block-beg-name ()
66 "JS-style hash symbol can have keyword name."
67 (ruby-should-indent "link_to \"home\", home_path, class: \"foo\"\n" 0))
69 (ert-deftest ruby-discern-singleton-class-from-heredoc ()
70 (ruby-assert-state "foo <<asd\n" 3 ?\n)
71 (ruby-assert-state "class <<asd\n" 3 nil))
73 (ert-deftest ruby-deep-indent ()
74 (let ((ruby-deep-arglist nil)
75 (ruby-deep-indent-paren '(?\( ?\{ ?\[ ?\] t)))
76 (ruby-should-indent "foo = [1,\n2" 7)
77 (ruby-should-indent "foo = {a: b,\nc: d" 7)
78 (ruby-should-indent "foo(a,\nb" 4)))
80 (ert-deftest ruby-deep-indent-disabled ()
81 (let ((ruby-deep-arglist nil)
82 (ruby-deep-indent-paren nil))
83 (ruby-should-indent "foo = [\n1" ruby-indent-level)
84 (ruby-should-indent "foo = {\na: b" ruby-indent-level)
85 (ruby-should-indent "foo(\na" ruby-indent-level)))
87 (ert-deftest ruby-indent-simple ()
88 (ruby-should-indent-buffer
89 "if foo
90 | bar
91 |end
92 |zot
94 "if foo
95 |bar
96 | end
97 | zot
98 |"))
100 (ert-deftest ruby-indent-keyword-label ()
101 (ruby-should-indent-buffer
102 "bar(class: XXX) do
103 | foo
104 |end
105 |bar
107 "bar(class: XXX) do
108 | foo
109 | end
110 | bar
111 |"))
113 (ert-deftest ruby-indent-method-with-question-mark ()
114 (ruby-should-indent-buffer
115 "if x.is_a?(XXX)
116 | foo
117 |end
119 "if x.is_a?(XXX)
120 | foo
121 | end
122 |"))
124 (ert-deftest ruby-indent-expr-in-regexp ()
125 (ruby-should-indent-buffer
126 "if /#{foo}/ =~ s
127 | x = 1
128 |end
130 "if /#{foo}/ =~ s
131 | x = 1
132 | end
133 |"))
135 (ert-deftest ruby-indent-singleton-class ()
136 :expected-result :failed ; Doesn't work yet, when no space before "<<".
137 (ruby-should-indent-buffer
138 "class<<bar
139 | foo
140 |end
142 "class<<bar
143 |foo
144 | end
145 |"))
147 (ert-deftest ruby-indent-array-literal ()
148 (let ((ruby-deep-indent-paren nil))
149 (ruby-should-indent-buffer
150 "foo = [
151 | bar
154 "foo = [
155 | bar
157 |"))
158 (ruby-should-indent-buffer
159 "foo do
160 | [bar]
161 |end
163 "foo do
164 |[bar]
165 | end
166 |"))
168 (ert-deftest ruby-indent-begin-end ()
169 (ruby-should-indent-buffer
170 "begin
171 | a[b]
172 |end
174 "begin
175 | a[b]
176 | end
177 |"))
179 (ert-deftest ruby-indent-array-after-paren-and-space ()
180 (ruby-should-indent-buffer
181 "class A
182 | def foo
183 | foo( [])
184 | end
185 |end
187 "class A
188 | def foo
189 |foo( [])
190 |end
191 | end
192 |"))
194 (ert-deftest ruby-move-to-block-stops-at-opening ()
195 (with-temp-buffer
196 (insert "def f\nend")
197 (beginning-of-line)
198 (ruby-mode)
199 (ruby-move-to-block -1)
200 (should (looking-at "f$"))))
202 (ert-deftest ruby-toggle-block-to-do-end ()
203 (with-temp-buffer
204 (insert "foo {|b|\n}\n")
205 (ruby-mode)
206 (search-backward "{")
207 (ruby-toggle-block)
208 (should (string= "foo do |b|\nend\n" (buffer-substring-no-properties
209 (point-min) (point-max))))))
211 (ert-deftest ruby-toggle-block-to-brace ()
212 (with-temp-buffer
213 (insert "foo do |b|\nend\n")
214 (ruby-mode)
215 (search-backward "do")
216 (ruby-toggle-block)
217 (should (string= "foo {|b|\n}\n" (buffer-substring-no-properties
218 (point-min) (point-max))))))
220 (provide 'ruby-mode-tests)
222 ;;; ruby-mode-tests.el ends here