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