1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
#!perl
#
# tests for skipping records matching a comment regex
#
use strict;
use File::Spec::Functions;
use FindBin qw( $Bin );
use Readonly;
use Test::Exception;
use Test::More tests => 5;
use Text::RecordParser;
Readonly my $TEST_DATA_DIR => catdir( $Bin, 'data' );
{
my $p = Text::RecordParser->new;
throws_ok { $p->comment('foo') } qr/look like a regex/i,
'"comment" rejects non-regex argument';
}
{
my $file = catfile( $TEST_DATA_DIR, 'commented.dat' );
my $p = Text::RecordParser->new(
filename => $file,
comment => qr/^#/,
);
$p->bind_header;
my $row1 = $p->fetchrow_hashref;
is( $row1->{'field1'}, 'foo', 'Field is "foo"' );
my $row2 = $p->fetchrow_hashref;
is( $row2->{'field2'}, 'bang', 'Field is "bang"' );
}
{
my $file = catfile( $TEST_DATA_DIR, 'commented2.dat' );
my $p = Text::RecordParser->new(
filename => $file,
comment => qr/^--/,
);
$p->bind_header;
my $row1 = $p->fetchrow_hashref;
is( $row1->{'field1'}, 'foo', 'Field is "foo"' );
my $row2 = $p->fetchrow_hashref;
is( $row2->{'field2'}, 'bang', 'Field is "bang"' );
}
|