This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix ptags
[perl5.git] / t / lib / parsewords.t
CommitLineData
1a3850a5
GA
1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
6}
7
1a3850a5
GA
8use Text::ParseWords;
9
9b599b2a 10print "1..15\n";
1a3850a5 11
9b599b2a 12@words = shellwords(qq(foo "bar quiz" zoo));
1a3850a5
GA
13print "not " if $words[0] ne 'foo';
14print "ok 1\n";
1a3850a5
GA
15print "not " if $words[1] ne 'bar quiz';
16print "ok 2\n";
1a3850a5
GA
17print "not " if $words[2] ne 'zoo';
18print "ok 3\n";
19
9b599b2a
GS
20# Test quotewords() with other parameters and null last field
21@words = quotewords(':+', 1, 'foo:::"bar:foo":zoo zoo:');
22print "not " unless join(";", @words) eq qq(foo;"bar:foo";zoo zoo;);
1a3850a5 23print "ok 4\n";
9b599b2a
GS
24
25# Test $keep eq 'delimiters' and last field zero
26@words = quotewords('\s+', 'delimiters', '4 3 2 1 0');
27print "not " unless join(";", @words) eq qq(4; ;3; ;2; ;1; ;0);
28print "ok 5\n";
29
30# Big ol' nasty test (thanks, Joerk!)
31$string = 'aaaa"bbbbb" cc\\ cc \\\\\\"dddd" eee\\\\\\"ffff" "gg"';
32
33# First with $keep == 1
34$result = join('|', parse_line('\s+', 1, $string));
35print "not " unless $result eq 'aaaa"bbbbb"|cc\\ cc|\\\\\\"dddd" eee\\\\\\"ffff"|"gg"';
36print "ok 6\n";
37
38# Now, $keep == 0
39$result = join('|', parse_line('\s+', 0, $string));
40print "not " unless $result eq 'aaaabbbbb|cc cc|\\"dddd eee\\"ffff|gg';
41print "ok 7\n";
42
43# Now test single quote behavior
44$string = 'aaaa"bbbbb" cc\\ cc \\\\\\"dddd\' eee\\\\\\"ffff\' gg';
45$result = join('|', parse_line('\s+', 0, $string));
46print "not " unless $result eq 'aaaabbbbb|cc cc|\\"dddd eee\\\\\\"ffff|gg';
47print "ok 8\n";
48
49# Make sure @nested_quotewords does the right thing
50@lists = nested_quotewords('\s+', 0, 'a b c', '1 2 3', 'x y z');
51print "not " unless (@lists == 3 && @{$lists[0]} == 3 && @{$lists[1]} == 3 && @{$lists[2]} == 3);
52print "ok 9\n";
53
54# Now test error return
55$string = 'foo bar baz"bach blech boop';
56
57@words = shellwords($string);
58print "not " if (@words);
59print "ok 10\n";
60
61@words = parse_line('s+', 0, $string);
62print "not " if (@words);
63print "ok 11\n";
64
65@words = quotewords('s+', 0, $string);
66print "not " if (@words);
67print "ok 12\n";
68
69@words = nested_quotewords('s+', 0, $string);
70print "not " if (@words);
71print "ok 13\n";
72
73# Now test empty fields
74$result = join('|', parse_line(':', 0, 'foo::0:"":::'));
75print "not " unless ($result eq 'foo||0||||');
76print "ok 14\n";
77
78# Test for 0 in quotes without $keep
79$result = join('|', parse_line(':', 0, ':"0":'));
80print "not " unless ($result eq '|0|');
81print "ok 15\n";