2014-06-26 18:17:53 -04:00
|
|
|
module Mutant
|
2014-06-27 12:07:45 -04:00
|
|
|
class Matcher
|
2014-06-26 18:17:53 -04:00
|
|
|
|
2014-07-03 17:16:12 -04:00
|
|
|
# Compiler for complex matchers
|
|
|
|
class Compiler
|
|
|
|
include Concord.new(:env, :config), AST::Sexp, Procto.call(:result)
|
2014-06-26 18:17:53 -04:00
|
|
|
|
|
|
|
# Return generated matcher
|
|
|
|
#
|
|
|
|
# @return [Mutant::Matcher]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2014-07-03 17:16:12 -04:00
|
|
|
def result
|
2014-07-06 10:30:28 -04:00
|
|
|
Filter.new(
|
|
|
|
Chain.build(config.match_expressions.map(&method(:matcher))),
|
|
|
|
predicate
|
|
|
|
)
|
2014-06-26 18:17:53 -04:00
|
|
|
end
|
|
|
|
|
2014-07-06 10:30:28 -04:00
|
|
|
# Subject expression prefix predicate
|
|
|
|
class SubjectPrefix
|
|
|
|
include Concord.new(:expression)
|
2014-06-26 18:17:53 -04:00
|
|
|
|
2014-07-06 10:30:28 -04:00
|
|
|
# Test if subject expression is matched by prefix
|
|
|
|
#
|
|
|
|
# @return [Boolean]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def call(subject)
|
|
|
|
expression.prefix?(subject.expression)
|
2014-07-03 17:16:12 -04:00
|
|
|
end
|
|
|
|
|
2014-07-06 10:30:28 -04:00
|
|
|
end # SubjectPrefix
|
|
|
|
|
|
|
|
private
|
2014-06-26 18:17:53 -04:00
|
|
|
|
|
|
|
# Return predicate
|
|
|
|
#
|
|
|
|
# @return [#call]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def predicate
|
|
|
|
if subject_selector && subject_rejector
|
|
|
|
Morpher::Evaluator::Predicate::Boolean::And.new([
|
|
|
|
subject_selector,
|
|
|
|
Morpher::Evaluator::Predicate::Negation.new(subject_rejector)
|
|
|
|
])
|
|
|
|
elsif subject_selector
|
|
|
|
subject_selector
|
|
|
|
elsif subject_rejector
|
|
|
|
Morpher::Evaluator::Predicate::Negation.new(subject_rejector)
|
|
|
|
else
|
2014-07-06 10:30:28 -04:00
|
|
|
Morpher::Evaluator::Predicate::Tautology.new
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Return subject selector
|
|
|
|
#
|
|
|
|
# @return [#call]
|
|
|
|
# if selector is present
|
|
|
|
#
|
|
|
|
# @return [nil]
|
|
|
|
# otherwise
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def subject_selector
|
|
|
|
selectors = config.subject_selects.map do |attribute, value|
|
|
|
|
Morpher.compile(s(:eql, s(:attribute, attribute), s(:static, value)))
|
2014-06-26 18:17:53 -04:00
|
|
|
end
|
2014-07-06 10:30:28 -04:00
|
|
|
|
|
|
|
Morpher::Evaluator::Predicate::Boolean::Or.new(selectors) if selectors.any?
|
2014-06-26 18:17:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Return subject rejector
|
|
|
|
#
|
|
|
|
# @return [#call]
|
|
|
|
# if there is a subject rejector
|
|
|
|
#
|
|
|
|
# @return [nil]
|
|
|
|
# otherwise
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def subject_rejector
|
2014-07-06 10:30:28 -04:00
|
|
|
rejectors = config.subject_ignores.map(&SubjectPrefix.method(:new))
|
2014-06-26 18:17:53 -04:00
|
|
|
|
|
|
|
Morpher::Evaluator::Predicate::Boolean::Or.new(rejectors) if rejectors.any?
|
|
|
|
end
|
|
|
|
|
2014-07-03 17:16:12 -04:00
|
|
|
# Return a matcher from expression
|
|
|
|
#
|
|
|
|
# @param [Mutant::Expression] expression
|
|
|
|
#
|
|
|
|
# @return [Matcher]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def matcher(expression)
|
|
|
|
expression.matcher(env)
|
|
|
|
end
|
|
|
|
|
|
|
|
end # Compiler
|
2014-06-27 12:07:45 -04:00
|
|
|
end # Matcher
|
2014-06-26 18:17:53 -04:00
|
|
|
end # Mutant
|