2012-07-23 19:41:08 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2012-08-10 14:23:39 -04:00
|
|
|
describe Mutant, 'method matching' do
|
|
|
|
after do
|
|
|
|
if defined?(::Foo)
|
|
|
|
Object.send(:remove_const, 'Foo')
|
2012-07-23 19:41:08 -04:00
|
|
|
end
|
2012-08-10 14:23:39 -04:00
|
|
|
end
|
2012-07-23 19:41:08 -04:00
|
|
|
|
2012-08-10 14:23:39 -04:00
|
|
|
before do
|
|
|
|
eval(body)
|
|
|
|
File.stub(:read => body)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:defaults) { {} }
|
|
|
|
|
|
|
|
context 'on instance methods' do
|
2012-08-16 13:10:24 -04:00
|
|
|
def name(node)
|
|
|
|
node.name
|
|
|
|
end
|
|
|
|
|
|
|
|
def arguments(node)
|
|
|
|
node.arguments
|
|
|
|
end
|
|
|
|
|
2012-08-10 14:23:39 -04:00
|
|
|
let(:pattern) { 'Foo#bar' }
|
|
|
|
let(:defaults) do
|
|
|
|
{
|
2012-08-15 22:10:54 -04:00
|
|
|
:scope => Foo,
|
2012-08-10 14:23:39 -04:00
|
|
|
:node_class => Rubinius::AST::Define,
|
|
|
|
:method_name => :bar,
|
|
|
|
:method_arity => 0
|
|
|
|
}
|
2012-07-26 13:25:23 -04:00
|
|
|
end
|
2012-08-01 07:30:08 -04:00
|
|
|
|
2012-08-10 14:23:39 -04:00
|
|
|
context 'when method is defined once' do
|
|
|
|
let(:body) do
|
|
|
|
<<-RUBY
|
|
|
|
class Foo
|
|
|
|
def bar; end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
2012-07-23 19:41:08 -04:00
|
|
|
|
2012-08-10 14:23:39 -04:00
|
|
|
let(:expectation) do
|
|
|
|
{ :method_line => 2 }
|
2012-07-23 19:41:08 -04:00
|
|
|
end
|
|
|
|
|
2012-08-10 14:23:39 -04:00
|
|
|
it_should_behave_like 'a method match'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when method is defined multiple times' do
|
|
|
|
context 'on differend lines' do
|
2012-07-26 13:25:23 -04:00
|
|
|
let(:body) do
|
2012-07-30 22:10:37 -04:00
|
|
|
<<-RUBY
|
2012-07-26 13:25:23 -04:00
|
|
|
class Foo
|
|
|
|
def bar; end
|
2012-08-10 14:23:39 -04:00
|
|
|
def bar(arg); end
|
2012-07-26 13:25:23 -04:00
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
2012-07-23 19:41:08 -04:00
|
|
|
|
2012-07-26 13:25:23 -04:00
|
|
|
let(:expectation) do
|
2012-08-10 14:23:39 -04:00
|
|
|
{
|
|
|
|
:method_line => 3,
|
|
|
|
:method_arity => 1
|
|
|
|
}
|
2012-07-26 13:25:23 -04:00
|
|
|
end
|
2012-07-23 19:41:08 -04:00
|
|
|
|
2012-07-26 13:25:23 -04:00
|
|
|
it_should_behave_like 'a method match'
|
|
|
|
end
|
2012-07-23 19:41:08 -04:00
|
|
|
|
2012-08-10 14:23:39 -04:00
|
|
|
context 'on the same line' do
|
|
|
|
let(:body) do
|
|
|
|
<<-RUBY
|
|
|
|
class Foo
|
|
|
|
def bar; end; def bar(arg); end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
2012-07-23 19:41:08 -04:00
|
|
|
|
2012-08-10 14:23:39 -04:00
|
|
|
let(:expectation) do
|
|
|
|
{
|
|
|
|
:method_line => 2,
|
|
|
|
:method_arity => 1
|
|
|
|
}
|
|
|
|
end
|
2012-07-23 19:41:08 -04:00
|
|
|
|
2012-08-10 14:23:39 -04:00
|
|
|
it_should_behave_like 'a method match'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'on the same line with differend scope' do
|
|
|
|
let(:body) do
|
|
|
|
<<-RUBY
|
|
|
|
class Foo
|
|
|
|
def self.bar; end; def bar(arg); end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:expectation) do
|
|
|
|
{
|
|
|
|
:method_line => 2,
|
|
|
|
:method_arity => 1
|
|
|
|
}
|
2012-07-26 13:25:23 -04:00
|
|
|
end
|
2012-07-23 19:41:08 -04:00
|
|
|
|
2012-08-10 14:23:39 -04:00
|
|
|
it_should_behave_like 'a method match'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when nested' do
|
|
|
|
let(:pattern) { 'Foo::Bar#baz' }
|
|
|
|
|
|
|
|
context 'in class' do
|
2012-07-26 13:25:23 -04:00
|
|
|
let(:body) do
|
2012-07-30 22:10:37 -04:00
|
|
|
<<-RUBY
|
2012-07-26 13:25:23 -04:00
|
|
|
class Foo
|
2012-08-10 14:23:39 -04:00
|
|
|
class Bar
|
|
|
|
def baz; end
|
|
|
|
end
|
2012-07-26 13:25:23 -04:00
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:expectation) do
|
|
|
|
{
|
2012-08-10 14:23:39 -04:00
|
|
|
:method_line => 3,
|
|
|
|
:method_name => :baz,
|
2012-08-15 22:10:54 -04:00
|
|
|
:scope => Foo::Bar
|
2012-07-26 13:25:23 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it_should_behave_like 'a method match'
|
|
|
|
end
|
|
|
|
|
2012-08-10 14:23:39 -04:00
|
|
|
context 'in module' do
|
2012-07-26 13:25:23 -04:00
|
|
|
let(:body) do
|
2012-07-30 22:10:37 -04:00
|
|
|
<<-RUBY
|
2012-08-10 14:23:39 -04:00
|
|
|
module Foo
|
|
|
|
class Bar
|
|
|
|
def baz; end
|
|
|
|
end
|
2012-07-26 13:25:23 -04:00
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:expectation) do
|
|
|
|
{
|
2012-08-10 14:23:39 -04:00
|
|
|
:method_line => 3,
|
|
|
|
:method_name => :baz,
|
2012-08-15 22:10:54 -04:00
|
|
|
:scope => Foo::Bar
|
2012-07-26 13:25:23 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it_should_behave_like 'a method match'
|
|
|
|
end
|
2012-08-10 14:23:39 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-07-26 13:25:23 -04:00
|
|
|
|
2012-08-10 14:23:39 -04:00
|
|
|
context 'on singleton methods' do
|
|
|
|
let(:pattern) { 'Foo.bar' }
|
|
|
|
let(:defaults) do
|
|
|
|
{
|
2012-08-15 22:10:54 -04:00
|
|
|
:scope => Foo,
|
2012-08-16 13:10:24 -04:00
|
|
|
:node_class => Rubinius::AST::DefineSingleton,
|
2012-08-10 14:23:39 -04:00
|
|
|
:method_arity => 0
|
|
|
|
}
|
|
|
|
end
|
2012-07-26 13:25:23 -04:00
|
|
|
|
2012-08-16 13:10:24 -04:00
|
|
|
def name(node)
|
|
|
|
node.body.name
|
|
|
|
end
|
|
|
|
|
|
|
|
def arguments(node)
|
|
|
|
node.body.arguments
|
|
|
|
end
|
|
|
|
|
2012-08-10 14:23:39 -04:00
|
|
|
context 'when defined on self' do
|
|
|
|
let(:body) do
|
|
|
|
<<-RUBY
|
|
|
|
class Foo
|
|
|
|
def self.bar; end
|
2012-07-26 13:25:23 -04:00
|
|
|
end
|
2012-08-10 14:23:39 -04:00
|
|
|
RUBY
|
2012-07-26 13:25:23 -04:00
|
|
|
end
|
2012-07-23 19:41:08 -04:00
|
|
|
|
2012-08-10 14:23:39 -04:00
|
|
|
|
|
|
|
let(:expectation) do
|
2012-07-26 13:25:23 -04:00
|
|
|
{
|
2012-08-10 14:23:39 -04:00
|
|
|
:method_name => :bar,
|
|
|
|
:method_line => 2,
|
2012-07-26 13:25:23 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2012-08-10 14:23:39 -04:00
|
|
|
it_should_behave_like 'a method match'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when defined on constant' do
|
|
|
|
|
|
|
|
context 'inside namespace' do
|
2012-07-26 13:25:23 -04:00
|
|
|
let(:body) do
|
|
|
|
<<-RUBY
|
|
|
|
class Foo
|
2012-08-10 14:23:39 -04:00
|
|
|
def Foo.bar; end
|
2012-07-26 13:25:23 -04:00
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
let(:expectation) do
|
|
|
|
{
|
|
|
|
:method_name => :bar,
|
|
|
|
:method_line => 2,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it_should_behave_like 'a method match'
|
|
|
|
end
|
|
|
|
|
2012-08-10 14:23:39 -04:00
|
|
|
context 'outside namespace' do
|
|
|
|
let(:body) do
|
|
|
|
<<-RUBY
|
|
|
|
class Foo; end;
|
|
|
|
def Foo.bar; end
|
|
|
|
RUBY
|
2012-07-26 13:25:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2012-08-10 14:23:39 -04:00
|
|
|
let(:expectation) do
|
|
|
|
{
|
|
|
|
:method_name => :bar,
|
|
|
|
:method_line => 2,
|
|
|
|
}
|
2012-07-26 13:25:23 -04:00
|
|
|
end
|
|
|
|
|
2012-08-10 14:23:39 -04:00
|
|
|
it_should_behave_like 'a method match'
|
|
|
|
end
|
|
|
|
end
|
2012-07-26 13:25:23 -04:00
|
|
|
|
2012-08-10 14:23:39 -04:00
|
|
|
context 'when defined multiple times in the same line' do
|
|
|
|
context 'with method on differend scope' do
|
|
|
|
let(:body) do
|
|
|
|
<<-RUBY
|
|
|
|
module Foo; end
|
2012-07-26 13:25:23 -04:00
|
|
|
|
2012-08-10 14:23:39 -04:00
|
|
|
module Bar
|
|
|
|
def self.baz; end; def Foo.baz(arg); end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
2012-07-26 13:25:23 -04:00
|
|
|
|
2012-08-10 14:23:39 -04:00
|
|
|
let(:pattern) { 'Bar.baz' }
|
2012-07-26 13:25:23 -04:00
|
|
|
|
2012-08-10 14:23:39 -04:00
|
|
|
let(:expectation) do
|
|
|
|
{
|
2012-08-15 22:10:54 -04:00
|
|
|
:scope => Bar,
|
2012-08-10 14:23:39 -04:00
|
|
|
:method_name => :baz,
|
|
|
|
:method_line => 4,
|
|
|
|
:method_arity => 0
|
|
|
|
}
|
2012-07-26 13:25:23 -04:00
|
|
|
end
|
2012-08-10 14:23:39 -04:00
|
|
|
|
|
|
|
it_should_behave_like 'a method match'
|
2012-07-26 13:25:23 -04:00
|
|
|
end
|
2012-07-23 19:41:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|