Add Mutant::Matcher.{parse,from_string}

These are used in CLI to create maters from strings
This commit is contained in:
Markus Schirp 2012-08-28 19:54:28 +02:00
parent 143acd1ca6
commit 46c9213991
4 changed files with 95 additions and 4 deletions

View file

@ -1,7 +1,8 @@
module Mutant
# Abstract matcher to find ASTs to mutate
class Matcher
include Enumerable, Abstract
include Enumerable, Abstract, Immutable
extend DescendantsTracker
# Enumerate subjects
#
@ -19,7 +20,36 @@ module Mutant
#
abstract_method :identification
private
# Return matcher
#
# @param [String] input
#
# @return [nil]
# returns nil as default implementation
#
# @api private
#
def self.parse(input)
nil
end
# Return match from string
#
# @param [String] input
#
# @return [Matcher]
# returns matcher input if successful
#
# @return [nil]
# returns nil otherwise
#
def self.from_string(input)
descendants.each do |descendant|
matcher = descendant.parse(input)
return matcher if matcher
end
nil
end
end
end

View file

@ -0,0 +1,49 @@
require 'spec_helper'
describe Mutant::Matcher, '.from_string' do
subject { object.from_string(input) }
let(:input) { mock('Input') }
let(:matcher) { mock('Matcher') }
let(:descendant_a) { mock('Descendant A', :parse => nil) }
let(:descendant_b) { mock('Descendant B', :parse => nil) }
let(:object) { described_class }
before do
described_class.stub(:descendants => [descendant_a, descendant_b])
end
context 'when no descendant takes the input' do
it { should be(nil) }
it_should_behave_like 'an idempotent method'
end
context 'when one descendant handles input' do
before do
descendant_a.stub(:parse => matcher)
end
it { should be(matcher) }
it_should_behave_like 'an idempotent method'
end
pending 'when more than one descendant handles input' do
let(:matcher_b) { mock('Matcher B') }
before do
descendant_a.stub(:parse => matcher)
descendant_b.stub(:parse => matcher_b)
end
it 'should return the first matcher' do
should be(matcher)
end
it_should_behave_like 'an idempotent method'
end
end

View file

@ -0,0 +1,12 @@
require 'spec_helper'
describe Mutant::Matcher, '.parse' do
subject { object.parse(input) }
let(:input) { mock('Input') }
let(:object) { described_class }
it { should be(nil) }
it_should_behave_like 'an idempotent method'
end

View file

@ -21,8 +21,8 @@ describe Mutant::Matcher::Method::Classifier, '.run' do
context 'with invalid notation' do
let(:input) { 'Foo' }
it 'should raise error' do
expect { subject }.to raise_error(ArgumentError, "Cannot determine subject from #{input.inspect}")
it 'should return nil' do
should be(nil)
end
end
end