Data-Beacon
view release on metacpan or search on metacpan
@l2 = @l2[0..3];
is_deeply( \@l2, $link, 'appendlink, list' );
}
# with prefix only
$b = beacon({PREFIX=>'x:'});
%t = (
'a|b|http://example.com/bar' => ['x:a','b','','http://example.com/bar'],
"a|b|http://example.com/bar\n" => ['x:a','b','','http://example.com/bar']
);
while (my ($line, $link) = each(%t)) {
ok( $b->appendline($line) );
$b->expanded; # multiple calls should not alter the link
$line =~ s/\n//;
is_deeply( [ $b->expanded ], $link, "expanded with PREFIX: $line" );
}
# file parsing
$b = beacon("~");
is( $b->errors, 1, 'failed to open file' );
$b = beacon( undef );
is( $b->errors, 0, 'no file specified' );
$b = beacon( "t/beacon1.txt" );
is_deeply( { $b->meta() }, {
'FORMAT' => 'BEACON',
'TARGET' => 'http://example.com/{ID}',
'FOO' => 'bar doz',
'PREFIX' => 'x:'
}, "parsing meta fields" );
is( $b->line, 6, 'line()' );
$b->parse();
is( $b->errors, 0 );
is( $b->count, 7 );
$b->parse("~");
is( $b->errors, 1 );
my $e = $b->lasterror;
is( $e, 'Failed to open ~', 'lasterror, scalar context' );
my @es = $b->lasterror;
is_deeply( \@es, [ 'Failed to open ~', 0, '' ], 'lasterror, list context' );
$b->parse( { } );
is( $b->errors, 1, 'cannot parse a hashref' );
# string parsing
$b->parse( \"x:from|x:to\n\n|comment" );
is( $b->count, 1, 'parse from string' );
is( $b->line, 3, '' );
is_deeply( [$b->link], ['x:from','','','x:to'] );
is_deeply( [$b->expanded], ['x:from','','','x:to'] );
$b->parse( \"\xEF\xBB\xBFx:from|x:to", links => sub { @l = @_; } );
is( $b->line, 1 );
is( $b->errors, 0 );
is_deeply( \@l, [ 'x:from', '', '', 'x:to' ], 'BOM' );
my @tmplines = ( '#FOO: bar', '#DOZ', '#BAZ: doz' );
$b->parse( from => sub { return shift @tmplines; } );
is( $b->line, 3, 'parse from code ref' );
is( $b->count, 0, '' );
is( $b->metafields, "#FORMAT: BEACON\n#BAZ: doz\n#FOO: bar\n#COUNT: 0\n" );
is( $b->link, undef, 'no links' );
$b->parse( from => sub { die 'hard'; } );
is( $b->errors, 1 );
ok( $b->lasterror =~ /^hard/, 'dead input will not kill us' );
# expected examples
$b = beacon( \"#EXAMPLES: a:b|c:d\na:b|to:1\nc:d|to:2" );
ok( $b->parse() );
$b = beacon( \"#EXAMPLES: a:b|c\na:b|to:1" );
$b->parse();
is_deeply( [ $b->lasterror ], [ 'examples not found: c',2,''], 'examples' );
$b = beacon( \"#EXAMPLES: a\n#PREFIX x:\na|to:1" );
ok( $b->parse() );
$b = beacon( \"#EXAMPLES: x:a\n#PREFIX x:\na|to:1" );
ok( $b->parse() );
# ensure that IDs are URIs
$b = beacon( \"xxx |foo" );
$b->parse();
is_deeply( [ $b->lasterror ], [ 'source is no URI: xxx',1,'xxx |foo' ],
'skipped non-URI id' );
# pull parsing
$b = beacon( \"\nid:1|t:1\n|comment\n" );
is_deeply( [$b->nextlink], ["id:1","","","t:1"] );
is_deeply( [$b->expanded], ["id:1","","","t:1"] );
is_deeply( [$b->nextlink], [] );
is_deeply( [$b->link], ["id:1","","","t:1"], 'last link' );
$b = beacon( \"id:1|t:1\na b|\nid:2|t:2" );
is_deeply( [$b->nextlink], ["id:1","","","t:1"] );
# a b| is ignored
is_deeply( [$b->nextlink], ["id:2","","","t:2"] );
is_deeply( [$b->link], ["id:2","","","t:2"] );
ok( !$b->nextlink );
is( $b->errors, 1 );
is_deeply( [ $b->lasterror ], [ 'source is no URI: a b',2,'a b|' ] );
# check method 'plainbeaconlink'
my @p = (
["",""],
["","","","http://example.com"]
);
while (@p) {
my $in = shift @p;
is( plainbeaconlink( @{$in} ), '', 'plainbeaconlink = ""');
}
@p = (
( run in 0.501 second using v1.01-cache-2.11-cpan-d8267643d1d )