From 998bf4119a99ca89fae602d585551c31bbc6caee Mon Sep 17 00:00:00 2001 From: mr-mixas Date: Thu, 30 Nov 2017 10:35:17 +0300 Subject: [PATCH] batch tests for hashes --- MANIFEST | 4 +- t/diff-structures.t | 201 -------------------------------------------- t/hashes.t | 166 ++++++++++++++++++++++++++++++++++++ 3 files changed, 168 insertions(+), 203 deletions(-) delete mode 100644 t/diff-structures.t create mode 100644 t/hashes.t diff --git a/MANIFEST b/MANIFEST index a4f0790..1dae609 100644 --- a/MANIFEST +++ b/MANIFEST @@ -1,7 +1,7 @@ Changes lib/Struct/Diff.pm Makefile.PL -MANIFEST This list of files +MANIFEST README t/000-load.t t/_common.pm @@ -9,7 +9,7 @@ t/arrays.t t/blessed.t t/coderefs.t t/diff-primitives.t -t/diff-structures.t +t/hashes.t t/list_diff.t t/manifest.t t/mixed_structures.t diff --git a/t/diff-structures.t b/t/diff-structures.t deleted file mode 100644 index a973731..0000000 --- a/t/diff-structures.t +++ /dev/null @@ -1,201 +0,0 @@ -#!perl -T - -use strict; -use warnings; - -use Struct::Diff qw(diff); -use Test::More tests => 21; - -use lib "t"; -use _common qw(j_diff scmp); - -local $Storable::canonical = 1; # to have equal snapshots for equal by data hashes - -my ($a, $b, $d, $frozen_a, $frozen_b); - -### hashes ### -$d = j_diff("empty_hash_vs_hash_with_one_key", {}, {one => 1}); -is_deeply($d, {D => {one => {A => 1}}}, "{} vs {one => 1}"); - -$d = j_diff("empty_hash_vs_hash_with_one_key_noA", {}, {one => 1}, noA => 1); -is_deeply($d, {}, "{} vs {a => 'va'}, noA => 1"); - -$d = j_diff("hash_with_one_key_vs_empty_hash", {one => 1}, {}); -is_deeply($d, {D => {one => {R => 1}}}, "{one => 1} vs {}"); - -$d = j_diff("hash_with_one_key_vs_empty_hash_noR", {one => 1}, {}, noR => 1); -is_deeply($d, {}, "{one => 1} vs {}, noR => 1"); - -$d = j_diff( - "subhash_emptied", - {one => {two => 2}}, - {one => {}}, -); -is_deeply( - $d, - {D => {one => {D => {two => {R => 2}}}}}, - "Subhash emptied" -); - -$d = j_diff( - "subhash_emptied_noR", - {one => {two => 2}}, - {one => {}}, - noR => 1 -); -is_deeply($d, {}, "Subhash emptied"); - - -$d = j_diff( - "subhash_filled", - {one => {}}, - {one => {two => 2}}, -); -is_deeply( - $d, - {D => {one => {D => {two => {A => 2}}}}}, - "Subhash filled" -); - -$d = j_diff( - "subhash_filled_noA", - {one => {}}, - {one => {two => 2}}, - noA => 1 -); -is_deeply($d, {}, "Subhash filled, but noA used"); - -$d = j_diff( - "nested_hashes_with_one_different_value", - {one =>{two => {three => 3}}}, - {one =>{two => {three => 4}}}, -); -is_deeply( - $d, - {D => {one => {D => {two => {D => {three => {N => 4,O => 3}}}}}}}, - "HASH: deep single-nested changed" -); - -$d = j_diff( - "one_key_added_to_subhash", - {one => {two => 2}}, - {one => {two => 2, three => 3}}, -); -is_deeply( - $d, - {D => {one => {D => {two => {U => 2}, three => {A => 3}}}}}, - "HASH: one subkey unchanged, one added, noU" -); - -$d = j_diff( - "one_key_added_to_subhash_noU", - {one => {two => 2}}, - {one => {two => 2, three => 3}}, - noU => 1 -); -is_deeply( - $d, - {D => {one => {D => {three => {A => 3}}}}}, - "HASH: one subkey unchanged, one added, noU" -); - -$d = j_diff( - "one_key_removed_from_subhash", - {one => {two => 2, three => 3}}, - {one => {two => 2}}, -); -is_deeply( - $d, - {D => {one => {D => {two => {U => 2}, three => {R => 3}}}}}, - "HASH: one subkey unchanged, one added, noU" -); - -$d = j_diff( - "one_key_removed_from_subhash_noU", - {one => {two => 2, three => 3}}, - {one => {two => 2}}, - noU => 1 -); -is_deeply( - $d, - {D => {one => {D => {three => {R => 3}}}}}, - "HASH: one subkey unchanged, one added, noU" -); - -$d = j_diff( - "deeply_nested_hash_vs_empty_hash", - {one => { two => { three => 3 }}}, - {}, - 'trimR' => 1 -); -is_deeply($d, {D => {one => { R => undef } }}, "{one => {two => {three => 3}}} vs {}, trimR => 1"); - -$d = j_diff( - "deeply_nested_hash_vs_empty_hash_trimR", - {one => { two => { three => 3 }}}, - {}, - 'trimR' => 1 -); -is_deeply($d, {D => {one => { R => undef } }}, "{one => {two => {three => 3}}} vs {}, trimR => 1"); - -$d = j_diff( - "deeply_nested_subhash_removed_from_hash", - {one => {two => {three => 3}}, four => 4}, - {four => 4}, -); -is_deeply( - $d, - {D => {one => {R => {two => {three => 3}}},four => {U => 4}}}, -); - -$d = j_diff( - "deeply_nested_subhash_removed_from_hash_trimR", - {one => { two => { three => 3}}, four => 4}, - {four => 4}, - 'trimR' => 1 -); -is_deeply( - $d, - {D => {one => {R => undef},four => {U => 4}}}, -); - -$d = j_diff( - "hash_with_one_different_value_noO", - {one => 1}, - {one => 2}, - noN => 1 -); -is_deeply($d, {D => {one => {O => 1}}}); - -$d = j_diff( - "hash_with_one_different_value_noO", - {one => 1}, - {one => 2}, - noO => 1 -); -is_deeply($d, {D => {one => {N => 2}}}); - -$a = {one => 1, two => {nine => 9, ten => 10}, three => 3}; -$b = {one => 1, two => {nine => 8, ten => 10}, four => 4}; - -$d = j_diff("complex_hash", $a, $b); -is_deeply( - $d, - { - D => { - one => {U => 1}, - two => {D => {nine => {N => 8,O => 9},ten => {U => 10}}}, - three => {R => 3}, - four => {A => 4} - } - }, - "complex hash" -); - -$d = j_diff("complex_hash_noU", $a, $b, noU => 1); -is_deeply( - $d, - {D => {two => {D => {nine => {N => 8,O => 9}}},three => {R => 3},four => {A => 4}}}, - "complex hash, noU => 1" -); - diff --git a/t/hashes.t b/t/hashes.t new file mode 100644 index 0000000..c12cfed --- /dev/null +++ b/t/hashes.t @@ -0,0 +1,166 @@ +#!perl -T + +use strict; +use warnings; + +use Test::More; + +use lib "t"; +use _common; + +my @TESTS = ( + { + a => {}, + b => {one => 1}, + desc => 'empty_hash_vs_hash_with_one_key', + diff => {D => {one => {A => 1}}}, + }, + { + a => {}, + b => {one => 1}, + desc => 'empty_hash_vs_hash_with_one_key_noA', + diff => {}, + opts => {noA => 1}, + }, + { + a => {one => 1}, + b => {}, + desc => 'hash_with_one_key_vs_empty_hash', + diff => {D => {one => {R => 1}}}, + }, + { + a => {one => 1}, + b => {}, + desc => 'hash_with_one_key_vs_empty_hash_noR', + diff => {}, + opts => {noR => 1}, + }, + { + a => {one => {two => 2}}, + b => {one => {}}, + desc => 'subhash_emptied', + diff => {D => {one => {D => {two => {R => 2}}}}}, + }, + { + a => {one => {two => 2}}, + b => {one => {}}, + desc => 'subhash_emptied_noR', + diff => {}, + opts => {noR => 1}, + }, + { + a => {one => {}}, + b => {one => {two => 2}}, + desc => 'subhash_filled', + diff => {D => {one => {D => {two => {A => 2}}}}}, + }, + { + a => {one => {}}, + b => {one => {two => 2}}, + desc => 'subhash_filled_noA', + diff => {}, + opts => {noA => 1}, + }, + { + a => {one =>{two => {three => 3}}}, + b => {one =>{two => {three => 4}}}, + desc => 'nested_hashes_with_one_different_value', + diff => {D => {one => {D => {two => {D => {three => {N => 4,O => 3}}}}}}}, + }, + { + a => {one => {two => 2}}, + b => {one => {two => 2, three => 3}}, + desc => 'one_key_added_to_subhash', + diff => {D => {one => {D => {two => {U => 2}, three => {A => 3}}}}}, + }, + { + a => {one => {two => 2}}, + b => {one => {two => 2, three => 3}}, + desc => 'one_key_added_to_subhash_noU', + diff => {D => {one => {D => {three => {A => 3}}}}}, + opts => {noU => 1}, + }, + { + a => {one => {two => 2, three => 3}}, + b => {one => {two => 2}}, + desc => 'one_key_removed_from_subhash', + diff => {D => {one => {D => {two => {U => 2}, three => {R => 3}}}}}, + }, + { + a => {one => {two => 2, three => 3}}, + b => {one => {two => 2}}, + desc => 'one_key_removed_from_subhash_noU', + diff => {D => {one => {D => {three => {R => 3}}}}}, + opts => {noU => 1}, + }, + { + a => {one => {two => {three => 3}}}, + b => {}, + desc => 'deeply_nested_hash_vs_empty_hash', + diff => {D => {one => {R => {two => {three => 3}}}}}, + }, + { + a => {one => {two => {three => 3}}}, + b => {}, + desc => 'deeply_nested_hash_vs_empty_hash_trimR', + diff => {D => {one => { R => undef } }}, + opts => {trimR => 1}, + }, + { + a => {one => {two => {three => 3}}, four => 4}, + b => {four => 4}, + desc => 'deeply_nested_subhash_removed_from_hash', + diff => {D => {one => {R => {two => {three => 3}}},four => {U => 4}}}, + }, + { + a => {one => {two => {three => 3}}, four => 4}, + b => {four => 4}, + desc => 'deeply_nested_subhash_removed_from_hash_trimR', + diff => {D => {one => {R => undef},four => {U => 4}}}, + opts => {trimR => 1}, + }, + { + a => {one => 1}, + b => {one => 2}, + desc => 'hash_with_one_different_value_noN', + diff => {D => {one => {O => 1}}}, + opts => {noN => 1}, + }, + { + a => {one => 1}, + b => {one => 2}, + desc => 'hash_with_one_different_value_noO', + diff => {D => {one => {N => 2}}}, + opts => {noO => 1}, + }, + { + a => {one => 1, two => {nine => 9, ten => 10}, three => 3}, + b => {one => 1, two => {nine => 8, ten => 10}, four => 4}, + desc => 'complex_hash', + diff => { + D => { + one => {U => 1}, + two => {D => {nine => {N => 8,O => 9},ten => {U => 10}}}, + three => {R => 3}, + four => {A => 4} + } + }, + }, + { + a => {one => 1, two => {nine => 9, ten => 10}, three => 3}, + b => {one => 1, two => {nine => 8, ten => 10}, four => 4}, + desc => 'complex_hash_noU', + diff => { + D => { + two => {D => {nine => {N => 8,O => 9}}}, + three => {R => 3}, + four => {A => 4} + } + }, + opts => {noU => 1}, + }, +); + +run_batch_tests(@TESTS); + +done_testing();