Login
Allow user configuration of maximum_violations_per_document.
[gknop/Perl-Critic.git] / t / 16_roundtrip_defaults.t
CommitLineData
aa03ef60
ES
1#!perl
2
3##############################################################################
4# $URL$
5# $Date$
6# $Author$
7# $Revision$
8##############################################################################
9
10use strict;
11use warnings;
12
13use English qw(-no_match_vars);
14
15use Test::More;
16
17use Perl::Critic::PolicyFactory (-test => 1);
18use Perl::Critic::Config;
19use Perl::Critic::ProfilePrototype;
20use Perl::Critic::Utils qw{ :characters :severities };
21
22#-----------------------------------------------------------------------------
23
aa03ef60
ES
24eval 'use Test::Deep'; ## no critic (
25plan( skip_all => 'Test::Deep requried to test round-trip of default values')
26 if $EVAL_ERROR;
27
28#-----------------------------------------------------------------------------
29
30my $default_configuration =
31 Perl::Critic::Config->new(
32 -profile => $EMPTY,
33 -severity => 1,
34 );
35my @default_policies = $default_configuration->policies();
36
37my $policy_test_count;
38
39$policy_test_count = 3 * @default_policies;
40foreach my $policy (@default_policies) {
41 if (
42 $policy->parameter_metadata_available()
43 and not $policy->isa('Perl::Critic::Policy::CodeLayout::RequireTidyCode')
44 ) {
45 $policy_test_count += scalar @{$policy->get_parameters()};
46 }
47}
48my $test_count = 12 + $policy_test_count;
49plan tests => $test_count;
50
51#-----------------------------------------------------------------------------
52
53my $profile_generator =
54 Perl::Critic::ProfilePrototype->new(
55 -policies => \@default_policies,
56 '-comment-out-parameters' => 0,
57 -config => $default_configuration,
58 );
59my $profile = $profile_generator->to_string();
60
61my $derived_configuration =
62 Perl::Critic::Config->new( -profile => \$profile );
63
64#-----------------------------------------------------------------------------
65
66my @derived_include = $derived_configuration->include();
67my @default_include = $default_configuration->include();
68cmp_deeply(
69 \@derived_include,
70 \@default_include,
71 'include',
72);
73
74#-----------------------------------------------------------------------------
75
76my @derived_exclude = $derived_configuration->exclude();
77my @default_exclude = $default_configuration->exclude();
78cmp_deeply(
79 \@derived_exclude,
80 \@default_exclude,
81 'exclude',
82);
83
84#-----------------------------------------------------------------------------
85
86my @derived_single_policy = $derived_configuration->single_policy();
87my @default_single_policy = $default_configuration->single_policy();
88cmp_deeply(
89 \@derived_single_policy,
90 \@default_single_policy,
91 'single_policy',
92);
93
94#-----------------------------------------------------------------------------
95
96is(
97 $derived_configuration->force(),
98 $default_configuration->force(),
99 'force',
100);
101
102#-----------------------------------------------------------------------------
103
104is(
105 $derived_configuration->only(),
106 $default_configuration->only(),
107 'only',
108);
109
110#-----------------------------------------------------------------------------
111
112is(
113 $derived_configuration->profile_strictness(),
114 $default_configuration->profile_strictness(),
115 'force',
116);
117
118#-----------------------------------------------------------------------------
119
120is(
121 $derived_configuration->color(),
122 $default_configuration->color(),
123 'color',
124);
125
126#-----------------------------------------------------------------------------
127
128cmp_ok(
129 $derived_configuration->severity(),
130 '==',
131 $default_configuration->severity(),
132 'severity',
133);
134
135#-----------------------------------------------------------------------------
136
137cmp_ok(
138 $derived_configuration->top(),
139 '==',
140 $default_configuration->top(),
141 'top',
142);
143
144#-----------------------------------------------------------------------------
145
146cmp_ok(
147 $derived_configuration->verbose(),
148 '==',
149 $default_configuration->verbose(),
150 'verbose',
151);
152
153#-----------------------------------------------------------------------------
154
155cmp_deeply(
156 $derived_configuration->theme(),
157 $default_configuration->theme(),
158 'theme',
159);
160
161#-----------------------------------------------------------------------------
162
163my @derived_policies = $derived_configuration->policies();
164
165my $policy_counts_match =
166 is(
167 scalar @derived_policies,
168 scalar @default_policies,
169 'same policy count'
170 );
171
172SKIP: {
173 skip
174 q{because there weren't the same number of policies},
175 $policy_test_count
176 if not $policy_counts_match;
177
178 for (my $x = 0; $x < @default_policies; $x++) {
179 my $derived_policy = $derived_policies[$x];
180 my $default_policy = $default_policies[$x];
181
182 is(
183 $derived_policy->get_short_name(),
184 $default_policy->get_short_name(),
185 'policy names match',
186 );
187 is(
188 $derived_policy->get_severity(),
189 $default_policy->get_severity(),
190 $default_policy->get_short_name() . ' severities match',
191 );
192 is(
193 $derived_policy->get_themes(),
194 $default_policy->get_themes(),
195 $default_policy->get_short_name() . ' themes match',
196 );
197
198 if (
199 $default_policy->parameter_metadata_available()
200 and not $default_policy->isa('Perl::Critic::Policy::CodeLayout::RequireTidyCode')
201 ) {
202 # Encapsulation violation alert!
203 foreach my $parameter ( @{$default_policy->get_parameters()} ) {
204 my $parameter_name =
205 $default_policy->__get_parameter_name( $parameter );
206
207 cmp_deeply(
208 $derived_policy->{$parameter_name},
209 $default_policy->{$parameter_name},
210 $default_policy->get_short_name()
211 . $SPACE
212 . $parameter_name
213 . ' match',
214 );
215 }
216 }
217 }
218}
219
220
221#-----------------------------------------------------------------------------
222# Local Variables:
223# mode: cperl
224# cperl-indent-level: 4
225# fill-column: 78
226# indent-tabs-mode: nil
227# c-indentation-style: bsd
228# End:
96fed375 229# ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :