Permalink
Browse files

Added tests for Text::CSV object passed to constructor

  • Loading branch information...
1 parent 4b09440 commit 693d14033f4e5a76df0f93ee0089a11ddd037755 @jberger committed Feb 8, 2012
Showing with 75 additions and 4 deletions.
  1. +23 −4 lib/Tie/Array/CSV.pm
  2. +52 −0 t/35-csv_obj.t
View
@@ -8,6 +8,8 @@ use Carp;
use Tie::File;
use Text::CSV;
+use Scalar::Util qw/blessed/;
+
use Tie::Array;
our @ISA = ('Tie::Array');
@@ -78,8 +80,13 @@ sub TIEARRAY {
tie @tiefile, 'Tie::File', $file, %{ $opts->{tie_file} || {} }
or croak "Cannot tie file $file";
- my $csv = Text::CSV->new($opts->{text_csv} || {})
- or croak "CSV (new) error: " . Text::CSV->error_diag();
+ my $csv;
+ if (blessed $opts->{text_csv} and $opts->{text_csv}->isa('Text::CSV')) {
+ $csv = $opts->{text_csv};
+ } else {
+ $csv = Text::CSV->new($opts->{text_csv} || {})
+ or croak "CSV (new) error: " . Text::CSV->error_diag();
+ }
my $self = {
file => \@tiefile,
@@ -372,7 +379,19 @@ C<tie_file> - hashref of options which are passed to the L<Tie::File> constructo
=item *
-C<text_csv> - hashref of options which are passed to the L<Text::CSV> constructor
+C<text_csv> - either:
+
+=over
+
+=item *
+
+hashref of options which are passed to the L<Text::CSV> constructor
+
+=item *
+
+an object which satisfies C<< isa('Text::CSV') >> (added in version 0.05)
+
+=back
=item *
@@ -389,7 +408,7 @@ Equivalent examples:
tie my @file, 'Tie::Array::CSV', 'filename', sep_char => ';';
-Note that as of version 0.05 the functionality from the former C<hold_row> option has been moved to the subclass module L<Tie::Array::CSV::HoldRow>. If deferring row operations is of interest to you, please see that module.
+Note that as of version 0.05 the functionality from the former C<hold_row> option has been separated into its own subclass module L<Tie::Array::CSV::HoldRow>. If deferring row operations is of interest to you, please see that module.
=head1 ERRORS
View
@@ -0,0 +1,52 @@
+
+use strict;
+use warnings;
+
+use Test::More;
+use File::Temp qw/tempfile/;
+
+use_ok( 'Tie::Array::CSV' );
+
+my $test_data = <<END_DATA;
+name,rank,serial number
+joel berger,plebe,1010101
+larry wall,general,1
+damian conway,colonel,1001
+END_DATA
+
+{
+ package My::Text::CSV;
+ use Text::CSV;
+ our @ISA = ('Text::CSV');
+
+ my $parsed_lines = 0;
+
+ sub my_parsed_lines { return $parsed_lines }
+
+ sub parse {
+ my $self = shift;
+
+ $parsed_lines++;
+
+ return $self->SUPER::parse(@_);
+ }
+}
+
+{
+ my ($fh, $file) = tempfile();
+ print $fh $test_data;
+
+ my $csv_obj = My::Text::CSV->new();
+
+ my @csv;
+ ok( tie(@csv, 'Tie::Array::CSV', $fh, {text_csv => $csv_obj}), "Tied CSV" );
+
+ is( scalar @csv, 4, "Report correct number of rows" );
+ is( scalar @{$csv[0]}, 3, "Report correct number of columns" );
+
+ ok( $csv_obj->my_parsed_lines, "Text::CSV subclass" );
+
+}
+
+done_testing();
+

0 comments on commit 693d140

Please sign in to comment.