Login
svn merge -r 2029:2018 ProhibitNegativeExpressionsInUnlessAndUntilConditions.run.PL
[gknop/Perl-Critic.git] / t / ControlStructures / ProhibitNegativeExpressionsInUnlessAndUntilConditions.run.PL
CommitLineData
9633a6db
ES
1#!/usr/bin/env perl
2
3use strict;
4use warnings;
5
6use English qw{-no_match_vars};
7
8use Carp qw{ confess };
9use Fatal qw{ open close };
10
f145a652 11our $VERSION = '1.080';
9633a6db
ES
12
13
14
15my $this_program = __FILE__;
16(my $test_file_name = $this_program) =~ s/ \.PL \z //xms;
17if ($this_program eq $test_file_name) {
18 confess
19 'Was not able to figure out the name of the file to generate.'
20 . "This program: $this_program.";
21}
22
6d34b243 23print "\n\nGenerating $test_file_name.\n";
9633a6db
ES
24
25
26
27open my $test_file, '>', $test_file_name
28 or die "Could not open $test_file_name: $ERRNO";
29
30
31print {$test_file} <<"END_HEADER";
32# Do not edit!!! This test suite generated by $this_program.
33END_HEADER
34
35foreach my $operator ( qw/ ! not / ) {
36 emit_not_operator_code($test_file, $operator);
37}
38emit_not_match_code($test_file);
39foreach my $operator ( qw/ ne != < > <= >= <=> lt gt le ge cmp / ) {
40 emit_comparator_code($test_file, $operator);
41}
42
43
44close $test_file;
6d34b243 45print "Done.\n\n";
9633a6db
ES
46
47#-----------------------------------------------------------------------------
48
49sub emit_not_operator_code {
50 my ($test_file, $operator) = @_;
51
a67dfb2e 52 print {$test_file} <<"END_NOT_OPERATOR_CODE";
9633a6db 53
a67dfb2e 54## name "$operator" within positive control structures
9633a6db
ES
55## failures 0
56## cut
57
a67dfb2e 58if ($operator \$foo) {
9633a6db
ES
59 blah();
60}
61
a67dfb2e
JRT
62if (\$foo) {
63 blah(\$foo);
9633a6db 64}
a67dfb2e
JRT
65elsif ($operator \$bar) {
66 blah(\$bar);
9633a6db
ES
67}
68else {
69 blah(undef);
70}
71
a67dfb2e 72while ($operator \$foo) {
9633a6db
ES
73 blah();
74}
75
a67dfb2e
JRT
76foreach my \$bar ( grep { $operator \$_ } \@foo ) {
77 blah(\$bar);
9633a6db
ES
78}
79
a67dfb2e
JRT
80for (my \$bar = 0; $operator \$bar; \$bar++) {
81 blah(\$bar);
9633a6db
ES
82}
83
84#-----------------------------------------------------------------------------
85
a67dfb2e 86## name "$operator" within positive postfix statement modifiers
9633a6db
ES
87## failures 0
88## cut
89
a67dfb2e 90blah() if $operator \$foo;
9633a6db 91
a67dfb2e 92blah() while $operator \$foo;
9633a6db 93
a67dfb2e 94blah(\$_) for grep { $operator \$_ } \@foo;
9633a6db
ES
95
96#-----------------------------------------------------------------------------
97
a67dfb2e 98## name "$operator" within negative control structures
9633a6db
ES
99## failures 2
100## cut
101
a67dfb2e 102unless ($operator \$foo) {
9633a6db
ES
103 blah();
104}
105
a67dfb2e 106until ($operator \$foo) {
9633a6db
ES
107 blah();
108}
109
110#-----------------------------------------------------------------------------
111
a67dfb2e 112## name "$operator" within negative postfix statement modifiers
9633a6db
ES
113## failures 2
114## cut
115
a67dfb2e 116blah() unless $operator \$foo;
9633a6db 117
a67dfb2e 118blah() until $operator \$foo;
9633a6db
ES
119
120#-----------------------------------------------------------------------------
121END_NOT_OPERATOR_CODE
122}
123
124#-----------------------------------------------------------------------------
125
126sub emit_not_match_code {
127 my ($test_file) = @_;
128
129 print {$test_file} <<'END_NOT_MATCH_CODE';
130
131## name "!~" within positive control structures
132## failures 0
133## cut
134
135if ($foo !~ m/bar/) {
136 blah();
137}
138
139if ($foo) {
140 blah($foo);
141}
142elsif ($bar !~ m/bar/) {
143 blah($bar);
144}
145else {
146 blah(undef);
147}
148
149while ($foo !~ m/bar/) {
150 blah();
151}
152
153foreach my $bar ( grep { $_ !~ m/baz/ } @foo ) {
154 blah($bar);
155}
156
157for (my $bar = 0; $bar =~ m/baz/; $bar++) {
158 blah($bar);
159}
160
161#-----------------------------------------------------------------------------
162
163## name "!~" within positive postfix statement modifiers
164## failures 0
165## cut
166
167blah() if $foo !~ m/bar/;
168
169blah() while $foo !~ m/bar/;
170
171blah($_) for grep { $_ !~ m/bar/ } @foo;
172
173#-----------------------------------------------------------------------------
174
175## name "!~" within negative control structures
176## failures 2
177## cut
178
179unless ($foo !~ m/bar/) {
180 blah();
181}
182
183until ($foo !~ m/bar/) {
184 blah();
185}
186
187#-----------------------------------------------------------------------------
188
189## name "!~" within negative postfix statement modifiers
190## failures 2
191## cut
192
193blah() unless $foo !~ m/bar/;
194
195blah() until $foo !~ m/bar/;
196
197#-----------------------------------------------------------------------------
198END_NOT_MATCH_CODE
199}
200
201#-----------------------------------------------------------------------------
202
203sub emit_comparator_code {
204 my ($test_file, $operator) = @_;
205
a67dfb2e 206 print {$test_file} <<"END_COMPARATOR_CODE";
9633a6db 207
a67dfb2e 208## name "$operator" within positive control structures
9633a6db
ES
209## failures 0
210## cut
211
a67dfb2e 212if (\$foo $operator \$bar) {
9633a6db
ES
213 blah();
214}
215
a67dfb2e
JRT
216if (\$foo $operator \$bar) {
217 blah(\$foo);
9633a6db 218}
a67dfb2e
JRT
219elsif (\$bar $operator \$baz) {
220 blah(\$bar);
9633a6db
ES
221}
222else {
223 blah(undef);
224}
225
a67dfb2e 226while (\$foo $operator \$bar) {
9633a6db
ES
227 blah();
228}
229
a67dfb2e
JRT
230foreach my \$bar ( grep { \$_ $operator \$baz } \@foo ) {
231 blah(\$bar);
9633a6db
ES
232}
233
a67dfb2e
JRT
234for (my \$bar = 0; \$bar $operator \$baz; \$bar++) {
235 blah(\$bar);
9633a6db
ES
236}
237
238#-----------------------------------------------------------------------------
239
a67dfb2e 240## name "$operator" within positive postfix statement modifiers
9633a6db
ES
241## failures 0
242## cut
243
a67dfb2e 244blah() if \$foo $operator \$bar;
9633a6db 245
a67dfb2e 246blah() while \$foo $operator \$bar;
9633a6db 247
a67dfb2e 248blah(\$_) for grep { \$_ $operator \$bar } \@foo;
9633a6db
ES
249
250#-----------------------------------------------------------------------------
251
a67dfb2e 252## name "$operator" within negative control structures
9633a6db
ES
253## failures 2
254## cut
255
a67dfb2e 256unless (\$foo $operator \$bar) {
9633a6db
ES
257 blah();
258}
259
a67dfb2e 260until (\$foo $operator \$bar) {
9633a6db
ES
261 blah();
262}
263
264#-----------------------------------------------------------------------------
265
a67dfb2e 266## name "$operator" within negative postfix statement modifiers
9633a6db
ES
267## failures 2
268## cut
269
a67dfb2e 270blah() unless \$foo $operator \$bar;
9633a6db 271
a67dfb2e 272blah() until \$foo $operator \$bar;
9633a6db
ES
273
274#-----------------------------------------------------------------------------
275END_COMPARATOR_CODE
276}
277
278#-----------------------------------------------------------------------------
279
280
281##############################################################################
282# $URL$
283# $Date$
284# $Author$
285# $Revision$
286##############################################################################
287
288# Local Variables:
289# mode: cperl
290# cperl-indent-level: 4
291# fill-column: 78
292# indent-tabs-mode: nil
293# c-indentation-style: bsd
294# End:
295# ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab :