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