ruby-mode-tests.el 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. ;;; ruby-mode-tests.el --- Test suite for ruby-mode
  2. ;; Copyright (C) 2012-2016 Free Software Foundation, Inc.
  3. ;; This file is part of GNU Emacs.
  4. ;; GNU Emacs is free software: you can redistribute it and/or modify
  5. ;; it under the terms of the GNU General Public License as published by
  6. ;; the Free Software Foundation, either version 3 of the License, or
  7. ;; (at your option) any later version.
  8. ;; GNU Emacs is distributed in the hope that it will be useful,
  9. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ;; GNU General Public License for more details.
  12. ;; You should have received a copy of the GNU General Public License
  13. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  14. ;;; Commentary:
  15. ;;; Code:
  16. (require 'ert)
  17. (require 'ruby-mode)
  18. (defmacro ruby-with-temp-buffer (contents &rest body)
  19. (declare (indent 1) (debug t))
  20. `(with-temp-buffer
  21. (insert ,contents)
  22. (ruby-mode)
  23. ,@body))
  24. (defun ruby-should-indent (content column)
  25. "Assert indentation COLUMN on the last line of CONTENT."
  26. (ruby-with-temp-buffer content
  27. (indent-according-to-mode)
  28. (should (= (current-indentation) column))))
  29. (defun ruby-should-indent-buffer (expected content)
  30. "Assert that CONTENT turns into EXPECTED after the buffer is re-indented.
  31. The whitespace before and including \"|\" on each line is removed."
  32. (ruby-with-temp-buffer (ruby-test-string content)
  33. (indent-region (point-min) (point-max))
  34. (should (string= (ruby-test-string expected) (buffer-string)))))
  35. (defun ruby-test-string (s &rest args)
  36. (apply 'format (replace-regexp-in-string "^[ \t]*|" "" s) args))
  37. (defun ruby-assert-state (content index value &optional point)
  38. "Assert syntax state values at the end of CONTENT.
  39. VALUES-PLIST is a list with alternating index and value elements."
  40. (ruby-with-temp-buffer content
  41. (when point (goto-char point))
  42. (syntax-propertize (point))
  43. (should (eq (nth index
  44. (parse-partial-sexp (point-min) (point)))
  45. value))))
  46. (defun ruby-assert-face (content pos face)
  47. (ruby-with-temp-buffer content
  48. (font-lock-ensure nil nil)
  49. (should (eq face (get-text-property pos 'face)))))
  50. (ert-deftest ruby-indent-after-symbol-made-from-string-interpolation ()
  51. "It can indent the line after symbol made using string interpolation."
  52. (ruby-should-indent "def foo(suffix)\n :\"bar#{suffix}\"\n"
  53. ruby-indent-level))
  54. (ert-deftest ruby-indent-after-js-style-symbol-with-block-beg-name ()
  55. "JS-style hash symbol can have keyword name."
  56. (ruby-should-indent "link_to \"home\", home_path, class: \"foo\"\n" 0))
  57. (ert-deftest ruby-discern-singleton-class-from-heredoc ()
  58. (ruby-assert-state "foo <<asd\n" 3 ?\n)
  59. (ruby-assert-state "class <<asd\n" 3 nil))
  60. (ert-deftest ruby-heredoc-font-lock ()
  61. (let ((s "foo <<eos.gsub('^ *', '')"))
  62. (ruby-assert-face s 9 font-lock-string-face)
  63. (ruby-assert-face s 10 nil)))
  64. (ert-deftest ruby-singleton-class-no-heredoc-font-lock ()
  65. (ruby-assert-face "class<<a" 8 nil))
  66. (ert-deftest ruby-heredoc-highlights-interpolations ()
  67. (ruby-assert-face "s = <<EOS\n #{foo}\nEOS" 15 font-lock-variable-name-face))
  68. (ert-deftest ruby-no-heredoc-inside-quotes ()
  69. (ruby-assert-state "\"<<\", \"\",\nfoo" 3 nil))
  70. (ert-deftest ruby-no-heredoc-left-shift ()
  71. ;; We can't really detect the left shift operator (like in similar
  72. ;; cases, it depends on the type of foo), so we just require for <<
  73. ;; to be preceded by a character from a known set.
  74. (ruby-assert-state "foo(a<<b)" 3 nil))
  75. (ert-deftest ruby-no-heredoc-class-self ()
  76. (ruby-assert-state "class <<self\nend" 3 nil))
  77. (ert-deftest ruby-exit!-font-lock ()
  78. (ruby-assert-face "exit!" 5 font-lock-builtin-face))
  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)))
  85. (ert-deftest ruby-deep-indent-disabled ()
  86. (let ((ruby-deep-arglist nil)
  87. (ruby-deep-indent-paren nil))
  88. (ruby-should-indent "foo = [\n1" ruby-indent-level)
  89. (ruby-should-indent "foo = {\na: b" ruby-indent-level)
  90. (ruby-should-indent "foo(\na" ruby-indent-level)))
  91. (ert-deftest ruby-indent-after-keyword-in-a-string ()
  92. (ruby-should-indent "a = \"abc\nif\"\n " 0)
  93. (ruby-should-indent "a = %w[abc\n def]\n " 0)
  94. (ruby-should-indent "a = \"abc\n def\"\n " 0))
  95. (ert-deftest ruby-regexp-doesnt-start-in-string ()
  96. (ruby-assert-state "'(/', /\d+/" 3 nil))
  97. (ert-deftest ruby-regexp-starts-after-string ()
  98. (ruby-assert-state "'(/', /\d+/" 3 ?/ 8))
  99. (ert-deftest ruby-regexp-interpolation-is-highlighted ()
  100. (ruby-assert-face "/#{foobs}/" 4 font-lock-variable-name-face))
  101. (ert-deftest ruby-regexp-skips-over-interpolation ()
  102. (ruby-assert-state "/#{foobs.join('/')}/" 3 nil))
  103. (ert-deftest ruby-regexp-continues-till-end-when-unclosed ()
  104. (ruby-assert-state "/bars" 3 ?/))
  105. (ert-deftest ruby-regexp-can-be-multiline ()
  106. (ruby-assert-state "/bars\ntees # toots \nfoos/" 3 nil))
  107. (ert-deftest ruby-slash-symbol-is-not-mistaken-for-regexp ()
  108. (ruby-assert-state ":/" 3 nil))
  109. (ert-deftest ruby-slash-char-literal-is-not-mistaken-for-regexp ()
  110. (ruby-assert-state "?/" 3 nil))
  111. (ert-deftest ruby-indent-simple ()
  112. (ruby-should-indent-buffer
  113. "if foo
  114. | bar
  115. |end
  116. |zot
  117. |"
  118. "if foo
  119. |bar
  120. | end
  121. | zot
  122. |"))
  123. (ert-deftest ruby-indent-keyword-label ()
  124. (ruby-should-indent-buffer
  125. "bar(class: XXX) do
  126. | foo
  127. |end
  128. |bar
  129. |"
  130. "bar(class: XXX) do
  131. | foo
  132. | end
  133. | bar
  134. |"))
  135. (ert-deftest ruby-indent-method-with-question-mark ()
  136. (ruby-should-indent-buffer
  137. "if x.is_a?(XXX)
  138. | foo
  139. |end
  140. |"
  141. "if x.is_a?(XXX)
  142. | foo
  143. | end
  144. |"))
  145. (ert-deftest ruby-indent-expr-in-regexp ()
  146. (ruby-should-indent-buffer
  147. "if /#{foo}/ =~ s
  148. | x = 1
  149. |end
  150. |"
  151. "if /#{foo}/ =~ s
  152. | x = 1
  153. | end
  154. |"))
  155. (ert-deftest ruby-indent-singleton-class ()
  156. (ruby-should-indent-buffer
  157. "class<<bar
  158. | foo
  159. |end
  160. |"
  161. "class<<bar
  162. |foo
  163. | end
  164. |"))
  165. (ert-deftest ruby-indent-inside-heredoc-after-operator ()
  166. (ruby-should-indent-buffer
  167. "b=<<eos
  168. | 42"
  169. "b=<<eos
  170. | 42"))
  171. (ert-deftest ruby-indent-inside-heredoc-after-space ()
  172. (ruby-should-indent-buffer
  173. "foo <<eos.gsub(' ', '*')
  174. | 42"
  175. "foo <<eos.gsub(' ', '*')
  176. | 42"))
  177. (ert-deftest ruby-indent-array-literal ()
  178. (let ((ruby-deep-indent-paren nil))
  179. (ruby-should-indent-buffer
  180. "foo = [
  181. | bar
  182. |]
  183. |"
  184. "foo = [
  185. | bar
  186. | ]
  187. |"))
  188. (ruby-should-indent-buffer
  189. "foo do
  190. | [bar]
  191. |end
  192. |"
  193. "foo do
  194. |[bar]
  195. | end
  196. |"))
  197. (ert-deftest ruby-indent-begin-end ()
  198. (ruby-should-indent-buffer
  199. "begin
  200. | a[b]
  201. |end
  202. |"
  203. "begin
  204. | a[b]
  205. | end
  206. |"))
  207. (ert-deftest ruby-indent-array-after-paren-and-space ()
  208. (ruby-should-indent-buffer
  209. "class A
  210. | def foo
  211. | foo( [])
  212. | end
  213. |end
  214. |"
  215. "class A
  216. | def foo
  217. |foo( [])
  218. |end
  219. | end
  220. |"))
  221. (ert-deftest ruby-indent-after-block-in-continued-expression ()
  222. (ruby-should-indent-buffer
  223. "var =
  224. | begin
  225. | val
  226. | end
  227. |statement"
  228. "var =
  229. |begin
  230. |val
  231. |end
  232. |statement"))
  233. (ert-deftest ruby-indent-spread-args-in-parens ()
  234. (let ((ruby-deep-indent-paren '(?\()))
  235. (ruby-should-indent-buffer
  236. "foo(1,
  237. | 2,
  238. | 3)
  239. |"
  240. "foo(1,
  241. | 2,
  242. | 3)
  243. |")))
  244. (ert-deftest ruby-align-to-stmt-keywords-t ()
  245. (let ((ruby-align-to-stmt-keywords t))
  246. (ruby-should-indent-buffer
  247. "foo = if bar?
  248. | 1
  249. |else
  250. | 2
  251. |end
  252. |
  253. |foo || begin
  254. | bar
  255. |end
  256. |
  257. |foo ||
  258. | begin
  259. | bar
  260. | end
  261. |"
  262. "foo = if bar?
  263. | 1
  264. |else
  265. | 2
  266. | end
  267. |
  268. | foo || begin
  269. | bar
  270. |end
  271. |
  272. | foo ||
  273. | begin
  274. |bar
  275. | end
  276. |")
  277. ))
  278. (ert-deftest ruby-align-to-stmt-keywords-case ()
  279. (let ((ruby-align-to-stmt-keywords '(case)))
  280. (ruby-should-indent-buffer
  281. "b = case a
  282. |when 13
  283. | 6
  284. |else
  285. | 42
  286. |end"
  287. "b = case a
  288. | when 13
  289. | 6
  290. | else
  291. | 42
  292. | end")))
  293. (ert-deftest ruby-align-chained-calls ()
  294. (let ((ruby-align-chained-calls t))
  295. (ruby-should-indent-buffer
  296. "one.two.three
  297. | .four
  298. |
  299. |my_array.select { |str| str.size > 5 }
  300. | .map { |str| str.downcase }"
  301. "one.two.three
  302. | .four
  303. |
  304. |my_array.select { |str| str.size > 5 }
  305. | .map { |str| str.downcase }")))
  306. (ert-deftest ruby-move-to-block-stops-at-indentation ()
  307. (ruby-with-temp-buffer "def f\nend"
  308. (beginning-of-line)
  309. (ruby-move-to-block -1)
  310. (should (looking-at "^def"))))
  311. (ert-deftest ruby-toggle-block-to-do-end ()
  312. (ruby-with-temp-buffer "foo {|b|\n}"
  313. (beginning-of-line)
  314. (ruby-toggle-block)
  315. (should (string= "foo do |b|\nend" (buffer-string)))))
  316. (ert-deftest ruby-toggle-block-to-brace ()
  317. (let ((pairs '((17 . "foo { |b| b + 2 }")
  318. (16 . "foo { |b|\n b + 2\n}"))))
  319. (dolist (pair pairs)
  320. (with-temp-buffer
  321. (let ((fill-column (car pair)))
  322. (insert "foo do |b|\n b + 2\nend")
  323. (ruby-mode)
  324. (beginning-of-line)
  325. (ruby-toggle-block)
  326. (should (string= (cdr pair) (buffer-string))))))))
  327. (ert-deftest ruby-toggle-block-to-multiline ()
  328. (ruby-with-temp-buffer "foo {|b| b + 1}"
  329. (beginning-of-line)
  330. (ruby-toggle-block)
  331. (should (string= "foo do |b|\n b + 1\nend" (buffer-string)))))
  332. (ert-deftest ruby-toggle-block-with-interpolation ()
  333. (ruby-with-temp-buffer "foo do\n \"#{bar}\"\nend"
  334. (beginning-of-line)
  335. (ruby-toggle-block)
  336. (should (string= "foo { \"#{bar}\" }" (buffer-string)))))
  337. (ert-deftest ruby-recognize-symbols-starting-with-at-character ()
  338. (ruby-assert-face ":@abc" 3 font-lock-constant-face))
  339. (ert-deftest ruby-hash-character-not-interpolation ()
  340. (ruby-assert-face "\"This is #{interpolation}\"" 15
  341. font-lock-variable-name-face)
  342. (ruby-assert-face "\"This is \\#{no interpolation} despite the #\""
  343. 15 font-lock-string-face)
  344. (ruby-assert-face "\n#@comment, not ruby code" 5 font-lock-comment-face)
  345. (ruby-assert-state "\n#@comment, not ruby code" 4 t)
  346. (ruby-assert-face "# A comment cannot have #{an interpolation} in it"
  347. 30 font-lock-comment-face)
  348. (ruby-assert-face "# #{comment}\n \"#{interpolation}\"" 16
  349. font-lock-variable-name-face))
  350. (ert-deftest ruby-interpolation-suppresses-quotes-inside ()
  351. (let ((s "\"<ul><li>#{@files.join(\"</li><li>\")}</li></ul>\""))
  352. (ruby-assert-state s 8 nil)
  353. (ruby-assert-face s 9 font-lock-string-face)
  354. (ruby-assert-face s 10 font-lock-variable-name-face)
  355. (ruby-assert-face s 41 font-lock-string-face)))
  356. (ert-deftest ruby-interpolation-suppresses-one-double-quote ()
  357. (let ((s "\"foo#{'\"'}\""))
  358. (ruby-assert-state s 8 nil)
  359. (ruby-assert-face s 8 font-lock-variable-name-face)
  360. (ruby-assert-face s 11 font-lock-string-face)))
  361. (ert-deftest ruby-interpolation-suppresses-one-backtick ()
  362. (let ((s "`as#{'`'}das`"))
  363. (ruby-assert-state s 8 nil)))
  364. (ert-deftest ruby-interpolation-keeps-non-quote-syntax ()
  365. (let ((s "\"foo#{baz.tee}bar\""))
  366. (ruby-with-temp-buffer s
  367. (goto-char (point-min))
  368. (ruby-mode)
  369. (syntax-propertize (point-max))
  370. (search-forward "tee")
  371. (should (string= (thing-at-point 'symbol) "tee")))))
  372. (ert-deftest ruby-interpolation-inside-percent-literal ()
  373. (let ((s "%( #{boo} )"))
  374. (ruby-assert-face s 1 font-lock-string-face)
  375. (ruby-assert-face s 4 font-lock-variable-name-face)
  376. (ruby-assert-face s 10 font-lock-string-face)
  377. (ruby-assert-state s 8 nil)))
  378. (ert-deftest ruby-interpolation-inside-percent-literal-with-paren ()
  379. :expected-result :failed
  380. (let ((s "%(^#{\")\"}^)"))
  381. (ruby-assert-face s 3 font-lock-string-face)
  382. (ruby-assert-face s 4 font-lock-variable-name-face)
  383. (ruby-assert-face s 10 font-lock-string-face)
  384. ;; It's confused by the closing paren in the middle.
  385. (ruby-assert-state s 8 nil)))
  386. (ert-deftest ruby-interpolation-inside-another-interpolation ()
  387. :expected-result :failed
  388. (let ((s "\"#{[a, b, c].map { |v| \"#{v}\" }.join}\""))
  389. (ruby-assert-face s 1 font-lock-string-face)
  390. (ruby-assert-face s 2 font-lock-variable-name-face)
  391. (ruby-assert-face s 38 font-lock-string-face)
  392. (ruby-assert-state s 8 nil)))
  393. (ert-deftest ruby-interpolation-inside-double-quoted-percent-literals ()
  394. (ruby-assert-face "%Q{foo #@bar}" 8 font-lock-variable-name-face)
  395. (ruby-assert-face "%W{foo #@bar}" 8 font-lock-variable-name-face)
  396. (ruby-assert-face "%r{foo #@bar}" 8 font-lock-variable-name-face)
  397. (ruby-assert-face "%x{foo #@bar}" 8 font-lock-variable-name-face))
  398. (ert-deftest ruby-no-interpolation-in-single-quoted-literals ()
  399. (ruby-assert-face "'foo #@bar'" 7 font-lock-string-face)
  400. (ruby-assert-face "%q{foo #@bar}" 8 font-lock-string-face)
  401. (ruby-assert-face "%w{foo #@bar}" 8 font-lock-string-face)
  402. (ruby-assert-face "%s{foo #@bar}" 8 font-lock-string-face))
  403. (ert-deftest ruby-interpolation-after-dollar-sign ()
  404. (ruby-assert-face "\"$#{balance}\"" 2 'font-lock-string-face)
  405. (ruby-assert-face "\"$#{balance}\"" 3 'font-lock-variable-name-face))
  406. (ert-deftest ruby-no-unknown-percent-literals ()
  407. ;; No folding of case.
  408. (ruby-assert-face "%S{foo}" 4 nil)
  409. (ruby-assert-face "%R{foo}" 4 nil))
  410. (ert-deftest ruby-no-nested-percent-literals ()
  411. (ruby-with-temp-buffer "a = %w[b %()]"
  412. (syntax-propertize (point))
  413. (should (null (nth 8 (syntax-ppss))))
  414. (should (eq t (nth 3 (syntax-ppss (1- (point-max))))))
  415. (search-backward "[")
  416. (should (eq t (nth 3 (syntax-ppss))))))
  417. (ert-deftest ruby-add-log-current-method-examples ()
  418. (let ((pairs '(("foo" . "#foo")
  419. ("C.foo" . ".foo")
  420. ("self.foo" . ".foo"))))
  421. (dolist (pair pairs)
  422. (let ((name (car pair))
  423. (value (cdr pair)))
  424. (ruby-with-temp-buffer (ruby-test-string
  425. "module M
  426. | class C
  427. | def %s
  428. | _
  429. | end
  430. | end
  431. |end"
  432. name)
  433. (search-backward "_")
  434. (forward-line)
  435. (should (string= (ruby-add-log-current-method)
  436. (format "M::C%s" value))))))))
  437. (ert-deftest ruby-add-log-current-method-outside-of-method ()
  438. (ruby-with-temp-buffer (ruby-test-string
  439. "module M
  440. | class C
  441. | def foo
  442. | end
  443. | _
  444. | end
  445. |end")
  446. (search-backward "_")
  447. (should (string= (ruby-add-log-current-method)"M::C"))))
  448. (ert-deftest ruby-add-log-current-method-in-singleton-class ()
  449. (ruby-with-temp-buffer (ruby-test-string
  450. "class C
  451. | class << self
  452. | def foo
  453. | _
  454. | end
  455. | end
  456. |end")
  457. (search-backward "_")
  458. (should (string= (ruby-add-log-current-method) "C.foo"))))
  459. (ert-deftest ruby-add-log-current-method-namespace-shorthand ()
  460. (ruby-with-temp-buffer (ruby-test-string
  461. "class C::D
  462. | def foo
  463. | _
  464. | end
  465. |end")
  466. (search-backward "_")
  467. (should (string= (ruby-add-log-current-method) "C::D#foo"))))
  468. (ert-deftest ruby-add-log-current-method-after-inner-class ()
  469. (ruby-with-temp-buffer (ruby-test-string
  470. "module M
  471. | class C
  472. | class D
  473. | end
  474. | def foo
  475. | _
  476. | end
  477. | end
  478. |end")
  479. (search-backward "_")
  480. (should (string= (ruby-add-log-current-method) "M::C#foo"))))
  481. (defvar ruby-block-test-example
  482. (ruby-test-string
  483. "class C
  484. | def foo
  485. | 1
  486. | end
  487. |
  488. | def bar
  489. | 2
  490. | end
  491. |
  492. | def baz
  493. |some do
  494. |3
  495. | end
  496. | end
  497. |end"))
  498. (defmacro ruby-deftest-move-to-block (name &rest body)
  499. (declare (indent defun))
  500. `(ert-deftest ,(intern (format "ruby-move-to-block-%s" name)) ()
  501. (with-temp-buffer
  502. (insert ruby-block-test-example)
  503. (ruby-mode)
  504. (goto-char (point-min))
  505. ,@body)))
  506. (ruby-deftest-move-to-block works-on-do
  507. (forward-line 10)
  508. (ruby-end-of-block)
  509. (should (= 13 (line-number-at-pos)))
  510. (ruby-beginning-of-block)
  511. (should (= 11 (line-number-at-pos))))
  512. (ruby-deftest-move-to-block zero-is-noop
  513. (forward-line 4)
  514. (ruby-move-to-block 0)
  515. (should (= 5 (line-number-at-pos))))
  516. (ruby-deftest-move-to-block ok-with-three
  517. (forward-line 1)
  518. (ruby-move-to-block 3)
  519. (should (= 14 (line-number-at-pos))))
  520. (ruby-deftest-move-to-block ok-with-minus-two
  521. (forward-line 9)
  522. (ruby-move-to-block -2)
  523. (should (= 2 (line-number-at-pos))))
  524. (ert-deftest ruby-move-to-block-skips-percent-literal ()
  525. (dolist (s (list (ruby-test-string
  526. "foo do
  527. | a = %%w(
  528. | def yaa
  529. | )
  530. |end")
  531. (ruby-test-string
  532. "foo do
  533. | a = %%w|
  534. | end
  535. | |
  536. |end")))
  537. (ruby-with-temp-buffer s
  538. (goto-char (point-min))
  539. (ruby-end-of-block)
  540. (should (= 5 (line-number-at-pos)))
  541. (ruby-beginning-of-block)
  542. (should (= 1 (line-number-at-pos))))))
  543. (ert-deftest ruby-move-to-block-skips-heredoc ()
  544. (ruby-with-temp-buffer
  545. (ruby-test-string
  546. "if something_wrong?
  547. | ActiveSupport::Deprecation.warn(<<-eowarn)
  548. | boo hoo
  549. | end
  550. | eowarn
  551. |end")
  552. (goto-char (point-min))
  553. (ruby-end-of-block)
  554. (should (= 6 (line-number-at-pos)))
  555. (ruby-beginning-of-block)
  556. (should (= 1 (line-number-at-pos)))))
  557. (ert-deftest ruby-move-to-block-does-not-fold-case ()
  558. (ruby-with-temp-buffer
  559. (ruby-test-string
  560. "foo do
  561. | Module.to_s
  562. |end")
  563. (let ((case-fold-search t))
  564. (ruby-beginning-of-block))
  565. (should (= 1 (line-number-at-pos)))))
  566. (ert-deftest ruby-move-to-block-moves-from-else-to-if ()
  567. (ruby-with-temp-buffer (ruby-test-string
  568. "if true
  569. | nested_block do
  570. | end
  571. |else
  572. |end")
  573. (goto-char (point-min))
  574. (forward-line 3)
  575. (ruby-beginning-of-block)
  576. (should (= 1 (line-number-at-pos)))))
  577. (ert-deftest ruby-beginning-of-defun-does-not-fold-case ()
  578. (ruby-with-temp-buffer
  579. (ruby-test-string
  580. "class C
  581. | def bar
  582. | Class.to_s
  583. | end
  584. |end")
  585. (goto-char (point-min))
  586. (forward-line 3)
  587. (let ((case-fold-search t))
  588. (beginning-of-defun))
  589. (should (= 2 (line-number-at-pos)))))
  590. (ert-deftest ruby-end-of-defun-skips-to-next-line-after-the-method ()
  591. (ruby-with-temp-buffer
  592. (ruby-test-string
  593. "class D
  594. | def tee
  595. | 'ho hum'
  596. | end
  597. |end")
  598. (goto-char (point-min))
  599. (forward-line 1)
  600. (end-of-defun)
  601. (should (= 5 (line-number-at-pos)))))
  602. (defvar ruby-sexp-test-example
  603. (ruby-test-string
  604. "class C
  605. | def foo
  606. | self.end
  607. | D.new.class
  608. | [1, 2, 3].map do |i|
  609. | i + 1
  610. | end.sum
  611. | end
  612. |end"))
  613. (ert-deftest ruby-forward-sexp-skips-method-calls-with-keyword-names ()
  614. (ruby-with-temp-buffer ruby-sexp-test-example
  615. (goto-line 2)
  616. (ruby-forward-sexp)
  617. (should (= 8 (line-number-at-pos)))))
  618. (ert-deftest ruby-backward-sexp-skips-method-calls-with-keyword-names ()
  619. (ruby-with-temp-buffer ruby-sexp-test-example
  620. (goto-line 8)
  621. (end-of-line)
  622. (ruby-backward-sexp)
  623. (should (= 2 (line-number-at-pos)))))
  624. (ert-deftest ruby--insert-coding-comment-ruby-style ()
  625. (with-temp-buffer
  626. (let ((ruby-encoding-magic-comment-style 'ruby))
  627. (ruby--insert-coding-comment "utf-8")
  628. (should (string= "# coding: utf-8\n" (buffer-string))))))
  629. (ert-deftest ruby--insert-coding-comment-emacs-style ()
  630. (with-temp-buffer
  631. (let ((ruby-encoding-magic-comment-style 'emacs))
  632. (ruby--insert-coding-comment "utf-8")
  633. (should (string= "# -*- coding: utf-8 -*-\n" (buffer-string))))))
  634. (ert-deftest ruby--insert-coding-comment-custom-style ()
  635. (with-temp-buffer
  636. (let ((ruby-encoding-magic-comment-style 'custom)
  637. (ruby-custom-encoding-magic-comment-template "# encoding: %s\n"))
  638. (ruby--insert-coding-comment "utf-8")
  639. (should (string= "# encoding: utf-8\n\n" (buffer-string))))))
  640. (provide 'ruby-mode-tests)
  641. ;;; ruby-mode-tests.el ends here