Separate rspec2 and rspec3 integrations

This commit is contained in:
Markus Schirp 2014-06-28 23:54:04 +00:00
parent f2e908dd35
commit 55b93b5d76
6 changed files with 111 additions and 66 deletions

View file

@ -1,3 +1,3 @@
---
threshold: 18
total_score: 950
total_score: 960

View file

@ -25,7 +25,8 @@ DuplicateMethodCall:
FeatureEnvy:
enabled: true
exclude:
- Mutant::Integration::Rspec#full_description
- Mutant::Integration::Rspec2#full_description
- Mutant::Integration::Rspec3#full_description
- Mutant::Matcher::Method::Instance#match?
- Mutant::Matcher::Method::Singleton#receiver?
- Mutant::Mutation::Evil#success?
@ -71,7 +72,6 @@ RepeatedConditional:
enabled: true
exclude:
- Mutant::Mutator
- Mutant::Integration::Rspec
- Mutant::Reporter::CLI
- Mutant::Meta::Example::DSL
max_ifs: 1
@ -95,7 +95,6 @@ TooManyStatements:
enabled: true
exclude:
- Mutant#self.singleton_subclass_instance
- Mutant::Integration::Rspec#new_reporter
- Mutant::Reporter::CLI::Report::Config#run
- Mutant::Reporter::CLI::Registry#included
- Mutant::Reporter::CLI#colorized_diff
@ -122,7 +121,9 @@ UncommunicativeMethodName:
accept: []
UncommunicativeModuleName:
enabled: true
exclude: []
exclude:
- Rspec2
- Rspec3
reject:
- !ruby/regexp /^.$/
- !ruby/regexp /[0-9]$/
@ -151,7 +152,10 @@ UtilityFunction:
exclude:
- Mutant::CLI#reporter
- Mutant::Integration::Rspec#configuration
- Mutant::Integration::Rspec#rspec2?
- Mutant::Integration::Rspec#options
- Mutant::Integration::Rspec2#full_description
- Mutant::Integration::Rspec2#new_reporter
- Mutant::Integration::Rspec3#full_description
- Mutant::Meta::Example::Verification#format_mutation
- Mutant::Mutation::Evil#success?
- Mutant::Mutation::Neutral#success?

View file

@ -1,11 +1,8 @@
module Mutant
class Integration
# Rspec integration
# Shared parts of rspec2/3 integration
class Rspec < self
register 'rspec'
RSPEC_2_VERSION_PREFIX = '2.'.freeze
include AbstractType
# Setup rspec integration
#
@ -73,58 +70,6 @@ module Mutant
end
memoize :example_group_index
# Return new reporter
#
# @param [StringIO] output
#
# @return [RSpec::Core::Reporter]
#
# @api private
#
def new_reporter(output)
reporter_class = RSpec::Core::Reporter
# rspec3 does require that one via a very indirect autoload setup
require 'rspec/core/formatters/base_text_formatter'
formatter = RSpec::Core::Formatters::BaseTextFormatter.new(output)
if rspec2?
reporter_class.new(formatter)
else
notifications = RSpec::Core::Formatters::Loader.allocate.send(:notifications_for, formatter.class)
reporter = reporter_class.new(configuration)
reporter.register_listener(formatter, *notifications)
reporter
end
end
# Test for rspec2
#
# @return [Boolean]
#
# @api private
#
def rspec2?
RSpec::Core::Version::STRING.start_with?(RSPEC_2_VERSION_PREFIX)
end
# Return full description for example group
#
# @param [RSpec::Core::ExampleGroup] example_group
#
# @return [String]
#
# @api private
#
def full_description(example_group)
metadata = example_group.metadata
if rspec2?
metadata.fetch(:example_group).fetch(:full_description)
else
metadata.fetch(:full_description)
end
end
# Return configuration
#
# @return [RSpec::Core::Configuration]
@ -143,9 +88,7 @@ module Mutant
# @api private
#
def options
options = RSpec::Core::ConfigurationOptions.new(%w[--fail-fast spec])
options.parse_options if rspec2?
options
RSpec::Core::ConfigurationOptions.new(%w[--fail-fast spec])
end
memoize :options, freezer: :noop

View file

@ -0,0 +1,48 @@
module Mutant
class Integration
# Rspec2 integration
class Rspec2 < Rspec
register 'rspec'
private
# Return options
#
# @return [RSpec::Core::ConfigurationOptions]
#
# @api private
#
def options
super.tap(&:parse_options)
end
# Return full description of example group
#
# @param [RSpec::Core::ExampleGroup] example_group
#
# @return [String]
#
# @api private
#
def full_description(example_group)
example_group.metadata.fetch(:example_group).fetch(:full_description)
end
# Return new reporter
#
# @param [StringIO] output
#
# @return [RSpec::Core::Reporter]
#
# @api private
#
def new_reporter(output)
formatter = RSpec::Core::Formatters::BaseTextFormatter.new(output)
RSpec::Core::Reporter.new(formatter)
end
end # Rspec2
end # Integration
end # Mutant

View file

@ -0,0 +1,41 @@
module Mutant
class Integration
# Rspec3 integration
class Rspec3 < Rspec
register 'rspec'
private
# Return full description for example group
#
# @param [RSpec::Core::ExampleGroup] example_group
#
# @return [String]
#
# @api private
#
def full_description(example_group)
example_group.metadata.fetch(:full_description)
end
# Return new reporter
#
# @param [StringIO] output
#
# @return [RSpec::Core::Reporter]
#
# @api private
#
def new_reporter(output)
formatter = RSpec::Core::Formatters::BaseTextFormatter.new(output)
notifications = RSpec::Core::Formatters::Loader.allocate.send(:notifications_for, formatter.class)
RSpec::Core::Reporter.new(configuration).tap do |reporter|
reporter.register_listener(formatter, *notifications)
end
end
end # Rspec3
end # Integration
end # Mutant

View file

@ -2,4 +2,13 @@
require 'rspec/core'
require 'rspec/version'
require 'rspec/core/formatters/base_text_formatter'
RSPEC_2_VERSION_PREFIX = '2.'.freeze
require 'mutant/integration/rspec'
if RSpec::Core::Version::STRING.start_with?(RSPEC_2_VERSION_PREFIX)
require 'mutant/integration/rspec2'
else
require 'mutant/integration/rspec3'
end