2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2011-03-08 09:40:33 -05:00
|
|
|
require 'test/unit'
|
|
|
|
require 'tempfile'
|
|
|
|
|
|
|
|
class TestRubyMode < Test::Unit::TestCase
|
|
|
|
MISCDIR = File.expand_path("../../../misc", __FILE__)
|
2011-06-14 23:28:47 -04:00
|
|
|
e = ENV["EMACS"] || "emacs"
|
|
|
|
emacs = %W"#{e} -q --no-site-file --batch --load #{MISCDIR}/ruby-mode.el"
|
2011-03-11 04:33:56 -05:00
|
|
|
begin
|
2011-06-14 23:28:47 -04:00
|
|
|
raise if IO.popen([e, "--version", :err=>[:child, :out]]) {|f| f.read}[/[0-9]+/].to_i < 23
|
2011-03-11 04:33:56 -05:00
|
|
|
IO.popen([*emacs, :err=>[:child, :out]]) {|f| f.read}
|
|
|
|
rescue
|
2011-03-08 09:40:33 -05:00
|
|
|
EMACS = nil
|
2011-03-11 04:33:56 -05:00
|
|
|
else
|
|
|
|
EMACS = (emacs if $? and $?.success?)
|
2011-03-08 09:40:33 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class TestRubyMode
|
|
|
|
EVAL_OPT = "--eval"
|
2016-09-23 21:18:34 -04:00
|
|
|
EXPR_SAVE = "(save-buffer 0)"
|
2016-09-23 21:19:43 -04:00
|
|
|
finish_mark = "ok-#{$$}"
|
|
|
|
FINISH_MARK = /^#{finish_mark}$/
|
|
|
|
EXPR_FINISH = "(print \'#{finish_mark})"
|
2011-03-08 09:40:33 -05:00
|
|
|
EXPR_RUBYMODE = "(ruby-mode)"
|
|
|
|
|
|
|
|
def run_emacs(src, *exprs)
|
|
|
|
tmp = Tempfile.new(%w"ruby-mode.test. .rb")
|
|
|
|
tmp.puts(src)
|
|
|
|
tmp.close
|
|
|
|
exprs = exprs.map {|expr| [EVAL_OPT, expr]}.flatten
|
|
|
|
exprs.unshift(EVAL_OPT, EXPR_RUBYMODE)
|
2016-09-23 21:18:34 -04:00
|
|
|
exprs.push(EVAL_OPT, EXPR_SAVE)
|
2016-09-23 21:19:43 -04:00
|
|
|
exprs.push(EVAL_OPT, EXPR_FINISH)
|
2011-03-08 09:40:33 -05:00
|
|
|
output = IO.popen([*EMACS, tmp.path, *exprs, err:[:child, :out]], "r") {|e| e.read}
|
|
|
|
tmp.open
|
|
|
|
result = tmp.read
|
|
|
|
return result, output
|
2014-08-16 02:22:48 -04:00
|
|
|
ensure
|
|
|
|
tmp.close!
|
2011-03-08 09:40:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
class TestIndent < self
|
|
|
|
EXPR_INDENT = "(indent-region (point-min) (point-max))"
|
|
|
|
|
|
|
|
def assert_indent(expected, source, *message)
|
|
|
|
if space = expected[/\A\n?(\s*\|)/, 1]
|
|
|
|
space = /^#{Regexp.quote(space)}/m
|
|
|
|
expected.gsub!(space, '')
|
|
|
|
source.gsub!(space, '')
|
|
|
|
end
|
2016-09-23 21:18:34 -04:00
|
|
|
result, output = run_emacs(source, EXPR_INDENT)
|
2016-09-23 21:19:43 -04:00
|
|
|
assert_match(FINISH_MARK, output)
|
2014-08-16 02:22:48 -04:00
|
|
|
assert_equal(expected, result, message(*message) {diff expected, result})
|
2011-03-08 09:40:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_simple
|
|
|
|
assert_indent('
|
|
|
|
|if foo
|
|
|
|
| bar
|
|
|
|
|end
|
|
|
|
|zot
|
|
|
|
|', '
|
|
|
|
|if foo
|
|
|
|
|bar
|
|
|
|
| end
|
|
|
|
| zot
|
|
|
|
|')
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_keyword_label
|
|
|
|
assert_indent('
|
|
|
|
|bar(class: XXX) do
|
|
|
|
| foo
|
|
|
|
|end
|
|
|
|
|bar
|
|
|
|
|', '
|
|
|
|
|bar(class: XXX) do
|
|
|
|
| foo
|
|
|
|
| end
|
|
|
|
| bar
|
|
|
|
|')
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_method_with_question_mark
|
|
|
|
assert_indent('
|
|
|
|
|if x.is_a?(XXX)
|
|
|
|
| foo
|
|
|
|
|end
|
|
|
|
|', '
|
|
|
|
|if x.is_a?(XXX)
|
|
|
|
| foo
|
|
|
|
| end
|
|
|
|
|')
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_expr_in_regexp
|
|
|
|
assert_indent('
|
|
|
|
|if /#{foo}/ =~ s
|
|
|
|
| x = 1
|
|
|
|
|end
|
|
|
|
|', '
|
|
|
|
|if /#{foo}/ =~ s
|
|
|
|
| x = 1
|
|
|
|
| end
|
|
|
|
|')
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_singleton_class
|
2011-03-14 20:53:31 -04:00
|
|
|
skip("pending")
|
2011-03-08 09:40:33 -05:00
|
|
|
assert_indent('
|
|
|
|
|class<<bar
|
|
|
|
| foo
|
|
|
|
|end
|
|
|
|
|', '
|
|
|
|
|class<<bar
|
|
|
|
|foo
|
|
|
|
| end
|
|
|
|
|')
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_array_literal
|
|
|
|
assert_indent('
|
|
|
|
|foo = [
|
|
|
|
| bar
|
|
|
|
|]
|
|
|
|
|', '
|
|
|
|
|foo = [
|
|
|
|
| bar
|
|
|
|
| ]
|
|
|
|
|')
|
|
|
|
assert_indent('
|
|
|
|
|foo do
|
|
|
|
| [bar]
|
|
|
|
|end
|
|
|
|
|', '
|
|
|
|
|foo do
|
|
|
|
|[bar]
|
|
|
|
| end
|
|
|
|
|')
|
|
|
|
end
|
2011-03-09 09:06:15 -05:00
|
|
|
|
2011-03-09 21:15:25 -05:00
|
|
|
def test_begin_end
|
2011-03-09 09:06:15 -05:00
|
|
|
assert_indent('
|
|
|
|
|begin
|
|
|
|
| a[b]
|
|
|
|
|end
|
|
|
|
|', '
|
|
|
|
|begin
|
|
|
|
| a[b]
|
|
|
|
| end
|
|
|
|
|')
|
|
|
|
end
|
2011-04-19 11:02:16 -04:00
|
|
|
|
|
|
|
def test_array_after_paren_and_space
|
|
|
|
assert_indent('
|
|
|
|
|class A
|
|
|
|
| def foo
|
|
|
|
| foo( [])
|
|
|
|
| end
|
|
|
|
|end
|
|
|
|
|', '
|
|
|
|
|class A
|
|
|
|
| def foo
|
|
|
|
|foo( [])
|
|
|
|
|end
|
|
|
|
| end
|
|
|
|
|')
|
|
|
|
end
|
2012-12-02 22:43:40 -05:00
|
|
|
|
|
|
|
def test_spread_arguments
|
|
|
|
assert_indent('
|
|
|
|
|foo(1,
|
|
|
|
| 2,
|
|
|
|
| 3)
|
|
|
|
|', '
|
|
|
|
|foo(1,
|
|
|
|
| 2,
|
|
|
|
| 3)
|
|
|
|
|')
|
|
|
|
end
|
2011-03-08 09:40:33 -05:00
|
|
|
end
|
|
|
|
end if TestRubyMode::EMACS
|