Skip to content

Commit

Permalink
diff fixed for emptied/filled subhashes
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-mixas committed Nov 13, 2017
1 parent 34fe109 commit 3f2fcf9
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Struct::Diff - Recursive diff for nested perl structures

# VERSION

Version 0.91
Version 0.92

# SYNOPSIS

Expand Down
10 changes: 5 additions & 5 deletions lib/Struct/Diff.pm
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ Struct::Diff - Recursive diff for nested perl structures
=head1 VERSION
Version 0.91
Version 0.92
=cut

our $VERSION = '0.91';
our $VERSION = '0.92';

=head1 SYNOPSIS
Expand Down Expand Up @@ -171,12 +171,13 @@ sub diff($$;@) {
my @keys = keys %{{ %{$a}, %{$b} }}; # uniq keys for both hashes
return $opts{noU} ? {} : { U => {} } unless (@keys);

my ($alt, $sd);
my ($alt, $sd, $same);
for my $key (@keys) {
if (exists $a->{$key} and exists $b->{$key}) {
if (freeze(\$a->{$key}) eq freeze(\$b->{$key})) {
$d->{U}->{$key} = $alt->{D}->{$key}->{U} = $a->{$key}
unless ($opts{noU});
$same++;
} else {
$sd = diff($a->{$key}, $b->{$key}, %opts);
if (exists $sd->{D}) {
Expand All @@ -197,8 +198,7 @@ sub diff($$;@) {
}
}

$d = $alt # return 'D' version of diff
if (keys %{$d} > 1 or ($sd) = values %{$d} and keys %{$sd} != @keys);
return $alt if ($alt and ($sd or $same and $same != @keys)); # 'D' version of diff
} elsif (ref $a eq 'Regexp' and $a != $b) {
if ($a eq $b) {
$d->{U} = $a unless ($opts{noU});
Expand Down
37 changes: 34 additions & 3 deletions t/diff-structures.t
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use strict;
use warnings;
use Storable qw(freeze);
use Struct::Diff qw(diff);
use Test::More tests => 39;
use Test::More tests => 42;

local $Storable::canonical = 1; # to have equal snapshots for equal by data hashes

Expand Down Expand Up @@ -137,6 +137,37 @@ is_deeply($d, {R => {a => 'va'}}, "{a => 'va'} vs {}");
$d = diff({ 'a' => 'va' }, {}, 'noR' => 1);
is_deeply($d, {}, "{a => 'va'} vs {}, noR => 1");

$d = diff(
{one => {two => 2}},
{one => {}},
);
is_deeply(
$d,
{D => {one => {R => {two => 2}}}},
"Subhash emptied"
);

$d = diff(
{one => {}},
{one => {two => 2}},
);
is_deeply(
$d,
{D => {one => {A => {two => 2}}}},
"Subhash filled"
);

$d = diff(
{one => {}},
{one => {two => 2}},
noA => 1
);
is_deeply(
$d,
{},
"Subhash filled, but noA used"
);

$d = diff(
{a =>{aa => {aaa => 'aaav'}}},
{a =>{aa => {aaa => 'aaan'}}},
Expand Down Expand Up @@ -169,10 +200,10 @@ is_deeply(
);

$d = diff({ 'a' => 'va' }, { 'a' => 'vb' }, 'noO' => 1);
is_deeply($d, {N => {a => 'vb'}}, "{a => 'va'} vs {a => 'vb'}, noO => 1");
is_deeply($d, {D => {a => {N=> 'vb'}}}, "{a => 'va'} vs {a => 'vb'}, noO => 1");

$d = diff({ 'a' => 'va' }, { 'a' => 'vb' }, 'noN' => 1);
is_deeply($d, {O => {a => 'va'}}, "{a => 'va'} vs {a => 'vb'}, noN => 1");
is_deeply($d, {D => {a => {O=>'va'}}}, "{a => 'va'} vs {a => 'vb'}, noN => 1");

$a = { 'a' => 'a1', 'b' => { 'ba' => 'ba1', 'bb' => 'bb1' }, 'c' => 'c1' };
$b = { 'a' => 'a1', 'b' => { 'ba' => 'ba2', 'bb' => 'bb1' }, 'd' => 'd1' };
Expand Down

0 comments on commit 3f2fcf9

Please sign in to comment.