free_mutant/spec/integration/mutant/method_matching_spec.rb
Markus Schirp 583668138e Add mutations on singleton methods
* Fixing singleton matcher to return Rubiniuy::AST::DefineSingleton
  and not Rubinius::AST::DefineSingletonScope
* Adjust counts of runner spec
2012-08-16 19:10:24 +02:00

268 lines
5.1 KiB
Ruby

require 'spec_helper'
describe Mutant, 'method matching' do
after do
if defined?(::Foo)
Object.send(:remove_const, 'Foo')
end
end
before do
eval(body)
File.stub(:read => body)
end
let(:defaults) { {} }
context 'on instance methods' do
def name(node)
node.name
end
def arguments(node)
node.arguments
end
let(:pattern) { 'Foo#bar' }
let(:defaults) do
{
:scope => Foo,
:node_class => Rubinius::AST::Define,
:method_name => :bar,
:method_arity => 0
}
end
context 'when method is defined once' do
let(:body) do
<<-RUBY
class Foo
def bar; end
end
RUBY
end
let(:expectation) do
{ :method_line => 2 }
end
it_should_behave_like 'a method match'
end
context 'when method is defined multiple times' do
context 'on differend lines' do
let(:body) do
<<-RUBY
class Foo
def bar; end
def bar(arg); end
end
RUBY
end
let(:expectation) do
{
:method_line => 3,
:method_arity => 1
}
end
it_should_behave_like 'a method match'
end
context 'on the same line' do
let(:body) do
<<-RUBY
class Foo
def bar; end; def bar(arg); end
end
RUBY
end
let(:expectation) do
{
:method_line => 2,
:method_arity => 1
}
end
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
}
end
it_should_behave_like 'a method match'
end
context 'when nested' do
let(:pattern) { 'Foo::Bar#baz' }
context 'in class' do
let(:body) do
<<-RUBY
class Foo
class Bar
def baz; end
end
end
RUBY
end
let(:expectation) do
{
:method_line => 3,
:method_name => :baz,
:scope => Foo::Bar
}
end
it_should_behave_like 'a method match'
end
context 'in module' do
let(:body) do
<<-RUBY
module Foo
class Bar
def baz; end
end
end
RUBY
end
let(:expectation) do
{
:method_line => 3,
:method_name => :baz,
:scope => Foo::Bar
}
end
it_should_behave_like 'a method match'
end
end
end
end
context 'on singleton methods' do
let(:pattern) { 'Foo.bar' }
let(:defaults) do
{
:scope => Foo,
:node_class => Rubinius::AST::DefineSingleton,
:method_arity => 0
}
end
def name(node)
node.body.name
end
def arguments(node)
node.body.arguments
end
context 'when defined on self' do
let(:body) do
<<-RUBY
class Foo
def self.bar; end
end
RUBY
end
let(:expectation) do
{
:method_name => :bar,
:method_line => 2,
}
end
it_should_behave_like 'a method match'
end
context 'when defined on constant' do
context 'inside namespace' do
let(:body) do
<<-RUBY
class Foo
def Foo.bar; end
end
RUBY
end
let(:expectation) do
{
:method_name => :bar,
:method_line => 2,
}
end
it_should_behave_like 'a method match'
end
context 'outside namespace' do
let(:body) do
<<-RUBY
class Foo; end;
def Foo.bar; end
RUBY
end
let(:expectation) do
{
:method_name => :bar,
:method_line => 2,
}
end
it_should_behave_like 'a method match'
end
end
context 'when defined multiple times in the same line' do
context 'with method on differend scope' do
let(:body) do
<<-RUBY
module Foo; end
module Bar
def self.baz; end; def Foo.baz(arg); end
end
RUBY
end
let(:pattern) { 'Bar.baz' }
let(:expectation) do
{
:scope => Bar,
:method_name => :baz,
:method_line => 4,
:method_arity => 0
}
end
it_should_behave_like 'a method match'
end
end
end
end