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