Skip to content

Commit

Permalink
Handle splat argument followed by regular arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
mvz committed Feb 7, 2014
1 parent da15cec commit 262270f
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 98 deletions.
3 changes: 3 additions & 0 deletions lib/ripper_ruby_parser/sexp_handlers/helper_methods.rb
Expand Up @@ -57,6 +57,9 @@ def generic_add_star exp
_, args, splatarg = exp.shift 3
items = handle_potentially_typeless_sexp args
items << s(:splat, process(splatarg))
until exp.empty?
items << process(exp.shift)
end
items
end

Expand Down
119 changes: 119 additions & 0 deletions test/unit/parser_method_calls_test.rb
Expand Up @@ -2,6 +2,125 @@

describe RipperRubyParser::Parser do
describe "#parse" do
describe "for method calls" do
describe "without a receiver" do
it "works without brackets" do
"foo bar".
must_be_parsed_as s(:call, nil, :foo,
s(:call, nil, :bar))
end

it "works with brackets" do
"foo(bar)".
must_be_parsed_as s(:call, nil, :foo,
s(:call, nil, :bar))
end

it "works with an empty parameter list and no brackets" do
"foo".
must_be_parsed_as s(:call, nil, :foo)
end

it "works with brackets around an empty parameter list" do
"foo()".
must_be_parsed_as s(:call, nil, :foo)
end

it "works for methods ending in a question mark" do
"foo?".
must_be_parsed_as s(:call, nil, :foo?)
end

it "works with nested calls without brackets" do
"foo bar baz".
must_be_parsed_as s(:call, nil, :foo,
s(:call, nil, :bar,
s(:call, nil, :baz)))
end

it "works with a non-final splat argument" do
"foo(bar, *baz, qux)".
must_be_parsed_as s(:call,
nil,
:foo,
s(:call, nil, :bar),
s(:splat, s(:call, nil, :baz)),
s(:call, nil, :qux))
end

it "works with a splat argument followed by several regular arguments" do
"foo(bar, *baz, qux, quuz)".
must_be_parsed_as s(:call,
nil,
:foo,
s(:call, nil, :bar),
s(:splat, s(:call, nil, :baz)),
s(:call, nil, :qux),
s(:call, nil, :quuz))
end
end

describe "with a receiver" do
it "works without brackets" do
"foo.bar baz".
must_be_parsed_as s(:call,
s(:call, nil, :foo),
:bar,
s(:call, nil, :baz))
end

it "works with brackets" do
"foo.bar(baz)".
must_be_parsed_as s(:call,
s(:call, nil, :foo),
:bar,
s(:call, nil, :baz))
end

it "works with brackets around a call with no brackets" do
"foo.bar(baz qux)".
must_be_parsed_as s(:call,
s(:call, nil, :foo),
:bar,
s(:call, nil, :baz,
s(:call, nil, :qux)))
end

it "works with nested calls without brackets" do
"foo.bar baz qux".
must_be_parsed_as s(:call,
s(:call, nil, :foo),
:bar,
s(:call, nil, :baz,
s(:call, nil, :qux)))
end
end

describe "with blocks" do
it "works for a do block" do
"foo.bar do baz; end".
must_be_parsed_as s(:iter,
s(:call,
s(:call, nil, :foo),
:bar),
s(:args),
s(:call, nil, :baz))
end

it "works for a do block with several statements" do
"foo.bar do baz; qux; end".
must_be_parsed_as s(:iter,
s(:call,
s(:call, nil, :foo),
:bar),
s(:args),
s(:block,
s(:call, nil, :baz),
s(:call, nil, :qux)))
end
end
end

describe "for calls to super" do
specify { "super".must_be_parsed_as s(:zsuper) }
specify { "super foo".must_be_parsed_as s(:super,
Expand Down
98 changes: 0 additions & 98 deletions test/unit/parser_test.rb
Expand Up @@ -419,104 +419,6 @@
end
end

describe "for method calls" do
describe "without a receiver" do
it "works without brackets" do
"foo bar".
must_be_parsed_as s(:call, nil, :foo,
s(:call, nil, :bar))
end

it "works with brackets" do
"foo(bar)".
must_be_parsed_as s(:call, nil, :foo,
s(:call, nil, :bar))
end

it "works with an empty parameter list and no brackets" do
"foo".
must_be_parsed_as s(:call, nil, :foo)
end

it "works with brackets around an empty parameter list" do
"foo()".
must_be_parsed_as s(:call, nil, :foo)
end

it "works for methods ending in a question mark" do
"foo?".
must_be_parsed_as s(:call, nil, :foo?)
end

it "works with nested calls without brackets" do
"foo bar baz".
must_be_parsed_as s(:call, nil, :foo,
s(:call, nil, :bar,
s(:call, nil, :baz)))
end
end

describe "with a receiver" do
it "works without brackets" do
"foo.bar baz".
must_be_parsed_as s(:call,
s(:call, nil, :foo),
:bar,
s(:call, nil, :baz))
end

it "works with brackets" do
"foo.bar(baz)".
must_be_parsed_as s(:call,
s(:call, nil, :foo),
:bar,
s(:call, nil, :baz))
end

it "works with brackets around a call with no brackets" do
"foo.bar(baz qux)".
must_be_parsed_as s(:call,
s(:call, nil, :foo),
:bar,
s(:call, nil, :baz,
s(:call, nil, :qux)))
end

it "works with nested calls without brackets" do
"foo.bar baz qux".
must_be_parsed_as s(:call,
s(:call, nil, :foo),
:bar,
s(:call, nil, :baz,
s(:call, nil, :qux)))
end
end

describe "with blocks" do
it "works for a do block" do
"foo.bar do baz; end".
must_be_parsed_as s(:iter,
s(:call,
s(:call, nil, :foo),
:bar),
s(:args),
s(:call, nil, :baz))
end

it "works for a do block with several statements" do
"foo.bar do baz; qux; end".
must_be_parsed_as s(:iter,
s(:call,
s(:call, nil, :foo),
:bar),
s(:args),
s(:block,
s(:call, nil, :baz),
s(:call, nil, :qux)))
end
end
end

describe "for blocks" do
it "works with no statements in the block body" do
"foo do; end".
Expand Down

0 comments on commit 262270f

Please sign in to comment.