From af33fd036631ea86e2c006eb8ea3306f0d4fd56b Mon Sep 17 00:00:00 2001 From: Markus Schirp Date: Sun, 13 Jan 2013 22:29:07 +0100 Subject: [PATCH] Add standalone method expansions --- lib/mutant.rb | 1 + lib/mutant/strategy/method_expansion.rb | 51 +++++++++++++++++++ .../class_methods/run_spec.rb | 49 ++++++++++++++++++ 3 files changed, 101 insertions(+) create mode 100644 lib/mutant/strategy/method_expansion.rb create mode 100644 spec/unit/mutant/strategy/method_expansion/class_methods/run_spec.rb 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