Remove CLI::Builder registry

This commit is contained in:
Markus Schirp 2014-01-18 00:15:42 +01:00
parent 32d3a461d3
commit ad82b0fc4f
9 changed files with 84 additions and 94 deletions

View file

@ -61,7 +61,7 @@ module Mutant
debug: @debug, debug: @debug,
matcher: matcher, matcher: matcher,
subject_predicate: @subject_predicate.output, subject_predicate: @subject_predicate.output,
strategy: @strategy.output, strategy: @strategy || Strategy::Null.new,
fail_fast: @fail_fast, fail_fast: @fail_fast,
reporter: reporter reporter: reporter
) )
@ -167,10 +167,8 @@ module Mutant
parser.separator(EMPTY_STRING) parser.separator(EMPTY_STRING)
parser.separator('Strategies:') parser.separator('Strategies:')
Builder::REGISTRY.each do |builder, instance_variable_name| builder = Builder::Predicate::Subject.new(@cache, parser)
builder = builder.new(@cache, parser) @subject_predicate = builder
instance_variable_set(instance_variable_name, builder)
end
end end
# Add environmental options # Add environmental options
@ -191,6 +189,22 @@ module Mutant
end end
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 # Add options
# #
# @param [Object] opts # @param [Object] opts
@ -203,6 +217,10 @@ module Mutant
opts.separator '' opts.separator ''
opts.separator 'Options:' 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| opts.on('--version', 'Print mutants version') do |name|
puts("mutant-#{Mutant::VERSION}") puts("mutant-#{Mutant::VERSION}")
Kernel.exit(0) Kernel.exit(0)

View file

@ -6,18 +6,6 @@ module Mutant
class Builder class Builder
include AbstractType 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
# #
# @return [Cache] # @return [Cache]
@ -71,8 +59,6 @@ module Mutant
# Bubject predicate builder # Bubject predicate builder
class Subject < self class Subject < self
register :@subject_predicate
# Initialize object # Initialize object
# #
# @api private # @api private

View file

@ -99,5 +99,25 @@ module Mutant
# #
abstract_method :run 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 # Killer
end # Mutant end # Mutant

View file

@ -2,7 +2,6 @@ require 'rspec'
require 'mutant/rspec/killer' require 'mutant/rspec/killer'
require 'mutant/rspec/strategy' require 'mutant/rspec/strategy'
require 'mutant/rspec/builder'
module Mutant module Mutant
module Rspec module Rspec

View file

@ -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

View file

@ -4,7 +4,8 @@ module Mutant
module Rspec module Rspec
# Rspec killer strategy # Rspec killer strategy
class Strategy < Mutant::Strategy class Strategy < Mutant::Strategy
include Equalizer.new
register 'rspec'
KILLER = Killer::Forking.new(Rspec::Killer) KILLER = Killer::Forking.new(Rspec::Killer)

View file

@ -6,6 +6,34 @@ module Mutant
class Strategy class Strategy
include AbstractType, Adamantium::Flat 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 # Perform strategy setup
# #
# @return [self] # @return [self]
@ -50,5 +78,15 @@ module Mutant
self.class::KILLER self.class::KILLER
end end
# Null strategy that never kills a mutation
class Null < self
register 'null'
KILLER = Killer::Null
end # Null
end # Strategy end # Strategy
end # Mutant end # Mutant

View file

@ -10,7 +10,7 @@ describe Mutant, 'rspec integration' do
end end
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 specify 'it allows to kill mutations' do
Kernel.system("#{base_cmd} ::TestApp::Literal#string").should be(true) Kernel.system("#{base_cmd} ::TestApp::Literal#string").should be(true)

View file

@ -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