From 188a1904d2ac2b31fb1eabfe0a730db85dc77084 Mon Sep 17 00:00:00 2001 From: Luke Fitzpatrick Date: Fri, 20 Jan 2017 10:24:09 +1100 Subject: [PATCH] Fix bootstrap block encode exclusions, add test. --- lib/Biodiverse/TreeNode/BootstrapBlock.pm | 12 +++++++---- t/32-BootstrapBlock.t | 26 ++++++++++++++++++----- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/lib/Biodiverse/TreeNode/BootstrapBlock.pm b/lib/Biodiverse/TreeNode/BootstrapBlock.pm index c486b2d57..4a561bc6b 100644 --- a/lib/Biodiverse/TreeNode/BootstrapBlock.pm +++ b/lib/Biodiverse/TreeNode/BootstrapBlock.pm @@ -68,11 +68,15 @@ sub decode_bootstrap_block { # excluded_keys is an array ref of keys not to include in the block sub encode_bootstrap_block { my ($self, %args) = @_; - my @excluded_keys = @{$args{exclusions}}; - my %boot_values = %$self; - delete $boot_values{@excluded_keys}; - + + if($args{exclusions}) { + my @excluded_keys = @{$args{exclusions}}; + foreach my $exclusion (@excluded_keys) { + delete $boot_values{$exclusion}; + } + } + my $json_string = encode_json \%boot_values; # the json encoder uses { and } to delimit data, but bootstrap diff --git a/t/32-BootstrapBlock.t b/t/32-BootstrapBlock.t index d323b2184..9ba358ffd 100644 --- a/t/32-BootstrapBlock.t +++ b/t/32-BootstrapBlock.t @@ -9,7 +9,7 @@ use Test::Lib; use Test::More; use Test::Exception; -use Biodiverse::BootstrapBlock; +use Biodiverse::TreeNode::BootstrapBlock; use Devel::Symdump; my $obj = Devel::Symdump->rnew(__PACKAGE__); @@ -29,7 +29,7 @@ sub main { # testing basic set_value/get_value operations on the bootstrap block. sub test_basic_operations { - my $bootstrap_block = Biodiverse::BootstrapBlock->new(); + my $bootstrap_block = Biodiverse::TreeNode::BootstrapBlock->new(); my %hash = ( "foo" => "bar", "footwo" => "bartwo", @@ -58,7 +58,7 @@ sub test_decode { ); - my $bootstrap_block = Biodiverse::BootstrapBlock->new(); + my $bootstrap_block = Biodiverse::TreeNode::BootstrapBlock->new(); foreach my $input (@raw_inputs) { $bootstrap_block->decode_bootstrap_block( raw_bootstrap => $input ); @@ -79,7 +79,7 @@ sub test_encode { "foothree" => "barthree", ); - my $bootstrap_block = Biodiverse::BootstrapBlock->new(); + my $bootstrap_block = Biodiverse::TreeNode::BootstrapBlock->new(); foreach my $key (keys %hash) { $bootstrap_block->set_value( key => $key, value => $hash{ $key } ); @@ -87,7 +87,6 @@ sub test_encode { my $actual = $bootstrap_block->encode_bootstrap_block(); - # we don't know what order the bootstrap block will be written, so # just look for the pairs we know should be there. foreach my $key (keys %hash) { @@ -95,4 +94,21 @@ sub test_encode { ok (index($actual, $expected_string) != -1, "Block contained $expected_string") } + + # also test an encoding with exclusions + my @exclusions = ("foo"); + $actual = + $bootstrap_block->encode_bootstrap_block(exclusions => \@exclusions); + + delete $hash{"foo"}; + foreach my $key (keys %hash) { + my $expected_string = "\"$key\":\"$hash{$key}\""; + ok (index($actual, $expected_string) != -1, + "Block contained $expected_string") + } + ok (index($actual, '"foo":"bar"') == -1, + "Block didn't contain excluded item") + + } +