Skip to content

Instantly share code, notes, and snippets.

@kentfredric
Created November 29, 2009 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kentfredric/244947 to your computer and use it in GitHub Desktop.
Save kentfredric/244947 to your computer and use it in GitHub Desktop.
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