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 (ert-deftest ruby-indent-after-symbol-made-from-string-interpolation ()
61 "It can indent the line after symbol made using string interpolation."
62 (ruby-should-indent "def foo(suffix)\n :\"bar#{suffix}\"\n"
65 (ert-deftest ruby-indent-after-js-style-symbol-with-block-beg-name ()
66 "JS-style hash symbol can have keyword name."
67 (ruby-should-indent "link_to \"home\", home_path, class: \"foo\"\n" 0))
69 (ert-deftest ruby-discern-singleton-class-from-heredoc ()
70 (ruby-assert-state "foo <<asd\n" 3 ?\n)
71 (ruby-assert-state "class <<asd\n" 3 nil))
73 (ert-deftest ruby-deep-indent ()
74 (let ((ruby-deep-arglist nil)
75 (ruby-deep-indent-paren '(?\( ?\{ ?\[ ?\] t)))
76 (ruby-should-indent "foo = [1,\n2" 7)
77 (ruby-should-indent "foo = {a: b,\nc: d" 7)
78 (ruby-should-indent "foo(a,\nb" 4)))
80 (ert-deftest ruby-deep-indent-disabled ()
81 (let ((ruby-deep-arglist nil)
82 (ruby-deep-indent-paren nil))
83 (ruby-should-indent "foo = [\n1" ruby-indent-level)
84 (ruby-should-indent "foo = {\na: b" ruby-indent-level)
85 (ruby-should-indent "foo(\na" ruby-indent-level)))
87 (ert-deftest ruby-indent-simple ()
88 (ruby-should-indent-buffer
100 (ert-deftest ruby-indent-keyword-label ()
101 (ruby-should-indent-buffer
113 (ert-deftest ruby-indent-method-with-question-mark ()
114 (ruby-should-indent-buffer
124 (ert-deftest ruby-indent-expr-in-regexp ()
125 (ruby-should-indent-buffer
135 (ert-deftest ruby-indent-singleton-class ()
136 :expected-result :failed ; Doesn't work yet, when no space before "<<".
137 (ruby-should-indent-buffer
147 (ert-deftest ruby-indent-array-literal ()
148 (let ((ruby-deep-indent-paren nil))
149 (ruby-should-indent-buffer
158 (ruby-should-indent-buffer
168 (ert-deftest ruby-indent-begin-end ()
169 (ruby-should-indent-buffer
179 (ert-deftest ruby-indent-array-after-paren-and-space ()
180 (ruby-should-indent-buffer
194 (ert-deftest ruby-move-to-block-stops-at-opening ()
196 (insert "def f\nend")
199 (ruby-move-to-block -1)
200 (should (looking-at "f$"))))
202 (ert-deftest ruby-toggle-block-to-do-end ()
204 (insert "foo {|b|\n}\n")
206 (search-backward "{")
208 (should (string= "foo do |b|\nend\n" (buffer-substring-no-properties
209 (point-min) (point-max))))))
211 (ert-deftest ruby-toggle-block-to-brace ()
213 (insert "foo do |b|\nend\n")
215 (search-backward "do")
217 (should (string= "foo {|b|\n}\n" (buffer-substring-no-properties
218 (point-min) (point-max))))))
220 (provide 'ruby-mode-tests)
222 ;;; ruby-mode-tests.el ends here