Skip to content

Commit

Permalink
Fix bootstrap block encode exclusions, add test.
Browse files Browse the repository at this point in the history
  • Loading branch information
LukedFitzpatrick committed Jan 19, 2017
1 parent 588341b commit 188a190
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
12 changes: 8 additions & 4 deletions lib/Biodiverse/TreeNode/BootstrapBlock.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 21 additions & 5 deletions t/32-BootstrapBlock.t
Original file line number Diff line number Diff line change
Expand Up @@ -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__);
Expand All @@ -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",
Expand Down Expand Up @@ -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 );
Expand All @@ -79,20 +79,36 @@ 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 } );
}

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) {
my $expected_string = "\"$key\":\"$hash{$key}\"";
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")


}

0 comments on commit 188a190

Please sign in to comment.