free_mutant/lib/mutant/reporter/cli.rb

157 lines
3.2 KiB
Ruby
Raw Normal View History

module Mutant
class Reporter
# Reporter that reports in human readable format
class CLI < self
include Concord.new(:io)
private
# Report subject
#
# @param [Subject] _subject
#
# @return [undefined]
#
# @api private
#
def subject(_subject)
end
2012-08-16 19:26:15 +02:00
# Report mutation
#
# @param [Mutation] _mutation
#
# @return [undefined]
#
# @api private
#
def mutation(_mutation)
end
# Report start
#
# @param [Mutant::Config] config
#
# @return [self]
#
# @api private
#
def config(config)
2013-01-15 23:46:05 +01:00
message = []
message << 'Mutant configuration:'
message << "Matcher: #{config.matcher.inspect }"
message << "Filter: #{config.filter.inspect }"
2013-01-15 23:46:05 +01:00
message << "Strategy: #{config.strategy.inspect}"
puts message.join("\n")
end
# Report killer
#
# @param [Killer] killer
2012-11-22 02:51:16 +01:00
#
# @return [self]
2012-11-22 02:51:16 +01:00
#
# @api private
#
def killer(killer)
status = killer.killed? ? 'Killed' : 'Alive'
color = killer.success? ? Color::GREEN : Color::RED
puts(colorize(color, "%s: %s (%02.2fs)" % [status, killer.identification, killer.runtime]))
unless killer.success?
colorized_diff(killer.mutation)
end
self
end
2012-11-22 02:51:16 +01:00
2013-04-17 20:31:21 -07:00
private
2012-08-16 18:02:03 +02: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 19:26:15 +02:00
# @return [String]
# returns colorized string if color is enabled
# returns unmodified message otherwise
#
2012-08-16 18:02:03 +02:00
def colorize(color, message)
color = Color::NONE unless color?
color.format(message)
end
# Write string to io
#
# @param [String] string
#
# @return [undefined]
#
# @api private
#
def puts(string="\n")
io.puts(string)
end
# Write colorized diff
#
# @param [Mutation] mutation
#
# @return [undefined]
#
# @api private
#
def colorized_diff(mutation)
2013-01-15 23:46:05 +01:00
if mutation.kind_of?(Mutation::Neutral)
2012-12-12 17:52:24 +01:00
puts mutation.original_source
return
end
original, current = mutation.original_source, mutation.source
differ = Differ.new(original, current)
diff = color? ? differ.colorized_diff : differ.diff
if diff.empty?
raise 'Unable to create a diff, so ast mutant or to_source does something strange!!'
end
puts(diff)
self
end
2012-08-16 18:02:03 +02:00
# Test for output to tty
#
# @return [true]
# returns true if output is a tty
2013-04-17 20:31:21 -07:00
#
2012-08-16 18:02:03 +02:00
# @return [false]
# returns false otherwise
#
# @api private
#
def tty?
@io.respond_to?(:tty?) && @io.tty?
end
memoize :tty?
2013-01-15 23:46:05 +01:00
end
end
end