Skip to content

Commit

Permalink
Add test for GH #734: (#758)
Browse files Browse the repository at this point in the history
This test checks a problems with the line tracking for violations.

* It doesn't seem to fail on 5.22.1, but it does on 5.18.2.
* If you have the policy 'CodeLayout::ProhibitHashBarewords', it
  will use that for the test.
* If you don't, it won't do anything.
  • Loading branch information
xsawyerx authored and petdance committed May 20, 2017
1 parent 2368af7 commit ed16f81
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions t/issues/gh-734.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#!perl

use strict;
use warnings;

use Perl::Critic::TestUtils qw(pcritique_with_violations);
use Readonly;

use Test::More;

Readonly::Scalar my $NUMBER_OF_TESTS => 5;

plan( tests => $NUMBER_OF_TESTS );

#-----------------------------------------------------------------------------

our $VERSION = '1.126';

#-----------------------------------------------------------------------------

Perl::Critic::TestUtils::block_perlcriticrc();

sub has_policy {
return eval {
require Perl::Critic::Policy::CodeLayout::ProhibitHashBarewords;
1;
};
}

my $policy = 'CodeLayout::ProhibitHashBarewords';
my $code;

#-----------------------------------------------------------------------------
SKIP: {

has_policy()
or skip 'You need CodeLayout::ProhibitHashBarewords policy for this test',
$NUMBER_OF_TESTS;

$code = <<'END_PERL';
my %hash = (
foo => 1,
bar => 2,
baz => 3,
quux => 4,
);
END_PERL

my @violations_re = (
qr{^ \s* foo \s =>}xms,
qr{^ \s* bar \s =>}xms,
qr{^ \s* baz \s =>}xms,
qr{^ \s* quux \s =>}xms,
);

my @violations;
eval { @violations = pcritique_with_violations( $policy, \$code ); 1; };
is( scalar @violations, 4, 'Found 4 violations' );

foreach my $violation (@violations) {
my $violation_re = shift @violations_re || qr{^NOPE$}xms;
like( $violation->source, $violation_re, 'Correct line for violation' );
}

} # end skip

#-----------------------------------------------------------------------------

# ensure we return true if this test is loaded by
# t/20_policy_pod_spelling.t_without_optional_dependencies.t
1;

# Local Variables:
# mode: cperl
# cperl-indent-level: 4
# fill-column: 78
# indent-tabs-mode: nil
# c-indentation-style: bsd
# End:
# ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :

0 comments on commit ed16f81

Please sign in to comment.