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/>.
26 (defun ruby-should-indent (content column
)
27 "Assert indentation COLUMN on the last line of CONTENT."
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."
39 (insert (ruby-test-string content
))
41 (indent-region (point-min) (point-max))
42 (should (string= (ruby-test-string expected
) (buffer-string)))))
44 (defun ruby-test-string (s &rest args
)
45 (apply 'format
(replace-regexp-in-string "^[ \t]*|" "" s
) args
))
47 (defun ruby-assert-state (content &rest values-plist
)
48 "Assert syntax state values at the end of CONTENT.
50 VALUES-PLIST is a list with alternating index and value elements."
54 (syntax-propertize (point))
56 (should (eq (nth (car values-plist
)
57 (parse-partial-sexp (point-min) (point)))
59 (setq values-plist
(cddr values-plist
)))))
61 (defun ruby-assert-face (content pos face
)
65 (font-lock-fontify-buffer)
66 (should (eq face
(get-text-property pos
'face
)))))
68 (ert-deftest ruby-indent-after-symbol-made-from-string-interpolation
()
69 "It can indent the line after symbol made using string interpolation."
70 (ruby-should-indent "def foo(suffix)\n :\"bar#{suffix}\"\n"
73 (ert-deftest ruby-indent-after-js-style-symbol-with-block-beg-name
()
74 "JS-style hash symbol can have keyword name."
75 (ruby-should-indent "link_to \"home\", home_path, class: \"foo\"\n" 0))
77 (ert-deftest ruby-discern-singleton-class-from-heredoc
()
78 (ruby-assert-state "foo <<asd\n" 3 ?
\n)
79 (ruby-assert-state "class <<asd\n" 3 nil
))
81 (ert-deftest ruby-heredoc-font-lock
()
82 (let ((s "foo <<eos.gsub('^ *', '')"))
83 (ruby-assert-face s
9 'font-lock-string-face
)
84 (ruby-assert-face s
10 nil
)))
86 (ert-deftest ruby-singleton-class-no-heredoc-font-lock
()
87 (ruby-assert-face "class<<a" 8 nil
))
89 (ert-deftest ruby-deep-indent
()
90 (let ((ruby-deep-arglist nil
)
91 (ruby-deep-indent-paren '(?\
( ?\
{ ?\
[ ?\
] t
)))
92 (ruby-should-indent "foo = [1,\n2" 7)
93 (ruby-should-indent "foo = {a: b,\nc: d" 7)
94 (ruby-should-indent "foo(a,\nb" 4)))
96 (ert-deftest ruby-deep-indent-disabled
()
97 (let ((ruby-deep-arglist nil
)
98 (ruby-deep-indent-paren nil
))
99 (ruby-should-indent "foo = [\n1" ruby-indent-level
)
100 (ruby-should-indent "foo = {\na: b" ruby-indent-level
)
101 (ruby-should-indent "foo(\na" ruby-indent-level
)))
103 (ert-deftest ruby-indent-after-keyword-in-a-string
()
104 (ruby-should-indent "a = \"abc\nif\"\n " 0)
105 (ruby-should-indent "a = %w[abc\n def]\n " 0)
106 (ruby-should-indent "a = \"abc\n def\"\n " 0))
108 (ert-deftest ruby-indent-simple
()
109 (ruby-should-indent-buffer
121 (ert-deftest ruby-indent-keyword-label
()
122 (ruby-should-indent-buffer
134 (ert-deftest ruby-indent-method-with-question-mark
()
135 (ruby-should-indent-buffer
145 (ert-deftest ruby-indent-expr-in-regexp
()
146 (ruby-should-indent-buffer
156 (ert-deftest ruby-indent-singleton-class
()
157 :expected-result
:failed
; Doesn't work yet, when no space before "<<".
158 (ruby-should-indent-buffer
168 (ert-deftest ruby-indent-array-literal
()
169 (let ((ruby-deep-indent-paren nil
))
170 (ruby-should-indent-buffer
179 (ruby-should-indent-buffer
189 (ert-deftest ruby-indent-begin-end
()
190 (ruby-should-indent-buffer
200 (ert-deftest ruby-indent-array-after-paren-and-space
()
201 (ruby-should-indent-buffer
215 (ert-deftest ruby-move-to-block-stops-at-indentation
()
217 (insert "def f\nend")
220 (ruby-move-to-block -
1)
221 (should (looking-at "^def"))))
223 (ert-deftest ruby-toggle-block-to-do-end
()
225 (insert "foo {|b|\n}")
229 (should (string= "foo do |b|\nend" (buffer-string)))))
231 (ert-deftest ruby-toggle-block-to-brace
()
232 (let ((pairs '((16 .
"foo {|b| b + 2 }")
233 (15 .
"foo {|b|\n b + 2\n}"))))
236 (let ((fill-column (car pair
)))
237 (insert "foo do |b|\n b + 2\nend")
241 (should (string= (cdr pair
) (buffer-string))))))))
243 (ert-deftest ruby-toggle-block-to-multiline
()
245 (insert "foo {|b| b + 1}")
249 (should (string= "foo do |b|\n b + 1\nend" (buffer-string)))))
251 (ert-deftest ruby-recognize-symbols-starting-with-at-character
()
252 (ruby-assert-face ":@abc" 3 'font-lock-constant-face
))
254 (ert-deftest ruby-hash-character-not-interpolation
()
255 (ruby-assert-face "\"This is #{interpolation}\"" 15
256 'font-lock-variable-name-face
)
257 (ruby-assert-face "\"This is \\#{no interpolation} despite the #\""
258 15 'font-lock-string-face
)
259 (ruby-assert-face "\n#@comment, not ruby code" 5 'font-lock-comment-face
)
260 (ruby-assert-state "\n#@comment, not ruby code" 4 t
)
261 (ruby-assert-face "# A comment cannot have #{an interpolation} in it"
262 30 'font-lock-comment-face
)
263 (ruby-assert-face "# #{comment}\n \"#{interpolation}\"" 16
264 'font-lock-variable-name-face
))
266 (ert-deftest ruby-add-log-current-method-examples
()
267 (let ((pairs '(("foo" .
"#foo")
269 ("self.foo" .
".foo"))))
270 (loop for
(name . value
) in pairs
272 (insert (ruby-test-string
281 (search-backward "def")
283 (should (string= (ruby-add-log-current-method)
284 (format "M::C%s" value
)))))))
286 (provide 'ruby-mode-tests
)
288 ;;; ruby-mode-tests.el ends here