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