Commit | Line | Data |
1a3850a5 |
1 | #!./perl |
2 | |
3 | BEGIN { |
4 | chdir 't' if -d 't'; |
20822f61 |
5 | @INC = '../lib'; |
1a3850a5 |
6 | } |
7 | |
9f1b1f2d |
8 | use warnings; |
1a3850a5 |
9 | use Text::ParseWords; |
10 | |
429b060a |
11 | print "1..21\n"; |
1a3850a5 |
12 | |
9b599b2a |
13 | @words = shellwords(qq(foo "bar quiz" zoo)); |
1a3850a5 |
14 | print "not " if $words[0] ne 'foo'; |
15 | print "ok 1\n"; |
1a3850a5 |
16 | print "not " if $words[1] ne 'bar quiz'; |
17 | print "ok 2\n"; |
1a3850a5 |
18 | print "not " if $words[2] ne 'zoo'; |
19 | print "ok 3\n"; |
20 | |
9f1b1f2d |
21 | { |
22 | # Gonna get some undefined things back |
23 | no warnings 'uninitialized' ; |
b174585d |
24 | |
9f1b1f2d |
25 | # Test quotewords() with other parameters and null last field |
26 | @words = quotewords(':+', 1, 'foo:::"bar:foo":zoo zoo:'); |
27 | print "not " unless join(";", @words) eq qq(foo;"bar:foo";zoo zoo;); |
28 | print "ok 4\n"; |
29 | } |
b174585d |
30 | |
9b599b2a |
31 | # Test $keep eq 'delimiters' and last field zero |
32 | @words = quotewords('\s+', 'delimiters', '4 3 2 1 0'); |
33 | print "not " unless join(";", @words) eq qq(4; ;3; ;2; ;1; ;0); |
34 | print "ok 5\n"; |
35 | |
36 | # Big ol' nasty test (thanks, Joerk!) |
37 | $string = 'aaaa"bbbbb" cc\\ cc \\\\\\"dddd" eee\\\\\\"ffff" "gg"'; |
38 | |
39 | # First with $keep == 1 |
40 | $result = join('|', parse_line('\s+', 1, $string)); |
41 | print "not " unless $result eq 'aaaa"bbbbb"|cc\\ cc|\\\\\\"dddd" eee\\\\\\"ffff"|"gg"'; |
42 | print "ok 6\n"; |
43 | |
44 | # Now, $keep == 0 |
45 | $result = join('|', parse_line('\s+', 0, $string)); |
46 | print "not " unless $result eq 'aaaabbbbb|cc cc|\\"dddd eee\\"ffff|gg'; |
47 | print "ok 7\n"; |
48 | |
49 | # Now test single quote behavior |
50 | $string = 'aaaa"bbbbb" cc\\ cc \\\\\\"dddd\' eee\\\\\\"ffff\' gg'; |
51 | $result = join('|', parse_line('\s+', 0, $string)); |
52 | print "not " unless $result eq 'aaaabbbbb|cc cc|\\"dddd eee\\\\\\"ffff|gg'; |
53 | print "ok 8\n"; |
54 | |
55 | # Make sure @nested_quotewords does the right thing |
56 | @lists = nested_quotewords('\s+', 0, 'a b c', '1 2 3', 'x y z'); |
57 | print "not " unless (@lists == 3 && @{$lists[0]} == 3 && @{$lists[1]} == 3 && @{$lists[2]} == 3); |
58 | print "ok 9\n"; |
59 | |
60 | # Now test error return |
61 | $string = 'foo bar baz"bach blech boop'; |
62 | |
63 | @words = shellwords($string); |
64 | print "not " if (@words); |
65 | print "ok 10\n"; |
66 | |
67 | @words = parse_line('s+', 0, $string); |
68 | print "not " if (@words); |
69 | print "ok 11\n"; |
70 | |
71 | @words = quotewords('s+', 0, $string); |
72 | print "not " if (@words); |
73 | print "ok 12\n"; |
74 | |
9f1b1f2d |
75 | { |
76 | # Gonna get some more undefined things back |
77 | no warnings 'uninitialized' ; |
b174585d |
78 | |
9f1b1f2d |
79 | @words = nested_quotewords('s+', 0, $string); |
80 | print "not " if (@words); |
81 | print "ok 13\n"; |
9b599b2a |
82 | |
9f1b1f2d |
83 | # Now test empty fields |
84 | $result = join('|', parse_line(':', 0, 'foo::0:"":::')); |
85 | print "not " unless ($result eq 'foo||0||||'); |
86 | print "ok 14\n"; |
9b599b2a |
87 | |
9f1b1f2d |
88 | # Test for 0 in quotes without $keep |
89 | $result = join('|', parse_line(':', 0, ':"0":')); |
90 | print "not " unless ($result eq '|0|'); |
91 | print "ok 15\n"; |
b174585d |
92 | |
9f1b1f2d |
93 | # Test for \001 in quoted string |
94 | $result = join('|', parse_line(':', 0, ':"' . "\001" . '":')); |
95 | print "not " unless ($result eq "|\1|"); |
96 | print "ok 16\n"; |
b174585d |
97 | |
9f1b1f2d |
98 | } |
b174585d |
99 | |
100 | # Now test perlish single quote behavior |
101 | $Text::ParseWords::PERL_SINGLE_QUOTE = 1; |
102 | $string = 'aaaa"bbbbb" cc\ cc \\\\\"dddd\' eee\\\\\"\\\'ffff\' gg'; |
103 | $result = join('|', parse_line('\s+', 0, $string)); |
104 | print "not " unless $result eq 'aaaabbbbb|cc cc|\"dddd eee\\\\"\'ffff|gg'; |
105 | print "ok 17\n"; |
f3a6e335 |
106 | |
107 | # test whitespace in the delimiters |
108 | @words = quotewords(' ', 1, '4 3 2 1 0'); |
109 | print "not " unless join(";", @words) eq qq(4;3;2;1;0); |
110 | print "ok 18\n"; |
a8c6c617 |
111 | |
112 | # [perl #30442] Text::ParseWords does not handle backslashed newline inside quoted text |
113 | $string = qq{"field1" "field2\\\nstill field2" "field3"}; |
114 | |
115 | $result = join('|', parse_line("\t", 1, $string)); |
116 | print "not " unless $result eq qq{"field1"|"field2\\\nstill field2"|"field3"}; |
117 | print "ok 19\n"; |
118 | |
119 | $result = join('|', parse_line("\t", 0, $string)); |
120 | print "not " unless $result eq "field1|field2\nstill field2|field3"; |
121 | print "ok 20\n"; |
429b060a |
122 | |
123 | # unicode |
124 | $string = qq{"field1"\x{1234}"field2\\\x{1234}still field2"\x{1234}"field3"}; |
125 | $result = join('|', parse_line("\x{1234}", 0, $string)); |
126 | print "not " unless $result eq "field1|field2\x{1234}still field2|field3"; |
127 | print "ok 21\n"; |