This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
The new Archive::Tar tests are TODO on VMS for reasons unrelated
[perl5.git] / lib / Text / ParseWords.t
1 #!./perl
2
3 BEGIN {
4     chdir 't' if -d 't';
5     @INC = '../lib';
6 }
7
8 use warnings;
9 use Text::ParseWords;
10 use Test::More tests => 27;
11
12 @words = shellwords(qq(foo "bar quiz" zoo));
13 is($words[0], 'foo');
14 is($words[1], 'bar quiz');
15 is($words[2], 'zoo');
16
17 {
18   # Gonna get some undefined things back
19   no warnings 'uninitialized' ;
20
21   # Test quotewords() with other parameters and null last field
22   @words = quotewords(':+', 1, 'foo:::"bar:foo":zoo zoo:');
23   is(join(";", @words), qq(foo;"bar:foo";zoo zoo;));
24 }
25
26 # Test $keep eq 'delimiters' and last field zero
27 @words = quotewords('\s+', 'delimiters', '4 3 2 1 0');
28 is(join(";", @words), qq(4; ;3; ;2; ;1; ;0));
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));
35 is($result, 'aaaa"bbbbb"|cc\\ cc|\\\\\\"dddd" eee\\\\\\"ffff"|"gg"');
36
37 # Now, $keep == 0
38 $result = join('|', parse_line('\s+', 0, $string));
39 is($result, 'aaaabbbbb|cc cc|\\"dddd eee\\"ffff|gg');
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));
44 is($result, 'aaaabbbbb|cc cc|\\"dddd eee\\\\\\"ffff|gg');
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');
48 is (@lists, 3);
49 is (@{$lists[0]}, 3);
50 is (@{$lists[1]}, 3);
51 is (@{$lists[2]}, 3);
52
53 # Now test error return
54 $string = 'foo bar baz"bach blech boop';
55
56 @words = shellwords($string);
57 is(@words, 0);
58
59 @words = parse_line('s+', 0, $string);
60 is(@words, 0);
61
62 @words = quotewords('s+', 0, $string);
63 is(@words, 0);
64
65 {
66   # Gonna get some more undefined things back
67   no warnings 'uninitialized' ;
68
69   @words = nested_quotewords('s+', 0, $string);
70   is(@words, 0);
71
72   # Now test empty fields
73   $result = join('|', parse_line(':', 0, 'foo::0:"":::'));
74   is($result, 'foo||0||||');
75
76   # Test for 0 in quotes without $keep
77   $result = join('|', parse_line(':', 0, ':"0":'));
78   is($result, '|0|');
79
80   # Test for \001 in quoted string
81   $result = join('|', parse_line(':', 0, ':"' . "\001" . '":'));
82   is($result, "|\1|");
83
84 }
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));
90 is($result, 'aaaabbbbb|cc cc|\"dddd eee\\\\"\'ffff|gg');
91
92 # test whitespace in the delimiters
93 @words = quotewords(' ', 1, '4 3 2 1 0');
94 is(join(";", @words), qq(4;3;2;1;0));
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));
100 is($result, qq{"field1"|"field2\\\nstill field2"|"field3"});
101
102 $result = join('|', parse_line("\t", 0, $string));
103 is($result, "field1|field2\nstill field2|field3");
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));
108 is($result, "field1|field2\x{1234}still field2|field3");
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));
114 is($result, "");
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\\ "));
119 is($result, "aa| | bb| |cc|dd|ee ");
120
121 $SIG{ALRM} = sub {die "Timeout!"};
122 alarm(3);
123 @words = Text::ParseWords::old_shellwords("foo\\");
124 is(@words, 1);
125 alarm(0);