Algorithm-Diff-Apply
view release on metacpan or search on metacpan
lib/Algorithm/Diff/Apply.pm view on Meta::CPAN
__apply_alternatives(target => \@ary,
alts => \@alts,
opts => \%opt);
return wantarray ? @ary : \@ary;
}
# Converts all the hunks in an Algorithm::Diff-style diff to a
# normalised form in which all hunks are a) still internally
# contiguous, and b) have start indices which refer to items in the
# original array, before any diffs are applied. Normally, hunks
# consisting of only inserts don't meet criterion b).
#
# Allso attaches hash data if the hashing function is defined.
sub __homogenise_diff
{
my ($orig_diff, %opt) = @_;
my @hdiff = ();
my $delta = 0; # difference between orig and resultant
foreach my $orig_hunk (@$orig_diff)
t/80optsimple.t view on Meta::CPAN
#!/usr/bin/perl -w
# Tests of simple, identical-chunk optimisation.
use Algorithm::Diff qw{diff};
use Test::Simple tests => 9;
use Algorithm::Diff::Apply qw{apply_diffs optimise_remove_duplicates};
use constant TEST_OPTIMISERS => [\&optimise_remove_duplicates];
use strict;
my ($original, $derived, $changes, $expected, $result);
$original = [qw{a b c d e f g h i j k l m n o p q}] ;
$derived = [qw{a b c d x y z h i j k l m n o p q}] ;
$changes = diff($original, $derived);
# First of all, we should get a conflict if we override the
# optimisation completely.
$expected = join(':', @$derived);
$result = join(':', apply_diffs($original, {optimisers => []},
d1 => $changes,
d2 => $changes,
));
ok($result =~ /\>\>\>/);
# Allowing normal behaviour makes the problem above go away ...
$result = join(':', apply_diffs($original, {optimisers => TEST_OPTIMISERS},
d1 => $changes,
d2 => $changes,
));
ok($result !~ /\>\>\>/);
ok($result eq $expected);
# ... no matter how many identical chunks we throw at it.
$result = join(':', apply_diffs($original, {optimisers => TEST_OPTIMISERS},
d1 => $changes,
d2 => $changes,
d3 => $changes,
d4 => $changes,
d5 => $changes,
));
ok($result !~ /\>\>\>/);
ok($result eq $expected);
# The blocks remaining after this optimisation can still conflict with
# other diffs - and when optimise_remove_duplicates() optimises a
# bunch of identical hunks from different tagged sequences, the
# remaining hunk is kept under the first tag.
my $derived2 = [qw{a b c d e f 1 2 3 h i j k l m n o p q}];
my $changes2 = diff($original, $derived2);
$result = join(':', apply_diffs($original, {optimisers => TEST_OPTIMISERS},
'_03_third' => $changes2,
'_02_second' => $changes, # }__ identical
'_01_first' => $changes, # }
));
ok($result =~ />>>/);
ok($result =~ />>>\s+_01_first/);
ok($result !~ />>>\s+_02_second/);
ok($result =~ />>>\s+_03_third/);
t/81opthash.t view on Meta::CPAN
#!/usr/bin/perl -w
# Ensure that key-based hashing works.
use Algorithm::Diff qw{diff};
use Test::Simple tests => 2;
use Algorithm::Diff::Apply qw{apply_diffs};
use strict;
my ($original, $derived1, $derived2, $changes1, $changes2, $result, $hasher);
# First consider double-underscore-prefixed numbers to all be
# identical. We shouldn't get a conflict.
sub mkhash
{
local $_ = shift;
/^__\d+$/ and return "__";
return $_;
}
$original = [qw{a b c d e f g h i j k l m n o x y}] ;
$derived1 = [qw{a b c d x y z h i j k l m n o __1 __4}] ;
$derived2 = [qw{a b c d x y z h i j k l m n o __3 __7}] ;
$changes1 = diff($original, $derived1);
$changes2 = diff($original, $derived2);
$result = join(':', apply_diffs($original, { key_generator => \&mkhash },
d1 => $changes1,
d2 => $changes2,
));
ok($result !~ /\>\>\>/);
# Then make sure we're not deluding ourselves by detecting a genuine
# conflict when called with an otherwise identical context.
$original = [qw{a b c d e f g h i j k l m n o x y}] ;
$derived1 = [qw{a b c d x y z h i j k l m n o __1 __4}] ;
$derived2 = [qw{a b c d z! y! x! h i j k l m n o __3 __7}] ;
$changes1 = diff($original, $derived1);
$changes2 = diff($original, $derived2);
$result = join(':', apply_diffs($original, { key_generator => \&mkhash },
d1 => $changes1,
d2 => $changes2,
));
ok($result =~ /\>\>\>/);
( run in 0.255 second using v1.01-cache-2.11-cpan-1c8d708658b )