1 ;;; ruby-mode-tests.el --- Test suite for ruby-mode
3 ;; Copyright (C) 2012-2013 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."
28 (ruby-with-temp-buffer content
30 (should (= (current-indentation) column
))))
32 (defun ruby-should-indent-buffer (expected content
)
33 "Assert that CONTENT turns into EXPECTED after the buffer is re-indented.
35 The whitespace before and including \"|\" on each line is removed."
36 (ruby-with-temp-buffer (ruby-test-string content
)
37 (indent-region (point-min) (point-max))
38 (should (string= (ruby-test-string expected
) (buffer-string)))))
40 (defmacro ruby-with-temp-buffer
(contents &rest body
)
41 (declare (indent 1) (debug t
))
47 (defun ruby-test-string (s &rest args
)
48 (apply 'format
(replace-regexp-in-string "^[ \t]*|" "" s
) args
))
50 (defun ruby-assert-state (content &rest values-plist
)
51 "Assert syntax state values at the end of CONTENT.
53 VALUES-PLIST is a list with alternating index and value elements."
54 (ruby-with-temp-buffer content
55 (syntax-propertize (point))
57 (should (eq (nth (car values-plist
)
58 (parse-partial-sexp (point-min) (point)))
60 (setq values-plist
(cddr values-plist
)))))
62 (defun ruby-assert-face (content pos face
)
63 (ruby-with-temp-buffer content
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-heredoc-font-lock
()
81 (let ((s "foo <<eos.gsub('^ *', '')"))
82 (ruby-assert-face s
9 font-lock-string-face
)
83 (ruby-assert-face s
10 nil
)))
85 (ert-deftest ruby-singleton-class-no-heredoc-font-lock
()
86 (ruby-assert-face "class<<a" 8 nil
))
88 (ert-deftest ruby-deep-indent
()
89 (let ((ruby-deep-arglist nil
)
90 (ruby-deep-indent-paren '(?\
( ?\
{ ?\
[ ?\
] t
)))
91 (ruby-should-indent "foo = [1,\n2" 7)
92 (ruby-should-indent "foo = {a: b,\nc: d" 7)
93 (ruby-should-indent "foo(a,\nb" 4)))
95 (ert-deftest ruby-deep-indent-disabled
()
96 (let ((ruby-deep-arglist nil
)
97 (ruby-deep-indent-paren nil
))
98 (ruby-should-indent "foo = [\n1" ruby-indent-level
)
99 (ruby-should-indent "foo = {\na: b" ruby-indent-level
)
100 (ruby-should-indent "foo(\na" ruby-indent-level
)))
102 (ert-deftest ruby-indent-after-keyword-in-a-string
()
103 (ruby-should-indent "a = \"abc\nif\"\n " 0)
104 (ruby-should-indent "a = %w[abc\n def]\n " 0)
105 (ruby-should-indent "a = \"abc\n def\"\n " 0))
107 (ert-deftest ruby-indent-simple
()
108 (ruby-should-indent-buffer
120 (ert-deftest ruby-indent-keyword-label
()
121 (ruby-should-indent-buffer
133 (ert-deftest ruby-indent-method-with-question-mark
()
134 (ruby-should-indent-buffer
144 (ert-deftest ruby-indent-expr-in-regexp
()
145 (ruby-should-indent-buffer
155 (ert-deftest ruby-indent-singleton-class
()
156 (ruby-should-indent-buffer
166 (ert-deftest ruby-indent-inside-heredoc-after-operator
()
167 (ruby-should-indent-buffer
173 (ert-deftest ruby-indent-inside-heredoc-after-space
()
174 (ruby-should-indent-buffer
175 "foo <<eos.gsub(' ', '*')
177 "foo <<eos.gsub(' ', '*')
180 (ert-deftest ruby-indent-array-literal
()
181 (let ((ruby-deep-indent-paren nil
))
182 (ruby-should-indent-buffer
191 (ruby-should-indent-buffer
201 (ert-deftest ruby-indent-begin-end
()
202 (ruby-should-indent-buffer
212 (ert-deftest ruby-indent-array-after-paren-and-space
()
213 (ruby-should-indent-buffer
227 (ert-deftest ruby-indent-after-block-in-continued-expression
()
228 (ruby-should-indent-buffer
240 (ert-deftest ruby-move-to-block-stops-at-indentation
()
241 (ruby-with-temp-buffer "def f\nend"
243 (ruby-move-to-block -
1)
244 (should (looking-at "^def"))))
246 (ert-deftest ruby-toggle-block-to-do-end
()
247 (ruby-with-temp-buffer "foo {|b|\n}"
250 (should (string= "foo do |b|\nend" (buffer-string)))))
252 (ert-deftest ruby-toggle-block-to-brace
()
253 (let ((pairs '((16 .
"foo {|b| b + 2 }")
254 (15 .
"foo {|b|\n b + 2\n}"))))
257 (let ((fill-column (car pair
)))
258 (insert "foo do |b|\n b + 2\nend")
262 (should (string= (cdr pair
) (buffer-string))))))))
264 (ert-deftest ruby-toggle-block-to-multiline
()
265 (ruby-with-temp-buffer "foo {|b| b + 1}"
268 (should (string= "foo do |b|\n b + 1\nend" (buffer-string)))))
270 (ert-deftest ruby-recognize-symbols-starting-with-at-character
()
271 (ruby-assert-face ":@abc" 3 font-lock-constant-face
))
273 (ert-deftest ruby-hash-character-not-interpolation
()
274 (ruby-assert-face "\"This is #{interpolation}\"" 15
275 font-lock-variable-name-face
)
276 (ruby-assert-face "\"This is \\#{no interpolation} despite the #\""
277 15 font-lock-string-face
)
278 (ruby-assert-face "\n#@comment, not ruby code" 5 font-lock-comment-face
)
279 (ruby-assert-state "\n#@comment, not ruby code" 4 t
)
280 (ruby-assert-face "# A comment cannot have #{an interpolation} in it"
281 30 font-lock-comment-face
)
282 (ruby-assert-face "# #{comment}\n \"#{interpolation}\"" 16
283 font-lock-variable-name-face
))
285 (ert-deftest ruby-interpolation-suppresses-quotes-inside
()
286 (let ((s "\"<ul><li>#{@files.join(\"</li><li>\")}</li></ul>\""))
287 (ruby-assert-state s
8 nil
)
288 (ruby-assert-face s
9 font-lock-string-face
)
289 (ruby-assert-face s
10 font-lock-variable-name-face
)
290 (ruby-assert-face s
41 font-lock-string-face
)))
292 (ert-deftest ruby-interpolation-suppresses-one-double-quote
()
293 (let ((s "\"foo#{'\"'}\""))
294 (ruby-assert-state s
8 nil
)
295 (ruby-assert-face s
8 font-lock-variable-name-face
)
296 (ruby-assert-face s
11 font-lock-string-face
)))
298 (ert-deftest ruby-interpolation-suppresses-one-backtick
()
299 (let ((s "`as#{'`'}das`"))
300 (ruby-assert-state s
8 nil
)))
302 (ert-deftest ruby-interpolation-keeps-non-quote-syntax
()
303 (let ((s "\"foo#{baz.tee}bar\""))
304 (ruby-with-temp-buffer s
305 (goto-char (point-min))
307 (font-lock-fontify-buffer)
308 (search-forward "tee")
309 (should (string= (thing-at-point 'symbol
) "tee")))))
311 (ert-deftest ruby-interpolation-inside-percent-literal-with-paren
()
312 :expected-result
:failed
313 (let ((s "%(^#{\")\"}^)"))
314 (ruby-assert-face s
3 font-lock-string-face
)
315 (ruby-assert-face s
4 font-lock-variable-name-face
)
316 (ruby-assert-face s
10 font-lock-string-face
)
317 ;; It's confused by the closing paren in the middle.
318 (ruby-assert-state s
8 nil
)))
320 (ert-deftest ruby-add-log-current-method-examples
()
321 (let ((pairs '(("foo" .
"#foo")
323 ("self.foo" .
".foo"))))
325 (let ((name (car pair
))
327 (ruby-with-temp-buffer (ruby-test-string
336 (search-backward "_")
338 (should (string= (ruby-add-log-current-method)
339 (format "M::C%s" value
))))))))
341 (ert-deftest ruby-add-log-current-method-outside-of-method
()
342 (ruby-with-temp-buffer (ruby-test-string
350 (search-backward "_")
351 (should (string= (ruby-add-log-current-method)"M::C"))))
353 (ert-deftest ruby-add-log-current-method-in-singleton-class
()
354 (ruby-with-temp-buffer (ruby-test-string
362 (search-backward "_")
363 (should (string= (ruby-add-log-current-method) "C.foo"))))
365 (ert-deftest ruby-add-log-current-method-namespace-shorthand
()
366 (ruby-with-temp-buffer (ruby-test-string
372 (search-backward "_")
373 (should (string= (ruby-add-log-current-method) "C::D#foo"))))
375 (ert-deftest ruby-add-log-current-method-after-inner-class
()
376 (ruby-with-temp-buffer (ruby-test-string
384 (search-backward "_")
385 (should (string= (ruby-add-log-current-method) "M::C"))))
387 (defvar ruby-block-test-example
404 (defmacro ruby-deftest-move-to-block
(name &rest body
)
405 `(ert-deftest ,(intern (format "ruby-move-to-block-%s" name
)) ()
407 (insert ruby-block-test-example
)
411 (put 'ruby-deftest-move-to-block
'lisp-indent-function
'defun
)
413 (ruby-deftest-move-to-block works-on-do
416 (should (= 12 (line-number-at-pos)))
417 (ruby-beginning-of-block)
418 (should (= 11 (line-number-at-pos))))
420 (ruby-deftest-move-to-block zero-is-noop
422 (ruby-move-to-block 0)
423 (should (= 5 (line-number-at-pos))))
425 (ruby-deftest-move-to-block ok-with-three
427 (ruby-move-to-block 3)
428 (should (= 13 (line-number-at-pos))))
430 (ruby-deftest-move-to-block ok-with-minus-two
432 (ruby-move-to-block -
2)
433 (should (= 2 (line-number-at-pos))))
435 (provide 'ruby-mode-tests
)
437 ;;; ruby-mode-tests.el ends here