]> git.vpit.fr Git - perl/modules/Perl-Critic-Policy-Dynamic-NoIndirect.git/blob - t/10-basic.t
29c2f103670bbdb913bdcb2dd3417f9fd88c8055
[perl/modules/Perl-Critic-Policy-Dynamic-NoIndirect.git] / t / 10-basic.t
1 #!perl -T
2
3 use strict;
4 use warnings;
5
6 my ($tests, $subtests);
7 BEGIN {
8  $tests    = 13;
9  $subtests = 3;
10 }
11
12 use Test::More tests => $tests + $subtests * 21;
13
14 use Perl::Critic::TestUtils qw/pcritique_with_violations/;
15
16 Perl::Critic::TestUtils::block_perlcriticrc();
17
18 my $policy = 'Dynamic::NoIndirect';
19
20 {
21  local $/ = "####";
22
23  my $id = 1;
24
25  while (<DATA>) {
26   s/^\s+//s;
27
28   my ($code, $expected) = split /^-+$/m, $_, 2;
29   my @expected = eval $expected;
30
31   my @violations = eval { pcritique_with_violations($policy, \$code) };
32
33   if ($@) {
34    diag "Compilation $id failed: $@";
35    next;
36   }
37
38   is @violations, @expected, "right count of violations $id";
39
40   for my $v (@violations) {
41    my $exp = shift @expected;
42
43    unless ($exp) {
44     fail "Unexpected violation for chunk $id: " . $v->description;
45     next;
46    }
47
48    my $pos = $v->location;
49    my ($meth, $obj, $line, $col) = @$exp;
50
51    like $v->description,
52         qr/^Indirect call of method \"\Q$meth\E\" on object \"\Q$obj\E\"/,
53         "description $id";
54    is   $pos->[0], $line, "line $id";
55    is   $pos->[1], $col,  "column $id";
56   }
57
58   ++$id;
59  }
60 }
61
62 __DATA__
63 my $x = new X;
64 ----
65 [ 'new', 'X', 1, 9 ]
66 ####
67 my $x = new X; $x = new X;
68 ----
69 [ 'new', 'X', 1, 9 ], [ 'new', 'X', 1, 21 ]
70 ####
71 my $x = new X    new X;
72 ----
73 [ 'new', 'X', 1, 9 ], [ 'new', 'X', 1, 18 ]
74 ####
75 my $x = new X;
76 my $y = new X;
77 ----
78 [ 'new', 'X', 1, 9 ], [ 'new', 'X', 2, 9 ]
79 ####
80 my $x = new
81             X;
82 ----
83 [ 'new', 'X', 1, 9 ]
84 ####
85 my $x = new
86  X new
87     X;
88 ----
89 [ 'new', 'X', 1, 9 ], [ 'new', 'X', 2, 4 ]
90 ####
91 my $x = new new;
92 ----
93 [ 'new', 'new', 1, 9 ]
94 ####
95 our $obj;
96 my $x = new $obj;
97 ----
98 [ 'new', '$obj', 2, 9 ]
99 ####
100 our $obj;
101 my $x = new $obj; $x = new $obj;
102 ----
103 [ 'new', '$obj', 2, 9 ], [ 'new', '$obj', 2, 24 ]
104 ####
105 our $obj;
106 my $x = new $obj    new $obj;
107 ----
108 [ 'new', '$obj', 2, 9 ], [ 'new', '$obj', 2, 21 ]
109 ####
110 our $obj;
111 my $x = new $obj;
112 my $y = new $obj;
113 ----
114 [ 'new', '$obj', 2, 9 ], [ 'new', '$obj', 3, 9 ]
115 ####
116 our $obj;
117 my $x = new
118             $obj;
119 ----
120 [ 'new', '$obj', 2, 9 ]
121 ####
122 our $obj;
123 my $x = new
124  $obj new
125     $obj;
126 ----
127 [ 'new', '$obj', 2, 9 ], [ 'new', '$obj', 3, 7 ]