File: 05_csv.t

package info (click to toggle)
libfinance-quotehist-perl 1.19-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 428 kB
  • sloc: perl: 1,940; makefile: 4
file content (58 lines) | stat: -rw-r--r-- 1,381 bytes parent folder | download | duplicates (6)
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
my $test_count;
BEGIN { $test_count = 161 }

use strict;
use Test::More tests => $test_count;

use FindBin;
use lib $FindBin::RealBin;
use testload;

my @headers = ( 'Date', 'Open', 'High', 'Low', 'Close',
                'Volume', 'Adj. Close*');

my($pp_present, $xs_present);
eval  { require Text::CSV_PP };
$pp_present = !$@;
eval  { require Text::CSV_XS };
$xs_present = !$@;

my $each_count = ($test_count - 1)/2;

SKIP: {
  my $class = 'Text::CSV_PP';
  skip "$class not installed",  $each_count unless $pp_present;
  use_ok($class);
  my $cp = $class->new;
  csv_parse($cp, csv_content());
}

SKIP: {
  my $class = 'Text::CSV_XS';
  skip "$class not installed",  $each_count unless $xs_present;
  use_ok($class);
  my $cp = $class->new;
  csv_parse($cp, csv_content());
}

ok($pp_present || $xs_present, "csv parsing class present");

###

sub csv_parse {
  my($cp, $str) = @_;
  my @rows = split(/\s*\n\s*/, $str);
  chomp @rows;
  my $first_line = shift @rows;
  ok($cp->parse($first_line), "header parse");
  my @fields = $cp->fields;
  cmp_ok(scalar @fields, '==', scalar @headers,  "header field count");
  foreach (0 .. $#headers) {
    cmp_ok($fields[$_], 'eq', $headers[$_], "header field match");
  }
  foreach my $line (@rows) {
    ok($cp->parse($line), 'line parse');
    my @fields = $cp->fields;
    cmp_ok(scalar @fields, '==', 7, "line field count");
  }
}