Skip Menu |

This queue is for tickets about the Text-CSV-Field CPAN distribution.

Report information
The Basics
Id: 55140
Status: resolved
Priority: 0/
Queue: Text-CSV-Field

People
Owner: Nobody in particular
Requestors: BARBIE [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: 1.00
Fixed in: (no value)



Subject: More tests and restructured code
The attached is based on the previous patch for the test bug, and restructures the code to better parse the file for errors, etc first. Adds several additional package and sample data files for additional tests. Not essential, but will help to make the package and module a little more robust.
Subject: test-csv-field-02.patch
--- Text-CSV-Field/Text-CSV-Field-1.00_01/Changes Tue Mar 2 13:46:05 2010 +++ Text-CSV-Field/Text-CSV-Field-1.00_02/Changes Tue Mar 2 13:31:58 2010 @@ -1,9 +1,17 @@ Revision history for Perl extension Field. +1.00_02 02/03/2010 + - changes by BARBIE. + - rename Field.t to 00use.t, and added several package tests. + - moved to using IO::File for better file handling + - moved file content validation into object creation. + - added new test file, 10creation.t and several test files. + - rearranged file validation and field storage into new(). + 1.00_01 02/03/2010 - fixed failing test (by BARBIE). 1.00 Sun Jan 31 20:15:43 2010 - original version; created by h2xs 1.23 with options - -AXc -n Field + -AXc -n Field --- Text-CSV-Field/Text-CSV-Field-1.00_01/lib/Text/CSV/Field.pm Mon Feb 1 15:40:42 2010 +++ Text-CSV-Field/Text-CSV-Field-1.00_02/lib/Text/CSV/Field.pm Tue Mar 2 13:28:37 2010 @@ -1,99 +1,92 @@ package Text::CSV::Field; + +use warnings; use strict; -BEGIN { - $Text::CSV::Field::VERSION = '1.00'; -} +use vars qw($VERSION); +$VERSION = '1.00'; +use IO::File; sub new { my ($class,$file,$sep) = @_; - open(FILE,$file) or die "can't open $file $!"; - defined($sep) or die "Missing field separator parameter\n"; - $sep or die "Empty field separator parameter\n"; + die "No file specified\n" unless($file); + die "File [$file] not accessible\n" unless(-f $file && -r $file); + die "Missing field separator parameter\n" unless(defined($sep)); + die "Empty field separator parameter\n" unless($sep); my $self = {_filename=>$file,_sep=>$sep}; - my @data = (); - # open file - while(<FILE>) { - chomp; - push(@data,$_); - } + bless($self,$class); + + # read data file + my $fh = IO::File->new($file,'r') or die "can't open $file $!"; + my @data = grep {!/^\s*$/} map {s/\s+$//;$_} <$fh>; + $fh->close; + + # check we got something + die "File is empty\n" if(!@data); + die "File contains no data found\n" if(@data == 1); + $self->{_data} = \@data; - # check sep + $self->_setdatafields(); - bless($self,$class); return $self; } -sub check_file { - my ($self) = shift; - my $num_fields = 0; - my $err_string = undef; - my @data = @{$self->{_data}}; - if (!@data) { - $err_string .= "File is empty\n"; - print "$err_string\n"; - exit(); - } - if (@data == 1) { - $err_string .= "no data found\n"; - print "$err_string\n"; - exit(); - } - for(my $i=0; $i < @data; $i++) { - my $temp = 0; - my @temp_arr = split($self->{_sep},${$self->{_data}}[$i]); - $temp = @temp_arr; - if ($i == 0) { - $num_fields = $temp; - } - else { - if ($num_fields != $temp) { - $err_string .= "Field number mismatch line# " . ($i+1) . " in " . $self->{_filename} . "\n"; - } - } - } - if ($err_string) { - print "$err_string\n"; - exit(); - } +sub getfield { + my $self = shift; + my $field = shift or die "Missing field input\n"; + + $field =~ tr/a-z/A-Z/; + return () unless($self->{$field}); + return @{$self->{$field}}; } -sub setdatafields{ - my ($self) = shift; - $self->check_file(); +sub _setdatafields { + my $self = shift; + $self->_check_file(); $self->{_total_number} = @{$self->{_data}}; - my @fieldnames = (); + my @fieldnames; for(my $j = 0; $j < @{$self->{_data}}; $j++) { - my @recs = split(/$self->{_sep}/,${$self->{_data}}[$j]); - for(my $k = 0; $k < @recs; $k++) { + my @fields = split(/$self->{_sep}/,${$self->{_data}}[$j]); + for(my $k = 0; $k < @fields; $k++) { if ($j == 0) { - push(@fieldnames,$recs[$k]); - $self->{$recs[$k]} = []; - #push(@{$self->{$fieldnames[$k]}},$recs[$k]); + push @fieldnames, uc $fields[$k]; + } else { + push @{$self->{$fieldnames[$k]}}, $fields[$k]; } - else { - push(@{$self->{$fieldnames[$k]}},$recs[$k]); - } } } } -sub getfield{ - my $self = shift; - my $field = shift; - $field or die "Missing field input\n"; - $self->setdatafields; - $field =~ tr/a-z/A-Z/; - return @{$self->{$field}}; +sub _check_file { + my $self = shift; + my $num_fields = 0; + my $err_string = undef; + my $records = @{$self->{_data}}; + + for(my $i=0; $i < $records; $i++) { + my $temp = 0; + my @temp_arr = split($self->{_sep},${$self->{_data}}[$i]); + $temp = @temp_arr; + if ($i == 0) { + $num_fields = $temp; + } elsif ($num_fields != $temp) { + $err_string .= "Field number mismatch [$temp/$num_fields] line# " . ($i+1) . " in " . $self->{_filename} . "\n"; + } + } + + die "$err_string\n" if($err_string); } + 1; + __END__ + =pod =head1 NAME @@ -119,8 +112,8 @@ my $obj = Text::CSV::Field->new('filename','field_sep'); -Construct a new parser. This takes two parameters csv filename and field seperator. -Both parameters are mandatory. +Construct a new parser. This takes two parameters csv filename and field +seperator. Both parameters are mandatory. =head2 getfield --- Text-CSV-Field/Text-CSV-Field-1.00_01/Makefile.PL Mon Feb 1 14:50:12 2010 +++ Text-CSV-Field/Text-CSV-Field-1.00_02/Makefile.PL Tue Mar 2 13:40:12 2010 @@ -1,8 +1,14 @@ -use 5.008008; +use 5.006; use ExtUtils::MakeMaker; # See lib/ExtUtils/MakeMaker.pm for details of how to influence # the contents of the Makefile that is written. WriteMakefile( - NAME => 'Field', - VERSION_FROM => 'lib/Text/CSV/Field.pm', # finds $VERSION + NAME => 'Text::CSV::Field', + VERSION_FROM => 'lib/Text/CSV/Field.pm', # finds $VERSION + PREREQ_PM => { + + 'IO::File' => 0, + + }, + NO_META => 1, ); --- Text-CSV-Field/Text-CSV-Field-1.00_01/MANIFEST Mon Feb 1 14:49:42 2010 +++ Text-CSV-Field/Text-CSV-Field-1.00_02/MANIFEST Tue Mar 2 13:33:23 2010 @@ -1,6 +1,14 @@ Changes +lib/Text/CSV/Field.pm Makefile.PL MANIFEST README -t/Field.t +t/00use.t -lib/Text/CSV/Field.pm +t/10creation.t +t/90podtest.t +t/91podcover.t +t/94metatest.t +t/data/file00.csv +t/data/file01.csv +t/data/file02.csv +t/data/file03.csv --- Text-CSV-Field/Text-CSV-Field-1.00_01/t/00use.t +++ Text-CSV-Field/Text-CSV-Field-1.00_02/t/00use.t Tue Mar 2 10:35:06 2010 @@ -0,0 +1,18 @@ +#!/usr/bin/perl -w +use strict; + +# Before `make install' is performed this script should be runnable with +# `make test'. After `make install' it should work as `perl Field.t' + +######################### + +# change 'tests => 1' to 'tests => last_test_to_print'; + +use Test::More tests => 1; +BEGIN { use_ok('Text::CSV::Field') }; + +######################### + +# Insert your test code below, the Test::More module is use()ed here so read +# its man page ( perldoc Test::More ) for help writing this test script. + --- Text-CSV-Field/Text-CSV-Field-1.00_01/t/10creation.t +++ Text-CSV-Field/Text-CSV-Field-1.00_02/t/10creation.t Tue Mar 2 13:30:39 2010 @@ -0,0 +1,41 @@ +#!/usr/bin/perl -w +use strict; + +use Test::More tests => 11; +use Text::CSV::Field; + +my ($test_file,$test_sep) = ('./t/data/file00.csv',','); + +{ + eval { Text::CSV::Field->new(); }; + like($@, qr/No file specified/,'.. missing file'); + eval { Text::CSV::Field->new('gibberish'); }; + like($@, qr/File \[gibberish\] not accessible/,'.. non-existent file'); + + eval { Text::CSV::Field->new($test_file); }; + like($@, qr/Missing field separator parameter/,'.. missing separator'); + eval { Text::CSV::Field->new($test_file,''); }; + like($@, qr/Empty field separator parameter/,'.. blank separator'); + + eval { Text::CSV::Field->new('./t/data/file01.csv',$test_sep); }; + like($@, qr/File is empty/,'.. empty file [file01.csv]'); + eval { Text::CSV::Field->new('./t/data/file02.csv',$test_sep); }; + like($@, qr/File contains no data found/,'.. no data in file [file02.csv]'); + eval { Text::CSV::Field->new('./t/data/file03.csv',$test_sep); }; + like($@, qr/Field number mismatch/,'.. missing column [file03.csv]'); +} + +{ + my $tcf; + eval { $tcf = Text::CSV::Field->new($test_file,$test_sep); }; + is($@,'','.. no object creation errors'); + isa_ok($tcf,'Text::CSV::Field'); + + #use Data::Dumper; + #diag(Dumper($tcf)); + + my @surnames = $tcf->getfield('surname'); + is(scalar(@surnames),4); + + is_deeply(\@surnames, ['Bloggs','Public','Sam','Sally']); +} --- Text-CSV-Field/Text-CSV-Field-1.00_01/t/90podtest.t +++ Text-CSV-Field/Text-CSV-Field-1.00_02/t/90podtest.t Tue Mar 2 13:43:02 2010 @@ -0,0 +1,10 @@ +use Test::More; + +# Skip if doing a regular install +plan skip_all => "Author tests not required for installation" + unless ( $ENV{AUTOMATED_TESTING} ); + +eval "use Test::Pod 1.00"; +plan skip_all => "Test::Pod 1.00 required for testing POD" if $@; +all_pod_files_ok(); + --- Text-CSV-Field/Text-CSV-Field-1.00_01/t/91podcover.t +++ Text-CSV-Field/Text-CSV-Field-1.00_02/t/91podcover.t Tue Mar 2 13:43:02 2010 @@ -0,0 +1,9 @@ +use Test::More; + +# Skip if doing a regular install +plan skip_all => "Author tests not required for installation" + unless ( $ENV{AUTOMATED_TESTING} ); + +eval "use Test::Pod::Coverage 0.08"; +plan skip_all => "Test::Pod::Coverage 0.08 required for testing POD coverage" if $@; +all_pod_coverage_ok(); --- Text-CSV-Field/Text-CSV-Field-1.00_01/t/94metatest.t +++ Text-CSV-Field/Text-CSV-Field-1.00_02/t/94metatest.t Tue Mar 2 13:43:02 2010 @@ -0,0 +1,23 @@ +use Test::More; +use Text::CSV::Field; + +# Skip if doing a regular install +plan skip_all => "Author tests not required for installation" + unless ( $ENV{AUTOMATED_TESTING} ); + +eval "use Test::CPAN::Meta 0.16"; +plan skip_all => "Test::CPAN::Meta 0.16 required for testing META.yml" if $@; + +plan no_plan; + +my $yaml = meta_spec_ok(undef,undef,@_); + +is($yaml->{version},$Text::CSV::Field::VERSION, + 'META.yml distribution version matches'); + +if($yaml->{provides}) { + for my $mod (keys %{$yaml->{provides}}) { + is($yaml->{provides}{$mod}{version},$Text::CSV::Field::VERSION, + "META.yml entry [$mod] version matches"); + } +} --- Text-CSV-Field/Text-CSV-Field-1.00_01/t/data/file00.csv +++ Text-CSV-Field/Text-CSV-Field-1.00_02/t/data/file00.csv Tue Mar 2 13:49:21 2010 @@ -0,0 +1,5 @@ +id,surname,firstname +1,Bloggs,Joe +2,Public,Joe +3,Sam,Uncle +4,Sally,Aunt --- Text-CSV-Field/Text-CSV-Field-1.00_01/t/data/file01.csv +++ Text-CSV-Field/Text-CSV-Field-1.00_02/t/data/file01.csv Tue Mar 2 13:48:48 2010 @@ -0,0 +1,3 @@ + + + --- Text-CSV-Field/Text-CSV-Field-1.00_01/t/data/file02.csv +++ Text-CSV-Field/Text-CSV-Field-1.00_02/t/data/file02.csv Tue Mar 2 13:49:28 2010 @@ -0,0 +1 @@ +id,surname,firstname --- Text-CSV-Field/Text-CSV-Field-1.00_01/t/data/file03.csv +++ Text-CSV-Field/Text-CSV-Field-1.00_02/t/data/file03.csv Tue Mar 2 13:49:21 2010 @@ -0,0 +1,5 @@ +id,surname,firstname +1,Bloggs +2,Public +3,Sam +4,Sally
Fixed in version 1.01