4 use Text::ParseWords;
\r
5 use Test::More tests => 27;
\r
7 @words = shellwords(qq(foo "bar quiz" zoo));
\r
8 is($words[0], 'foo');
\r
9 is($words[1], 'bar quiz');
\r
10 is($words[2], 'zoo');
\r
13 # Gonna get some undefined things back
\r
14 no warnings 'uninitialized' ;
\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
21 # Test $keep eq 'delimiters' and last field zero
\r
22 @words = quotewords('\s+', 'delimiters', '4 3 2 1 0');
\r
23 is(join(";", @words), qq(4; ;3; ;2; ;1; ;0));
\r
25 # Big ol' nasty test (thanks, Joerk!)
\r
26 $string = 'aaaa"bbbbb" cc\\ cc \\\\\\"dddd" eee\\\\\\"ffff" "gg"';
\r
28 # First with $keep == 1
\r
29 $result = join('|', parse_line('\s+', 1, $string));
\r
30 is($result, 'aaaa"bbbbb"|cc\\ cc|\\\\\\"dddd" eee\\\\\\"ffff"|"gg"');
\r
33 $result = join('|', parse_line('\s+', 0, $string));
\r
34 is($result, 'aaaabbbbb|cc cc|\\"dddd eee\\"ffff|gg');
\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
39 is($result, 'aaaabbbbb|cc cc|\\"dddd eee\\\\\\"ffff|gg');
\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
44 is (@{$lists[0]}, 3);
\r
45 is (@{$lists[1]}, 3);
\r
46 is (@{$lists[2]}, 3);
\r
48 # Now test error return
\r
49 $string = 'foo bar baz"bach blech boop';
\r
51 @words = shellwords($string);
\r
54 @words = parse_line('s+', 0, $string);
\r
57 @words = quotewords('s+', 0, $string);
\r
61 # Gonna get some more undefined things back
\r
62 no warnings 'uninitialized' ;
\r
64 @words = nested_quotewords('s+', 0, $string);
\r
67 # Now test empty fields
\r
68 $result = join('|', parse_line(':', 0, 'foo::0:"":::'));
\r
69 is($result, 'foo||0||||');
\r
71 # Test for 0 in quotes without $keep
\r
72 $result = join('|', parse_line(':', 0, ':"0":'));
\r
75 # Test for \001 in quoted string
\r
76 $result = join('|', parse_line(':', 0, ':"' . "\001" . '":'));
\r
77 is($result, "|\1|");
\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
85 is($result, 'aaaabbbbb|cc cc|\"dddd eee\\\\"\'ffff|gg');
\r
87 # test whitespace in the delimiters
\r
88 @words = quotewords(' ', 1, '4 3 2 1 0');
\r
89 is(join(";", @words), qq(4;3;2;1;0));
\r
91 # [perl #30442] Text::ParseWords does not handle backslashed newline inside quoted text
\r
92 $string = qq{"field1" "field2\\\nstill field2" "field3"};
\r
94 $result = join('|', parse_line("\t", 1, $string));
\r
95 is($result, qq{"field1"|"field2\\\nstill field2"|"field3"});
\r
97 $result = join('|', parse_line("\t", 0, $string));
\r
98 is($result, "field1|field2\nstill field2|field3");
\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
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
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
116 is($result, "aa| | bb| |cc|dd|ee ");
\r
118 $SIG{ALRM} = sub {die "Timeout!"};
\r
120 @words = Text::ParseWords::old_shellwords("foo\\");
\r