Commit | Line | Data |
---|---|---|
c28662a8 DG |
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 | ||
9cd80478 | 26 | (defun ruby-should-indent (content column) |
9d2ed8a2 | 27 | "Assert indentation COLUMN on the last line of CONTENT." |
9cd80478 DG |
28 | (with-temp-buffer |
29 | (insert content) | |
30 | (ruby-mode) | |
31 | (ruby-indent-line) | |
9d2ed8a2 DG |
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)) | |
c3268831 | 43 | (should (string= (fix-indent expected) (buffer-string)))))) |
9cd80478 DG |
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 | ||
0ba2d4b6 DG |
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 | ||
9cd80478 | 66 | (ert-deftest ruby-indent-after-symbol-made-from-string-interpolation () |
c28662a8 | 67 | "It can indent the line after symbol made using string interpolation." |
9cd80478 DG |
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)) | |
c28662a8 | 78 | |
9d2ed8a2 DG |
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 | ||
0ba2d4b6 DG |
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 | ||
9d2ed8a2 DG |
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 | ||
0d9e2599 NN |
205 | (ert-deftest ruby-move-to-block-stops-at-opening () |
206 | (with-temp-buffer | |
207 | (insert "def f\nend") | |
208 | (beginning-of-line) | |
209 | (ruby-mode) | |
210 | (ruby-move-to-block -1) | |
211 | (should (looking-at "f$")))) | |
212 | ||
213 | (ert-deftest ruby-toggle-block-to-do-end () | |
214 | (with-temp-buffer | |
c3268831 | 215 | (insert "foo {|b|\n}") |
0d9e2599 | 216 | (ruby-mode) |
c3268831 | 217 | (beginning-of-line) |
0d9e2599 | 218 | (ruby-toggle-block) |
c3268831 | 219 | (should (string= "foo do |b|\nend" (buffer-string))))) |
0d9e2599 NN |
220 | |
221 | (ert-deftest ruby-toggle-block-to-brace () | |
32fb8162 DG |
222 | (let ((pairs '((16 . "foo {|b| b + 2 }") |
223 | (15 . "foo {|b|\n b + 2\n}")))) | |
224 | (dolist (pair pairs) | |
225 | (with-temp-buffer | |
226 | (let ((fill-column (car pair))) | |
227 | (insert "foo do |b|\n b + 2\nend") | |
228 | (ruby-mode) | |
229 | (beginning-of-line) | |
230 | (ruby-toggle-block) | |
231 | (should (string= (cdr pair) (buffer-string)))))))) | |
c3268831 DG |
232 | |
233 | (ert-deftest ruby-toggle-block-to-multiline () | |
234 | (with-temp-buffer | |
235 | (insert "foo {|b| b + 1}") | |
236 | (ruby-mode) | |
237 | (beginning-of-line) | |
0d9e2599 | 238 | (ruby-toggle-block) |
c3268831 | 239 | (should (string= "foo do |b|\n b + 1\nend" (buffer-string))))) |
0d9e2599 | 240 | |
0ba2d4b6 DG |
241 | (ert-deftest ruby-recognize-symbols-starting-with-at-character () |
242 | (ruby-assert-face ":@abc" 3 'font-lock-constant-face)) | |
243 | ||
244 | (ert-deftest ruby-hash-character-not-interpolation () | |
245 | (ruby-assert-face "\"This is #{interpolation}\"" 15 | |
246 | 'font-lock-variable-name-face) | |
247 | (ruby-assert-face "\"This is \\#{no interpolation} despite the #\"" | |
248 | 15 'font-lock-string-face) | |
616c6c36 DG |
249 | (ruby-assert-face "\n#@comment, not ruby code" 5 'font-lock-comment-face) |
250 | (ruby-assert-state "\n#@comment, not ruby code" 4 t) | |
0ba2d4b6 | 251 | (ruby-assert-face "# A comment cannot have #{an interpolation} in it" |
616c6c36 DG |
252 | 30 'font-lock-comment-face) |
253 | (ruby-assert-face "# #{comment}\n \"#{interpolation}\"" 16 | |
254 | 'font-lock-variable-name-face)) | |
0ba2d4b6 | 255 | |
c28662a8 DG |
256 | (provide 'ruby-mode-tests) |
257 | ||
258 | ;;; ruby-mode-tests.el ends here |