File: 02-filename-fh.t

package info (click to toggle)
libtext-recordparser-perl 1.6.5-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 440 kB
  • sloc: perl: 3,351; makefile: 4
file content (200 lines) | stat: -rw-r--r-- 5,652 bytes parent folder | download | duplicates (5)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!perl

#
# tests for "filename," "fh," "data," etc.
#

use strict;
use File::Spec::Functions;
use File::Temp qw( tempfile );
use FindBin qw( $Bin );
use IO::File;
use Readonly;
use Test::Exception;
use Test::More tests => 41;
use Text::RecordParser;

Readonly my $TEST_DATA_DIR => catdir( $Bin, 'data' );

{
    my $p = Text::RecordParser->new;

    is( $p->filename, '', 'Filename is blank' );

    my $file = catfile( $TEST_DATA_DIR, 'simpsons.csv' );
    is( $p->filename($file), $file, 'Filename sets OK' );

    throws_ok { $p->filename($TEST_DATA_DIR) } qr/cannot use dir/i, 
        'filename rejects directory for argument';

    my $bad_file = catfile( $TEST_DATA_DIR, 'non-existent' );
    throws_ok { $p->filename($bad_file) } qr/file does not exist/i, 
        'filename rejects non-existent file';

    my @fields = $p->field_list;
    ok( @fields, 'Got field list' );
    my $file2 = catfile( $TEST_DATA_DIR, 'simpsons.tab' );
    $p->filename( $file2 ); 
    my @fields2 = $p->field_list;
    ok( join(',', @fields) ne join(',', @fields2), 
        'Field list is flushed when resetting filename' );
}

#
# Filehandle tests
#
{
    my $p = Text::RecordParser->new;

    my $file = catfile( $TEST_DATA_DIR, 'simpsons.csv' );
    open my $fh, '<', $file or die "Read of '$file' failed: $!";
    is ( ref $p->fh( $fh ), 'GLOB', 'fh is a filehandle' );

    # Cause an error by closing the existing fh.
    close $fh;
    my $tabfile = catfile( $TEST_DATA_DIR, 'simpsons.tab' );
    open my $fh2, '<', $tabfile or die "Read of '$tabfile' failed: $!";

    throws_ok { $p->fh( $fh2 ) } qr/can't close existing/i, 
        'fh catches bad close';

    throws_ok { $p->fh('') } qr/doesn't look like a filehandle/i, 
        'fh catches bad arg';

    my $file3 = catfile( $TEST_DATA_DIR, 'simpsons.cvs');
    my $io = IO::File->new( $file3 );
    is ( ref $p->fh( $io ), 'GLOB', 'fh is a filehandle' );
}

{
    # cause an error on a closed filehandle
    my ( $fh, $filename ) = tempfile();

    my $p = Text::RecordParser->new( fh => $fh );

    close $fh;
    unlink $filename;

    my $file = catfile( $TEST_DATA_DIR, 'simpsons.csv' );

    throws_ok { $p->filename( $file ) } qr/Can't close previously opened/, 
        'filename dies trying to close a closed filehandle';
}

{
    # cause an error on a disappearing file
    my $p = Text::RecordParser->new;
    my ( $fh, $filename ) = tempfile();
    $p->filename( $filename );
    close $fh;
    unlink $filename;
    throws_ok { my $data = $p->fh } qr/Cannot read '\Q$filename\E'/, 
        'fh dies on bad file'; 
}

#
# Data tests
#
{
    my $p = Text::RecordParser->new;
    throws_ok { $p->data() } qr/without any arguments/, 
        'data called without args dies';
}

{
    my $p = Text::RecordParser->new;
    throws_ok { $p->data('') } qr/no usable/i, 'data dies with no usable data';
}

{
    my $p = Text::RecordParser->new;
    my $scalar = "lname,fname,age\nSmith,Joan,20\nDoe,James,21\n";
    $p->data( \$scalar );
    $p->bind_header;
    my @fields = $p->field_list;

    is( scalar @fields, 3, 'data accepted scalar ref' );
}

{
    my $p = Text::RecordParser->new;
    $p->data( "lname,fname,age\n", "Smith,Joan,20\nDoe,James,21\n" );
    $p->bind_header;
    my @fields = $p->field_list;

    is( scalar @fields, 3, 'data accepted an array' );
}

{
    my $p = Text::RecordParser->new;
    my @array = ( "lname,fname,age\n", "Smith,Joan,20\nDoe,James,21\n" );
    $p->data( \@array );
    $p->bind_header;
    my @fields = $p->field_list;

    is( scalar @fields, 3, 'data accepted an array ref' );
}

{
    my $p      = Text::RecordParser->new;
    my $scalar = "lname,fname,age\nSmith,Joan,20\nDoe,James,21\n";

    ok( $p->data( $scalar ), 'data accepts a scalar' );
    $p->bind_header;
    my @fields = $p->field_list;
    is( $fields[0], 'lname', 'lname field' );
    is( $fields[1], 'fname', 'fname field' );
    is( $fields[2], 'age', 'age field' );

    my $rec = $p->fetchrow_hashref;
    is( $rec->{'lname'}, 'Smith', 'lname = "Smith"' );
    is( $rec->{'fname'}, 'Joan', 'fname = "Joan"' );
    is( $rec->{'age'}, '20', 'age = "20"' );

    $rec = $p->fetchrow_array;
    is( $rec->[0], 'Doe', 'lname = "Doe"' );
    is( $rec->[1], 'James', 'fname = "James"' );
    is( $rec->[2], '21', 'age = "21"' );

    $p->data( 
        "name\tinstrument\n", 
        "Miles Davis\ttrumpet\n", 
        "Art Blakey\tdrums\n" 
    );

    $p->field_separator("\t");
    $p->bind_header;
    @fields = $p->field_list;
    is( $fields[0], 'name', 'name field' );
    is( $fields[1], 'instrument', 'instrument field' );

    $rec = $p->fetchrow_array;
    is( $rec->[0], 'Miles Davis', 'name = "Miles Davis"' );
    is( $rec->[1], 'trumpet', 'instrument = "trumpet"' );

    $rec = $p->fetchrow_hashref;
    is( $rec->{'name'}, 'Art Blakey', 'name = "Art Blakey"' );
    is( $rec->{'instrument'}, 'drums', 'instrument = "drums"' );

    my $filename = "$Bin/data/simpsons.csv";
    open my $fh, "<$filename" or die "Can't read '$filename': $!";
    is ( $p->data( $fh ), 1, 'data accepts a filehandle' );
    is ( UNIVERSAL::isa( $p->fh, 'GLOB' ), 1, 'fh is a GLOB' );
}

{
    my $p    = Text::RecordParser->new(
        data => "lname,fname,age\nSmith,Joan,20\nDoe,James,21\n"
    );

    $p->bind_header;
    my @fields = $p->field_list;
    is( $fields[0], 'lname', 'lname field' );
    is( $fields[1], 'fname', 'fname field' );
    is( $fields[2], 'age', 'age field' );

    my $rec = $p->fetchrow_hashref;
    is( $rec->{'lname'}, 'Smith', 'lname = "Smith"' );
    is( $rec->{'fname'}, 'Joan', 'fname = "Joan"' );
    is( $rec->{'age'}, '20', 'age = "20"' );
}