This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
versions threads modules
[perl5.git] / lib / Text / ParseWords.t
CommitLineData
1a3850a5
GA
1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
20822f61 5 @INC = '../lib';
1a3850a5
GA
6}
7
9f1b1f2d 8use warnings;
1a3850a5
GA
9use Text::ParseWords;
10
f3a6e335 11print "1..18\n";
1a3850a5 12
9b599b2a 13@words = shellwords(qq(foo "bar quiz" zoo));
1a3850a5
GA
14print "not " if $words[0] ne 'foo';
15print "ok 1\n";
1a3850a5
GA
16print "not " if $words[1] ne 'bar quiz';
17print "ok 2\n";
1a3850a5
GA
18print "not " if $words[2] ne 'zoo';
19print "ok 3\n";
20
9f1b1f2d
GS
21{
22 # Gonna get some undefined things back
23 no warnings 'uninitialized' ;
b174585d 24
9f1b1f2d
GS
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
GS
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
GS
75{
76 # Gonna get some more undefined things back
77 no warnings 'uninitialized' ;
b174585d 78
9f1b1f2d
GS
79 @words = nested_quotewords('s+', 0, $string);
80 print "not " if (@words);
81 print "ok 13\n";
9b599b2a 82
9f1b1f2d
GS
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
GS
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
GS
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
MH
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
IZ
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";