diff --git a/lib/mutant.rb b/lib/mutant.rb index 5e9a0e24..649fd75d 100644 --- a/lib/mutant.rb +++ b/lib/mutant.rb @@ -98,6 +98,7 @@ require 'mutant/killer/static' require 'mutant/killer/rspec' require 'mutant/killer/forking' require 'mutant/strategy' +require 'mutant/strategy/method_expansion' require 'mutant/strategy/rspec' require 'mutant/strategy/rspec/example_lookup' require 'mutant/runner' diff --git a/lib/mutant/strategy/method_expansion.rb b/lib/mutant/strategy/method_expansion.rb new file mode 100644 index 00000000..96844809 --- /dev/null +++ b/lib/mutant/strategy/method_expansion.rb @@ -0,0 +1,51 @@ +module Mutant + class Strategy + module MethodExpansion + + # Run method name expansion + # + # @param [Symbol] name + # + # @return [Symbol] + # + # @api private + # + def self.run(name) + name = map(name) || expand(name) + end + + # Return mapped name + # + # @param [Symbol] name + # + # @return [Symbol] + # if name was mapped + # + # @return [nil] + # otherwise + # + # @api private + # + def self.map(name) + OPERATOR_EXPANSIONS[name] + end + private_class_method :map + + # Return expanded name + # + # @param [Symbol] name + # + # @return [Symbol] + # + # @api private + # + def self.expand(name) + METHOD_NAME_EXPANSIONS.inject(name) do |name, (regexp, expansion)| + name.to_s.gsub(regexp, expansion) + end.to_sym + end + private_class_method :expand + + end + end +end diff --git a/spec/unit/mutant/strategy/method_expansion/class_methods/run_spec.rb b/spec/unit/mutant/strategy/method_expansion/class_methods/run_spec.rb new file mode 100644 index 00000000..2201b4d3 --- /dev/null +++ b/spec/unit/mutant/strategy/method_expansion/class_methods/run_spec.rb @@ -0,0 +1,49 @@ +require 'spec_helper' + +describe Mutant::Strategy::MethodExpansion, '.run' do + subject { object.run(name) } + + let(:object) { described_class } + + context 'unexpandable and unmapped name' do + let(:name) { :foo } + + it { should be(:foo) } + end + + context 'expanded name' do + + context 'predicate' do + let(:name) { :foo? } + + it { should be(:foo_predicate) } + end + + context 'writer' do + let(:name) { :foo= } + + it { should be(:foo_writer) } + end + + context 'bang' do + let(:name) { :foo! } + + it { should be(:foo_bang) } + end + + end + + context 'operator expansions' do + + Mutant::OPERATOR_EXPANSIONS.each do |name, expansion| + context "#{name}" do + let(:name) { name } + + it "should expand to #{expansion}" do + should be(expansion) + end + end + end + + end +end