This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add symlink support to Cwd::_vms_abs_path.
[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 9use Text::ParseWords;
9983eac8 10use Test::More tests => 27;
1a3850a5 11
9b599b2a 12@words = shellwords(qq(foo "bar quiz" zoo));
46f0e7a5
NC
13is($words[0], 'foo');
14is($words[1], 'bar quiz');
15is($words[2], 'zoo');
1a3850a5 16
9f1b1f2d
GS
17{
18 # Gonna get some undefined things back
19 no warnings 'uninitialized' ;
b174585d 20
9f1b1f2d
GS
21 # Test quotewords() with other parameters and null last field
22 @words = quotewords(':+', 1, 'foo:::"bar:foo":zoo zoo:');
46f0e7a5 23 is(join(";", @words), qq(foo;"bar:foo";zoo zoo;));
9f1b1f2d 24}
b174585d 25
9b599b2a
GS
26# Test $keep eq 'delimiters' and last field zero
27@words = quotewords('\s+', 'delimiters', '4 3 2 1 0');
46f0e7a5 28is(join(";", @words), qq(4; ;3; ;2; ;1; ;0));
9b599b2a
GS
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));
46f0e7a5 35is($result, 'aaaa"bbbbb"|cc\\ cc|\\\\\\"dddd" eee\\\\\\"ffff"|"gg"');
9b599b2a
GS
36
37# Now, $keep == 0
38$result = join('|', parse_line('\s+', 0, $string));
46f0e7a5 39is($result, 'aaaabbbbb|cc cc|\\"dddd eee\\"ffff|gg');
9b599b2a
GS
40
41# Now test single quote behavior
42$string = 'aaaa"bbbbb" cc\\ cc \\\\\\"dddd\' eee\\\\\\"ffff\' gg';
43$result = join('|', parse_line('\s+', 0, $string));
46f0e7a5 44is($result, 'aaaabbbbb|cc cc|\\"dddd eee\\\\\\"ffff|gg');
9b599b2a
GS
45
46# Make sure @nested_quotewords does the right thing
47@lists = nested_quotewords('\s+', 0, 'a b c', '1 2 3', 'x y z');
46f0e7a5
NC
48is (@lists, 3);
49is (@{$lists[0]}, 3);
50is (@{$lists[1]}, 3);
51is (@{$lists[2]}, 3);
9b599b2a
GS
52
53# Now test error return
54$string = 'foo bar baz"bach blech boop';
55
56@words = shellwords($string);
46f0e7a5 57is(@words, 0);
9b599b2a
GS
58
59@words = parse_line('s+', 0, $string);
46f0e7a5 60is(@words, 0);
9b599b2a
GS
61
62@words = quotewords('s+', 0, $string);
46f0e7a5 63is(@words, 0);
9b599b2a 64
9f1b1f2d
GS
65{
66 # Gonna get some more undefined things back
67 no warnings 'uninitialized' ;
b174585d 68
9f1b1f2d 69 @words = nested_quotewords('s+', 0, $string);
46f0e7a5 70 is(@words, 0);
9b599b2a 71
9f1b1f2d
GS
72 # Now test empty fields
73 $result = join('|', parse_line(':', 0, 'foo::0:"":::'));
46f0e7a5 74 is($result, 'foo||0||||');
9b599b2a 75
9f1b1f2d
GS
76 # Test for 0 in quotes without $keep
77 $result = join('|', parse_line(':', 0, ':"0":'));
46f0e7a5 78 is($result, '|0|');
b174585d 79
9f1b1f2d
GS
80 # Test for \001 in quoted string
81 $result = join('|', parse_line(':', 0, ':"' . "\001" . '":'));
46f0e7a5 82 is($result, "|\1|");
b174585d 83
9f1b1f2d 84}
b174585d
MH
85
86# Now test perlish single quote behavior
87$Text::ParseWords::PERL_SINGLE_QUOTE = 1;
88$string = 'aaaa"bbbbb" cc\ cc \\\\\"dddd\' eee\\\\\"\\\'ffff\' gg';
89$result = join('|', parse_line('\s+', 0, $string));
46f0e7a5 90is($result, 'aaaabbbbb|cc cc|\"dddd eee\\\\"\'ffff|gg');
f3a6e335
IZ
91
92# test whitespace in the delimiters
93@words = quotewords(' ', 1, '4 3 2 1 0');
46f0e7a5 94is(join(";", @words), qq(4;3;2;1;0));
a8c6c617
MHM
95
96# [perl #30442] Text::ParseWords does not handle backslashed newline inside quoted text
97$string = qq{"field1" "field2\\\nstill field2" "field3"};
98
99$result = join('|', parse_line("\t", 1, $string));
46f0e7a5 100is($result, qq{"field1"|"field2\\\nstill field2"|"field3"});
a8c6c617
MHM
101
102$result = join('|', parse_line("\t", 0, $string));
46f0e7a5 103is($result, "field1|field2\nstill field2|field3");
429b060a
MHM
104
105# unicode
106$string = qq{"field1"\x{1234}"field2\\\x{1234}still field2"\x{1234}"field3"};
107$result = join('|', parse_line("\x{1234}", 0, $string));
46f0e7a5 108is($result, "field1|field2\x{1234}still field2|field3");
30799d55
MHM
109
110# missing quote after matching regex used to hang after change #22997
111"1234" =~ /(1)(2)(3)(4)/;
112$string = qq{"missing quote};
113$result = join('|', shellwords($string));
46f0e7a5 114is($result, "");
d5c14ab2
AT
115
116# make sure shellwords strips out leading whitespace and trailng undefs
117# from parse_line, so it's behavior is more like /bin/sh
118$result = join('|', shellwords(" aa \\ \\ bb ", " \\ ", "cc dd ee\\ "));
46f0e7a5 119is($result, "aa| | bb| |cc|dd|ee ");
9983eac8
AB
120
121$SIG{ALRM} = sub {die "Timeout!"};
122alarm(3);
123@words = Text::ParseWords::old_shellwords("foo\\");
124is(@words, 1);
125alarm(0);