File: PerlMinimumVersionAndWhy-pmv.t

package info (click to toggle)
libperl-critic-pulp-perl 99-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,700 kB
  • sloc: perl: 13,768; sh: 285; makefile: 6; ansic: 1
file content (375 lines) | stat: -rwxr-xr-x 15,240 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#!/usr/bin/perl -w

# Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019, 2021 Kevin Ryde

# This file is part of Perl-Critic-Pulp.
#
# Perl-Critic-Pulp is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 3, or (at your option) any later
# version.
#
# Perl-Critic-Pulp is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with Perl-Critic-Pulp.  If not, see <http://www.gnu.org/licenses/>.


# Tests with Perl::MinimumVersion available.


use 5.006;
use strict;
use warnings;
use Test::More;
use Perl::Critic;

my $critic;
eval {
  $critic = Perl::Critic->new
    ('-profile' => '',
     '-single-policy' => '^Perl::Critic::Policy::Compatibility::PerlMinimumVersionAndWhy$');
  1;
}
  or plan skip_all => "cannot create Critic object -- $@";

my @policies = $critic->policies;
if (@policies == 0) {
  plan skip_all => "due to policy not initializing";
}

plan tests => 180;

use lib 't';
use MyTestHelpers;
BEGIN { MyTestHelpers::nowarnings() }

is (scalar @policies, 1, 'single policy PerlMinimumVersionAndWhy');
my $policy = $policies[0];
diag "Perl::MinimumVersion ", Perl::MinimumVersion->VERSION;

{
  my $want_version = 99;
  ok (eval { $policy->VERSION($want_version); 1 },
      "VERSION object check $want_version");
  my $check_version = $want_version + 1000;
  ok (! eval { $policy->VERSION($check_version); 1 },
      "VERSION object check $check_version");
}

my $have_pulp_bareword_double_colon
  = exists $Perl::MinimumVersion::CHECKS{_Pulp__bareword_double_colon};
diag "pulp bareword double colon: ",($have_pulp_bareword_double_colon||0);

my $have_pulp_5010_magic_fix
  = exists $Perl::MinimumVersion::CHECKS{_Pulp__5010_magic__fix};
diag "pulp magic fix: ",($have_pulp_5010_magic_fix||0);

foreach my $data (
                  ## no critic (RequireInterpolationOfMetachars)

                  # Modern::Perl equivalent to 5.010
                  [ 0, 'use Modern::Perl;        exists &foo' ],
                  [ 0, 'use Modern::Perl "2014"; exists &foo' ],

                  # _Pulp__5010_stacked_filetest
                  [ 0, 'use 5.008; if (-e "/tmp/foo.txt") { }' ],
                  [ 1, 'use 5.008; if (-e -x "/tmp/foo.txt") { }' ],
                  [ 1, 'use 5.008; if (-e -x -f "/tmp/foo.txt") { }' ],
                  [ 0, 'use 5.010; if (-e -x "/tmp/foo.txt") { }' ],
                  [ 0, 'use 5.010; if (-e -x -f "/tmp/foo.txt") { }' ],

                  # _Pulp__eval_line_directive_first_thing
                  [ 1, 'use 5.006; eval "#line 123"' ],
                  [ 0, 'use 5.008; eval "#line 123"' ],
                  #
                  [ 1, 'use 5.006; eval "# line 123"' ],
                  [ 1, "use 5.006; eval '#\tline 123'" ],
                  [ 1, "use 5.006; eval q{#line 123}" ],
                  [ 0, "use 5.006; eval q{\n#line 123}" ],
                  [ 1, 'use 5.006; eval <<HERE
#line 123
HERE
' ],
                  [ 1, 'use 5.006; eval <<"HERE"
#line 123
HERE
' ],


                  # _Pulp__keys_of_array
                  [ 1, 'use 5.010; keys @foo' ],
                  [ 0, 'use 5.012; keys @foo' ],
                  [ 1, 'use 5.010; keys @$foo' ],
                  [ 0, 'use 5.012; keys @$foo' ],
                  [ 1, 'use 5.010; keys @{$foo}' ],
                  [ 0, 'use 5.012; keys @{$foo}' ],
                  [ 1, 'use 5.010; keys(((@{$foo})))' ],
                  [ 0, 'use 5.012; keys(((@{$foo})))' ],
                  # _Pulp__values_of_array
                  [ 1, 'use 5.010; values @foo' ],
                  [ 0, 'use 5.012; values @foo' ],
                  # _Pulp__each_of_array
                  [ 1, 'use 5.010; each @foo' ],
                  [ 0, 'use 5.012; each @foo' ],

                  # _Pulp__var_method_without_parens
                  [ 1, 'use 5.005; $obj->$method' ],
                  [ 0, 'use 5.006; $obj->$method' ],
                  [ 1, 'use 5.005; Foo->$method == 123' ],
                  [ 0, 'use 5.006; Foo->$method == 123' ],
                  # parens always ok
                  [ 0, 'use 5.005; Foo->$method()' ],
                  [ 0, 'use 5.005; $obj->$method()' ],
                  [ 0, 'use 5.005; $obj->$method(123)' ],

                  # _Pulp__UNIVERSAL_methods_5004
                  [ 1, 'require 5; Foo->VERSION' ],
                  [ 0, 'use 5.004; Foo->VERSION' ],
                  [ 1, 'require 5; Foo->isa("Bar")' ],
                  [ 0, 'use 5.004; Foo->isa("Bar")' ],
                  [ 1, 'require 5; Foo->can("new")' ],
                  [ 0, 'use 5.004; Foo->can("new")' ],

                  # _Pulp__UNIVERSAL_methods_5004
                  [ 1, 'require 5; Foo->DOES' ],
                  [ 1, 'use 5.008; Foo->DOES' ],
                  [ 0, 'use 5.010; Foo->DOES' ],

                  # _Pulp__delete_array_elem
                  [ 1, 'use 5.005; delete $x[0]' ],
                  [ 0, 'use 5.006; delete $x[0]' ],
                  [ 1, 'use 5.005; delete($x[1])' ],
                  [ 0, 'use 5.005; delete $x[0]',
                    { _skip_checks => '_Pulp__delete_array_elem'} ],
                  #
                  [ 1, 'delete $x[0][1]' ],
                  [ 1, 'delete($x[0][1])' ],
                  [ 1, 'delete((((($x[0][1])))))' ],
                  [ 1, 'delete(($x[0][1]))' ],
                  [ 0, 'delete $x[0]{key}' ],
                  [ 0, 'delete($x[0]{key})' ],
                  [ 0, 'delete $x[0]->{key}' ],
                  [ 1, 'delete $x[0]->{key}->[123]' ],
                  [ 1, 'delete $x[0]->{key}[123]' ],
                  [ 1, 'delete $x[0]{key}->[123]' ],
                  [ 1, 'delete $x[0]{key}[123]' ],
                  [ 1, 'delete($x[0]{key}[123])' ],

                  # _Pulp__my_list_with_undef
                  [ 1, 'my (undef, $y)' ],
                  [ 1, 'my (undef, $y) = @_' ],
                  [ 0, 'my ($x)' ],
                  [ 0, 'my ($x, $y)' ],
                  [ 1, 'my ($x, ($y, undef), $z) = @_' ],
                  [ 1, 'my ($x, ($y, (undef, $w)), $z) = @_' ],
                  [ 1, 'my ((((((undef))))))' ],
                  [ 1, 'my (undef' ],
                  [ 1, 'my ((((((undef' ],
                  [ 0, 'use 5.005; my (undef, $y)' ],

                  # _Pulp__fat_comma_across_newline
                  [ 0, "return (foo =>\n123)" ],
                  [ 1, "return (foo\n=>\n123)" ],
                  [ 1, "return (foo\t\n\t=>\n123)" ],
                  [ 1, "return (foo # foo\n=>\n123)" ],
                  [ 1, "return (foo # foo\n\n=>\n123)" ],
                  [ 1, "return (foo # 'comment'\n \n # 'comment'\n=>\n123)" ],
                  # method calls
                  [ 0, "return (Foo->bar => 123" ],
                  [ 0, "return (Foo->bar \n => 123" ],
                  [ 0, "return (Foo -> bar \n => 123" ],

                  # _Pulp__arrow_coderef_call
                  [ 1, '$coderef->()' ],
                  [ 1, '$coderef->(1,2,3)' ],
                  [ 1, '$hashref->{code}->()' ],
                  [ 1, '$hashref->{code}->(1,2,3)' ],
                  [ 0, 'use 5.004; $coderef->()' ],

                  # _Pulp__for_loop_variable_using_my
                  [ 1, 'foreach my $i (1,2,3) { }' ],
                  [ 0, 'use 5.004; foreach my $i (1,2,3) { }' ],
                  [ 0, 'foreach $i (1,2,3) { }' ],
                  [ 0, 'foreach (1,2,3) { }' ],
                  [ 1, 'for my $i (1,2,3) { }' ],
                  [ 0, 'use 5.004; for my $i (1,2,3) { }' ],
                  [ 0, 'for $i (1,2,3) { }' ],
                  [ 0, 'for (1,2,3) { }' ],

                  # _Pulp__use_version_number
                  [ 1, 'use 5' ],
                  [ 1, 'use 5.003' ],
                  [ 0, 'use 5.004' ],
                  #
                  # these are ok if Foo is using Exporter.pm ...
                  # [ 1, 'require 5.003; use Foo 1.0' ],
                  # [ 0, 'require 5.004; use Foo 1.0' ],
                  # [ 0, 'use Foo 1.0, 2.0' ],  # args not ver num

                  # _Pulp__special_literal__PACKAGE__
                  [ 1, 'require 5.003; my $str = __PACKAGE__;' ],
                  [ 0, 'use 5.004; my $str = __PACKAGE__;' ],
                  [ 0, 'require 5.003; my %hash = (__PACKAGE__ => 1);' ],
                  [ 1, 'require 5.003; my %hash = (__PACKAGE__,   1);' ],
                  [ 0, 'require 5.003; my $elem = $hash{__PACKAGE__};' ],

                  # _Pulp__exists_array_elem
                  [ 1, 'use 5.005; exists $x[0]' ],
                  [ 0, 'use 5.006; exists $x[0]' ],
                  [ 0, 'use 5.005; exists($x[1])',
                    { _skip_checks => '_Pulp__delete_array_elem _Pulp__exists_array_elem'} ],

                  # _Pulp__exists_sub
                  [ 1, 'use 5.005; exists &foo' ],
                  [ 0, 'use 5.006; exists &foo' ],
                  [ 1, 'use 5.005; exists(&foo)' ],

                  # _Pulp__0b_number
                  [ 1, 'use 5.005; 0b01101101' ],
                  [ 0, 'use 5.006; 0b01101101' ],

                  # _Pulp__syswrite_length_optional
                  [ 1, 'use 5.005; syswrite($fh,$str)' ],
                  [ 0, 'use 5.006; syswrite($fh,$str)' ],
                  [ 0, 'use 5.005; syswrite($fh,$str,$length)' ],
                  [ 0, 'use 5.006; syswrite($fh,$str,$length)' ],
                  [ 0, 'use 5.005; syswrite($fh,$str,$length,$offset)' ],
                  [ 0, 'use 5.006; syswrite($fh,$str,$length,$offset)' ],
                  [ 0, 'use 5.005; syswrite()' ],  # bogus, but unreported
                  [ 0, 'use 5.006; syswrite()' ],
                  [ 0, 'use 5.005; syswrite($fh)' ], # bogus, but unreported
                  [ 0, 'use 5.006; syswrite($fh)' ],

                  # _Pulp__open_my_filehandle
                  [ 1, 'use 5.005; open my $fh, "foo.txt"' ],
                  [ 0, 'use 5.006; open my $fh, "foo.txt"' ],
                  [ 0, 'use 5.006; open FH, "foo.txt"' ],

                  [ 1, 'use 5.005; open(my $fh, "foo.txt")' ],
                  [ 0, 'use 5.006; open(my $fh, "foo.txt")' ],
                  [ 0, 'use 5.006; open(FH, "foo.txt")' ],

                  [ 1, 'use 5.005; pipe my $read, my $write' ],
                  [ 1, 'use 5.005; pipe IN, my $write' ],
                  [ 1, 'use 5.005; pipe my $read, OUT' ],
                  [ 0, 'use 5.006; pipe my $read, my $write' ],
                  [ 0, 'use 5.006; pipe IN, my $write' ],
                  [ 0, 'use 5.006; pipe my $read, OUT' ],
                  [ 0, 'use 5.005; pipe IN, OUT' ],

                  [ 1, 'socketpair my $one, my $two' ],
                  [ 1, 'socketpair ONE, my $two' ],
                  [ 1, 'socketpair my $one, TWO' ],
                  [ 0, 'socketpair ONE, TWO' ],
                  [ 1, 'socketpair func(), my $two' ],

                  [ 0, 'open my $fh = gensym(), "foo.txt"' ],
                  [ 0, 'open(my $fh = gensym(), "foo.txt")' ],
                  [ 1, 'pipe my $one, my $two = gensym()' ],
                  [ 1, 'pipe my $one = gensym(), my $two' ],
                  [ 0, 'pipe my $one = gensym(), my $two = gensym()' ],


                  # _Pulp__bareword_double_colon
                  [ ($have_pulp_bareword_double_colon ? 1 : 0),
                    'use 5.004; foo(Foo::Bar::)' ],
                  [ 0, 'use 5.005; foo(Foo::Bar::)' ],

                  #
                  # pack(), unpack()
                  #

                  # _Pulp__5004_pack_format
                  [ 1, 'require 5.002; pack "w", 123' ],
                  [ 0, 'use 5.004; pack "w", 123' ],
                  [ 0, 'require 5.002; pack "$w", 123' ],
                  [ 1, "require 5.002; pack 'i'.<<HERE, 123
w
HERE
" ],
                  [ 1, "require 5.002; pack w => 123" ],
                  [ 1, 'require 5.002; unpack "i".w => $bytes' ],
                  [ 0, "require 5.002; pack MYFORMAT(), 123" ],
                  [ 0, "require 5.002; pack MYFORMAT, 123" ],

                  # _Pulp__5006_pack_format
                  [ 1, 'use 5.005; pack ("Z", "hello")' ],
                  [ 0, 'use 5.006; pack ("Z", "hello")' ],
                  [ 1, 'use 5.005; pack ("Z#comment", "hello")' ],
                  [ 0, 'use 5.006; pack ("Z#comment", "hello")' ],

                  # _Pulp__5008_pack_format
                  [ 1, 'use 5.006; pack ("F", 1.5)' ],
                  [ 0, 'use 5.008; pack ("F", 1.5)' ],
                  [ 1, 'use 5.006; pack ("L[20]", 1.5)' ],
                  [ 0, 'use 5.008; pack ("L[20]", 1.5)' ],

                  # _Pulp__5010_pack_format
                  [ 1, 'use 5.008; unpack ("i<", $bytes)' ],
                  [ 0, 'use 5.010; unpack ("i<", $bytes)' ],
                  [ 1, 'unpack ("i<", $bytes)',
                    { _above_version => version->new('5.8.0') } ],
                  [ 0, 'unpack ("i<", $bytes)',
                    { _above_version => version->new('5.10.0') } ],


                  # _Pulp__5010_qr_m_working_properly
                  #
                  [ 1, 'use 5.008; qr/^x$/m' ],
                  [ 0, 'use 5.010; qr/^x$/m' ],
                  [ 1, 'use 5.006; my $re = qr/pattern/m;' ],
                  [ 0, 'use 5.010; my $re = qr/pattern/m;' ],
                  #
                  # plain patterns ok, only qr// bad
                  [ 0, '$str =~ /^foo$/m' ],
                  [ 0, '$str =~ m{^foo$}m' ],
                  #
                  # with other modifiers
                  [ 1, 'use 5.008; qr/^x$/im' ],
                  [ 1, 'use 5.008; qr/^x$/ms' ],
                  #
                  # other modifiers
                  [ 0, 'use 5.006; my $re = qr/pattern/s;' ],
                  [ 0, 'use 5.006; my $re = qr/pattern/i;' ],
                  [ 0, 'use 5.006; my $re = qr/pattern/x;' ],
                  [ 0, 'use 5.006; my $re = qr/pattern/o;' ],


                  # _Pulp__5010_magic__fix
                  # _Pulp__5010_operators__fix
                  #
                  [ ($have_pulp_5010_magic_fix ? 1 : 0), "1 // 2" ],
                  [ ($have_pulp_5010_magic_fix ? 1 : 0), "use 5.008; 1 // 2" ],
                  [ 0, "use 5.010; 1 // 2" ],

                 ) {
  my ($want_count, $str, $options) = @$data;
  $policy->{'_skip_checks'} = '';      # default
  $policy->{'_above_version'} = undef; # default

  my $name = "str: '$str'";
  foreach my $key (keys %$options) {
    $name .= " $key=$options->{$key}";
    $policy->{$key} = $options->{$key};
  }

  my @violations = $critic->critique (\$str);

  # only the Pulp ones, not any Perl::MinimumVersion itself might gain
  @violations = grep {$_->description =~ /^_Pulp_/} @violations;

  my $got_count = scalar @violations;
  is ($got_count, $want_count, $name);

  if ($got_count != $want_count) {
    foreach (@violations) {
      diag ($_->description);
    }
  }
}

exit 0;