File: 76_magic.t

package info (click to toggle)
libtext-csv-xs-perl 1.60-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,368 kB
  • sloc: perl: 8,771; makefile: 9
file content (128 lines) | stat: -rw-r--r-- 3,149 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/perl

use strict;
use warnings;

#use Test::More "no_plan";
 use Test::More tests => 44;

BEGIN {
    use_ok "Text::CSV_XS", ();
    plan skip_all => "Cannot load Text::CSV_XS" if $@;
    }

my $tfn = "_76test.csv"; END { -f $tfn and unlink $tfn; }
my $csv = Text::CSV_XS->new ({ binary => 1, eol => "\n" });

my $fh;
my $foo;
my $bar;
my @foo = ("#", 1..3);

tie $foo, "Foo";
ok ($csv->combine (@$foo),		"combine () from magic");
untie $foo;
is_deeply ([$csv->fields], \@foo,	"column_names ()");

tie $bar, "Bar";
$bar = "#";
ok ($csv->combine ($bar, @{$foo}[1..3]),"combine () from magic");
untie $bar;
is_deeply ([$csv->fields], \@foo,	"column_names ()");

tie $foo, "Foo";
open  $fh, ">", $tfn or die "$tfn: $!\n";
ok ($csv->print ($fh, $foo),		"print with unused magic scalar");
close $fh;
untie $foo;

open  $fh, "<", $tfn or die "$tfn: $!\n";
is_deeply ($csv->getline ($fh), \@foo,	"Content read-back");
close $fh;

tie $foo, "Foo";
ok ($csv->column_names ($foo),		"column_names () from magic");
untie $foo;
is_deeply ([$csv->column_names], \@foo,	"column_names ()");

open  $fh, "<", $tfn or die "$tfn: $!\n";
tie $bar, "Bar";
ok ($csv->bind_columns (\$bar, \my ($f0, $f1, $f2)), "bind");
ok ($csv->getline ($fh),		"fetch with magic");
is_deeply ([$bar,$f0,$f1,$f2], \@foo,	"columns fetched on magic");
# free any refs
is ($csv->bind_columns (undef), undef,	"bind column clear");
untie $bar;
close $fh;

$csv->eol (undef);
ok ($csv->combine ("us", undef, 3),	"Combine with undef");
is ($csv->string, "us,,3",		"Default");
foreach my $us ("\\N", 1, ",,,", "", "\xe2\x80\xa2", "\x{2205}") {
    ok (defined ($csv->undef_str ($us)),"Set undef_str with method");
    ok ($csv->combine ("us", undef, 3),	"Combine with undef");
    is ($csv->string, "us,$us,3",		"String after method");
    }

tie my $us, "Bar";
$us = "NULL";
ok ($csv->undef_str ($us),		"Set undef_str from tied scalar");
ok ($csv->combine ("us", undef, 3),	"Combine with undef");
is ($csv->string, "us,NULL,3",		"String after method");
$us = "\\N";
ok ($csv->undef_str ($us),		"Set undef_str from tied scalar");
ok ($csv->combine ("us", undef, 3),	"Combine with undef");
is ($csv->string, "us,\\N,3",		"String after method");
$us = undef;
is ($csv->undef_str ($us), undef,	"Set undef_str from tied scalar");
ok ($csv->combine ("us", undef, 3),	"Combine with undef");
is ($csv->string, "us,,3",		"String after method");
untie $us;

$csv = Text::CSV_XS->new ({ undef_str => "\\N" });
ok ($csv->combine ("us", undef, 3),	"Combine with undef");
is ($csv->string, "us,\\N,3",		"String after undef_str from constructor");

{   package Foo;
    use strict;
    use warnings;

    require Tie::Scalar;
    use vars qw( @ISA );
    @ISA = qw(Tie::Scalar);

    sub FETCH {
	[ "#", 1 .. 3 ];
	} # FETCH

    sub TIESCALAR {
	bless [], "Foo";
	} # TIESCALAR

    1;
    }

{   package Bar;

    use strict;
    use warnings;

    require Tie::Scalar;
    use vars qw( @ISA );
    @ISA = qw(Tie::Scalar);

    sub FETCH {
	return ${$_[0]};
	} # FETCH

    sub STORE {
	${$_[0]} = $_[1];
	} # STORE

    sub TIESCALAR {
	my $bar;
	bless \$bar, "Bar";
	} # TIESCALAR

    1;
    }