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