Login
More self compliance
[gknop/Perl-Critic.git] / t / 16_roundtrip_defaults.t
1 #!perl
2
3 ##############################################################################
4 #      $URL$
5 #     $Date$
6 #   $Author$
7 # $Revision$
8 ##############################################################################
9
10 use 5.006001;
11 use strict;
12 use warnings;
13
14 use English qw(-no_match_vars);
15
16 use Perl::Critic::PolicyFactory (-test => 1);
17 use Perl::Critic::Config;
18 use Perl::Critic::ProfilePrototype;
19 use Perl::Critic::Utils qw{ :characters :severities };
20
21 use Test::More;
22
23 #-----------------------------------------------------------------------------
24
25 our $VERSION = '1.093_01';
26
27 #-----------------------------------------------------------------------------
28
29 eval 'use Test::Deep'; ## no critic (StringyEval)
30 plan( skip_all => 'Test::Deep requried to test round-trip of default values')
31     if $EVAL_ERROR;
32
33 #-----------------------------------------------------------------------------
34
35 my $default_configuration =
36     Perl::Critic::Config->new(
37         -profile => $EMPTY,
38         -severity => 1,
39         -theme => 'core',
40     );
41 my @default_policies = $default_configuration->policies();
42
43 my $policy_test_count;
44
45 $policy_test_count = 4 * @default_policies;
46 foreach 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 }
54 my $test_count = 12 + $policy_test_count;
55 plan tests => $test_count;
56
57 #-----------------------------------------------------------------------------
58
59 my $profile_generator =
60     Perl::Critic::ProfilePrototype->new(
61         -policies                   => \@default_policies,
62         '-comment-out-parameters'   => 0,
63         -config                     => $default_configuration,
64     );
65 my $profile = $profile_generator->to_string();
66
67 my $derived_configuration =
68     Perl::Critic::Config->new( -profile => \$profile );
69
70 #-----------------------------------------------------------------------------
71
72 my @derived_include = $derived_configuration->include();
73 my @default_include = $default_configuration->include();
74 cmp_deeply(
75     \@derived_include,
76     \@default_include,
77     'include',
78 );
79
80 #-----------------------------------------------------------------------------
81
82 my @derived_exclude = $derived_configuration->exclude();
83 my @default_exclude = $default_configuration->exclude();
84 cmp_deeply(
85     \@derived_exclude,
86     \@default_exclude,
87     'exclude',
88 );
89
90 #-----------------------------------------------------------------------------
91
92 my @derived_single_policy = $derived_configuration->single_policy();
93 my @default_single_policy = $default_configuration->single_policy();
94 cmp_deeply(
95     \@derived_single_policy,
96     \@default_single_policy,
97     'single_policy',
98 );
99
100 #-----------------------------------------------------------------------------
101
102 is(
103     $derived_configuration->force(),
104     $default_configuration->force(),
105     'force',
106 );
107
108 #-----------------------------------------------------------------------------
109
110 is(
111     $derived_configuration->only(),
112     $default_configuration->only(),
113     'only',
114 );
115
116 #-----------------------------------------------------------------------------
117
118 is(
119     $derived_configuration->profile_strictness(),
120     $default_configuration->profile_strictness(),
121     'force',
122 );
123
124 #-----------------------------------------------------------------------------
125
126 is(
127     $derived_configuration->color(),
128     $default_configuration->color(),
129     'color',
130 );
131
132 #-----------------------------------------------------------------------------
133
134 cmp_ok(
135     $derived_configuration->severity(),
136     q<==>,
137     $default_configuration->severity(),
138     'severity',
139 );
140
141 #-----------------------------------------------------------------------------
142
143 cmp_ok(
144     $derived_configuration->top(),
145     q<==>,
146     $default_configuration->top(),
147     'top',
148 );
149
150 #-----------------------------------------------------------------------------
151
152 cmp_ok(
153     $derived_configuration->verbose(),
154     q<==>,
155     $default_configuration->verbose(),
156     'verbose',
157 );
158
159 #-----------------------------------------------------------------------------
160
161 cmp_deeply(
162     $derived_configuration->theme(),
163     $default_configuration->theme(),
164     'theme',
165 );
166
167 #-----------------------------------------------------------------------------
168
169 my @derived_policies = $derived_configuration->policies();
170
171 my $policy_counts_match =
172     is(
173         scalar @derived_policies,
174         scalar @default_policies,
175         'same policy count'
176     );
177
178 SKIP: {
179     skip
180         q{because there weren't the same number of policies},
181             $policy_test_count
182         if not $policy_counts_match;
183
184     for (my $x = 0; $x < @default_policies; $x++) { ## no critic (ProhibitCStyleForLoops)
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(
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(
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:
240 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :