1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
|
#!perl
use 5.006001;
use strict;
use warnings;
use English qw(-no_match_vars);
use List::MoreUtils qw(any all none);
use Perl::Critic::TestUtils;
use Perl::Critic::PolicyFactory;
use Perl::Critic::UserProfile;
use Perl::Critic::Theme;
use Test::More tests => 66;
our $VERSION = '1.138';
use Perl::Critic::TestUtils;
Perl::Critic::TestUtils::assert_version( $VERSION );
ILLEGAL_RULES: {
my @invalid_rules = (
'$cosmetic', ## no critic (RequireInterpolationOfMetachars)
'"cosmetic"',
'#cosmetic > bugs',
'cosmetic / bugs',
'cosmetic % bugs',
'cosmetic + [bugs - pbp]',
'cosmetic + {bugs - pbp}',
'cosmetic @ bugs ^ pbp',
);
for my $invalid ( @invalid_rules ) {
eval { Perl::Critic::Theme::->new( -rule => $invalid ) };
like(
$EVAL_ERROR,
qr/invalid [ ] character/xms,
qq{Invalid rule: "$invalid"},
);
}
}
#-----------------------------------------------------------------------------
VALID_RULES: {
my @valid_rules = (
'cosmetic',
'!cosmetic',
'-cosmetic',
'not cosmetic',
'cosmetic + bugs',
'cosmetic - bugs',
'cosmetic + (bugs - pbp)',
'cosmetic+(bugs-pbp)',
'cosmetic || bugs',
'cosmetic && bugs',
'cosmetic || (bugs - pbp)',
'cosmetic||(bugs-pbp)',
'cosmetic or bugs',
'cosmetic and bugs',
'cosmetic or (bugs not pbp)',
);
for my $valid ( @valid_rules ) {
my $theme = Perl::Critic::Theme->new( -rule => $valid );
ok( $theme, qq{Valid expression: "$valid"} );
}
}
#-----------------------------------------------------------------------------
TRANSLATIONS: {
my %expressions = (
'cosmetic' => 'cosmetic',
'!cosmetic' => '!cosmetic',
'-cosmetic' => '!cosmetic',
'not cosmetic' => '! cosmetic',
'cosmetic + bugs', => 'cosmetic || bugs',
'cosmetic - bugs', => 'cosmetic && ! bugs',
'cosmetic + (bugs - pbp)' => 'cosmetic || (bugs && ! pbp)',
'cosmetic+(bugs-pbp)' => 'cosmetic||(bugs&& !pbp)',
'cosmetic or bugs' => 'cosmetic || bugs',
'cosmetic and bugs' => 'cosmetic && bugs',
'cosmetic and (bugs or pbp)' => 'cosmetic && (bugs || pbp)',
'cosmetic + bugs' => 'cosmetic || bugs',
'cosmetic * bugs' => 'cosmetic && bugs',
'cosmetic * (bugs + pbp)' => 'cosmetic && (bugs || pbp)',
'cosmetic || bugs', => 'cosmetic || bugs',
'!cosmetic && bugs', => '!cosmetic && bugs',
'cosmetic && not (bugs or pbp)'=> 'cosmetic && ! (bugs || pbp)',
);
while ( my ($raw, $expected) = each %expressions ) {
my $cooked = Perl::Critic::Theme::cook_rule( $raw );
is( $cooked, $expected, qq{Theme cooking: '$raw' -> '$cooked'});
}
}
#-----------------------------------------------------------------------------
Perl::Critic::TestUtils::block_perlcriticrc();
{
my $profile = Perl::Critic::UserProfile->new( -profile => q{} );
my $factory = Perl::Critic::PolicyFactory->new( -profile => $profile );
my @policy_names = Perl::Critic::PolicyFactory::site_policy_names();
my @pols = map { $factory->create_policy( -name => $_ ) } @policy_names;
#--------------
my $rule = 'cosmetic';
my $theme = Perl::Critic::Theme->new( -rule => $rule );
my @members = grep { $theme->policy_is_thematic( -policy => $_) } @pols;
ok(
( all { has_theme( $_, 'cosmetic' ) } @members ),
'theme rule: "cosmetic"',
);
#--------------
$rule = 'cosmetic - pbp';
$theme = Perl::Critic::Theme->new( -rule => $rule );
@members = grep { $theme->policy_is_thematic( -policy => $_) } @pols;
ok(
( all { has_theme( $_, 'cosmetic' ) } @members ),
'theme rule: "cosmetic - pbp", all has_theme(cosmetic)',
);
ok(
( none { has_theme( $_, 'pbp') } @members ),
'theme rule: "cosmetic - pbp", none has_theme(pbp)',
);
$rule = 'cosmetic and not pbp';
$theme = Perl::Critic::Theme->new( -rule => $rule );
@members = grep { $theme->policy_is_thematic( -policy => $_) } @pols;
ok(
( all { has_theme( $_, 'cosmetic' ) } @members ),
'theme rule: "cosmetic and not pbp", all has_theme(cosmetic)',
);
ok(
( none { has_theme( $_, 'pbp') } @members ),
'theme rule: "cosmetic and not pbp", none has_theme(pbp)',
);
$rule = 'cosmetic && ! pbp';
$theme = Perl::Critic::Theme->new( -rule => $rule );
@members = grep { $theme->policy_is_thematic( -policy => $_) } @pols;
ok(
( all { has_theme( $_, 'cosmetic' ) } @members ),
'theme rule: "cosmetic && ! pbp", all has_theme(cosmetic)',
);
ok(
( none { has_theme( $_, 'pbp') } @members ),
'theme rule: "cosmetic && ! pbp", none has_theme(pbp)',
);
#--------------
$rule = 'cosmetic + pbp';
$theme = Perl::Critic::Theme->new( -rule => $rule );
@members = grep { $theme->policy_is_thematic( -policy => $_) } @pols;
ok(
( all { has_theme($_, 'cosmetic') || has_theme($_, 'pbp') } @members ),
'theme rule: "cosmetic + pbp"',
);
$rule = 'cosmetic || pbp';
$theme = Perl::Critic::Theme->new( -rule => $rule );
@members = grep { $theme->policy_is_thematic( -policy => $_) } @pols;
ok(
( all { has_theme($_, 'cosmetic') || has_theme($_, 'pbp') } @members ),
'theme rule: "cosmetic || pbp"',
);
$rule = 'cosmetic or pbp';
$theme = Perl::Critic::Theme->new( -rule => $rule );
@members = grep { $theme->policy_is_thematic( -policy => $_) } @pols;
ok(
( all { has_theme($_, 'cosmetic') || has_theme($_, 'pbp') } @members),
'theme rule: "cosmetic or pbp"',
);
#--------------
$rule = 'bugs * pbp';
$theme = Perl::Critic::Theme->new( -rule => $rule );
@members = grep { $theme->policy_is_thematic( -policy => $_) } @pols;
ok(
( all { has_theme($_, 'bugs') } @members ),
'theme rule: "bugs * pbp", all has_theme(bugs)',
);
ok(
( all { has_theme($_, 'pbp') } @members ),
'theme rule: "bugs * pbp", all has_theme(pbp)',
);
$rule = 'bugs and pbp';
$theme = Perl::Critic::Theme->new( -rule => $rule );
@members = grep { $theme->policy_is_thematic( -policy => $_) } @pols;
ok(
( all { has_theme($_, 'bugs') } @members ),
'theme rule: "bugs and pbp", all has_theme(bugs)',
);
ok(
( all { has_theme($_, 'pbp') } @members ),
'theme rule: "bugs and pbp", all has_theme(pbp)',
);
$rule = 'bugs && pbp';
$theme = Perl::Critic::Theme->new( -rule => $rule );
@members = grep { $theme->policy_is_thematic( -policy => $_) } @pols;
ok(
( all { has_theme($_, 'bugs') } @members ),
'theme rule: "bugs && pbp", all has_theme(bugs)',
);
ok(
( all { has_theme($_, 'pbp') } @members ),
'theme rule: "bugs && pbp", all has_theme(pbp)',
);
#-------------
$rule = 'pbp - (danger * security)';
$theme = Perl::Critic::Theme->new( -rule => $rule );
@members = grep { $theme->policy_is_thematic( -policy => $_) } @pols;
ok(
( all { has_theme($_, 'pbp') } @members ),
'theme rule: "pbp - (danger * security)", all has_theme(pbp)',
);
ok(
( none { has_theme($_, 'danger') && has_theme($_, 'security') } @members ),
'theme rule: "pbp - (danger * security)", none has_theme(danger && security)',
);
$rule = 'pbp and ! (danger and security)';
$theme = Perl::Critic::Theme->new( -rule => $rule );
@members = grep { $theme->policy_is_thematic( -policy => $_) } @pols;
ok(
( all { has_theme($_, 'pbp') } @members ),
'theme rule: "pbp and not (danger and security)", all has_theme(pbp)',
);
ok(
( none { has_theme($_, 'danger') && has_theme($_, 'security') } @members ),
'theme rule: "pbp and not (danger and security)", none has_theme(danger && security)',
);
$rule = 'pbp && not (danger && security)';
$theme = Perl::Critic::Theme->new( -rule => $rule );
@members = grep { $theme->policy_is_thematic( -policy => $_) } @pols;
ok(
( all { has_theme($_, 'pbp') } @members ),
'theme rule: "pbp && not (danger && security)", all has_theme(pbp)',
);
ok(
( none { has_theme($_, 'danger') && has_theme($_, 'security') } @members ),
'theme rule: "pbp && not (danger && security)", none has_theme(danger && security)',
);
#--------------
$rule = 'bogus';
$theme = Perl::Critic::Theme->new( -rule => $rule );
@members = grep { $theme->policy_is_thematic( -policy => $_) } @pols;
is( scalar @members, 0, 'bogus theme' );
$rule = 'bogus - pbp';
$theme = Perl::Critic::Theme->new( -rule => $rule );
@members = grep { $theme->policy_is_thematic( -policy => $_) } @pols;
is( scalar @members, 0, 'bogus theme' );
$rule = q{};
$theme = Perl::Critic::Theme->new( -rule => $rule );
@members = grep { $theme->policy_is_thematic( -policy => $_) } @pols;
is( scalar @members, scalar @pols, 'empty theme' );
$rule = q{};
$theme = Perl::Critic::Theme->new( -rule => $rule );
@members = grep { $theme->policy_is_thematic( -policy => $_) } @pols;
is( scalar @members, scalar @pols, 'undef theme' );
#--------------
# Exceptions
$rule = 'cosmetic *(';
$theme = Perl::Critic::Theme->new( -rule => $rule );
eval{ $theme->policy_is_thematic( -policy => $pols[0] ) };
like(
$EVAL_ERROR,
qr/syntax [ ] error/xms,
'invalid theme expression',
);
}
#-----------------------------------------------------------------------------
sub has_theme {
my ($policy, $theme) = @_;
return any { $_ eq $theme } $policy->get_themes();
}
##############################################################################
# Local Variables:
# mode: cperl
# cperl-indent-level: 4
# fill-column: 78
# indent-tabs-mode: nil
# c-indentation-style: bsd
# End:
# ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :
|