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