File: 20-basic.t

package info (click to toggle)
libtie-array-csv-perl 0.08-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 128 kB
  • sloc: perl: 302; makefile: 2
file content (81 lines) | stat: -rw-r--r-- 2,131 bytes parent folder | download
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

use strict;
use warnings;

use Test::More;
use File::Temp qw/tempfile/;

use Text::CSV;

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

{
  my ($fh, $file) = tempfile();
  print $fh $test_data;

  my @csv;
  ok( tie(@csv, 'Tie::Array::CSV', $fh), "Tied CSV" );

  is( scalar @csv, 4, "Report correct number of rows" );
  is( scalar @{$csv[0]}, 3, "Report correct number of columns" );

  is( $csv[0][0], "name", "Find individual element" );
  is( $csv[1][1], "plebe", "Find another element" );

  $csv[1][1] = "peon";

  is( $csv[1][1], "peon", "Modified element" );

  is( shift @{ $csv[3] }, "damian conway", "Shifted element gotten" );
  is( $csv[3][0], "colonel", "Shifted element removed from array" );

  is( pop @{ $csv[3] }, 1001, "Popped element gotten" );
  is( scalar @{ $csv[3] }, 1, "Popped element removed" );

  push @{ $csv[3] }, 1002;
  is( $csv[3][1], 1002, "Pushed element added" );

  pop @csv;
  is( scalar @csv, 3, "Pop outer array removes row" );

  my $header = ["name", "rank", "serial number"];
  is_deeply( shift @csv, $header, "Shifted outer array row gotten" );

  push @csv, $header;
  is_deeply( delete $csv[-1], $header, "Delete on outer array, row gotten" );

  is( $csv[0][0], "joel berger", "Shift outer array removed row" );

  $csv[1][2] += 2;

  is( $csv[1][2], 3, "In place addition" );
  is( ++$csv[1][2], 4, "In place pre-increment" );

  $csv[1][2] .= '2';
  is( $csv[1][2], 42, "In place string join" );

  ok( exists $csv[0], "First element exists" );
  ok( ! exists $csv[1000], "1000th element doesn't exist" );

  push @csv, [ "tom christiansen", "major", 1101 ];

  ok( untie @csv, "Untie succeeds" );

  seek $fh, 0, 0;
  my $parser = Text::CSV->new();

  is_deeply( $parser->getline($fh), ["joel berger", "peon", 1010101 ], "File was updated 1" );
  is_deeply( $parser->getline($fh), ["larry wall", "general", 42 ], "File was updated 2" );
  is_deeply( $parser->getline($fh), [ "tom christiansen", "major", 1101 ], "File was updated 3" );
  
}

done_testing();