ruby-mode-tests.el 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  1. ;;; ruby-mode-tests.el --- Test suite for ruby-mode
  2. ;; Copyright (C) 2012-2017 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-regexp-is-not-mistaken-for-slash-symbol ()
  112. (ruby-assert-state "x = /foo:/" 3 nil))
  113. (ert-deftest ruby-indent-simple ()
  114. (ruby-should-indent-buffer
  115. "if foo
  116. | bar
  117. |end
  118. |zot
  119. |"
  120. "if foo
  121. |bar
  122. | end
  123. | zot
  124. |"))
  125. (ert-deftest ruby-indent-keyword-label ()
  126. (ruby-should-indent-buffer
  127. "bar(class: XXX) do
  128. | foo
  129. |end
  130. |bar
  131. |"
  132. "bar(class: XXX) do
  133. | foo
  134. | end
  135. | bar
  136. |"))
  137. (ert-deftest ruby-indent-method-with-question-mark ()
  138. (ruby-should-indent-buffer
  139. "if x.is_a?(XXX)
  140. | foo
  141. |end
  142. |"
  143. "if x.is_a?(XXX)
  144. | foo
  145. | end
  146. |"))
  147. (ert-deftest ruby-indent-expr-in-regexp ()
  148. (ruby-should-indent-buffer
  149. "if /#{foo}/ =~ s
  150. | x = 1
  151. |end
  152. |"
  153. "if /#{foo}/ =~ s
  154. | x = 1
  155. | end
  156. |"))
  157. (ert-deftest ruby-indent-singleton-class ()
  158. (ruby-should-indent-buffer
  159. "class<<bar
  160. | foo
  161. |end
  162. |"
  163. "class<<bar
  164. |foo
  165. | end
  166. |"))
  167. (ert-deftest ruby-indent-inside-heredoc-after-operator ()
  168. (ruby-should-indent-buffer
  169. "b=<<eos
  170. | 42"
  171. "b=<<eos
  172. | 42"))
  173. (ert-deftest ruby-indent-inside-heredoc-after-space ()
  174. (ruby-should-indent-buffer
  175. "foo <<eos.gsub(' ', '*')
  176. | 42"
  177. "foo <<eos.gsub(' ', '*')
  178. | 42"))
  179. (ert-deftest ruby-indent-array-literal ()
  180. (let ((ruby-deep-indent-paren nil))
  181. (ruby-should-indent-buffer
  182. "foo = [
  183. | bar
  184. |]
  185. |"
  186. "foo = [
  187. | bar
  188. | ]
  189. |"))
  190. (ruby-should-indent-buffer
  191. "foo do
  192. | [bar]
  193. |end
  194. |"
  195. "foo do
  196. |[bar]
  197. | end
  198. |"))
  199. (ert-deftest ruby-indent-begin-end ()
  200. (ruby-should-indent-buffer
  201. "begin
  202. | a[b]
  203. |end
  204. |"
  205. "begin
  206. | a[b]
  207. | end
  208. |"))
  209. (ert-deftest ruby-indent-array-after-paren-and-space ()
  210. (ruby-should-indent-buffer
  211. "class A
  212. | def foo
  213. | foo( [])
  214. | end
  215. |end
  216. |"
  217. "class A
  218. | def foo
  219. |foo( [])
  220. |end
  221. | end
  222. |"))
  223. (ert-deftest ruby-indent-after-block-in-continued-expression ()
  224. (ruby-should-indent-buffer
  225. "var =
  226. | begin
  227. | val
  228. | end
  229. |statement"
  230. "var =
  231. |begin
  232. |val
  233. |end
  234. |statement"))
  235. (ert-deftest ruby-indent-spread-args-in-parens ()
  236. (let ((ruby-deep-indent-paren '(?\()))
  237. (ruby-should-indent-buffer
  238. "foo(1,
  239. | 2,
  240. | 3)
  241. |"
  242. "foo(1,
  243. | 2,
  244. | 3)
  245. |")))
  246. (ert-deftest ruby-align-to-stmt-keywords-t ()
  247. (let ((ruby-align-to-stmt-keywords t))
  248. (ruby-should-indent-buffer
  249. "foo = if bar?
  250. | 1
  251. |else
  252. | 2
  253. |end
  254. |
  255. |foo || begin
  256. | bar
  257. |end
  258. |
  259. |foo ||
  260. | begin
  261. | bar
  262. | end
  263. |"
  264. "foo = if bar?
  265. | 1
  266. |else
  267. | 2
  268. | end
  269. |
  270. | foo || begin
  271. | bar
  272. |end
  273. |
  274. | foo ||
  275. | begin
  276. |bar
  277. | end
  278. |")
  279. ))
  280. (ert-deftest ruby-align-to-stmt-keywords-case ()
  281. (let ((ruby-align-to-stmt-keywords '(case)))
  282. (ruby-should-indent-buffer
  283. "b = case a
  284. |when 13
  285. | 6
  286. |else
  287. | 42
  288. |end"
  289. "b = case a
  290. | when 13
  291. | 6
  292. | else
  293. | 42
  294. | end")))
  295. (ert-deftest ruby-align-chained-calls ()
  296. (let ((ruby-align-chained-calls t))
  297. (ruby-should-indent-buffer
  298. "one.two.three
  299. | .four
  300. |
  301. |my_array.select { |str| str.size > 5 }
  302. | .map { |str| str.downcase }"
  303. "one.two.three
  304. | .four
  305. |
  306. |my_array.select { |str| str.size > 5 }
  307. | .map { |str| str.downcase }")))
  308. (ert-deftest ruby-move-to-block-stops-at-indentation ()
  309. (ruby-with-temp-buffer "def f\nend"
  310. (beginning-of-line)
  311. (ruby-move-to-block -1)
  312. (should (looking-at "^def"))))
  313. (ert-deftest ruby-toggle-block-to-do-end ()
  314. (ruby-with-temp-buffer "foo {|b|\n}"
  315. (beginning-of-line)
  316. (ruby-toggle-block)
  317. (should (string= "foo do |b|\nend" (buffer-string)))))
  318. (ert-deftest ruby-toggle-block-to-brace ()
  319. (let ((pairs '((17 . "foo { |b| b + 2 }")
  320. (16 . "foo { |b|\n b + 2\n}"))))
  321. (dolist (pair pairs)
  322. (with-temp-buffer
  323. (let ((fill-column (car pair)))
  324. (insert "foo do |b|\n b + 2\nend")
  325. (ruby-mode)
  326. (beginning-of-line)
  327. (ruby-toggle-block)
  328. (should (string= (cdr pair) (buffer-string))))))))
  329. (ert-deftest ruby-toggle-block-to-multiline ()
  330. (ruby-with-temp-buffer "foo {|b| b + 1}"
  331. (beginning-of-line)
  332. (ruby-toggle-block)
  333. (should (string= "foo do |b|\n b + 1\nend" (buffer-string)))))
  334. (ert-deftest ruby-toggle-block-with-interpolation ()
  335. (ruby-with-temp-buffer "foo do\n \"#{bar}\"\nend"
  336. (beginning-of-line)
  337. (ruby-toggle-block)
  338. (should (string= "foo { \"#{bar}\" }" (buffer-string)))))
  339. (ert-deftest ruby-recognize-symbols-starting-with-at-character ()
  340. (ruby-assert-face ":@abc" 3 font-lock-constant-face))
  341. (ert-deftest ruby-hash-character-not-interpolation ()
  342. (ruby-assert-face "\"This is #{interpolation}\"" 15
  343. font-lock-variable-name-face)
  344. (ruby-assert-face "\"This is \\#{no interpolation} despite the #\""
  345. 15 font-lock-string-face)
  346. (ruby-assert-face "\n#@comment, not ruby code" 5 font-lock-comment-face)
  347. (ruby-assert-state "\n#@comment, not ruby code" 4 t)
  348. (ruby-assert-face "# A comment cannot have #{an interpolation} in it"
  349. 30 font-lock-comment-face)
  350. (ruby-assert-face "# #{comment}\n \"#{interpolation}\"" 16
  351. font-lock-variable-name-face))
  352. (ert-deftest ruby-interpolation-suppresses-quotes-inside ()
  353. (let ((s "\"<ul><li>#{@files.join(\"</li><li>\")}</li></ul>\""))
  354. (ruby-assert-state s 8 nil)
  355. (ruby-assert-face s 9 font-lock-string-face)
  356. (ruby-assert-face s 10 font-lock-variable-name-face)
  357. (ruby-assert-face s 41 font-lock-string-face)))
  358. (ert-deftest ruby-interpolation-suppresses-one-double-quote ()
  359. (let ((s "\"foo#{'\"'}\""))
  360. (ruby-assert-state s 8 nil)
  361. (ruby-assert-face s 8 font-lock-variable-name-face)
  362. (ruby-assert-face s 11 font-lock-string-face)))
  363. (ert-deftest ruby-interpolation-suppresses-one-backtick ()
  364. (let ((s "`as#{'`'}das`"))
  365. (ruby-assert-state s 8 nil)))
  366. (ert-deftest ruby-interpolation-keeps-non-quote-syntax ()
  367. (let ((s "\"foo#{baz.tee}bar\""))
  368. (ruby-with-temp-buffer s
  369. (goto-char (point-min))
  370. (ruby-mode)
  371. (syntax-propertize (point-max))
  372. (search-forward "tee")
  373. (should (string= (thing-at-point 'symbol) "tee")))))
  374. (ert-deftest ruby-interpolation-inside-percent-literal ()
  375. (let ((s "%( #{boo} )"))
  376. (ruby-assert-face s 1 font-lock-string-face)
  377. (ruby-assert-face s 4 font-lock-variable-name-face)
  378. (ruby-assert-face s 10 font-lock-string-face)
  379. (ruby-assert-state s 8 nil)))
  380. (ert-deftest ruby-interpolation-inside-percent-literal-with-paren ()
  381. :expected-result :failed
  382. (let ((s "%(^#{\")\"}^)"))
  383. (ruby-assert-face s 3 font-lock-string-face)
  384. (ruby-assert-face s 4 font-lock-variable-name-face)
  385. (ruby-assert-face s 10 font-lock-string-face)
  386. ;; It's confused by the closing paren in the middle.
  387. (ruby-assert-state s 8 nil)))
  388. (ert-deftest ruby-interpolation-inside-another-interpolation ()
  389. :expected-result :failed
  390. (let ((s "\"#{[a, b, c].map { |v| \"#{v}\" }.join}\""))
  391. (ruby-assert-face s 1 font-lock-string-face)
  392. (ruby-assert-face s 2 font-lock-variable-name-face)
  393. (ruby-assert-face s 38 font-lock-string-face)
  394. (ruby-assert-state s 8 nil)))
  395. (ert-deftest ruby-interpolation-inside-double-quoted-percent-literals ()
  396. (ruby-assert-face "%Q{foo #@bar}" 8 font-lock-variable-name-face)
  397. (ruby-assert-face "%W{foo #@bar}" 8 font-lock-variable-name-face)
  398. (ruby-assert-face "%r{foo #@bar}" 8 font-lock-variable-name-face)
  399. (ruby-assert-face "%x{foo #@bar}" 8 font-lock-variable-name-face))
  400. (ert-deftest ruby-no-interpolation-in-single-quoted-literals ()
  401. (ruby-assert-face "'foo #@bar'" 7 font-lock-string-face)
  402. (ruby-assert-face "%q{foo #@bar}" 8 font-lock-string-face)
  403. (ruby-assert-face "%w{foo #@bar}" 8 font-lock-string-face)
  404. (ruby-assert-face "%s{foo #@bar}" 8 font-lock-string-face))
  405. (ert-deftest ruby-interpolation-after-dollar-sign ()
  406. (ruby-assert-face "\"$#{balance}\"" 2 'font-lock-string-face)
  407. (ruby-assert-face "\"$#{balance}\"" 3 'font-lock-variable-name-face))
  408. (ert-deftest ruby-no-unknown-percent-literals ()
  409. ;; No folding of case.
  410. (ruby-assert-face "%S{foo}" 4 nil)
  411. (ruby-assert-face "%R{foo}" 4 nil))
  412. (ert-deftest ruby-no-nested-percent-literals ()
  413. (ruby-with-temp-buffer "a = %w[b %()]"
  414. (syntax-propertize (point))
  415. (should (null (nth 8 (syntax-ppss))))
  416. (should (eq t (nth 3 (syntax-ppss (1- (point-max))))))
  417. (search-backward "[")
  418. (should (eq t (nth 3 (syntax-ppss))))))
  419. (ert-deftest ruby-add-log-current-method-examples ()
  420. (let ((pairs '(("foo" . "#foo")
  421. ("C.foo" . ".foo")
  422. ("self.foo" . ".foo"))))
  423. (dolist (pair pairs)
  424. (let ((name (car pair))
  425. (value (cdr pair)))
  426. (ruby-with-temp-buffer (ruby-test-string
  427. "module M
  428. | class C
  429. | def %s
  430. | _
  431. | end
  432. | end
  433. |end"
  434. name)
  435. (search-backward "_")
  436. (forward-line)
  437. (should (string= (ruby-add-log-current-method)
  438. (format "M::C%s" value))))))))
  439. (ert-deftest ruby-add-log-current-method-outside-of-method ()
  440. (ruby-with-temp-buffer (ruby-test-string
  441. "module M
  442. | class C
  443. | def foo
  444. | end
  445. | _
  446. | end
  447. |end")
  448. (search-backward "_")
  449. (should (string= (ruby-add-log-current-method)"M::C"))))
  450. (ert-deftest ruby-add-log-current-method-in-singleton-class ()
  451. (ruby-with-temp-buffer (ruby-test-string
  452. "class C
  453. | class << self
  454. | def foo
  455. | _
  456. | end
  457. | end
  458. |end")
  459. (search-backward "_")
  460. (should (string= (ruby-add-log-current-method) "C.foo"))))
  461. (ert-deftest ruby-add-log-current-method-namespace-shorthand ()
  462. (ruby-with-temp-buffer (ruby-test-string
  463. "class C::D
  464. | def foo
  465. | _
  466. | end
  467. |end")
  468. (search-backward "_")
  469. (should (string= (ruby-add-log-current-method) "C::D#foo"))))
  470. (ert-deftest ruby-add-log-current-method-after-inner-class ()
  471. (ruby-with-temp-buffer (ruby-test-string
  472. "module M
  473. | class C
  474. | class D
  475. | end
  476. | def foo
  477. | _
  478. | end
  479. | end
  480. |end")
  481. (search-backward "_")
  482. (should (string= (ruby-add-log-current-method) "M::C#foo"))))
  483. (defvar ruby-block-test-example
  484. (ruby-test-string
  485. "class C
  486. | def foo
  487. | 1
  488. | end
  489. |
  490. | def bar
  491. | 2
  492. | end
  493. |
  494. | def baz
  495. |some do
  496. |3
  497. | end
  498. | end
  499. |end"))
  500. (defmacro ruby-deftest-move-to-block (name &rest body)
  501. (declare (indent defun))
  502. `(ert-deftest ,(intern (format "ruby-move-to-block-%s" name)) ()
  503. (with-temp-buffer
  504. (insert ruby-block-test-example)
  505. (ruby-mode)
  506. (goto-char (point-min))
  507. ,@body)))
  508. (ruby-deftest-move-to-block works-on-do
  509. (forward-line 10)
  510. (ruby-end-of-block)
  511. (should (= 13 (line-number-at-pos)))
  512. (ruby-beginning-of-block)
  513. (should (= 11 (line-number-at-pos))))
  514. (ruby-deftest-move-to-block zero-is-noop
  515. (forward-line 4)
  516. (ruby-move-to-block 0)
  517. (should (= 5 (line-number-at-pos))))
  518. (ruby-deftest-move-to-block ok-with-three
  519. (forward-line 1)
  520. (ruby-move-to-block 3)
  521. (should (= 14 (line-number-at-pos))))
  522. (ruby-deftest-move-to-block ok-with-minus-two
  523. (forward-line 9)
  524. (ruby-move-to-block -2)
  525. (should (= 2 (line-number-at-pos))))
  526. (ert-deftest ruby-move-to-block-skips-percent-literal ()
  527. (dolist (s (list (ruby-test-string
  528. "foo do
  529. | a = %%w(
  530. | def yaa
  531. | )
  532. |end")
  533. (ruby-test-string
  534. "foo do
  535. | a = %%w|
  536. | end
  537. | |
  538. |end")))
  539. (ruby-with-temp-buffer s
  540. (goto-char (point-min))
  541. (ruby-end-of-block)
  542. (should (= 5 (line-number-at-pos)))
  543. (ruby-beginning-of-block)
  544. (should (= 1 (line-number-at-pos))))))
  545. (ert-deftest ruby-move-to-block-skips-heredoc ()
  546. (ruby-with-temp-buffer
  547. (ruby-test-string
  548. "if something_wrong?
  549. | ActiveSupport::Deprecation.warn(<<-eowarn)
  550. | boo hoo
  551. | end
  552. | eowarn
  553. |end")
  554. (goto-char (point-min))
  555. (ruby-end-of-block)
  556. (should (= 6 (line-number-at-pos)))
  557. (ruby-beginning-of-block)
  558. (should (= 1 (line-number-at-pos)))))
  559. (ert-deftest ruby-move-to-block-does-not-fold-case ()
  560. (ruby-with-temp-buffer
  561. (ruby-test-string
  562. "foo do
  563. | Module.to_s
  564. |end")
  565. (let ((case-fold-search t))
  566. (ruby-beginning-of-block))
  567. (should (= 1 (line-number-at-pos)))))
  568. (ert-deftest ruby-move-to-block-moves-from-else-to-if ()
  569. (ruby-with-temp-buffer (ruby-test-string
  570. "if true
  571. | nested_block do
  572. | end
  573. |else
  574. |end")
  575. (goto-char (point-min))
  576. (forward-line 3)
  577. (ruby-beginning-of-block)
  578. (should (= 1 (line-number-at-pos)))))
  579. (ert-deftest ruby-beginning-of-defun-does-not-fold-case ()
  580. (ruby-with-temp-buffer
  581. (ruby-test-string
  582. "class C
  583. | def bar
  584. | Class.to_s
  585. | end
  586. |end")
  587. (goto-char (point-min))
  588. (forward-line 3)
  589. (let ((case-fold-search t))
  590. (beginning-of-defun))
  591. (should (= 2 (line-number-at-pos)))))
  592. (ert-deftest ruby-end-of-defun-skips-to-next-line-after-the-method ()
  593. (ruby-with-temp-buffer
  594. (ruby-test-string
  595. "class D
  596. | def tee
  597. | 'ho hum'
  598. | end
  599. |end")
  600. (goto-char (point-min))
  601. (forward-line 1)
  602. (end-of-defun)
  603. (should (= 5 (line-number-at-pos)))))
  604. (defvar ruby-sexp-test-example
  605. (ruby-test-string
  606. "class C
  607. | def foo
  608. | self.end
  609. | D.new.class
  610. | [1, 2, 3].map do |i|
  611. | i + 1
  612. | end.sum
  613. | end
  614. |end"))
  615. (ert-deftest ruby-forward-sexp-skips-method-calls-with-keyword-names ()
  616. (ruby-with-temp-buffer ruby-sexp-test-example
  617. (goto-line 2)
  618. (ruby-forward-sexp)
  619. (should (= 8 (line-number-at-pos)))))
  620. (ert-deftest ruby-backward-sexp-skips-method-calls-with-keyword-names ()
  621. (ruby-with-temp-buffer ruby-sexp-test-example
  622. (goto-line 8)
  623. (end-of-line)
  624. (ruby-backward-sexp)
  625. (should (= 2 (line-number-at-pos)))))
  626. (ert-deftest ruby-toggle-string-quotes-quotes-correctly ()
  627. (let ((pairs
  628. '(("puts '\"foo\"\\''" . "puts \"\\\"foo\\\"'\"")
  629. ("puts \"'foo'\\\"\"" . "puts '\\'foo\\'\"'"))))
  630. (dolist (pair pairs)
  631. (ruby-with-temp-buffer (car pair)
  632. (beginning-of-line)
  633. (search-forward "foo")
  634. (ruby-toggle-string-quotes)
  635. (should (string= (buffer-string) (cdr pair)))))))
  636. (ert-deftest ruby--insert-coding-comment-ruby-style ()
  637. (with-temp-buffer
  638. (let ((ruby-encoding-magic-comment-style 'ruby))
  639. (ruby--insert-coding-comment "utf-8")
  640. (should (string= "# coding: utf-8\n" (buffer-string))))))
  641. (ert-deftest ruby--insert-coding-comment-emacs-style ()
  642. (with-temp-buffer
  643. (let ((ruby-encoding-magic-comment-style 'emacs))
  644. (ruby--insert-coding-comment "utf-8")
  645. (should (string= "# -*- coding: utf-8 -*-\n" (buffer-string))))))
  646. (ert-deftest ruby--insert-coding-comment-custom-style ()
  647. (with-temp-buffer
  648. (let ((ruby-encoding-magic-comment-style 'custom)
  649. (ruby-custom-encoding-magic-comment-template "# encoding: %s\n"))
  650. (ruby--insert-coding-comment "utf-8")
  651. (should (string= "# encoding: utf-8\n\n" (buffer-string))))))
  652. (provide 'ruby-mode-tests)
  653. ;;; ruby-mode-tests.el ends here