3 ##############################################################################
4 # $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/t/14_policy_parameters.t $
5 # $Date: 2008-06-06 00:48:04 -0500 (Fri, 06 Jun 2008) $
8 ##############################################################################
14 use English qw(-no_match_vars);
16 use Perl::Critic::UserProfile qw();
17 use Perl::Critic::PolicyFactory (-test => 1);
18 use Perl::Critic::PolicyParameter qw{ $NO_DESCRIPTION_AVAILABLE };
19 use Perl::Critic::Utils qw( policy_short_name );
20 use Perl::Critic::TestUtils qw(bundled_policy_names);
22 use Test::More; #plan set below!
24 Perl::Critic::TestUtils::block_perlcriticrc();
26 #-----------------------------------------------------------------------------
27 # This script proves that each policy that ships with Perl::Critic overrides
28 # the supported_parameters() method and, assuming that the policy is
29 # configurable, that each parameter can parse its own default_string.
31 # This script also verifies that Perl::Critic::PolicyFactory throws an
32 # exception when we try to create a policy with bogus parameters. However, it
33 # is your responsibility to verify that valid parameters actually work as
34 # expected. You can do this by using the #parms directive in the *.run files.
35 #-----------------------------------------------------------------------------
37 # Figure out how many tests there will be...
38 my @all_policies = bundled_policy_names();
39 my @all_params = map { $_->supported_parameters() } @all_policies;
40 my $ntests = @all_policies + 2 * @all_params;
41 plan( tests => $ntests );
43 #-----------------------------------------------------------------------------
45 for my $policy ( @all_policies ) {
46 test_has_declared_parameters( $policy );
47 test_invalid_parameters( $policy );
48 test_supported_parameters( $policy );
51 #-----------------------------------------------------------------------------
53 sub test_supported_parameters {
54 my $policy_name = shift;
55 my @supported_params = $policy_name->supported_parameters();
56 my $config = Perl::Critic::Config->new( -profile => 'NONE' );
58 for my $param_specification ( @supported_params ) {
60 Perl::Critic::PolicyParameter->new($param_specification);
61 my $param_name = $parameter->get_name();
62 my $description = $parameter->get_description();
65 $description && $description ne $NO_DESCRIPTION_AVAILABLE,
66 qq{Param "$param_name" for policy "$policy_name" has a description},
70 -policy => $policy_name,
72 $param_name => $parameter->get_default_string(),
75 eval { $config->add_policy( %args ) };
79 qq{Created policy "$policy_name" with param "$param_name"},
86 #-----------------------------------------------------------------------------
88 sub test_invalid_parameters {
90 my $bogus_params = { bogus => 'shizzle' };
91 my $profile = Perl::Critic::UserProfile->new( -profile => 'NONE' );
92 my $factory = Perl::Critic::PolicyFactory->new( -profile => $profile );
94 my $policy_name = policy_short_name($policy);
95 my $label = qq{Created $policy_name with bogus parameters};
97 eval { $factory->create_policy(-name => $policy, -params => $bogus_params) };
100 qr/The $policy_name policy doesn't take a "bogus" option/,
105 #-----------------------------------------------------------------------------
107 sub test_has_declared_parameters {
109 if ( not $policy->can('supported_parameters') ) {
110 fail( qq{I don't know if $policy supports params} );
111 diag( qq{This means $policy needs a supported_parameters() method} );
116 #-----------------------------------------------------------------------------
118 # ensure we run true if this test is loaded by
119 # t/14_policy_parameters.t_without_optional_dependencies.t
122 ###############################################################################
125 # cperl-indent-level: 4
127 # indent-tabs-mode: nil
128 # c-indentation-style: bsd
130 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :