Login
I didn't like all the "\$foo" in the templates for the run files, so I
[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
f2762f23 52 my $code_template = <<'END_NOT_OPERATOR_CODE';
9633a6db 53
f2762f23 54## name "%OPERATOR%" within positive control structures
9633a6db
ES
55## failures 0
56## cut
57
f2762f23 58if (%OPERATOR% $foo) {
9633a6db
ES
59 blah();
60}
61
f2762f23
JRT
62if ($foo) {
63 blah($foo);
9633a6db 64}
f2762f23
JRT
65elsif (%OPERATOR% $bar) {
66 blah($bar);
9633a6db
ES
67}
68else {
69 blah(undef);
70}
71
f2762f23 72while (%OPERATOR% $foo) {
9633a6db
ES
73 blah();
74}
75
f2762f23
JRT
76foreach my $bar ( grep { %OPERATOR% $_ } \@foo ) {
77 blah($bar);
9633a6db
ES
78}
79
f2762f23
JRT
80for (my $bar = 0; %OPERATOR% $bar; $bar++) {
81 blah($bar);
9633a6db
ES
82}
83
84#-----------------------------------------------------------------------------
85
f2762f23 86## name "%OPERATOR%" within positive postfix statement modifiers
9633a6db
ES
87## failures 0
88## cut
89
f2762f23 90blah() if %OPERATOR% $foo;
9633a6db 91
f2762f23 92blah() while %OPERATOR% $foo;
9633a6db 93
f2762f23 94blah($_) for grep { %OPERATOR% $_ } \@foo;
9633a6db
ES
95
96#-----------------------------------------------------------------------------
97
f2762f23 98## name "%OPERATOR%" within negative control structures
9633a6db
ES
99## failures 2
100## cut
101
f2762f23 102unless (%OPERATOR% $foo) {
9633a6db
ES
103 blah();
104}
105
f2762f23 106until (%OPERATOR% $foo) {
9633a6db
ES
107 blah();
108}
109
110#-----------------------------------------------------------------------------
111
f2762f23 112## name "%OPERATOR%" within negative postfix statement modifiers
9633a6db
ES
113## failures 2
114## cut
115
f2762f23 116blah() unless %OPERATOR% $foo;
9633a6db 117
f2762f23 118blah() until %OPERATOR% $foo;
9633a6db
ES
119
120#-----------------------------------------------------------------------------
121END_NOT_OPERATOR_CODE
f2762f23
JRT
122
123 $code_template =~ s{%OPERATOR%}{$operator}gmx;
124 print {$test_file} $code_template;
9633a6db
ES
125}
126
127#-----------------------------------------------------------------------------
128
129sub emit_not_match_code {
130 my ($test_file) = @_;
131
132 print {$test_file} <<'END_NOT_MATCH_CODE';
133
134## name "!~" within positive control structures
135## failures 0
136## cut
137
138if ($foo !~ m/bar/) {
139 blah();
140}
141
142if ($foo) {
143 blah($foo);
144}
145elsif ($bar !~ m/bar/) {
146 blah($bar);
147}
148else {
149 blah(undef);
150}
151
152while ($foo !~ m/bar/) {
153 blah();
154}
155
156foreach my $bar ( grep { $_ !~ m/baz/ } @foo ) {
157 blah($bar);
158}
159
160for (my $bar = 0; $bar =~ m/baz/; $bar++) {
161 blah($bar);
162}
163
164#-----------------------------------------------------------------------------
165
166## name "!~" within positive postfix statement modifiers
167## failures 0
168## cut
169
170blah() if $foo !~ m/bar/;
171
172blah() while $foo !~ m/bar/;
173
174blah($_) for grep { $_ !~ m/bar/ } @foo;
175
176#-----------------------------------------------------------------------------
177
178## name "!~" within negative control structures
179## failures 2
180## cut
181
182unless ($foo !~ m/bar/) {
183 blah();
184}
185
186until ($foo !~ m/bar/) {
187 blah();
188}
189
190#-----------------------------------------------------------------------------
191
192## name "!~" within negative postfix statement modifiers
193## failures 2
194## cut
195
196blah() unless $foo !~ m/bar/;
197
198blah() until $foo !~ m/bar/;
199
200#-----------------------------------------------------------------------------
201END_NOT_MATCH_CODE
202}
203
204#-----------------------------------------------------------------------------
205
206sub emit_comparator_code {
207 my ($test_file, $operator) = @_;
208
f2762f23 209 my $code_template = <<'END_COMPARATOR_CODE';
9633a6db 210
f2762f23 211## name "%OPERATOR%" within positive control structures
9633a6db
ES
212## failures 0
213## cut
214
f2762f23 215if ($foo %OPERATOR% $bar) {
9633a6db
ES
216 blah();
217}
218
f2762f23
JRT
219if ($foo %OPERATOR% $bar) {
220 blah($foo);
9633a6db 221}
f2762f23
JRT
222elsif ($bar %OPERATOR% $baz) {
223 blah($bar);
9633a6db
ES
224}
225else {
226 blah(undef);
227}
228
f2762f23 229while ($foo %OPERATOR% $bar) {
9633a6db
ES
230 blah();
231}
232
f2762f23
JRT
233foreach my $bar ( grep { $_ %OPERATOR% $baz } \@foo ) {
234 blah($bar);
9633a6db
ES
235}
236
f2762f23
JRT
237for (my $bar = 0; $bar %OPERATOR% $baz; $bar++) {
238 blah($bar);
9633a6db
ES
239}
240
241#-----------------------------------------------------------------------------
242
f2762f23 243## name "%OPERATOR%" within positive postfix statement modifiers
9633a6db
ES
244## failures 0
245## cut
246
f2762f23 247blah() if $foo %OPERATOR% $bar;
9633a6db 248
f2762f23 249blah() while $foo %OPERATOR% $bar;
9633a6db 250
f2762f23 251blah($_) for grep { $_ %OPERATOR% $bar } \@foo;
9633a6db
ES
252
253#-----------------------------------------------------------------------------
254
f2762f23 255## name "%OPERATOR%" within negative control structures
9633a6db
ES
256## failures 2
257## cut
258
f2762f23 259unless ($foo %OPERATOR% $bar) {
9633a6db
ES
260 blah();
261}
262
f2762f23 263until ($foo %OPERATOR% $bar) {
9633a6db
ES
264 blah();
265}
266
267#-----------------------------------------------------------------------------
268
f2762f23 269## name "%OPERATOR%" within negative postfix statement modifiers
9633a6db
ES
270## failures 2
271## cut
272
f2762f23 273blah() unless $foo %OPERATOR% $bar;
9633a6db 274
f2762f23 275blah() until $foo %OPERATOR% $bar;
9633a6db
ES
276
277#-----------------------------------------------------------------------------
278END_COMPARATOR_CODE
f2762f23
JRT
279
280 $code_template =~ s{%OPERATOR%}{$operator}gmx;
281 print {$test_file} $code_template;
9633a6db
ES
282}
283
284#-----------------------------------------------------------------------------
285
286
287##############################################################################
288# $URL$
289# $Date$
290# $Author$
291# $Revision$
292##############################################################################
293
294# Local Variables:
295# mode: cperl
296# cperl-indent-level: 4
297# fill-column: 78
298# indent-tabs-mode: nil
299# c-indentation-style: bsd
300# End:
301# ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab :