Skip to content

Commit

Permalink
Skeleton for unquted bootstrap block import handling.
Browse files Browse the repository at this point in the history
Updates #630
  • Loading branch information
LukedFitzpatrick committed Jan 20, 2017
1 parent d9b6c9d commit 56ef3b5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
20 changes: 17 additions & 3 deletions lib/Biodiverse/TreeNode/BootstrapBlock.pm
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,16 @@ sub decode_bootstrap_block {
my ($self, %args) = @_;
my $input = $args{ raw_bootstrap };

# fix up unquoted key/value pairs i.e. add quotes because the json
# decoder doesn't work without them.
$input = $self->fix_up_unquoted_bootstrap_block( block => $input );

# will replace first and last use of [ and ] respectively.
$input =~ s/\[/\{/;
$input = scalar reverse $input; # cheeky
$input =~ s/\]/\}/;
$input = scalar reverse $input;


# TODO Make this deal with unquoted bootstrap blocks: currently fails.

my $decoded_hash = decode_json $input;

foreach my $key (keys %$decoded_hash) {
Expand Down Expand Up @@ -95,4 +96,17 @@ sub encode_bootstrap_block {
}



# add quotes to unquoted bootstrap blocks
# e.g. [key:value,key2:value2] goes to ["key":"value","key2":"value2"]
sub fix_up_unquoted_bootstrap_block {
my ($self, %args) = @_;
my $block = $args{block};

# do some crazy regex here

return $block;
}


1;
26 changes: 22 additions & 4 deletions t/32-BootstrapBlock.t
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ sub main {
no strict 'refs';
$sub->();
}

done_testing();
return 0;
}
Expand All @@ -49,7 +48,7 @@ sub test_basic_operations {
}

sub test_decode {
my @raw_inputs = ('["foo":"bar","footwo":"bartwo","foothree":"barthree"]');
my @raw_inputs = ('["foo":"bar",\'footwo\':"bartwo",foothree:barthree]');


my %hash = ( "foo" => "bar",
Expand Down Expand Up @@ -97,7 +96,7 @@ sub test_encode {

# also test an encoding with exclusions
my @exclusions = ("foo");
$actual =
$actual =
$bootstrap_block->encode_bootstrap_block(exclusions => \@exclusions);

delete $hash{"foo"};
Expand All @@ -108,7 +107,26 @@ sub test_encode {
}
ok (index($actual, '"foo":"bar"') == -1,
"Block didn't contain excluded item")
}


sub test_fix_up_unquoted_bootstrap_block {
my $bootstrap_block = Biodiverse::TreeNode::BootstrapBlock->new();
my %test_hash = (
'[key:value,key2:value2]' => '["key":"value","key2":"value2"]',
'[key:value]' => '["key":"value"]',
'["key":"value"]' => '["key":"value"]',
'["key":"value",key2:value2,"key3":"value3"]'
=> '["key":"value","key2":"value2","key3":"value3"]',
);

foreach my $key (keys %test_hash) {
my $result =
$bootstrap_block->fix_up_unquoted_bootstrap_block( block => $key);

is ( $result,
$test_hash{$key},
"$key processed correctly",
);
}
}

0 comments on commit 56ef3b5

Please sign in to comment.