This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Enhance lfs tests: check every seek and sysseek
[perl5.git] / t / lib / parsewords.t
CommitLineData
1a3850a5
GA
1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
93430cb4 5 unshift @INC, '../lib';
1a3850a5
GA
6}
7
1a3850a5
GA
8use Text::ParseWords;
9
f3a6e335 10print "1..18\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
b174585d
MH
20# Gonna get some undefined things back
21local($^W) = 0;
22
9b599b2a
GS
23# Test quotewords() with other parameters and null last field
24@words = quotewords(':+', 1, 'foo:::"bar:foo":zoo zoo:');
25print "not " unless join(";", @words) eq qq(foo;"bar:foo";zoo zoo;);
1a3850a5 26print "ok 4\n";
9b599b2a 27
b174585d
MH
28$^W = 1;
29
9b599b2a
GS
30# Test $keep eq 'delimiters' and last field zero
31@words = quotewords('\s+', 'delimiters', '4 3 2 1 0');
32print "not " unless join(";", @words) eq qq(4; ;3; ;2; ;1; ;0);
33print "ok 5\n";
34
35# Big ol' nasty test (thanks, Joerk!)
36$string = 'aaaa"bbbbb" cc\\ cc \\\\\\"dddd" eee\\\\\\"ffff" "gg"';
37
38# First with $keep == 1
39$result = join('|', parse_line('\s+', 1, $string));
40print "not " unless $result eq 'aaaa"bbbbb"|cc\\ cc|\\\\\\"dddd" eee\\\\\\"ffff"|"gg"';
41print "ok 6\n";
42
43# Now, $keep == 0
44$result = join('|', parse_line('\s+', 0, $string));
45print "not " unless $result eq 'aaaabbbbb|cc cc|\\"dddd eee\\"ffff|gg';
46print "ok 7\n";
47
48# Now test single quote behavior
49$string = 'aaaa"bbbbb" cc\\ cc \\\\\\"dddd\' eee\\\\\\"ffff\' gg';
50$result = join('|', parse_line('\s+', 0, $string));
51print "not " unless $result eq 'aaaabbbbb|cc cc|\\"dddd eee\\\\\\"ffff|gg';
52print "ok 8\n";
53
54# Make sure @nested_quotewords does the right thing
55@lists = nested_quotewords('\s+', 0, 'a b c', '1 2 3', 'x y z');
56print "not " unless (@lists == 3 && @{$lists[0]} == 3 && @{$lists[1]} == 3 && @{$lists[2]} == 3);
57print "ok 9\n";
58
59# Now test error return
60$string = 'foo bar baz"bach blech boop';
61
62@words = shellwords($string);
63print "not " if (@words);
64print "ok 10\n";
65
66@words = parse_line('s+', 0, $string);
67print "not " if (@words);
68print "ok 11\n";
69
70@words = quotewords('s+', 0, $string);
71print "not " if (@words);
72print "ok 12\n";
73
b174585d
MH
74# Gonna get some more undefined things back
75$^W = 0;
76
9b599b2a
GS
77@words = nested_quotewords('s+', 0, $string);
78print "not " if (@words);
79print "ok 13\n";
80
81# Now test empty fields
82$result = join('|', parse_line(':', 0, 'foo::0:"":::'));
83print "not " unless ($result eq 'foo||0||||');
84print "ok 14\n";
85
86# Test for 0 in quotes without $keep
87$result = join('|', parse_line(':', 0, ':"0":'));
88print "not " unless ($result eq '|0|');
89print "ok 15\n";
b174585d
MH
90
91# Test for \001 in quoted string
92$result = join('|', parse_line(':', 0, ':"' . "\001" . '":'));
93print "not " unless ($result eq "|\1|");
94print "ok 16\n";
95
96$^W = 1;
97
98# Now test perlish single quote behavior
99$Text::ParseWords::PERL_SINGLE_QUOTE = 1;
100$string = 'aaaa"bbbbb" cc\ cc \\\\\"dddd\' eee\\\\\"\\\'ffff\' gg';
101$result = join('|', parse_line('\s+', 0, $string));
102print "not " unless $result eq 'aaaabbbbb|cc cc|\"dddd eee\\\\"\'ffff|gg';
103print "ok 17\n";
f3a6e335
IZ
104
105# test whitespace in the delimiters
106@words = quotewords(' ', 1, '4 3 2 1 0');
107print "not " unless join(";", @words) eq qq(4;3;2;1;0);
108print "ok 18\n";