Created
November 29, 2009 15:18
-
-
Save kentfredric/244947 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package duplicatePackage; | |
# $Id:$ | |
use strict; | |
use warnings; | |
use Perl::Critic; | |
use Test::More; | |
my @strings = ( | |
q| { package => "", }|, # 0 => Fails | |
q| +{ package => "", }|, # 1 => Fails | |
q| { 'package' => "", }|, # 2 | |
q| +{ 'package' => "", }|, # 3 => Fails | |
q| { 'package' , "", }|, # 4 | |
q| +{ 'package' , "", }|, # 5 => Fails. | |
); | |
sub generate_code { | |
my ( $package, $signature ) = @_; | |
return sprintf <<'EOF', $package, $signature; | |
package %s; | |
use strict; | |
use warnings; | |
use Data::Dumper qw( Dumper ); | |
sub subx { | |
my $foo = [ {}, %s ,]; | |
return $foo; | |
} | |
subx(); | |
1; | |
EOF | |
} | |
my $testid = 0; | |
sub generate_test { | |
my $id = $testid++; | |
my $signature = shift; | |
my $package = "Test::Package::_${id}_"; | |
my $code = generate_code( $package, $signature ); | |
return { | |
id => $id, | |
package => $package, | |
code => $code, | |
signature => $signature, | |
}; | |
} | |
my $critic = Perl::Critic->new( -include => [ 'ProhibitCommaSeparatedStatements', 'ProhibitMultiplePackages' ] ); | |
for my $string ( @strings ){ | |
my $testdata = generate_test( $string ); | |
undef $@; | |
ok( eval $testdata->{code} , 'Evaling the codeblock for test ' . $testdata->{id} . 'works' ); | |
ok( !$@ , 'No Eval errors for ' . $testdata->{id} ); | |
is_deeply( $testdata->{package}->subx() , [ {}, {'package', '' }], 'Data structure is solid for ' . $testdata->{id} ); | |
my @violations = $critic->critique( \$testdata->{code}); | |
ok( !@violations , 'Violations is empty for ' . $testdata->{id} ) or diag explain { test => $testdata, violations => "@violations" }; | |
} | |
1; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment