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