free_mutant/lib/mutant/runner.rb

149 lines
2.9 KiB
Ruby
Raw Normal View History

2012-08-14 16:45:34 -04:00
module Mutant
2013-04-20 14:50:36 -04:00
# Runner baseclass
2012-08-14 16:45:34 -04:00
class Runner
include Adamantium, Concord.new(:env), Procto.call(:result)
# Initialize object
#
# @return [undefined]
#
# @api private
#
def initialize(env)
super
@stop = false
config.integration.setup
progress(env)
2012-08-14 16:45:34 -04:00
@result = Result::Env.compute do
{
env: env,
subject_results: visit_collection(env.subjects, &method(:run_subject))
}
end
config.reporter.report(result)
end
# Return result
#
# @return [Result::Env]
#
# @api private
#
attr_reader :result
private
# Run subject
2012-08-16 13:26:15 -04:00
#
# @return [Report::Subject]
2012-08-16 13:26:15 -04:00
#
# @api private
#
def run_subject(subject)
Result::Subject.compute do
{
subject: subject,
mutation_results: visit_collection(subject.mutations, &method(:run_mutation))
}
end
2013-01-15 17:46:05 -05:00
end
# Run mutation
#
# @param [Mutation]
#
# @return [Report::Mutation]
#
2013-07-15 04:47:44 -04:00
# @api private
#
def run_mutation(mutation)
Result::Mutation.compute do
{
mutation: mutation,
test_results: kill_mutation(mutation)
}
end
end
# Kill mutation
#
# @param [Mutation] mutation
#
# @return [Array<Result::Test>]
#
# @api private
#
def kill_mutation(mutation)
mutation.subject.tests.each_with_object([]) do |test, results|
results << result = run_mutation_test(mutation, test).tap(&method(:progress))
return results if mutation.killed_by?(result)
end
end
# Return config
#
# @return [Config]
#
# @api private
#
def config
env.config
end
# Visit collection
2013-03-27 10:52:02 -04:00
#
# @return [Array<Result>]
2013-03-27 10:52:02 -04:00
#
# @api private
2013-04-17 23:31:21 -04:00
#
def visit_collection(collection)
collection.each_with_object([]) do |item, results|
progress(item)
start = Time.now
results << result = yield(item).update(runtime: Time.now - start).tap(&method(:progress))
return results if @stop ||= config.fail_fast? && result.fail?
end
end
2012-08-14 16:45:34 -04:00
# Report progress
2013-04-20 14:50:36 -04:00
#
# @param [Object] object
#
# @return [undefined]
#
# @api private
#
def progress(object)
config.reporter.progress(object)
2013-04-20 14:50:36 -04:00
end
# Return test result
#
# @return [Report::Test]
#
# @api private
#
def run_mutation_test(mutation, test)
time = Time.now
config.isolation.call do
mutation.insert
test.run
end.update(test: test, mutation: mutation)
rescue Isolation::Error => exception
Result::Test.new(
test: test,
mutation: mutation,
runtime: Time.now - time,
output: exception.message,
passed: false
)
end
2013-06-14 14:54:02 -04:00
end # Runner
end # Mutant