Allow diffs to be created from overlapping hunks

[fix #228][fix #225]
This commit is contained in:
Markus Schirp 2014-08-11 17:35:18 +00:00
parent c9848f9e84
commit c976dfb209
5 changed files with 63 additions and 8 deletions

View file

@ -29,6 +29,7 @@ FeatureEnvy:
# Nature of OptionParser :(
- Mutant::CLI#add_environment_options
- Mutant::Env#scope_name
- Mutant::Diff#minimized_hunks
- Mutant::Integration::Rspec#run
- Mutant::Integration::Rspec::Rspec2#full_description
- Mutant::Integration::Rspec::Rspec3#full_description

View file

@ -18,9 +18,11 @@ module Mutant
# @api private
#
def diff
return unless diffs.length.equal?(1)
::Diff::LCS::Hunk.new(old, new, diffs.first, max_length, 0)
.diff(:unified) << NEWLINE
return if diffs.length.equal?(0)
minimized_hunks.map do |hunk|
hunk.diff(:unified) << NEWLINE
end.join
end
memoize :diff
@ -77,7 +79,41 @@ module Mutant
def diffs
::Diff::LCS.diff(old, new)
end
memoize :diffs
# Return hunks
#
# @return [Array<Diff::LCS::Hunk>]
#
# @api private
#
def hunks
diffs.map do |diff|
::Diff::LCS::Hunk.new(old, new, diff, max_length, 0)
end
end
# Return minimized hunks
#
# @return [Array<Diff::LCS::Hunk>]
#
# @api pirvate
#
# rubocop gets that one wrong. hunks = hunks() iS NOT equivalent to hunks = hunks, unparser does this right ;)
#
# rubocop:disable MethodCallParentheses
#
def minimized_hunks
hunks = hunks()
hunks.drop(1).each_with_object([hunks.first]) do |right, aggregate|
left = aggregate.last
if right.overlaps?(left)
right.merge(left)
aggregate.pop
end
aggregate << right
end
end
# Return max length
#

View file

@ -27,7 +27,7 @@ module Mutant
:>= => [:>, :==, :eql?, :equal?],
:<= => [:<, :==, :eql?, :equal?],
:> => [:==, :eql?, :equal?],
:< => [:==, :eql?, :equal?],
:< => [:==, :eql?, :equal?]
)
private

View file

@ -41,6 +41,26 @@ RSpec.describe Mutant::Diff do
subject { object.diff }
context 'when there is a diff at begin and end' do
let(:old) { %w[foo bar foo] }
let(:new) { %w[baz bar baz] }
let(:expectation) do
strip_indent(<<-STR)
@@ -1,4 +1,4 @@
-foo
+baz
bar
-foo
+baz
STR
end
it { should eql(expectation) }
it_should_behave_like 'an idempotent method'
end
context 'when there is a diff at begin of hunk' do
let(:old) { %w[foo bar] }
let(:new) { %w[baz bar] }

View file

@ -140,9 +140,7 @@ RSpec.describe Mutant::Reporter::CLI do
subject { described_class.build(output) }
let(:progressive_format) do
described_class::Format::Progressive.new(
tty: tty?,
)
described_class::Format::Progressive.new(tty: tty?)
end
let(:framed_format) do