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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
#!perl
use strict;
use File::Spec::Functions;
use FindBin '$Bin';
use Readonly;
use Test::Exception;
use Test::More tests => 26;
Readonly my $TEST_DATA_DIR => catdir( $Bin, 'data' );
require_ok( 'Text::RecordParser' );
require_ok( 'Text::RecordParser::Tab' );
#
# Vanilla "new," test defaults
#
{
my $p = Text::RecordParser->new;
isa_ok( $p, 'Text::RecordParser' );
is( $p->filename, '', 'Filename is blank' );
is( $p->fh, undef, 'Filehandle is undefined' );
is( $p->field_filter, '', 'Field filter is blank' );
is( $p->header_filter, '', 'Header filter is blank' );
is( $p->field_separator, ',', 'Default separator is a comma' );
is( $p->trim, undef, 'Default trim value is undefined' );
}
#
# New with arguments
#
{
my $file = catfile( $TEST_DATA_DIR, 'simpsons.csv' );
my $p = Text::RecordParser->new($file);
is( $p->filename, $file, 'Filename sets OK' );
}
{
my $file = catfile( $TEST_DATA_DIR, 'simpsons.csv' );
my $p = Text::RecordParser->new( { filename => $file } );
is( $p->filename, $file, 'Filename as hashref sets OK' );
}
{
my $file = catfile( $TEST_DATA_DIR, 'simpsons.csv' );
my $p = Text::RecordParser->new(
filename => $file,
field_separator => "\t",
record_separator => "\n\n",
field_filter => sub { $_ = shift; s/ /_/g; $_ },
header_filter => sub { $_ = shift; s/\s+/_/g; lc $_ },
trim => 1,
);
is( $p->filename, $file, 'Filename set OK' );
is( $p->field_separator, "\t", 'Field separator is a tab' );
is( $p->record_separator, "\n\n", 'Record separator is two newlines' );
is( ref $p->field_filter, 'CODE', 'Field filter is code' );
is( ref $p->header_filter, 'CODE', 'Header filter is code' );
is( $p->trim, 1, 'Trim mode is on' );
}
{
my $p = Text::RecordParser->new;
is( $p->trim, undef, 'trim with no args is undefined' );
is( $p->trim('foo'), 1, 'trim with non-false arg is true' );
is( $p->trim(''), 0, 'trim with false arg is false' );
}
#
# New with shortened arguments
#
{
my $p = Text::RecordParser->new({
fs => "\t",
rs => "\n\n",
});
is( $p->field_separator, "\t", 'Shortened field separator arg OK' );
is( $p->record_separator, "\n\n", 'Shortened record separator arg OK' );
}
#
# New with too many arguments
#
{
throws_ok {
my $p = Text::RecordParser->new(
filename => catfile( $TEST_DATA_DIR, 'simpsons.csv' ),
data => "foo\tbar\tbaz",
);
}
qr/too many arguments/,
'new dies because of too many data args';
}
#
# New with just one arg
#
{
my $file = catfile( $TEST_DATA_DIR, 'simpsons.csv' );
my $p = Text::RecordParser->new( $file );
is( $p->filename, $file, 'One argument taken as filename' );
}
#
# New Tab
#
{
my $p = Text::RecordParser::Tab->new;
isa_ok( $p, 'Text::RecordParser' );
is( $p->field_separator, "\t", 'New T::RP::Tab has tab for field sep' );
}
|