From aaa96183b98e781869a428148384321412bced55 Mon Sep 17 00:00:00 2001 From: Markus Schirp Date: Thu, 16 Aug 2012 19:26:15 +0200 Subject: [PATCH] Bring back yard coverage to 100% --- lib/mutant/color.rb | 31 +++++++++++++++++++ lib/mutant/differ.rb | 63 ++++++++++++++++++++++++++------------ lib/mutant/killer.rb | 13 ++++++-- lib/mutant/reporter/cli.rb | 8 +++-- lib/mutant/runner.rb | 52 +++++++++++++++++++++++++++++++ lib/mutant/subject.rb | 5 +++ 6 files changed, 149 insertions(+), 23 deletions(-) diff --git a/lib/mutant/color.rb b/lib/mutant/color.rb index 61b89923..be5cb784 100644 --- a/lib/mutant/color.rb +++ b/lib/mutant/color.rb @@ -3,18 +3,49 @@ module Mutant class Color include Immutable + # Initialize color object + # + # @param [Fixnum] code + # + # @return [undefined] + # + # @api private + # def initialize(code) @code = code end + # Format text with color + # + # @param [String] text + # + # @return [String] + # + # @api private + # def format(text) "\e[#{@code}m#{text}\e[0m" end NONE = Class.new(self) do + # Initialize null color + # + # @return [undefined] + # + # @api private + # def initialize(*) end + # Format null color + # + # @param [String] text + # + # @return [String] + # returns the argument string + # + # @api private + # def format(text) text end diff --git a/lib/mutant/differ.rb b/lib/mutant/differ.rb index 1a1cb211..3bc1c951 100644 --- a/lib/mutant/differ.rb +++ b/lib/mutant/differ.rb @@ -1,35 +1,31 @@ module Mutant + # Class to create diffs from source code class Differ include Immutable - def initialize(old, new) - @new, @old = new.lines.map(&:chomp), old.lines.map(&:chomp) - @diffs = Diff::LCS.diff(@old, @new) - end - - def format - :unified - end - - def context_lines - 3 - end - - def length_difference - @new.size - @old.size - end - + # Return source diff + # + # @return [String] + # + # @api private + # def diff output = '' @diffs.each do |piece| - hunk = Diff::LCS::Hunk.new(@old, @new, piece, context_lines, length_difference) - output << hunk.diff(format) + hunk = Diff::LCS::Hunk.new(@old, @new, piece, CONTEXT_LINES, @length_difference) + output << hunk.diff(FORMAT) output << "\n" end output end memoize :diff + # Return colorized source diff + # + # @return [String] + # + # @api private + # def colorized_diff diff.lines.map do |line| self.class.colorize_line(line) @@ -37,6 +33,35 @@ module Mutant end memoize :colorized_diff + private + + FORMAT = :unified + CONTEXT_LINES = 3 + + # Initialize differ object + # + # @param [String] old + # @param [String] new + # + # @return [undefined] + # + # @api private + # + def initialize(old, new) + @new, @old = new.lines.map(&:chomp), old.lines.map(&:chomp) + @length_difference = @new.size - @old.size + @diffs = Diff::LCS.diff(@old, @new) + end + + # Return colorized diff line + # + # @param [String] line + # + # @return [String] + # returns colorized line + # + # @api private + # def self.colorize_line(line) case line[0].chr when '+' diff --git a/lib/mutant/killer.rb b/lib/mutant/killer.rb index a50a5f4c..a181701a 100644 --- a/lib/mutant/killer.rb +++ b/lib/mutant/killer.rb @@ -4,7 +4,7 @@ module Mutant include Immutable, Abstract extend MethodObject - # Check if mutant was killed + # Test for kill failure # # @return [true] # returns true when mutant was killed @@ -30,6 +30,8 @@ module Mutant # # @return [String] # + # @api private + # def original_source mutation.original_source end @@ -38,16 +40,23 @@ module Mutant # # @return [String] # + # @api private + # def mutation_source mutation.source end private + # Return mutation to kill + # + # @return [Mutation] + # + # @api private attr_reader :mutation private :mutation - # Initialize runner and run the test + # Initialize killer object # # @param [Mutation] mutation # diff --git a/lib/mutant/reporter/cli.rb b/lib/mutant/reporter/cli.rb index c4e9a274..970d3f22 100644 --- a/lib/mutant/reporter/cli.rb +++ b/lib/mutant/reporter/cli.rb @@ -14,9 +14,9 @@ module Mutant @io.puts("Subject: #{subject.identification}") end - # Report mutations + # Report mutation # - # @param [Mutation] mutations + # @param [Mutation] mutation # # @return [self] # @@ -81,6 +81,10 @@ module Mutant # # @api private # + # @return [String] + # returns colorized string if color is enabled + # returns unmodified message otherwise + # def colorize(color, message) color = Color::NONE unless color? color.format(message) diff --git a/lib/mutant/runner.rb b/lib/mutant/runner.rb index 6cf009a4..33a6133c 100644 --- a/lib/mutant/runner.rb +++ b/lib/mutant/runner.rb @@ -4,17 +4,47 @@ module Mutant include Immutable extend MethodObject + # Return killers with errors + # + # @return [Enumerable] + # + # @api private + # attr_reader :errors + # Test for failure + # + # @return [true] + # returns true when there are left mutations + # + # @return [false] + # returns false othewise + # + # @api private + # def fail? !errors.empty? end private + # Return reporter + # + # @return [Reporter] + # + # @api private + # attr_reader :reporter private :reporter + # Initialize runner object + # + # @param [Hash] options + # + # @return [undefined] + # + # @api private + # def initialize(options) @killer = options.fetch(:killer) do raise ArgumentError, 'Missing :killer in options' @@ -31,6 +61,12 @@ module Mutant run end + # Run mutation killers on subjects + # + # @return [undefined] + # + # @api private + # def run @subjects = subjects.each do |subject| reporter.subject(subject) @@ -38,6 +74,14 @@ module Mutant end end + # Run mutation killers on subject + # + # @param [Subject] subject + # + # @return [undefined] + # + # @api private + # def run_subject(subject) subject.each do |mutation| reporter.mutation(mutation) @@ -46,6 +90,14 @@ module Mutant subject.reset end + # Run killer on mutation + # + # @param [Mutation] mutation + # + # @return [undefined] + # + # @api private + # def kill(mutation) killer = @killer.run(mutation) reporter.killer(killer) diff --git a/lib/mutant/subject.rb b/lib/mutant/subject.rb index 6c231e73..e06d1713 100644 --- a/lib/mutant/subject.rb +++ b/lib/mutant/subject.rb @@ -86,6 +86,11 @@ module Mutant memoize :original_root # Reset subject into original state + # + # @return [self] + # + # @api private + # def reset Loader.run(original_root) end