Initial import from FreeBSD RELENG_4:
[games.git] / contrib / perl5 / t / lib / parsewords.t
1 #!./perl
2
3 BEGIN {
4     chdir 't' if -d 't';
5     @INC = '../lib';
6 }
7
8 use Text::ParseWords;
9
10 print "1..18\n";
11
12 @words = shellwords(qq(foo "bar quiz" zoo));
13 print "not " if $words[0] ne 'foo';
14 print "ok 1\n";
15 print "not " if $words[1] ne 'bar quiz';
16 print "ok 2\n";
17 print "not " if $words[2] ne 'zoo';
18 print "ok 3\n";
19
20 # Gonna get some undefined things back
21 local($^W) = 0;
22
23 # Test quotewords() with other parameters and null last field
24 @words = quotewords(':+', 1, 'foo:::"bar:foo":zoo zoo:');
25 print "not " unless join(";", @words) eq qq(foo;"bar:foo";zoo zoo;);
26 print "ok 4\n";
27
28 $^W = 1;
29
30 # Test $keep eq 'delimiters' and last field zero
31 @words = quotewords('\s+', 'delimiters', '4 3 2 1 0');
32 print "not " unless join(";", @words) eq qq(4; ;3; ;2; ;1; ;0);
33 print "ok 5\n";
34
35 # Big ol' nasty test (thanks, Joerk!)
36 $string = 'aaaa"bbbbb" cc\\ cc \\\\\\"dddd" eee\\\\\\"ffff" "gg"';
37
38 # First with $keep == 1
39 $result = join('|', parse_line('\s+', 1, $string));
40 print "not " unless $result eq 'aaaa"bbbbb"|cc\\ cc|\\\\\\"dddd" eee\\\\\\"ffff"|"gg"';
41 print "ok 6\n";
42
43 # Now, $keep == 0
44 $result = join('|', parse_line('\s+', 0, $string));
45 print "not " unless $result eq 'aaaabbbbb|cc cc|\\"dddd eee\\"ffff|gg';
46 print "ok 7\n";
47
48 # Now test single quote behavior
49 $string = 'aaaa"bbbbb" cc\\ cc \\\\\\"dddd\' eee\\\\\\"ffff\' gg';
50 $result = join('|', parse_line('\s+', 0, $string));
51 print "not " unless $result eq 'aaaabbbbb|cc cc|\\"dddd eee\\\\\\"ffff|gg';
52 print "ok 8\n";
53
54 # Make sure @nested_quotewords does the right thing
55 @lists = nested_quotewords('\s+', 0, 'a b c', '1 2 3', 'x y z');
56 print "not " unless (@lists == 3 && @{$lists[0]} == 3 && @{$lists[1]} == 3 && @{$lists[2]} == 3);
57 print "ok 9\n";
58
59 # Now test error return
60 $string = 'foo bar baz"bach blech boop';
61
62 @words = shellwords($string);
63 print "not " if (@words);
64 print "ok 10\n";
65
66 @words = parse_line('s+', 0, $string);
67 print "not " if (@words);
68 print "ok 11\n";
69
70 @words = quotewords('s+', 0, $string);
71 print "not " if (@words);
72 print "ok 12\n";
73
74 # Gonna get some more undefined things back
75 $^W = 0;
76
77 @words = nested_quotewords('s+', 0, $string);
78 print "not " if (@words);
79 print "ok 13\n";
80
81 # Now test empty fields
82 $result = join('|', parse_line(':', 0, 'foo::0:"":::'));
83 print "not " unless ($result eq 'foo||0||||');
84 print "ok 14\n";
85
86 # Test for 0 in quotes without $keep
87 $result = join('|', parse_line(':', 0, ':"0":'));
88 print "not " unless ($result eq '|0|');
89 print "ok 15\n";
90
91 # Test for \001 in quoted string
92 $result = join('|', parse_line(':', 0, ':"' . "\001" . '":'));
93 print "not " unless ($result eq "|\1|");
94 print "ok 16\n";
95
96 $^W = 1;
97
98 # Now test perlish single quote behavior
99 $Text::ParseWords::PERL_SINGLE_QUOTE = 1;
100 $string = 'aaaa"bbbbb" cc\ cc \\\\\"dddd\' eee\\\\\"\\\'ffff\' gg';
101 $result = join('|', parse_line('\s+', 0, $string));
102 print "not " unless $result eq 'aaaabbbbb|cc cc|\"dddd eee\\\\"\'ffff|gg';
103 print "ok 17\n";
104
105 # test whitespace in the delimiters
106 @words = quotewords(' ', 1, '4 3 2 1 0');
107 print "not " unless join(";", @words) eq qq(4;3;2;1;0);
108 print "ok 18\n";