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