]> git.vpit.fr Git - perl/modules/Perl-Critic-Policy-Dynamic-NoIndirect.git/blobdiff - t/10-basic.t
Document and test using indirect inside the auditted code
[perl/modules/Perl-Critic-Policy-Dynamic-NoIndirect.git] / t / 10-basic.t
index 68715f2d72fa84638ea4ae3386b4cc1d2ed7ecbd..0c1c23d3e11fddf598038f6a35aed87ef8128be8 100644 (file)
@@ -3,13 +3,14 @@
 use strict;
 use warnings;
 
-my ($tests, $subtests);
+my ($tests, $reports, $subtests);
 BEGIN {
- $tests    = 15;
+ $tests    = 27;
+ $reports  = 42;
  $subtests = 3;
 }
 
-use Test::More tests => $tests + $subtests * 25;
+use Test::More tests => $tests + $subtests * $reports;
 
 use Perl::Critic::TestUtils qw/pcritique_with_violations/;
 
@@ -17,6 +18,14 @@ Perl::Critic::TestUtils::block_perlcriticrc();
 
 my $policy = 'Dynamic::NoIndirect';
 
+sub expect {
+ my ($meth, $obj) = @_;
+ $obj = ($obj =~ /^\s*\{/) ? "a block" : "object \"\Q$obj\E\"";
+ qr/^Indirect call of method \"\Q$meth\E\" on $obj/,
+}
+
+sub zap (&) { }
+
 {
  local $/ = "####";
 
@@ -25,7 +34,7 @@ my $policy = 'Dynamic::NoIndirect';
  while (<DATA>) {
   s/^\s+//s;
 
-  my ($code, $expected) = split /^-+$/m, $_, 2;
+  my ($code, $expected) = split /^-{4,}$/m, $_, 2;
   my @expected = eval $expected;
 
   my @violations = eval { pcritique_with_violations($policy, \$code) };
@@ -48,9 +57,7 @@ my $policy = 'Dynamic::NoIndirect';
    my $pos = $v->location;
    my ($meth, $obj, $line, $col) = @$exp;
 
-   like $v->description,
-        qr/^Indirect call of method \"\Q$meth\E\" on object \"\Q$obj\E\"/,
-        "description $id";
+   like $v->description, expect($meth, $obj), "description $id";
    is   $pos->[0], $line, "line $id";
    is   $pos->[1], $col,  "column $id";
   }
@@ -64,6 +71,9 @@ my $x = new X;
 ----
 [ 'new', 'X', 1, 9 ]
 ####
+use indirect; my $x = new X;
+----
+####
 my $x = new X; $x = new X;
 ----
 [ 'new', 'X', 1, 9 ], [ 'new', 'X', 1, 21 ]
@@ -97,6 +107,10 @@ my $x = new new;
 [ 'new', 'new', 1, 9 ]
 ####
 our $obj;
+use indirect; my $x = new $obj;
+----
+####
+our $obj;
 my $x = new $obj;
 ----
 [ 'new', '$obj', 2, 9 ]
@@ -134,3 +148,56 @@ my $x = new
     $obj;
 ----
 [ 'new', '$obj', 2, 9 ], [ 'new', '$obj', 3, 7 ]
+####
+my $x = main::zap { };
+----
+####
+my $x = meh { };
+----
+[ 'meh', '{', 1, 9 ]
+####
+my $x = meh {
+ 1
+};
+----
+[ 'meh', '{', 1, 9 ]
+####
+my $x =
+ meh { 1; 1
+ };
+----
+[ 'meh', '{', 2, 2 ]
+####
+my $x = meh {
+ new X;
+};
+----
+[ 'meh', '{', 1, 9 ], [ 'new', 'X', 2, 2 ]
+####
+our $obj;
+my $x = meh {
+ new $obj;
+}
+----
+[ 'meh', '{', 2, 9 ], [ 'new', '$obj', 3, 2 ]
+####
+my $x = meh { } new
+                X;
+----
+[ 'meh', '{', 1, 9 ], [ 'new', 'X', 1, 17 ]
+####
+our $obj;
+my $x = meh { } new
+                $obj;
+----
+[ 'meh', '{', 2, 9 ], [ 'new', '$obj', 2, 17 ]
+####
+our $obj;
+my $x = meh { new X } new $obj;
+----
+[ 'meh', '{', 2, 9 ], [ 'new', 'X', 2, 15 ], [ 'new', '$obj', 2, 23 ]
+####
+our $obj;
+my $x = meh { new $obj } new X;
+----
+[ 'meh', '{', 2, 9 ], [ 'new', '$obj', 2, 15 ], [ 'new', 'X', 2, 26 ]