From ad82b0fc4fdc9ed0f17a9d6bcc18f544a348f16b Mon Sep 17 00:00:00 2001 From: Markus Schirp Date: Sat, 18 Jan 2014 00:15:42 +0100 Subject: [PATCH] Remove CLI::Builder registry --- lib/mutant/cli.rb | 28 +++++++++++--- lib/mutant/cli/builder.rb | 14 ------- lib/mutant/killer.rb | 20 ++++++++++ lib/mutant/rspec.rb | 1 - lib/mutant/rspec/builder.rb | 51 -------------------------- lib/mutant/rspec/strategy.rb | 3 +- lib/mutant/strategy.rb | 38 +++++++++++++++++++ spec/integration/mutant/rspec_spec.rb | 2 +- spec/unit/mutant/rspec/builder_spec.rb | 21 ----------- 9 files changed, 84 insertions(+), 94 deletions(-) delete mode 100644 lib/mutant/rspec/builder.rb delete mode 100644 spec/unit/mutant/rspec/builder_spec.rb diff --git a/lib/mutant/cli.rb b/lib/mutant/cli.rb index 6135069f..e90ff092 100644 --- a/lib/mutant/cli.rb +++ b/lib/mutant/cli.rb @@ -61,7 +61,7 @@ module Mutant debug: @debug, matcher: matcher, subject_predicate: @subject_predicate.output, - strategy: @strategy.output, + strategy: @strategy || Strategy::Null.new, fail_fast: @fail_fast, reporter: reporter ) @@ -167,10 +167,8 @@ module Mutant parser.separator(EMPTY_STRING) parser.separator('Strategies:') - Builder::REGISTRY.each do |builder, instance_variable_name| - builder = builder.new(@cache, parser) - instance_variable_set(instance_variable_name, builder) - end + builder = Builder::Predicate::Subject.new(@cache, parser) + @subject_predicate = builder end # Add environmental options @@ -191,6 +189,22 @@ module Mutant end end + # Add runner + # + # @param [String] runner_name + # + # @return [undefined] + # + # @api private + # + def set_strategy(name) + require "mutant/#{name}" + @strategy = Strategy.lookup(name).new + rescue LoadError + $stderr.puts("Cannot load strategy: #{name.inspect}") + raise + end + # Add options # # @param [Object] opts @@ -203,6 +217,10 @@ module Mutant opts.separator '' opts.separator 'Options:' + opts.on('--via STRATEGY', 'Use STRATEGY for killing mutations') do |runner| + set_strategy(runner) + end + opts.on('--version', 'Print mutants version') do |name| puts("mutant-#{Mutant::VERSION}") Kernel.exit(0) diff --git a/lib/mutant/cli/builder.rb b/lib/mutant/cli/builder.rb index b49a13b4..3b3a05de 100644 --- a/lib/mutant/cli/builder.rb +++ b/lib/mutant/cli/builder.rb @@ -6,18 +6,6 @@ module Mutant class Builder include AbstractType - REGISTRY = {} - - # Register builder - # - # @return [undefined] - # - # @api private - # - def self.register(instance_variable_name) - REGISTRY[self] = instance_variable_name - end - # Return cache # # @return [Cache] @@ -71,8 +59,6 @@ module Mutant # Bubject predicate builder class Subject < self - register :@subject_predicate - # Initialize object # # @api private diff --git a/lib/mutant/killer.rb b/lib/mutant/killer.rb index da1e1577..3618bfd0 100644 --- a/lib/mutant/killer.rb +++ b/lib/mutant/killer.rb @@ -99,5 +99,25 @@ module Mutant # abstract_method :run + # Null killer that never kills a mutation + class Null < self + + private + + # Run killer + # + # @return [true] + # when mutant was killed + # + # @return [false] + # otherwise + # + # @api private + # + def run + false + end + + end end # Killer end # Mutant diff --git a/lib/mutant/rspec.rb b/lib/mutant/rspec.rb index 5e85fdeb..17cbcf09 100644 --- a/lib/mutant/rspec.rb +++ b/lib/mutant/rspec.rb @@ -2,7 +2,6 @@ require 'rspec' require 'mutant/rspec/killer' require 'mutant/rspec/strategy' -require 'mutant/rspec/builder' module Mutant module Rspec diff --git a/lib/mutant/rspec/builder.rb b/lib/mutant/rspec/builder.rb deleted file mode 100644 index 657d08a4..00000000 --- a/lib/mutant/rspec/builder.rb +++ /dev/null @@ -1,51 +0,0 @@ -module Mutant - module Rspec - # Rspec strategy builder - class Builder < CLI::Builder - - register :@strategy - - # Initialize object - # - # @return [undefined] - # - # @api private - # - def initialize(*) - @rspec = false - super - end - - # Return strategy - # - # @return [Strategy::Rspec] - # - # @api private - # - def output - unless @rspec - raise Error, 'No strategy given' - end - - Strategy.new - end - - private - - # Add cli options - # - # @param [OptionParser] parser - # - # @return [undefined] - # - # @api private - # - def add_options - parser.on('--rspec', 'kills mutations with rspec') do - @rspec = true - end - end - - end # Builder - end # Rspec -end # Mutant diff --git a/lib/mutant/rspec/strategy.rb b/lib/mutant/rspec/strategy.rb index 4ad063fc..b7597d37 100644 --- a/lib/mutant/rspec/strategy.rb +++ b/lib/mutant/rspec/strategy.rb @@ -4,7 +4,8 @@ module Mutant module Rspec # Rspec killer strategy class Strategy < Mutant::Strategy - include Equalizer.new + + register 'rspec' KILLER = Killer::Forking.new(Rspec::Killer) diff --git a/lib/mutant/strategy.rb b/lib/mutant/strategy.rb index 34ec92a0..be6d7b18 100644 --- a/lib/mutant/strategy.rb +++ b/lib/mutant/strategy.rb @@ -6,6 +6,34 @@ module Mutant class Strategy include AbstractType, Adamantium::Flat + REGISTRY = {} + + # Lookup strategy for name + # + # @param [String] name + # + # @return [Strategy] + # if found + # + # @api private + # + def self.lookup(name) + REGISTRY.fetch(name) + end + + # Register strategy + # + # @param [String] name + # + # @return [undefined] + # + # @api private + # + def self.register(name) + REGISTRY[name] = self + end + private_class_method :register + # Perform strategy setup # # @return [self] @@ -50,5 +78,15 @@ module Mutant self.class::KILLER end + # Null strategy that never kills a mutation + class Null < self + + register 'null' + + KILLER = Killer::Null + + end # Null + end # Strategy + end # Mutant diff --git a/spec/integration/mutant/rspec_spec.rb b/spec/integration/mutant/rspec_spec.rb index 7d9ccc54..b6d55bf2 100644 --- a/spec/integration/mutant/rspec_spec.rb +++ b/spec/integration/mutant/rspec_spec.rb @@ -10,7 +10,7 @@ describe Mutant, 'rspec integration' do end end - let(:base_cmd) { 'bundle exec mutant -I lib --require test_app --rspec' } + let(:base_cmd) { 'bundle exec mutant -I lib --require test_app --via rspec' } specify 'it allows to kill mutations' do Kernel.system("#{base_cmd} ::TestApp::Literal#string").should be(true) diff --git a/spec/unit/mutant/rspec/builder_spec.rb b/spec/unit/mutant/rspec/builder_spec.rb deleted file mode 100644 index 047bc0cd..00000000 --- a/spec/unit/mutant/rspec/builder_spec.rb +++ /dev/null @@ -1,21 +0,0 @@ -# encoding: utf-8 - -require 'spec_helper' - -describe Mutant::Rspec::Builder do - - let(:option_parser) { OptionParser.new } - - let(:cache) { Mutant::Cache.new } - let(:object) { described_class.new(cache, option_parser) } - - let(:default_strategy) do - Mutant::Rspec::Strategy.new - end - - specify do - object - option_parser.parse!(%w[--rspec]) - expect(object.output).to eql(default_strategy) - end -end