File: 02_policy.t

package info (click to toggle)
libperl-critic-perl 1.156-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 3,544 kB
  • sloc: perl: 24,092; lisp: 341; makefile: 7
file content (180 lines) | stat: -rw-r--r-- 4,641 bytes parent folder | download
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
#!perl

use 5.010001;
use strict;
use warnings;

use English qw<-no_match_vars>;

use Test::More tests => 29;

our $VERSION = '1.156';

use Perl::Critic::TestUtils;
Perl::Critic::TestUtils::assert_version( $VERSION );

# Perl::Critic::Policy is an abstract class, so it can't be instantiated
# directly.  So we test it by declaring test classes that inherit from it.

## no critic (ProhibitMultiplePackages, RequireFilenameMatchesPackage)
package PolicyTest;
use parent 'Perl::Critic::Policy';

package PolicyTestOverriddenDefaultMaximumViolations;
use parent 'Perl::Critic::Policy';

sub default_maximum_violations_per_document { return 31; }

#-----------------------------------------------------------------------------

package main;
## use critic

my $p = PolicyTest->new();
isa_ok($p, 'PolicyTest');


local $EVAL_ERROR = undef;
eval { $p->violates(); 1 };
ok($EVAL_ERROR, 'abstract violates() throws exception');


is(
    $p->is_enabled(),
    undef,
    'is_enabled() initially returns undef',
);


ok( !! $p->is_safe(), 'is_safe() returns a true value by default.' );


# Test default application...
is($p->applies_to(), 'PPI::Element', 'applies_to()');


# Test default maximum violations per document...
is(
    $p->default_maximum_violations_per_document(),
    undef,
    'default_maximum_violations_per_document()',
);
is(
    $p->get_maximum_violations_per_document(),
    undef,
    'get_maximum_violations_per_document()',
);

# Change maximum violations level...
$p->set_maximum_violations_per_document(3);

# Test maximum violations again...
is(
    $p->default_maximum_violations_per_document(),
    undef,
    q<default_maximum_violations_per_document() hasn't changed>,
);
is(
    $p->get_maximum_violations_per_document(),
    3,
    q<get_maximum_violations_per_document() returns new value>,
);


my $overridden_default = PolicyTestOverriddenDefaultMaximumViolations->new();
isa_ok($overridden_default, 'PolicyTestOverriddenDefaultMaximumViolations');

is(
    $overridden_default->is_enabled(),
    undef,
    'is_enabled() initially returns undef',
);

# Test default maximum violations per document...
is(
    $overridden_default->default_maximum_violations_per_document(),
    31,
    'default_maximum_violations_per_document() overridden',
);
is(
    $overridden_default->get_maximum_violations_per_document(),
    31,
    'get_maximum_violations_per_document() overridden',
);

# Change maximum violations level...
$overridden_default->set_maximum_violations_per_document(undef);

# Test maximum violations again...
is(
    $overridden_default->default_maximum_violations_per_document(),
    31,
    q<default_maximum_violations_per_document() overridden hasn't changed>,
);
is(
    $overridden_default->get_maximum_violations_per_document(),
    undef,
    q<get_maximum_violations_per_document() overridden returns new undefined value>,
);


# Test default severity...
is( $p->default_severity(), 1, 'default_severity()');
is( $p->get_severity(), 1, 'get_severity()' );

# Change severity level...
$p->set_severity(3);

# Test severity again...
is( $p->default_severity(), 1, q<default_severity() hasn't changed.>);
is( $p->get_severity(), 3, q<get_severity() returns the new value.> );


# Test default theme...
is_deeply( [$p->default_themes()], [], 'default_themes()');
is_deeply( [$p->get_themes()], [], 'get_themes()');

# Change theme
$p->set_themes( qw(c b a) ); # unsorted

# Test theme again...
is_deeply( [$p->default_themes()], [], q<default_themes() hasn't changed.>);
is_deeply(
    [$p->get_themes()],
    [qw(a b c)],
    'get_themes() returns the new value, sorted.',
);

# Append theme
$p->add_themes( qw(f e d) ); # unsorted

# Test theme again...
is_deeply( [$p->default_themes()], [], q<default_themes() hasn't changed.>);
is_deeply(
    [$p->get_themes()],
    [ qw(a b c d e f) ],
    'get_themes() returns the new value, sorted.',
);


# Test format getter/setters
is( Perl::Critic::Policy::get_format, '%p', 'Default policy format');

my $new_format = '%p %s [%t]';
Perl::Critic::Policy::set_format( $new_format ); # Set format
is( Perl::Critic::Policy::get_format, $new_format, 'Changed policy format');

my $expected_string = 'PolicyTest 3 [a b c d e f]';
is( $p->to_string(), $expected_string, 'Stringification by to_string()');
is( "$p", $expected_string, 'Stringification by overloading');


##############################################################################
# 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 :