3 ##############################################################################
4 # $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/t/02_policy.t $
5 # $Date: 2008-06-06 00:48:04 -0500 (Fri, 06 Jun 2008) $
8 ##############################################################################
14 use English qw<-no_match_vars>;
16 use Test::More tests => 26;
19 #-----------------------------------------------------------------------------
20 # Perl::Critic::Policy is an abstract class, so it can't be instantiated
21 # directly. So we test it by declaring test classes that inherit from it.
24 use base 'Perl::Critic::Policy';
26 package PolicyTestOverriddenDefaultMaximumViolations;
27 use base 'Perl::Critic::Policy';
29 sub default_maximum_violations_per_document { return 31; }
31 #-----------------------------------------------------------------------------
35 my $p = PolicyTest->new();
36 isa_ok($p, 'PolicyTest');
39 eval { $p->violates(); };
40 ok($EVAL_ERROR, 'abstract violates() throws exception');
43 # Test default application...
44 is($p->applies_to(), 'PPI::Element', 'applies_to()');
47 # Test default maximum violations per document...
49 $p->default_maximum_violations_per_document(),
51 'default_maximum_violations_per_document()',
54 $p->get_maximum_violations_per_document(),
56 'get_maximum_violations_per_document()',
59 # Change maximum violations level...
60 $p->set_maximum_violations_per_document(3);
62 # Test maximum violations again...
64 $p->default_maximum_violations_per_document(),
66 q<default_maximum_violations_per_document() hasn't changed>,
69 $p->get_maximum_violations_per_document(),
71 q<get_maximum_violations_per_document() returns new value>,
75 my $overridden_default = PolicyTestOverriddenDefaultMaximumViolations->new();
76 isa_ok($overridden_default, 'PolicyTestOverriddenDefaultMaximumViolations');
78 # Test default maximum violations per document...
80 $overridden_default->default_maximum_violations_per_document(),
82 'default_maximum_violations_per_document() overridden',
85 $overridden_default->get_maximum_violations_per_document(),
87 'get_maximum_violations_per_document() overridden',
90 # Change maximum violations level...
91 $overridden_default->set_maximum_violations_per_document(undef);
93 # Test maximum violations again...
95 $overridden_default->default_maximum_violations_per_document(),
97 q<default_maximum_violations_per_document() overridden hasn't changed>,
100 $overridden_default->get_maximum_violations_per_document(),
102 q<get_maximum_violations_per_document() overridden returns new undefined value>,
106 # Test default severity...
107 is( $p->default_severity(), 1, 'default_severity()');
108 is( $p->get_severity(), 1, 'get_severity()' );
110 # Change severity level...
113 # Test severity again...
114 is( $p->default_severity(), 1 ); #Still the same
115 is( $p->get_severity(), 3 ); #Should have new value
118 # Test default theme...
119 is_deeply( [$p->default_themes()], [], 'default_themes()');
120 is_deeply( [$p->get_themes()], [], 'get_themes()');
123 $p->set_themes( qw(c b a) ); #unsorted
125 # Test theme again...
126 is_deeply( [$p->default_themes()], [] ); #Still the same
127 is_deeply( [$p->get_themes()], [qw(a b c)] ); #Should have new value, sorted
130 $p->add_themes( qw(f e d) ); #unsorted
132 # Test theme again...
133 is_deeply( [$p->default_themes()], [] ); #Still the same
134 is_deeply( [$p->get_themes()], [ qw(a b c d e f) ] ); #Should have new value, sorted
137 # Test format getter/setters
138 is( Perl::Critic::Policy::get_format, "%p\n", 'Default policy format');
140 my $new_format = '%p %s [%t]';
141 Perl::Critic::Policy::set_format( $new_format ); #Set format
142 is( Perl::Critic::Policy::get_format, $new_format, 'Changed policy format');
144 my $expected_string = 'PolicyTest 3 [a b c d e f]';
145 is( $p->to_string(), $expected_string, 'Stringification by to_string()');
146 is( "$p", $expected_string, 'Stringification by overloading');
149 #-----------------------------------------------------------------------------
151 # ensure we run true if this test is loaded by
152 # t/02_policy.t_without_optional_dependencies.t
156 ##############################################################################
159 # cperl-indent-level: 4
161 # indent-tabs-mode: nil
162 # c-indentation-style: bsd
164 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :