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