-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy path12_acc.t
284 lines (265 loc) · 11.9 KB
/
12_acc.t
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/usr/bin/perl
use strict;
$^W = 1; # use warnings core since 5.6
use Test::More tests => 245;
BEGIN {
$ENV{PERL_TEXT_CSV} = $ENV{TEST_PERL_TEXT_CSV} || 0;
use_ok "Text::CSV";
plan skip_all => "Cannot load Text::CSV" if $@;
}
my $Backend = Text::CSV->backend;
my $csv;
ok ($csv = Text::CSV->new, "new ()");
is ($csv->quote_char, '"', "quote_char");
is ($csv->quote, '"', "quote");
is ($csv->escape_char, '"', "escape_char");
is ($csv->sep_char, ",", "sep_char");
is ($csv->sep, ",", "sep");
is ($csv->eol, "", "eol");
is ($csv->always_quote, 0, "always_quote");
is ($csv->binary, 0, "binary");
is ($csv->keep_meta_info, 0, "keep_meta_info");
is ($csv->allow_loose_quotes, 0, "allow_loose_quotes");
is ($csv->allow_loose_escapes, 0, "allow_loose_escapes");
is ($csv->allow_unquoted_escape, 0, "allow_unquoted_escape");
is ($csv->allow_whitespace, 0, "allow_whitespace");
is ($csv->blank_is_undef, 0, "blank_is_undef");
is ($csv->empty_is_undef, 0, "empty_is_undef");
is ($csv->auto_diag, 0, "auto_diag");
is ($csv->diag_verbose, 0, "diag_verbose");
is ($csv->verbatim, 0, "verbatim");
is ($csv->formula, "none", "formula");
is ($csv->strict, 0, "strict");
is ($csv->strict_eol, 0, "strict_eol");
is ($csv->skip_empty_rows, 0, "skip_empty_rows");
is ($csv->quote_space, 1, "quote_space");
is ($csv->quote_empty, 0, "quote_empty");
is ($csv->escape_null, 1, "escape_null");
is ($csv->quote_null, 1, "quote_null");
is ($csv->quote_binary, 1, "quote_binary");
is ($csv->record_number, 0, "record_number");
is ($csv->decode_utf8, 1, "decode_utf8");
is ($csv->undef_str, undef, "undef_str");
is ($csv->comment_str, undef, "comment_str");
is ($csv->binary (1), 1, "binary (1)");
my @fld = ( 'txt =, "Hi!"', "Yes", "", 2, undef, "1.09", "\r", undef );
ok ($csv->combine (@fld), "combine");
is ($csv->string,
qq{"txt =, ""Hi!""",Yes,,2,,1.09,"\r",}, "string");
is ($csv->sep_char (";"), ";", "sep_char (;)");
is ($csv->sep ("**"), "**", "sep (**)");
is ($csv->sep (";"), ";", "sep (;)");
is ($csv->sep_char (), ";", "sep_char ()");
is ($csv->quote_char ("="), "=", "quote_char (=)");
is ($csv->quote_char (undef), undef, "quote_char (undef)");
is ($csv->{quote_char}, undef, "{quote_char} (undef)");
is ($csv->quote (undef), "", "quote (undef)");
is ($csv->quote (""), "", "quote (undef)");
is ($csv->quote ("**"), "**", "quote (**)");
is ($csv->quote ("="), "=", "quote (=)");
is ($csv->eol (undef), "", "eol (undef)");
is ($csv->eol (""), "", "eol ('')");
is ($csv->eol ("\r"), "\r", "eol (\\r)");
is ($csv->keep_meta_info (1), 1, "keep_meta_info (1)");
is ($csv->keep_meta_info (0), 0, "keep_meta_info (0)");
is ($csv->keep_meta_info (""), 0, "keep_meta_info ('')");
is ($csv->keep_meta_info (undef), 0, "keep_meta_info (undef)");
is ($csv->keep_meta_info ("false"), 0, "keep_meta_info (undef)");
is ($csv->keep_meta_info ("true"), 1, "keep_meta_info (undef)");
is ($csv->always_quote (undef), 0, "always_quote (undef)");
is ($csv->always_quote (1), 1, "always_quote (1)");
is ($csv->allow_loose_quotes (1), 1, "allow_loose_quotes (1)");
is ($csv->allow_loose_escapes (1), 1, "allow_loose_escapes (1)");
is ($csv->allow_unquoted_escape (1), 1, "allow_unquoted_escape (1)");
is ($csv->allow_whitespace (1), 1, "allow_whitespace (1)");
is ($csv->blank_is_undef (1), 1, "blank_is_undef (1)");
is ($csv->empty_is_undef (1), 1, "empty_is_undef (1)");
is ($csv->auto_diag (1), 1, "auto_diag (1)");
is ($csv->auto_diag (2), 2, "auto_diag (2)");
is ($csv->auto_diag (9), 9, "auto_diag (9)");
is ($csv->auto_diag ("true"), 1, "auto_diag (\"true\")");
is ($csv->auto_diag ("false"), 0, "auto_diag (\"false\")");
is ($csv->auto_diag (undef), 0, "auto_diag (undef)");
is ($csv->auto_diag (""), 0, "auto_diag (\"\")");
is ($csv->diag_verbose (1), 1, "diag_verbose (1)");
is ($csv->diag_verbose (2), 2, "diag_verbose (2)");
is ($csv->diag_verbose (9), 9, "diag_verbose (9)");
is ($csv->diag_verbose ("true"), 1, "diag_verbose (\"true\")");
is ($csv->diag_verbose ("false"), 0, "diag_verbose (\"false\")");
is ($csv->diag_verbose (undef), 0, "diag_verbose (undef)");
is ($csv->diag_verbose (""), 0, "diag_verbose (\"\")");
is ($csv->verbatim (1), 1, "verbatim (1)");
is ($csv->formula ("diag"), "diag", "formula (\"diag\")");
is ($csv->strict (1), 1, "strict (1)");
is ($csv->strict_eol (1), 1, "strict_eol (1)");
is ($csv->skip_empty_rows (1), 1, "skip_empty_rows (1)");
is ($csv->quote_space (1), 1, "quote_space (1)");
is ($csv->quote_empty (1), 1, "quote_empty (1)");
is ($csv->escape_null (1), 1, "escape_null (1)");
is ($csv->quote_null (1), 1, "quote_null (1)");
is ($csv->quote_binary (1), 1, "quote_binary (1)");
is ($csv->escape_char (undef), undef, "escape_char (undef)");
is ($csv->{escape_char}, undef, "{escape_char} (undef)");
is ($csv->escape_char ("\\"), "\\", "escape_char (\\)");
ok ($csv->combine (@fld), "combine");
is ($csv->string,
qq{=txt \\=, "Hi!"=;=Yes=;==;=2=;;=1.09=;=\r=;\r}, "string");
is ($csv->undef_str ("-"), "-", "undef_str");
is ($csv->comment_str ("#"), "#", "comment_str");
is ($csv->allow_whitespace (0), 0, "allow_whitespace (0)");
is ($csv->quote_space (0), 0, "quote_space (0)");
is ($csv->quote_empty (0), 0, "quote_empty (0)");
is ($csv->escape_null (0), 0, "escape_null (0)");
is ($csv->quote_null (0), 0, "quote_null (0)");
is ($csv->quote_binary (0), 0, "quote_binary (0)");
is ($csv->decode_utf8 (0), 0, "decode_utf8 (0)");
is ($csv->sep ("--"), "--", "sep (\"--\")");
is ($csv->sep_char (), "\0", "sep_char");
is ($csv->quote ("++"), "++", "quote (\"++\")");
is ($csv->quote_char (), "\0", "quote_char");
is ($csv->undef_str (undef), undef, "undef_str");
is ($csv->comment_str (undef), undef, "comment_str");
# Test single-byte specials in UTF-8 mode
is ($csv->sep ("|"), "|", "sep |");
is ($csv->sep_char (), "|", "sep_char");
chop (my $s = "|\x{20ac}");
is ($csv->sep ($s), "|", "sep |");
is ($csv->sep (), "|", "sep_char");
is ($csv->sep_char (), "|", "sep_char");
is ($csv->quote ("'"), "'", "quote '");
is ($csv->quote_char (), "'", "quote_char");
chop (my $q = "'\x{20ac}");
is ($csv->quote ($q), "'", "quote '");
is ($csv->quote (), "'", "quote_char");
is ($csv->quote_char (), "'", "quote_char");
# Funny settings, all three translate to \0 internally
ok ($csv = Text::CSV->new ({
sep => "::::::::::",
quote_char => undef,
escape_char => undef,
}), "new (undef ...)");
is ($csv->sep_char, "\0", "sep_char undef");
is ($csv->sep, "::::::::::", "sep long");
is ($csv->quote_char, undef, "quote_char undef");
is ($csv->quote, undef, "quote undef");
is ($csv->escape_char, undef, "escape_char undef");
ok ($csv->parse ("foo"), "parse (foo)");
$csv->sep_char (",");
is ($csv->record_number, 1, "record_number");
ok ($csv->parse ("foo"), "parse (foo)");
is ($csv->record_number, 2, "record_number");
ok (!$csv->parse ("foo,foo\0bar"), "parse (foo)");
$csv->escape_char ("\\");
ok (!$csv->parse ("foo,foo\0bar"), "parse (foo)");
$csv->binary (1);
ok ( $csv->parse ("foo,foo\0bar"), "parse (foo)");
# Attribute aliasses
ok ($csv = Text::CSV->new ({ quote_always => 1, verbose_diag => 1}));
is ($csv->always_quote, 1, "always_quote = quote_always");
is ($csv->diag_verbose, 1, "diag_verbose = verbose_diag");
ok ($csv = Text::CSV->new ({ escape_char => undef }), "undef escape aliases");
is ($csv->escape_char, undef, "escape_char is undef");
ok ($csv = Text::CSV->new ({ quote => undef }), "undef quote aliases");
is ($csv->quote_char, undef, "quote_char is undef");
is ($csv->quote, undef, "quote is undef");
# Some forbidden combinations
foreach my $ws (" ", "\t") {
ok ($csv = Text::CSV->new ({ escape_char => $ws }), "New blank escape");
eval { ok ($csv->allow_whitespace (1), "Allow ws") };
is (($csv->error_diag)[0], 1002, "Wrong combo");
ok ($csv = Text::CSV->new ({ quote_char => $ws }), "New blank quote");
eval { ok ($csv->allow_whitespace (1), "Allow ws") };
is (($csv->error_diag)[0], 1002, "Wrong combo");
ok ($csv = Text::CSV->new ({ allow_whitespace => 1 }), "New ws 1");
eval { ok ($csv->escape_char ($ws), "esc") };
is (($csv->error_diag)[0], 1002, "Wrong combo");
ok ($csv = Text::CSV->new ({ allow_whitespace => 1 }), "New ws 1");
eval { ok ($csv->quote_char ($ws), "esc") };
is (($csv->error_diag)[0], 1002, "Wrong combo");
}
foreach my $esc (undef, "", " ", "\t", "!!!!!!") {
foreach my $quo (undef, "", " ", "\t", "!!!!!!") {
defined $esc && $esc =~ m/[ \t]/ or
defined $quo && $quo =~ m/[ \t]/ or next;
my $wc = join " " => map {
!defined $_ ? "<undef>" :
$_ eq "" ? "<empty>" :
$_ eq " " ? "<sp>" :
$_ eq "\t" ? "<tab>" : $_ }
"esc:", $esc, "quo:", $quo;
eval { $csv = Text::CSV->new ({
escape => $esc,
quote => $quo,
allow_whitespace => 1,
}) };
like ((Text::CSV::error_diag)[1], qr{^INI - allow_whitespace}, "Wrong combo - error message: $wc");
is ((Text::CSV::error_diag)[0], 1002, "Wrong combo - numeric error: $wc");
}
}
# Test 1003 in constructor
foreach my $x ("\r", "\n", "\r\n", "x\n", "\rx") {
foreach my $attr (qw( sep_char quote_char escape_char )) {
#ok (1, "attr: $attr => ", $x =~ s/\n/\\n/gr =~ s/\r/\\r/gr);
eval { $csv = Text::CSV->new ({ $attr => $x }) };
is ((Text::CSV::error_diag)[0], 1003, "eol in $attr");
}
}
# Test 1003 in methods
foreach my $attr (qw( sep_char quote_char escape_char )) {
ok ($csv = Text::CSV->new, "New");
eval { ok ($csv->$attr ("\n"), "$attr => \\n") };
is (($csv->error_diag)[0], 1003, "not allowed");
}
# Too long attr (max 16)
$csv = Text::CSV->new ({ quote => "'" });
my $xl = "X" x 32;
eval { $csv->eol ($xl); };
is (($csv->error_diag)[0], 1005, "eol too long");
is ($csv->eol (), "", "eol unchanged");
eval { $csv->sep ($xl); };
is (($csv->error_diag)[0], 1006, "sep too long");
is ($csv->sep (), ",", "sep unchanged");
eval { $csv->quote ($xl); };
is (($csv->error_diag)[0], 1007, "quo too long");
is ($csv->quote (), "'", "quo unchanged");
eval { $csv = Text::CSV->new ({ eol => $xl }); };
is ($csv, undef, "new with EOL too long");
is ((Text::CSV::error_diag)[0], 1005, "error set");
eval { $csv = Text::CSV->new ({ sep => $xl }); };
is ($csv, undef, "new with SEP too long");
is ((Text::CSV::error_diag)[0], 1006, "error set");
eval { $csv = Text::CSV->new ({ quote => $xl }); };
is ($csv, undef, "new with QUO too long");
is ((Text::CSV::error_diag)[0], 1007, "error set");
# And test erroneous calls
is (Text::CSV::new (0), undef, "new () as function");
is (Text::CSV::error_diag (), "usage: my \$csv = $Backend->new ([{ option => value, ... }]);",
"Generic usage () message");
is (Text::CSV->new ({ oel => "" }), undef, "typo in attr");
is (Text::CSV::error_diag (), "INI - Unknown attribute 'oel'", "Unsupported attr");
is (Text::CSV->new ({ _STATUS => "" }), undef, "private attr");
is (Text::CSV::error_diag (), "INI - Unknown attribute '_STATUS'", "Unsupported private attr");
foreach my $arg (undef, 0, "", " ", 1, [], [ 0 ], *STDOUT) {
is (Text::CSV->new ($arg), undef, "Illegal type for first arg");
is ((Text::CSV::error_diag)[0], 1000, "Should be a hashref - numeric error");
}
my $attr = [ sort qw(
eol
sep_char sep quote_char quote escape_char
binary decode_utf8
auto_diag diag_verbose
blank_is_undef empty_is_undef
allow_whitespace allow_loose_quotes allow_loose_escapes allow_unquoted_escape
always_quote quote_space quote_empty quote_binary
escape_null
keep_meta_info
verbatim strict strict_eol skip_empty_rows formula
undef_str comment_str
types
callbacks
ENCODING
)];
is_deeply ([ Text::CSV::known_attributes () ], $attr, "Known attributes (function)");
is_deeply ([ Text::CSV->known_attributes () ], $attr, "Known attributes (class method)");
is_deeply ([ Text::CSV->new->known_attributes () ], $attr, "Known attributes (method)");
1;