File: 30-construction.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 (128 lines) | stat: -rw-r--r-- 3,275 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
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

use strict;
use warnings;

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

use Text::CSV;

use_ok( 'Tie::Array::CSV' );

my @test_data = (
  ['name', 'rank', 'serial number'],
  ['joel berger', 'plebe', 1010101],
  ['larry wall', 'general', 1],
  ['damian conway', 'colonel', 1001],
);

sub make_test_string {
  my $sep = shift || ',';

  my $string;
  for (@test_data) {
    $string .= join($sep, @$_) . "\n";
  }

  return $string;
}

{ # test constructors with only file arguments

  my ($fh, $file) = tempfile();
  print $fh make_test_string();

  {
    tie my @csv, 'Tie::Array::CSV', $fh;
    isa_ok( tied(@csv), 'Tie::Array::CSV' );
    is_deeply( \@csv, \@test_data, 'tie contructor, no args');
  }

  {
    tie my @csv, 'Tie::Array::CSV', {file => $fh};
    isa_ok( tied(@csv), 'Tie::Array::CSV' );
    is_deeply( \@csv, \@test_data, 'tie contructor, no args');
  }

  {
    tie my @csv, 'Tie::Array::CSV', file => $fh;
    isa_ok( tied(@csv), 'Tie::Array::CSV' );
    is_deeply( \@csv, \@test_data, 'tie contructor, no args');
  }

  {
    my $csv = Tie::Array::CSV->new($fh);
    isa_ok( tied(@$csv), 'Tie::Array::CSV' );
    is_deeply( $csv, \@test_data, 'new contructor, no args');
  }

  {
    my $csv = Tie::Array::CSV->new( {file => $fh} );
    isa_ok( tied(@$csv), 'Tie::Array::CSV' );
    is_deeply( $csv, \@test_data, 'new contructor, no args');
  }

  {
    my $csv = Tie::Array::CSV->new( file => $fh );
    isa_ok( tied(@$csv), 'Tie::Array::CSV' );
    is_deeply( $csv, \@test_data, 'new contructor, no args');
  }

}

{ # test contructors with sep_char arguments

  my ($fh, $file) = tempfile();
  print $fh make_test_string(';');

  {
    tie my @csv, 'Tie::Array::CSV', $fh, { text_csv => { sep_char => ';' } };
    isa_ok( tied(@csv), 'Tie::Array::CSV' );
    is_deeply( \@csv, \@test_data, 'tie contructor, opts->text_csv->sep_char');
  }

  {
    tie my @csv, 'Tie::Array::CSV', $fh, text_csv => { sep_char => ';' };
    isa_ok( tied(@csv), 'Tie::Array::CSV' );
    is_deeply( \@csv, \@test_data, 'tie contructor, opts->text_csv->sep_char');
  }

  {
    tie my @csv, 'Tie::Array::CSV', $fh, { sep_char => ';' };
    isa_ok( tied(@csv), 'Tie::Array::CSV' );
    is_deeply( \@csv, \@test_data, 'tie contructor, opts->text_csv->sep_char');
  }

  {
    tie my @csv, 'Tie::Array::CSV', $fh, sep_char => ';';
    isa_ok( tied(@csv), 'Tie::Array::CSV' );
    is_deeply( \@csv, \@test_data, 'tie contructor, opts->text_csv->sep_char');
  }

  {
    my $csv = Tie::Array::CSV->new($fh, { text_csv => { sep_char => ';' } } );
    isa_ok( tied(@$csv), 'Tie::Array::CSV' );
    is_deeply( $csv, \@test_data, 'new contructor, opts->text_csv->sep_char');
  }

  {
    my $csv = Tie::Array::CSV->new($fh, text_csv => { sep_char => ';' } );
    isa_ok( tied(@$csv), 'Tie::Array::CSV' );
    is_deeply( $csv, \@test_data, 'new contructor, text_csv->sep_char');
  }

  {
    my $csv = Tie::Array::CSV->new($fh, { sep_char => ';' } );
    isa_ok( tied(@$csv), 'Tie::Array::CSV' );
    is_deeply( $csv, \@test_data, 'new contructor, text_csv->sep_char');
  }

  {
    my $csv = Tie::Array::CSV->new($fh, sep_char => ';' );
    isa_ok( tied(@$csv), 'Tie::Array::CSV' );
    is_deeply( $csv, \@test_data, 'new contructor, text_csv->sep_char');
  }

}

done_testing();