This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
261d81f3a4c05cea8f919bc260eeab718199d1ba
[perl5.git] / lib / Text / ParseWords.t
1 #!./perl
2
3 BEGIN {
4     chdir 't' if -d 't';
5     @INC = '../lib';
6 }
7
8 use warnings;
9 use Text::ParseWords;
10
11 print "1..18\n";
12
13 @words = shellwords(qq(foo "bar quiz" zoo));
14 print "not " if $words[0] ne 'foo';
15 print "ok 1\n";
16 print "not " if $words[1] ne 'bar quiz';
17 print "ok 2\n";
18 print "not " if $words[2] ne 'zoo';
19 print "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');
33 print "not " unless join(";", @words) eq qq(4; ;3; ;2; ;1; ;0);
34 print "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));
41 print "not " unless $result eq 'aaaa"bbbbb"|cc\\ cc|\\\\\\"dddd" eee\\\\\\"ffff"|"gg"';
42 print "ok 6\n";
43
44 # Now, $keep == 0
45 $result = join('|', parse_line('\s+', 0, $string));
46 print "not " unless $result eq 'aaaabbbbb|cc cc|\\"dddd eee\\"ffff|gg';
47 print "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));
52 print "not " unless $result eq 'aaaabbbbb|cc cc|\\"dddd eee\\\\\\"ffff|gg';
53 print "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');
57 print "not " unless (@lists == 3 && @{$lists[0]} == 3 && @{$lists[1]} == 3 && @{$lists[2]} == 3);
58 print "ok 9\n";
59
60 # Now test error return
61 $string = 'foo bar baz"bach blech boop';
62
63 @words = shellwords($string);
64 print "not " if (@words);
65 print "ok 10\n";
66
67 @words = parse_line('s+', 0, $string);
68 print "not " if (@words);
69 print "ok 11\n";
70
71 @words = quotewords('s+', 0, $string);
72 print "not " if (@words);
73 print "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));
104 print "not " unless $result eq 'aaaabbbbb|cc cc|\"dddd eee\\\\"\'ffff|gg';
105 print "ok 17\n";
106
107 # test whitespace in the delimiters
108 @words = quotewords(' ', 1, '4 3 2 1 0');
109 print "not " unless join(";", @words) eq qq(4;3;2;1;0);
110 print "ok 18\n";