1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6#use Test::More "no_plan";
7 use Test::More tests => 44;
8
9BEGIN {
10    use_ok "Text::CSV_XS", ();
11    plan skip_all => "Cannot load Text::CSV_XS" if $@;
12    }
13
14my $tfn = "_76test.csv"; END { -f $tfn and unlink $tfn; }
15my $csv = Text::CSV_XS->new ({ binary => 1, eol => "\n" });
16
17my $fh;
18my $foo;
19my $bar;
20my @foo = ("#", 1..3);
21
22tie $foo, "Foo";
23ok ($csv->combine (@$foo),		"combine () from magic");
24untie $foo;
25is_deeply ([$csv->fields], \@foo,	"column_names ()");
26
27tie $bar, "Bar";
28$bar = "#";
29ok ($csv->combine ($bar, @{$foo}[1..3]),"combine () from magic");
30untie $bar;
31is_deeply ([$csv->fields], \@foo,	"column_names ()");
32
33tie $foo, "Foo";
34open  $fh, ">", $tfn or die "$tfn: $!\n";
35ok ($csv->print ($fh, $foo),		"print with unused magic scalar");
36close $fh;
37untie $foo;
38
39open  $fh, "<", $tfn or die "$tfn: $!\n";
40is_deeply ($csv->getline ($fh), \@foo,	"Content read-back");
41close $fh;
42
43tie $foo, "Foo";
44ok ($csv->column_names ($foo),		"column_names () from magic");
45untie $foo;
46is_deeply ([$csv->column_names], \@foo,	"column_names ()");
47
48open  $fh, "<", $tfn or die "$tfn: $!\n";
49tie $bar, "Bar";
50ok ($csv->bind_columns (\$bar, \my ($f0, $f1, $f2)), "bind");
51ok ($csv->getline ($fh),		"fetch with magic");
52is_deeply ([$bar,$f0,$f1,$f2], \@foo,	"columns fetched on magic");
53# free any refs
54is ($csv->bind_columns (undef), undef,	"bind column clear");
55untie $bar;
56close $fh;
57
58$csv->eol (undef);
59ok ($csv->combine ("us", undef, 3),	"Combine with undef");
60is ($csv->string, "us,,3",		"Default");
61foreach my $us ("\\N", 1, ",,,", "", "\xe2\x80\xa2", "\x{2205}") {
62    ok (defined ($csv->undef_str ($us)),"Set undef_str with method");
63    ok ($csv->combine ("us", undef, 3),	"Combine with undef");
64    is ($csv->string, "us,$us,3",		"String after method");
65    }
66
67tie my $us, "Bar";
68$us = "NULL";
69ok ($csv->undef_str ($us),		"Set undef_str from tied scalar");
70ok ($csv->combine ("us", undef, 3),	"Combine with undef");
71is ($csv->string, "us,NULL,3",		"String after method");
72$us = "\\N";
73ok ($csv->undef_str ($us),		"Set undef_str from tied scalar");
74ok ($csv->combine ("us", undef, 3),	"Combine with undef");
75is ($csv->string, "us,\\N,3",		"String after method");
76$us = undef;
77is ($csv->undef_str ($us), undef,	"Set undef_str from tied scalar");
78ok ($csv->combine ("us", undef, 3),	"Combine with undef");
79is ($csv->string, "us,,3",		"String after method");
80untie $us;
81
82$csv = Text::CSV_XS->new ({ undef_str => "\\N" });
83ok ($csv->combine ("us", undef, 3),	"Combine with undef");
84is ($csv->string, "us,\\N,3",		"String after undef_str from constructor");
85
86{   package Foo;
87    use strict;
88    use warnings;
89
90    require Tie::Scalar;
91    use vars qw( @ISA );
92    @ISA = qw(Tie::Scalar);
93
94    sub FETCH {
95	[ "#", 1 .. 3 ];
96	} # FETCH
97
98    sub TIESCALAR {
99	bless [], "Foo";
100	} # TIESCALAR
101
102    1;
103    }
104
105{   package Bar;
106
107    use strict;
108    use warnings;
109
110    require Tie::Scalar;
111    use vars qw( @ISA );
112    @ISA = qw(Tie::Scalar);
113
114    sub FETCH {
115	return ${$_[0]};
116	} # FETCH
117
118    sub STORE {
119	${$_[0]} = $_[1];
120	} # STORE
121
122    sub TIESCALAR {
123	my $bar;
124	bless \$bar, "Bar";
125	} # TIESCALAR
126
127    1;
128    }
129