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-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."
53 (syntax-propertize (point))
55 (should (eq (nth (car values-plist
)
56 (parse-partial-sexp (point-min) (point)))
58 (setq values-plist
(cddr values-plist
)))))
60 (defun ruby-assert-face (content pos face
)
64 (font-lock-fontify-buffer)
65 (should (eq face
(get-text-property pos
'face
)))))
67 (ert-deftest ruby-indent-after-symbol-made-from-string-interpolation
()
68 "It can indent the line after symbol made using string interpolation."
69 (ruby-should-indent "def foo(suffix)\n :\"bar#{suffix}\"\n"
72 (ert-deftest ruby-indent-after-js-style-symbol-with-block-beg-name
()
73 "JS-style hash symbol can have keyword name."
74 (ruby-should-indent "link_to \"home\", home_path, class: \"foo\"\n" 0))
76 (ert-deftest ruby-discern-singleton-class-from-heredoc
()
77 (ruby-assert-state "foo <<asd\n" 3 ?
\n)
78 (ruby-assert-state "class <<asd\n" 3 nil
))
80 (ert-deftest ruby-deep-indent
()
81 (let ((ruby-deep-arglist nil
)
82 (ruby-deep-indent-paren '(?\
( ?\
{ ?\
[ ?\
] t
)))
83 (ruby-should-indent "foo = [1,\n2" 7)
84 (ruby-should-indent "foo = {a: b,\nc: d" 7)
85 (ruby-should-indent "foo(a,\nb" 4)))
87 (ert-deftest ruby-deep-indent-disabled
()
88 (let ((ruby-deep-arglist nil
)
89 (ruby-deep-indent-paren nil
))
90 (ruby-should-indent "foo = [\n1" ruby-indent-level
)
91 (ruby-should-indent "foo = {\na: b" ruby-indent-level
)
92 (ruby-should-indent "foo(\na" ruby-indent-level
)))
94 (ert-deftest ruby-indent-after-keyword-in-a-string
()
95 (ruby-should-indent "a = \"abc\nif\"\n " 0)
96 (ruby-should-indent "a = %w[abc\n def]\n " 0)
97 (ruby-should-indent "a = \"abc\n def\"\n " 0))
99 (ert-deftest ruby-indent-simple
()
100 (ruby-should-indent-buffer
112 (ert-deftest ruby-indent-keyword-label
()
113 (ruby-should-indent-buffer
125 (ert-deftest ruby-indent-method-with-question-mark
()
126 (ruby-should-indent-buffer
136 (ert-deftest ruby-indent-expr-in-regexp
()
137 (ruby-should-indent-buffer
147 (ert-deftest ruby-indent-singleton-class
()
148 :expected-result
:failed
; Doesn't work yet, when no space before "<<".
149 (ruby-should-indent-buffer
159 (ert-deftest ruby-indent-array-literal
()
160 (let ((ruby-deep-indent-paren nil
))
161 (ruby-should-indent-buffer
170 (ruby-should-indent-buffer
180 (ert-deftest ruby-indent-begin-end
()
181 (ruby-should-indent-buffer
191 (ert-deftest ruby-indent-array-after-paren-and-space
()
192 (ruby-should-indent-buffer
206 (ert-deftest ruby-move-to-block-stops-at-opening
()
208 (insert "def f\nend")
211 (ruby-move-to-block -
1)
212 (should (looking-at "f$"))))
214 (ert-deftest ruby-toggle-block-to-do-end
()
216 (insert "foo {|b|\n}\n")
218 (search-backward "{")
220 (should (string= "foo do |b|\nend\n" (buffer-substring-no-properties
221 (point-min) (point-max))))))
223 (ert-deftest ruby-toggle-block-to-brace
()
225 (insert "foo do |b|\nend\n")
227 (search-backward "do")
229 (should (string= "foo {|b|\n}\n" (buffer-substring-no-properties
230 (point-min) (point-max))))))
232 (ert-deftest ruby-recognize-symbols-starting-with-at-character
()
233 (ruby-assert-face ":@abc" 3 'font-lock-constant-face
))
235 (ert-deftest ruby-hash-character-not-interpolation
()
236 (ruby-assert-face "\"This is #{interpolation}\"" 15
237 'font-lock-variable-name-face
)
238 (ruby-assert-face "\"This is \\#{no interpolation} despite the #\""
239 15 'font-lock-string-face
)
240 (ruby-assert-face "\n#@comment, not ruby code" 5 'font-lock-comment-face
)
241 (ruby-assert-state "\n#@comment, not ruby code" 4 t
)
242 (ruby-assert-face "# A comment cannot have #{an interpolation} in it"
243 30 'font-lock-comment-face
)
244 (ruby-assert-face "# #{comment}\n \"#{interpolation}\"" 16
245 'font-lock-variable-name-face
))
247 (provide 'ruby-mode-tests
)
249 ;;; ruby-mode-tests.el ends here