3 ##############################################################################
4 # $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/t/11_policyfactory.t $
5 # $Date: 2008-06-06 00:48:04 -0500 (Fri, 06 Jun 2008) $
8 ##############################################################################
13 use English qw(-no_match_vars);
14 use Test::More (tests => 10);
15 use Perl::Critic::UserProfile;
16 use Perl::Critic::PolicyFactory (-test => 1);
18 # common P::C testing tools
19 use Perl::Critic::TestUtils qw();
20 Perl::Critic::TestUtils::block_perlcriticrc();
22 #-----------------------------------------------------------------------------
25 my $policy_name = 'Perl::Critic::Policy::Modules::ProhibitEvilModules';
26 my $params = {severity => 2, set_themes => 'betty', add_themes => 'wilma'};
28 my $userprof = Perl::Critic::UserProfile->new( -profile => 'NONE' );
29 my $pf = Perl::Critic::PolicyFactory->new( -profile => $userprof );
33 my $policy = $pf->create_policy( -name => $policy_name, -params => $params );
34 is( ref $policy, $policy_name, 'Created correct type of policy');
36 my $severity = $policy->get_severity();
37 is( $severity, 2, 'Set the severity');
39 my @themes = $policy->get_themes();
40 is_deeply( \@themes, [ qw(betty wilma) ], 'Set the theme');
43 #-----------------------------------------------------------------------------
44 # Using short module name.
46 my $policy_name = 'Variables::ProhibitPunctuationVars';
47 my $params = {set_themes => 'betty', add_themes => 'wilma'};
49 my $userprof = Perl::Critic::UserProfile->new( -profile => 'NONE' );
50 my $pf = Perl::Critic::PolicyFactory->new( -profile => $userprof );
54 my $policy = $pf->create_policy( -name => $policy_name, -params => $params );
55 my $policy_name_long = 'Perl::Critic::Policy::' . $policy_name;
56 is( ref $policy, $policy_name_long, 'Created correct type of policy');
58 my @themes = $policy->get_themes();
59 is_deeply( \@themes, [ qw(betty wilma) ], 'Set the theme');
62 #-----------------------------------------------------------------------------
63 # Test exception handling
66 my $userprof = Perl::Critic::UserProfile->new( -profile => 'NONE' );
67 my $pf = Perl::Critic::PolicyFactory->new( -profile => $userprof );
69 # Try missing arguments
70 eval{ $pf->create_policy() };
71 like( $EVAL_ERROR, qr/The -name argument/m, 'create without -name arg' );
73 # Try creating bogus policy
74 eval{ $pf->create_policy( -name => 'Perl::Critic::Foo' ) };
75 like( $EVAL_ERROR, qr/Can't locate object method/m, 'create bogus policy' );
77 # Try using a bogus severity level
78 my $policy_name = 'Modules::RequireVersionVar';
79 my $policy_params = {severity => 'bogus'};
80 eval{ $pf->create_policy( -name => $policy_name, -params => $policy_params)};
81 like( $EVAL_ERROR, qr/Invalid severity: "bogus"/m, 'create policy w/ bogus severity' );
84 #-----------------------------------------------------------------------------
85 # Test warnings about bogus policies
88 my $last_warning = q{}; #Trap warning messages here
89 local $SIG{__WARN__} = sub { $last_warning = shift };
91 my $profile = { 'Perl::Critic::Bogus' => {} };
92 my $userprof = Perl::Critic::UserProfile->new( -profile => $profile );
93 my $pf = Perl::Critic::PolicyFactory->new( -profile => $userprof );
94 like( $last_warning, qr/^Policy ".*Bogus" is not installed/m );
97 $profile = { '-Perl::Critic::Shizzle' => {} };
98 $userprof = Perl::Critic::UserProfile->new( -profile => $profile );
99 $pf = Perl::Critic::PolicyFactory->new( -profile => $userprof );
100 like( $last_warning, qr/^Policy ".*Shizzle" is not installed/m );
104 #-----------------------------------------------------------------------------
106 # ensure we run true if this test is loaded by
107 # t/11_policyfactory.t_without_optional_dependencies.t
110 ##############################################################################
113 # cperl-indent-level: 4
115 # indent-tabs-mode: nil
116 # c-indentation-style: bsd
118 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :