1#!perl 2 3use strict; 4use warnings; 5 6use English qw(-no_match_vars); 7 8use Perl::Critic::UserProfile qw(); 9use Perl::Critic::PolicyFactory (-test => 1); 10use Perl::Critic::PolicyParameter qw{ $NO_DESCRIPTION_AVAILABLE }; 11use Perl::Critic::Utils qw( policy_short_name ); 12use Perl::Critic::TestUtils qw(bundled_policy_names); 13 14use Test::More tests => 12; 15 16Perl::Critic::TestUtils::block_perlcriticrc(); 17 18#----------------------------------------------------------------------------- 19# This script proves that each policy that ships with Perl::Critic overrides 20# the supported_parameters() method and, assuming that the policy is 21# configurable, that each parameter can parse its own default_string. 22# 23# This script also verifies that Perl::Critic::PolicyFactory throws an 24# exception when we try to create a policy with bogus parameters. However, it 25# is your responsibility to verify that valid parameters actually work as 26# expected. You can do this by using the #parms directive in the *.run files. 27#----------------------------------------------------------------------------- 28 29# Figure out how many tests there will be... 30my @all_policies = sort ( bundled_policy_names() ); 31 32for my $policy ( @all_policies ) { 33 test_has_declared_parameters( $policy ); 34 test_supported_parameters( $policy ); 35} 36 37#----------------------------------------------------------------------------- 38 39sub test_supported_parameters { 40 my $policy_name = shift; 41 my @supported_params = $policy_name->supported_parameters(); 42 my $config = Perl::Critic::Config->new( -profile => 'NONE' ); 43 44 for my $param_specification ( @supported_params ) { 45 my $parameter = 46 Perl::Critic::PolicyParameter->new($param_specification); 47 my $param_name = $parameter->get_name(); 48 my $description = $parameter->get_description(); 49 50 ok( 51 $description && $description ne $NO_DESCRIPTION_AVAILABLE, 52 qq{Param "$param_name" for policy "$policy_name" has a description}, 53 ); 54 55 my %args = ( 56 -policy => $policy_name, 57 -params => { 58 $param_name => $parameter->get_default_string(), 59 } 60 ); 61 eval { $config->add_policy( %args ) }; 62 is( 63 $EVAL_ERROR, 64 q{}, 65 qq{Created policy "$policy_name" with param "$param_name"}, 66 ); 67 } 68 69 return; 70} 71 72#----------------------------------------------------------------------------- 73 74sub test_has_declared_parameters { 75 my $policy = shift; 76 if ( not $policy->can('supported_parameters') ) { 77 fail( qq{I don't know if $policy supports params} ); 78 diag( qq{This means $policy needs a supported_parameters() method} ); 79 } 80 return; 81} 82 83#----------------------------------------------------------------------------- 84 85# ensure we run true if this test is loaded by 86# t/14_policy_parameters.t_without_optional_dependencies.t 871; 88 89############################################################################### 90# Local Variables: 91# mode: cperl 92# cperl-indent-level: 4 93# fill-column: 78 94# indent-tabs-mode: nil 95# c-indentation-style: bsd 96# End: 97# ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround : 98