2012-08-15 22:10:54 -04:00
|
|
|
module Mutant
|
|
|
|
class Reporter
|
|
|
|
# Reporter that reports in human readable format
|
|
|
|
class CLI < self
|
2013-05-21 20:40:33 -04:00
|
|
|
include Concord::Public.new(:io)
|
2012-08-29 07:36:29 -04:00
|
|
|
|
2013-04-20 20:20:18 -04:00
|
|
|
ACTIONS = {
|
|
|
|
Config => :config,
|
|
|
|
Subject => :subject,
|
|
|
|
Killer => :killer,
|
|
|
|
Runner::Subject => :subject_results,
|
|
|
|
Runner::Config => :config_results
|
|
|
|
}.freeze
|
|
|
|
|
|
|
|
# Perform lookup
|
|
|
|
#
|
|
|
|
# @param [Object] object
|
|
|
|
#
|
|
|
|
# @return [Symbol]
|
|
|
|
# if found
|
|
|
|
#
|
2013-06-14 14:54:02 -04:00
|
|
|
# @raise [RuntimeError]
|
2013-04-20 20:20:18 -04:00
|
|
|
# otherwise
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def self.lookup(object)
|
|
|
|
current = klass = object.class
|
|
|
|
begin
|
|
|
|
symbol = ACTIONS[current]
|
|
|
|
return symbol if symbol
|
|
|
|
current = current.superclass
|
|
|
|
end while current < ::Object
|
|
|
|
raise "No reporter for #{klass}"
|
|
|
|
end
|
|
|
|
|
|
|
|
# Report object
|
|
|
|
#
|
|
|
|
# @param [Object] object
|
|
|
|
#
|
|
|
|
# @return [self]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def report(object)
|
|
|
|
method = self.class.lookup(object)
|
|
|
|
send(method, object)
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2013-04-20 14:47:07 -04:00
|
|
|
private
|
2012-08-15 22:10:54 -04:00
|
|
|
|
2013-04-20 14:47:07 -04:00
|
|
|
# Report subject
|
2012-12-10 18:17:19 -05:00
|
|
|
#
|
2013-04-20 20:20:18 -04:00
|
|
|
# @param [Subject] subject
|
2012-12-10 18:17:19 -05:00
|
|
|
#
|
2013-04-20 14:47:07 -04:00
|
|
|
# @return [undefined]
|
2012-12-10 18:17:19 -05:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2013-04-20 20:20:18 -04:00
|
|
|
def subject(subject)
|
|
|
|
puts
|
|
|
|
puts(subject.identification)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Report subject results
|
|
|
|
#
|
2013-06-14 14:54:02 -04:00
|
|
|
# @param [Subject]
|
2013-04-20 20:20:18 -04:00
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def subject_results(runner)
|
|
|
|
mutations = runner.mutations
|
2013-04-20 20:26:36 -04:00
|
|
|
puts if mutations.any?
|
2013-04-20 20:20:18 -04:00
|
|
|
time = mutations.map(&:runtime).inject(0, :+)
|
|
|
|
mutations = mutations.length
|
|
|
|
fails = runner.failed_mutations
|
|
|
|
fails = fails.length
|
|
|
|
kills = mutations - fails
|
|
|
|
coverage = kills.to_f / mutations * 100
|
|
|
|
puts('(%02d/%02d) %3d%% - %0.02fs' % [kills, mutations, coverage, time])
|
2012-12-10 18:17:19 -05:00
|
|
|
end
|
|
|
|
|
2012-08-16 13:26:15 -04:00
|
|
|
# Report mutation
|
2012-08-15 22:10:54 -04:00
|
|
|
#
|
2013-04-20 14:47:07 -04:00
|
|
|
# @param [Mutation] _mutation
|
2012-08-15 22:10:54 -04:00
|
|
|
#
|
2013-04-20 14:47:07 -04:00
|
|
|
# @return [undefined]
|
2012-08-15 22:10:54 -04:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2013-04-20 14:47:07 -04:00
|
|
|
def mutation(_mutation)
|
2012-08-15 22:10:54 -04:00
|
|
|
end
|
|
|
|
|
2013-04-20 20:20:18 -04:00
|
|
|
# Report configuration
|
2012-11-21 16:28:08 -05:00
|
|
|
#
|
|
|
|
# @param [Mutant::Config] config
|
|
|
|
#
|
|
|
|
# @return [self]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2013-04-20 14:47:07 -04:00
|
|
|
def config(config)
|
2013-04-20 20:48:58 -04:00
|
|
|
puts 'Mutant configuration:'
|
|
|
|
puts "Matcher: #{config.matcher.inspect }"
|
|
|
|
puts "Filter: #{config.filter.inspect }"
|
|
|
|
puts "Strategy: #{config.strategy.inspect}"
|
2012-11-21 16:28:08 -05:00
|
|
|
end
|
|
|
|
|
2013-04-20 20:20:18 -04:00
|
|
|
# Report configuration results
|
|
|
|
#
|
|
|
|
# TODO: Break this stuff into smaller methods or factor out in a subclass
|
|
|
|
#
|
|
|
|
# @param [Reporter::Config] runner
|
|
|
|
#
|
|
|
|
# @return [self]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def config_results(runner)
|
|
|
|
message = []
|
|
|
|
subjects = runner.subjects
|
|
|
|
mutations = subjects.map(&:mutations).flatten
|
|
|
|
killtime = mutations.map(&:runtime).inject(0, :+)
|
|
|
|
kills = mutations.select(&:success?)
|
|
|
|
|
|
|
|
subjects = subjects.length
|
|
|
|
mutations = mutations.length
|
|
|
|
kills = kills.length
|
|
|
|
coverage = kills.to_f / mutations * 100
|
|
|
|
runtime = runner.runtime
|
|
|
|
|
|
|
|
overhead = (runtime - killtime) / runtime * 100
|
|
|
|
|
|
|
|
puts "Subjects: #{subjects}"
|
|
|
|
puts "Mutations: #{mutations}"
|
|
|
|
puts "Kills: #{kills}"
|
2013-04-20 20:23:46 -04:00
|
|
|
puts 'Runtime: %0.2fs' % runtime
|
2013-04-20 20:20:18 -04:00
|
|
|
puts 'Killtime: %0.2fs' % killtime
|
|
|
|
puts 'Overhead: %0.2f%%' % overhead
|
|
|
|
puts 'Coverage: %0.2f%%' % coverage
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2013-01-21 17:13:34 -05:00
|
|
|
# Report killer
|
|
|
|
#
|
|
|
|
# @param [Killer] killer
|
2012-11-21 20:51:16 -05:00
|
|
|
#
|
2013-01-21 17:13:34 -05:00
|
|
|
# @return [self]
|
2012-11-21 20:51:16 -05:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2013-04-20 14:47:07 -04:00
|
|
|
def killer(killer)
|
2013-04-20 20:20:18 -04:00
|
|
|
if killer.success?
|
|
|
|
char('.', Color::GREEN)
|
|
|
|
return
|
2013-01-21 17:13:34 -05:00
|
|
|
end
|
2013-04-20 20:20:18 -04:00
|
|
|
char('F', Color::RED)
|
2013-01-21 17:13:34 -05:00
|
|
|
self
|
|
|
|
end
|
2012-11-21 20:51:16 -05:00
|
|
|
|
2013-04-20 20:20:18 -04:00
|
|
|
# Write colorized char
|
|
|
|
#
|
|
|
|
# @param [String] char
|
|
|
|
# @param [Color]
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def char(char, color)
|
2013-04-20 20:48:58 -04:00
|
|
|
io = self.io
|
2013-04-20 20:20:18 -04:00
|
|
|
io.write(colorize(color, char))
|
|
|
|
io.flush
|
|
|
|
end
|
2012-08-15 22:10:54 -04:00
|
|
|
|
2012-08-16 12:02:03 -04:00
|
|
|
# Test for colored output
|
|
|
|
#
|
|
|
|
# @return [true]
|
|
|
|
# returns true if output is colored
|
|
|
|
#
|
|
|
|
# @return [false]
|
|
|
|
# returns false otherwise
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def color?
|
|
|
|
tty?
|
|
|
|
end
|
|
|
|
|
|
|
|
# Colorize message
|
|
|
|
#
|
|
|
|
# @param [Color] color
|
|
|
|
# @param [String] message
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-08-16 13:26:15 -04:00
|
|
|
# @return [String]
|
|
|
|
# returns colorized string if color is enabled
|
|
|
|
# returns unmodified message otherwise
|
|
|
|
#
|
2012-08-16 12:02:03 -04:00
|
|
|
def colorize(color, message)
|
|
|
|
color = Color::NONE unless color?
|
|
|
|
color.format(message)
|
|
|
|
end
|
|
|
|
|
2012-11-21 16:28:08 -05:00
|
|
|
# Write string to io
|
|
|
|
#
|
|
|
|
# @param [String] string
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def puts(string="\n")
|
|
|
|
io.puts(string)
|
|
|
|
end
|
|
|
|
|
2012-11-24 17:28:23 -05:00
|
|
|
# Write colorized diff
|
|
|
|
#
|
2012-12-12 16:31:14 -05:00
|
|
|
# @param [Mutation] mutation
|
2012-11-24 17:28:23 -05:00
|
|
|
#
|
2012-12-10 18:17:19 -05:00
|
|
|
# @return [undefined]
|
2012-11-24 17:28:23 -05:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-12-10 18:17:19 -05:00
|
|
|
def colorized_diff(mutation)
|
2013-01-15 17:46:05 -05:00
|
|
|
if mutation.kind_of?(Mutation::Neutral)
|
2012-12-12 11:52:24 -05:00
|
|
|
puts mutation.original_source
|
2012-12-10 18:17:19 -05:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
original, current = mutation.original_source, mutation.source
|
2012-11-24 17:28:23 -05:00
|
|
|
differ = Differ.new(original, current)
|
|
|
|
diff = color? ? differ.colorized_diff : differ.diff
|
2012-12-10 18:17:19 -05:00
|
|
|
|
2012-11-24 17:28:23 -05:00
|
|
|
if diff.empty?
|
2013-04-20 14:47:07 -04:00
|
|
|
raise 'Unable to create a diff, so ast mutant or to_source does something strange!!'
|
2012-11-24 17:28:23 -05:00
|
|
|
end
|
2012-12-10 18:17:19 -05:00
|
|
|
|
2012-11-24 17:28:23 -05:00
|
|
|
puts(diff)
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2012-08-16 12:02:03 -04:00
|
|
|
# Test for output to tty
|
|
|
|
#
|
|
|
|
# @return [true]
|
|
|
|
# returns true if output is a tty
|
2013-04-17 23:31:21 -04:00
|
|
|
#
|
2012-08-16 12:02:03 -04:00
|
|
|
# @return [false]
|
|
|
|
# returns false otherwise
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def tty?
|
|
|
|
@io.respond_to?(:tty?) && @io.tty?
|
|
|
|
end
|
|
|
|
memoize :tty?
|
2013-01-15 17:46:05 -05:00
|
|
|
|
2013-06-14 14:54:02 -04:00
|
|
|
end # CLI
|
|
|
|
end # Reporter
|
|
|
|
end # Mutant
|