2014-06-30 09:06:57 -04:00
|
|
|
module Mutant
|
|
|
|
# Abstract base class for mutant environments
|
|
|
|
class Env
|
2014-07-03 17:16:12 -04:00
|
|
|
include Adamantium, Concord::Public.new(:config, :cache), Procto.call(:run)
|
2014-06-30 09:06:57 -04:00
|
|
|
|
2014-07-03 17:16:12 -04:00
|
|
|
# Return new env
|
2014-06-30 09:06:57 -04:00
|
|
|
#
|
2014-07-03 17:16:12 -04:00
|
|
|
# @param [Config] config
|
|
|
|
#
|
|
|
|
# @return [Env]
|
2014-06-30 09:06:57 -04:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2014-07-05 23:00:21 -04:00
|
|
|
def self.new(config, cache = Cache.new)
|
|
|
|
super(config, cache)
|
2014-07-03 17:16:12 -04:00
|
|
|
end
|
2014-06-30 09:06:57 -04:00
|
|
|
|
2014-07-03 17:16:12 -04:00
|
|
|
# Initialize env
|
2014-06-30 09:06:57 -04:00
|
|
|
#
|
2014-07-03 17:16:12 -04:00
|
|
|
# @return [undefined]
|
2014-06-30 09:06:57 -04:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2014-07-03 17:16:12 -04:00
|
|
|
def initialize(*)
|
|
|
|
super
|
2014-06-30 09:06:57 -04:00
|
|
|
|
2014-07-03 17:16:12 -04:00
|
|
|
infect
|
|
|
|
initialize_matchable_scopes
|
|
|
|
initialize_subjects
|
|
|
|
end
|
|
|
|
|
|
|
|
# Run mutant producing a report on configured env
|
2014-06-30 09:06:57 -04:00
|
|
|
#
|
2014-07-03 17:16:12 -04:00
|
|
|
# @return [Report]
|
2014-06-30 09:06:57 -04:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2014-07-03 17:16:12 -04:00
|
|
|
def run
|
|
|
|
Runner.call(self)
|
|
|
|
end
|
2014-06-30 09:06:57 -04:00
|
|
|
|
2014-06-30 09:19:47 -04:00
|
|
|
# Print warning message
|
|
|
|
#
|
|
|
|
# @param [String]
|
|
|
|
#
|
|
|
|
# @return [self]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def warn(message)
|
2014-07-03 17:16:12 -04:00
|
|
|
config.reporter.warn(message)
|
2014-06-30 09:19:47 -04:00
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2014-07-03 17:16:12 -04:00
|
|
|
# Return subjects
|
|
|
|
#
|
|
|
|
# @return [Array<Subject>]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
attr_reader :subjects
|
|
|
|
|
2014-07-02 13:47:51 -04:00
|
|
|
# Return all usable match scopes
|
|
|
|
#
|
2014-07-03 17:16:12 -04:00
|
|
|
# @return [Array<Matcher::Scope>]
|
2014-07-02 13:47:51 -04:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2014-07-03 17:16:12 -04:00
|
|
|
attr_reader :matchable_scopes
|
2014-07-02 13:47:51 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# Return scope name
|
|
|
|
#
|
|
|
|
# @param [Class, Module] scope
|
|
|
|
#
|
|
|
|
# @return [String]
|
2014-07-10 18:56:01 -04:00
|
|
|
# if scope has a name and does not raise exceptions obtaining it
|
2014-07-02 13:47:51 -04:00
|
|
|
#
|
|
|
|
# @return [nil]
|
|
|
|
# otherwise
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
# rubocop:disable LineLength
|
|
|
|
#
|
|
|
|
def scope_name(scope)
|
|
|
|
scope.name
|
|
|
|
rescue => exception
|
2014-07-10 18:56:01 -04:00
|
|
|
warn("While obtaining #{scope.class}#name from: #{scope.inspect} It raised an error: #{exception.inspect} fix your lib!")
|
2014-07-02 13:47:51 -04:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
# Try to turn scope into expression
|
|
|
|
#
|
|
|
|
# @param [Class, Module] scope
|
|
|
|
#
|
|
|
|
# @return [Expression]
|
|
|
|
# if scope can be represented in an expression
|
|
|
|
#
|
|
|
|
# @return [nil]
|
|
|
|
# otherwise
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def expression(scope)
|
|
|
|
name = scope_name(scope) or return
|
|
|
|
|
|
|
|
unless name.kind_of?(String)
|
2014-07-07 15:54:28 -04:00
|
|
|
warn("#{scope.class}#name from: #{scope.inspect} did not return a String or nil. Fix your lib to support normal ruby semantics!")
|
2014-07-02 13:47:51 -04:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
Expression.try_parse(name)
|
|
|
|
end
|
|
|
|
|
2014-07-03 17:16:12 -04:00
|
|
|
# Initialize subjects
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def initialize_subjects
|
|
|
|
@subjects = Matcher::Compiler.call(self, config.matcher_config).to_a
|
|
|
|
end
|
|
|
|
|
|
|
|
# Infect environment
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def infect
|
|
|
|
config.includes.each(&$LOAD_PATH.method(:<<))
|
|
|
|
config.requires.each(&method(:require))
|
|
|
|
end
|
|
|
|
|
|
|
|
# Initialize matchable scopes
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def initialize_matchable_scopes
|
|
|
|
@matchable_scopes = ObjectSpace.each_object(Module).each_with_object([]) do |scope, aggregate|
|
|
|
|
expression = expression(scope)
|
|
|
|
aggregate << Matcher::Scope.new(self, scope, expression) if expression
|
|
|
|
end.sort_by(&:identification)
|
|
|
|
end
|
2014-06-30 09:06:57 -04:00
|
|
|
|
2014-07-03 17:16:12 -04:00
|
|
|
end # Env
|
2014-06-30 09:06:57 -04:00
|
|
|
end # Mutant
|