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-indent-after-block-in-continued-expression ()
206 (ruby-should-indent-buffer
218 (ert-deftest ruby-move-to-block-stops-at-indentation ()
220 (insert "def f\nend")
223 (ruby-move-to-block -1)
224 (should (looking-at "^def"))))
226 (ert-deftest ruby-toggle-block-to-do-end ()
228 (insert "foo {|b|\n}")
232 (should (string= "foo do |b|\nend" (buffer-string)))))
234 (ert-deftest ruby-toggle-block-to-brace ()
235 (let ((pairs '((16 . "foo {|b| b + 2 }")
236 (15 . "foo {|b|\n b + 2\n}"))))
239 (let ((fill-column (car pair)))
240 (insert "foo do |b|\n b + 2\nend")
244 (should (string= (cdr pair) (buffer-string))))))))
246 (ert-deftest ruby-toggle-block-to-multiline ()
248 (insert "foo {|b| b + 1}")
252 (should (string= "foo do |b|\n b + 1\nend" (buffer-string)))))
254 (ert-deftest ruby-recognize-symbols-starting-with-at-character ()
255 (ruby-assert-face ":@abc" 3 'font-lock-constant-face))
257 (ert-deftest ruby-hash-character-not-interpolation ()
258 (ruby-assert-face "\"This is #{interpolation}\"" 15
259 'font-lock-variable-name-face)
260 (ruby-assert-face "\"This is \\#{no interpolation} despite the #\""
261 15 'font-lock-string-face)
262 (ruby-assert-face "\n#@comment, not ruby code" 5 'font-lock-comment-face)
263 (ruby-assert-state "\n#@comment, not ruby code" 4 t)
264 (ruby-assert-face "# A comment cannot have #{an interpolation} in it"
265 30 'font-lock-comment-face)
266 (ruby-assert-face "# #{comment}\n \"#{interpolation}\"" 16
267 'font-lock-variable-name-face))
269 (provide 'ruby-mode-tests)
271 ;;; ruby-mode-tests.el ends here