]> code.delx.au - gnu-emacs/blob - test/automated/ruby-mode-tests.el
Merge from upstream ruby-mode.el
[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-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 (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))
64
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))
68
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))
72
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)))
79
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)))
86
87 (ert-deftest ruby-indent-simple ()
88 (ruby-should-indent-buffer
89 "if foo
90 | bar
91 |end
92 |zot
93 |"
94 "if foo
95 |bar
96 | end
97 | zot
98 |"))
99
100 (ert-deftest ruby-indent-keyword-label ()
101 (ruby-should-indent-buffer
102 "bar(class: XXX) do
103 | foo
104 |end
105 |bar
106 |"
107 "bar(class: XXX) do
108 | foo
109 | end
110 | bar
111 |"))
112
113 (ert-deftest ruby-indent-method-with-question-mark ()
114 (ruby-should-indent-buffer
115 "if x.is_a?(XXX)
116 | foo
117 |end
118 |"
119 "if x.is_a?(XXX)
120 | foo
121 | end
122 |"))
123
124 (ert-deftest ruby-indent-expr-in-regexp ()
125 (ruby-should-indent-buffer
126 "if /#{foo}/ =~ s
127 | x = 1
128 |end
129 |"
130 "if /#{foo}/ =~ s
131 | x = 1
132 | end
133 |"))
134
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
141 |"
142 "class<<bar
143 |foo
144 | end
145 |"))
146
147 (ert-deftest ruby-indent-array-literal ()
148 (let ((ruby-deep-indent-paren nil))
149 (ruby-should-indent-buffer
150 "foo = [
151 | bar
152 |]
153 |"
154 "foo = [
155 | bar
156 | ]
157 |"))
158 (ruby-should-indent-buffer
159 "foo do
160 | [bar]
161 |end
162 |"
163 "foo do
164 |[bar]
165 | end
166 |"))
167
168 (ert-deftest ruby-indent-begin-end ()
169 (ruby-should-indent-buffer
170 "begin
171 | a[b]
172 |end
173 |"
174 "begin
175 | a[b]
176 | end
177 |"))
178
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
186 |"
187 "class A
188 | def foo
189 |foo( [])
190 |end
191 | end
192 |"))
193
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$"))))
201
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))))))
210
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))))))
219
220 (provide 'ruby-mode-tests)
221
222 ;;; ruby-mode-tests.el ends here