xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/lib/Text/ParseWords.t (revision 0:68f95e015346)
1#!./perl
2
3BEGIN {
4    chdir 't' if -d 't';
5    @INC = '../lib';
6}
7
8use warnings;
9use Text::ParseWords;
10
11print "1..18\n";
12
13@words = shellwords(qq(foo "bar quiz" zoo));
14print "not " if $words[0] ne 'foo';
15print "ok 1\n";
16print "not " if $words[1] ne 'bar quiz';
17print "ok 2\n";
18print "not " if $words[2] ne 'zoo';
19print "ok 3\n";
20
21{
22  # Gonna get some undefined things back
23  no warnings 'uninitialized' ;
24
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}
30
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
75{
76  # Gonna get some more undefined things back
77  no warnings 'uninitialized' ;
78
79  @words = nested_quotewords('s+', 0, $string);
80  print "not " if (@words);
81  print "ok 13\n";
82
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";
87
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";
92
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";
97
98}
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";
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";
111