This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Update Params-Check to CPAN version 0.38
[perl5.git] / cpan / Text-ParseWords / t / ParseWords.t
CommitLineData
9480d411
NC
1#!./perl\r
2\r
9480d411
NC
3use warnings;\r
4use Text::ParseWords;\r
5use Test::More tests => 27;\r
6\r
7@words = shellwords(qq(foo "bar quiz" zoo));\r
8is($words[0], 'foo');\r
9is($words[1], 'bar quiz');\r
10is($words[2], 'zoo');\r
11\r
12{\r
13 # Gonna get some undefined things back\r
14 no warnings 'uninitialized' ;\r
15\r
16 # Test quotewords() with other parameters and null last field\r
17 @words = quotewords(':+', 1, 'foo:::"bar:foo":zoo zoo:');\r
18 is(join(";", @words), qq(foo;"bar:foo";zoo zoo;));\r
19}\r
20\r
21# Test $keep eq 'delimiters' and last field zero\r
22@words = quotewords('\s+', 'delimiters', '4 3 2 1 0');\r
23is(join(";", @words), qq(4; ;3; ;2; ;1; ;0));\r
24\r
25# Big ol' nasty test (thanks, Joerk!)\r
26$string = 'aaaa"bbbbb" cc\\ cc \\\\\\"dddd" eee\\\\\\"ffff" "gg"';\r
27\r
28# First with $keep == 1\r
29$result = join('|', parse_line('\s+', 1, $string));\r
30is($result, 'aaaa"bbbbb"|cc\\ cc|\\\\\\"dddd" eee\\\\\\"ffff"|"gg"');\r
31\r
32# Now, $keep == 0\r
33$result = join('|', parse_line('\s+', 0, $string));\r
34is($result, 'aaaabbbbb|cc cc|\\"dddd eee\\"ffff|gg');\r
35\r
36# Now test single quote behavior\r
37$string = 'aaaa"bbbbb" cc\\ cc \\\\\\"dddd\' eee\\\\\\"ffff\' gg';\r
38$result = join('|', parse_line('\s+', 0, $string));\r
39is($result, 'aaaabbbbb|cc cc|\\"dddd eee\\\\\\"ffff|gg');\r
40\r
41# Make sure @nested_quotewords does the right thing\r
42@lists = nested_quotewords('\s+', 0, 'a b c', '1 2 3', 'x y z');\r
43is (@lists, 3);\r
44is (@{$lists[0]}, 3);\r
45is (@{$lists[1]}, 3);\r
46is (@{$lists[2]}, 3);\r
47\r
48# Now test error return\r
49$string = 'foo bar baz"bach blech boop';\r
50\r
51@words = shellwords($string);\r
52is(@words, 0);\r
53\r
54@words = parse_line('s+', 0, $string);\r
55is(@words, 0);\r
56\r
57@words = quotewords('s+', 0, $string);\r
58is(@words, 0);\r
59\r
60{\r
61 # Gonna get some more undefined things back\r
62 no warnings 'uninitialized' ;\r
63\r
64 @words = nested_quotewords('s+', 0, $string);\r
65 is(@words, 0);\r
66\r
67 # Now test empty fields\r
68 $result = join('|', parse_line(':', 0, 'foo::0:"":::'));\r
69 is($result, 'foo||0||||');\r
70\r
71 # Test for 0 in quotes without $keep\r
72 $result = join('|', parse_line(':', 0, ':"0":'));\r
73 is($result, '|0|');\r
74\r
75 # Test for \001 in quoted string\r
76 $result = join('|', parse_line(':', 0, ':"' . "\001" . '":'));\r
77 is($result, "|\1|");\r
78\r
79}\r
80\r
81# Now test perlish single quote behavior\r
82$Text::ParseWords::PERL_SINGLE_QUOTE = 1;\r
83$string = 'aaaa"bbbbb" cc\ cc \\\\\"dddd\' eee\\\\\"\\\'ffff\' gg';\r
84$result = join('|', parse_line('\s+', 0, $string));\r
85is($result, 'aaaabbbbb|cc cc|\"dddd eee\\\\"\'ffff|gg');\r
86\r
87# test whitespace in the delimiters\r
88@words = quotewords(' ', 1, '4 3 2 1 0');\r
89is(join(";", @words), qq(4;3;2;1;0));\r
90\r
91# [perl #30442] Text::ParseWords does not handle backslashed newline inside quoted text\r
92$string = qq{"field1" "field2\\\nstill field2" "field3"};\r
93\r
94$result = join('|', parse_line("\t", 1, $string));\r
95is($result, qq{"field1"|"field2\\\nstill field2"|"field3"});\r
96\r
97$result = join('|', parse_line("\t", 0, $string));\r
98is($result, "field1|field2\nstill field2|field3");\r
99\r
100SKIP: { # unicode\r
101 skip "No unicode",1 if $]<5.008;\r
102 $string = qq{"field1"\x{1234}"field2\\\x{1234}still field2"\x{1234}"field3"};\r
103 $result = join('|', parse_line("\x{1234}", 0, $string));\r
104 is($result, "field1|field2\x{1234}still field2|field3",'Unicode');\r
105}\r
106\r
107# missing quote after matching regex used to hang after change #22997\r
108"1234" =~ /(1)(2)(3)(4)/;\r
109$string = qq{"missing quote};\r
110$result = join('|', shellwords($string));\r
111is($result, "");\r
112\r
113# make sure shellwords strips out leading whitespace and trailng undefs\r
114# from parse_line, so it's behavior is more like /bin/sh\r
115$result = join('|', shellwords(" aa \\ \\ bb ", " \\ ", "cc dd ee\\ "));\r
116is($result, "aa| | bb| |cc|dd|ee ");\r
117\r
118$SIG{ALRM} = sub {die "Timeout!"};\r
119alarm(3);\r
120@words = Text::ParseWords::old_shellwords("foo\\");\r
121is(@words, 1);\r
122alarm(0);\r