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 (cl-flet ((fix-indent (s) (replace-regexp-in-string "^[ \t]*|" "" s
)))
40 (insert (fix-indent content
))
42 (indent-region (point-min) (point-max))
43 (should (string= (fix-indent expected
) (buffer-string))))))
45 (defun ruby-assert-state (content &rest values-plist
)
46 "Assert syntax state values at the end of CONTENT.
48 VALUES-PLIST is a list with alternating index and value elements."
52 (syntax-propertize (point))
54 (should (eq (nth (car values-plist
)
55 (parse-partial-sexp (point-min) (point)))
57 (setq values-plist
(cddr values-plist
)))))
59 (defun ruby-assert-face (content pos face
)
63 (font-lock-fontify-buffer)
64 (should (eq face
(get-text-property pos
'face
)))))
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"
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))
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
))
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)))
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
)))
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))
98 (ert-deftest ruby-indent-simple
()
99 (ruby-should-indent-buffer
111 (ert-deftest ruby-indent-keyword-label
()
112 (ruby-should-indent-buffer
124 (ert-deftest ruby-indent-method-with-question-mark
()
125 (ruby-should-indent-buffer
135 (ert-deftest ruby-indent-expr-in-regexp
()
136 (ruby-should-indent-buffer
146 (ert-deftest ruby-indent-singleton-class
()
147 :expected-result
:failed
; Doesn't work yet, when no space before "<<".
148 (ruby-should-indent-buffer
158 (ert-deftest ruby-indent-array-literal
()
159 (let ((ruby-deep-indent-paren nil
))
160 (ruby-should-indent-buffer
169 (ruby-should-indent-buffer
179 (ert-deftest ruby-indent-begin-end
()
180 (ruby-should-indent-buffer
190 (ert-deftest ruby-indent-array-after-paren-and-space
()
191 (ruby-should-indent-buffer
205 (ert-deftest ruby-move-to-block-stops-at-indentation
()
207 (insert "def f\nend")
210 (ruby-move-to-block -
1)
211 (should (looking-at "^def"))))
213 (ert-deftest ruby-toggle-block-to-do-end
()
215 (insert "foo {|b|\n}")
219 (should (string= "foo do |b|\nend" (buffer-string)))))
221 (ert-deftest ruby-toggle-block-to-brace
()
222 (let ((pairs '((16 .
"foo {|b| b + 2 }")
223 (15 .
"foo {|b|\n b + 2\n}"))))
226 (let ((fill-column (car pair
)))
227 (insert "foo do |b|\n b + 2\nend")
231 (should (string= (cdr pair
) (buffer-string))))))))
233 (ert-deftest ruby-toggle-block-to-multiline
()
235 (insert "foo {|b| b + 1}")
239 (should (string= "foo do |b|\n b + 1\nend" (buffer-string)))))
241 (ert-deftest ruby-recognize-symbols-starting-with-at-character
()
242 (ruby-assert-face ":@abc" 3 'font-lock-constant-face
))
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
)
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
)
251 (ruby-assert-face "# A comment cannot have #{an interpolation} in it"
252 30 'font-lock-comment-face
)
253 (ruby-assert-face "# #{comment}\n \"#{interpolation}\"" 16
254 'font-lock-variable-name-face
))
256 (provide 'ruby-mode-tests
)
258 ;;; ruby-mode-tests.el ends here