3 ##############################################################################
4 # $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/t/09_theme.t $
5 # $Date: 2008-06-06 00:48:04 -0500 (Fri, 06 Jun 2008) $
8 ##############################################################################
13 use English qw(-no_match_vars);
15 use List::MoreUtils qw(any all none);
16 use Test::More (tests => 66);
18 use Perl::Critic::TestUtils;
19 use Perl::Critic::PolicyFactory;
20 use Perl::Critic::UserProfile;
21 use Perl::Critic::Theme;
23 #-----------------------------------------------------------------------------
33 'cosmetic + [bugs - pbp]',
34 'cosmetic + {bugs - pbp}',
35 'cosmetic @ bugs ^ pbp',
38 for my $invalid ( @invalid_rules ) {
39 eval { Perl::Critic::Theme::->new( -rule => $invalid ) };
40 like( $EVAL_ERROR, qr/invalid character/, qq{Invalid rule: "$invalid"});
44 #-----------------------------------------------------------------------------
56 'cosmetic + (bugs - pbp)',
57 'cosmetic+(bugs-pbp)',
61 'cosmetic || (bugs - pbp)',
62 'cosmetic||(bugs-pbp)',
66 'cosmetic or (bugs not pbp)',
69 for my $valid ( @valid_rules ) {
70 my $theme = Perl::Critic::Theme->new( -rule => $valid );
71 ok( $theme, qq{Valid expression: "$valid"} );
75 #-----------------------------------------------------------------------------
80 'cosmetic' => 'cosmetic',
81 '!cosmetic' => '!cosmetic',
82 '-cosmetic' => '!cosmetic',
83 'not cosmetic' => '! cosmetic',
84 'cosmetic + bugs', => 'cosmetic || bugs',
85 'cosmetic - bugs', => 'cosmetic && ! bugs',
86 'cosmetic + (bugs - pbp)' => 'cosmetic || (bugs && ! pbp)',
87 'cosmetic+(bugs-pbp)' => 'cosmetic||(bugs&& !pbp)',
88 'cosmetic or bugs' => 'cosmetic || bugs',
89 'cosmetic and bugs' => 'cosmetic && bugs',
90 'cosmetic and (bugs or pbp)' => 'cosmetic && (bugs || pbp)',
91 'cosmetic + bugs' => 'cosmetic || bugs',
92 'cosmetic * bugs' => 'cosmetic && bugs',
93 'cosmetic * (bugs + pbp)' => 'cosmetic && (bugs || pbp)',
94 'cosmetic || bugs', => 'cosmetic || bugs',
95 '!cosmetic && bugs', => '!cosmetic && bugs',
96 'cosmetic && not (bugs or pbp)'=> 'cosmetic && ! (bugs || pbp)'
99 while ( my ($raw, $expected) = each %expressions ) {
100 my $cooked = Perl::Critic::Theme::cook_rule( $raw );
101 is( $cooked, $expected, qq{Theme cooking: '$raw' -> '$cooked'});
106 #-----------------------------------------------------------------------------
108 Perl::Critic::TestUtils::block_perlcriticrc();
111 my $profile = Perl::Critic::UserProfile->new( -profile => q{} );
112 my $factory = Perl::Critic::PolicyFactory->new( -profile => $profile );
113 my @policy_names = Perl::Critic::PolicyFactory::site_policy_names();
114 my @pols = map { $factory->create_policy( -name => $_ ) } @policy_names;
118 my $rule = 'cosmetic';
119 my $theme = Perl::Critic::Theme->new( -rule => $rule );
120 my @members = grep { $theme->policy_is_thematic( -policy => $_) } @pols;
121 ok( all { has_theme( $_, 'cosmetic' ) } @members );
125 $rule = 'cosmetic - pbp';
126 $theme = Perl::Critic::Theme->new( -rule => $rule );
127 @members = grep { $theme->policy_is_thematic( -policy => $_) } @pols;
128 ok( all { has_theme( $_, 'cosmetic' ) } @members );
129 ok( none { has_theme( $_, 'pbp') } @members );
131 $rule = 'cosmetic and not pbp';
132 $theme = Perl::Critic::Theme->new( -rule => $rule );
133 @members = grep { $theme->policy_is_thematic( -policy => $_) } @pols;
134 ok( all { has_theme( $_, 'cosmetic' ) } @members );
135 ok( none { has_theme( $_, 'pbp') } @members );
137 $rule = 'cosmetic && ! pbp';
138 $theme = Perl::Critic::Theme->new( -rule => $rule );
139 @members = grep { $theme->policy_is_thematic( -policy => $_) } @pols;
140 ok( all { has_theme( $_, 'cosmetic' ) } @members );
141 ok( none { has_theme( $_, 'pbp') } @members );
145 $rule = 'cosmetic + pbp';
146 $theme = Perl::Critic::Theme->new( -rule => $rule );
147 @members = grep { $theme->policy_is_thematic( -policy => $_) } @pols;
148 ok( all { has_theme($_, 'cosmetic') ||
149 has_theme($_, 'pbp') } @members );
151 $rule = 'cosmetic || pbp';
152 $theme = Perl::Critic::Theme->new( -rule => $rule );
153 @members = grep { $theme->policy_is_thematic( -policy => $_) } @pols;
154 ok( all { has_theme($_, 'cosmetic') ||
155 has_theme($_, 'pbp') } @members );
157 $rule = 'cosmetic or pbp';
158 $theme = Perl::Critic::Theme->new( -rule => $rule );
159 @members = grep { $theme->policy_is_thematic( -policy => $_) } @pols;
160 ok( all { has_theme($_, 'cosmetic') ||
161 has_theme($_, 'pbp') } @members );
165 $rule = 'bugs * pbp';
166 $theme = Perl::Critic::Theme->new( -rule => $rule );
167 @members = grep { $theme->policy_is_thematic( -policy => $_) } @pols;
168 ok( all { has_theme($_, 'bugs') } @members );
169 ok( all { has_theme($_, 'pbp') } @members );
171 $rule = 'bugs and pbp';
172 $theme = Perl::Critic::Theme->new( -rule => $rule );
173 @members = grep { $theme->policy_is_thematic( -policy => $_) } @pols;
174 ok( all { has_theme($_, 'bugs') } @members );
175 ok( all { has_theme($_, 'pbp') } @members );
177 $rule = 'bugs && pbp';
178 $theme = Perl::Critic::Theme->new( -rule => $rule );
179 @members = grep { $theme->policy_is_thematic( -policy => $_) } @pols;
180 ok( all { has_theme($_, 'bugs') } @members );
181 ok( all { has_theme($_, 'pbp') } @members );
185 $rule = 'pbp - (danger * security)';
186 $theme = Perl::Critic::Theme->new( -rule => $rule );
187 @members = grep { $theme->policy_is_thematic( -policy => $_) } @pols;
188 ok( all { has_theme($_, 'pbp') } @members );
189 ok( none { has_theme($_, 'danger') &&
190 has_theme($_, 'security') } @members );
192 $rule = 'pbp and ! (danger and security)';
193 $theme = Perl::Critic::Theme->new( -rule => $rule );
194 @members = grep { $theme->policy_is_thematic( -policy => $_) } @pols;
195 ok( all { has_theme($_, 'pbp') } @members );
196 ok( none { has_theme($_, 'danger') &&
197 has_theme($_, 'security') } @members );
199 $rule = 'pbp && not (danger && security)';
200 $theme = Perl::Critic::Theme->new( -rule => $rule );
201 @members = grep { $theme->policy_is_thematic( -policy => $_) } @pols;
202 ok( all { has_theme($_, 'pbp') } @members );
203 ok( none { has_theme($_, 'danger') &&
204 has_theme($_, 'security') } @members );
209 $theme = Perl::Critic::Theme->new( -rule => $rule );
210 @members = grep { $theme->policy_is_thematic( -policy => $_) } @pols;
211 is( scalar @members, 0, 'bogus theme' );
213 $rule = 'bogus - pbp';
214 $theme = Perl::Critic::Theme->new( -rule => $rule );
215 @members = grep { $theme->policy_is_thematic( -policy => $_) } @pols;
216 is( scalar @members, 0, 'bogus theme' );
219 $theme = Perl::Critic::Theme->new( -rule => $rule );
220 @members = grep { $theme->policy_is_thematic( -policy => $_) } @pols;
221 is( scalar @members, scalar @pols, 'empty theme' );
224 $theme = Perl::Critic::Theme->new( -rule => $rule );
225 @members = grep { $theme->policy_is_thematic( -policy => $_) } @pols;
226 is( scalar @members, scalar @pols, 'undef theme' );
231 $rule = 'cosmetic *(';
232 $theme = Perl::Critic::Theme->new( -rule => $rule );
233 eval{ $theme->policy_is_thematic( -policy => $pols[0] ) };
234 like( $EVAL_ERROR, qr/syntax error/, 'invalid theme expression' );
238 #-----------------------------------------------------------------------------
241 my ($policy, $theme) = @_;
242 return any { $_ eq $theme } $policy->get_themes();
245 #-----------------------------------------------------------------------------
247 # ensure we run true if this test is loaded by
248 # t/09_theme.t_without_optional_dependencies.t
251 ##############################################################################
254 # cperl-indent-level: 4
256 # indent-tabs-mode: nil
257 # c-indentation-style: bsd
259 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :