Login
8eef0bf015106fb276c99762d08e643e45dccec0
[gknop/Perl-Critic.git] / t / 15_statistics.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::Statistics;
18 use Perl::Critic::TestUtils;
19
20 use Test::More tests => 24;
21
22 #-----------------------------------------------------------------------------
23
24 our $VERSION = '1.118';
25
26 #-----------------------------------------------------------------------------
27
28 Perl::Critic::TestUtils::block_perlcriticrc();
29
30 #-----------------------------------------------------------------------------
31
32 my $package = 'Perl::Critic::Statistics';
33
34 my @methods = qw(
35     average_sub_mccabe
36     lines
37     modules
38     new
39     statements
40     subs
41     total_violations
42     violations_by_policy
43     violations_by_severity
44     statements_other_than_subs
45     violations_per_file
46     violations_per_statement
47     violations_per_line_of_code
48 );
49
50 for my $method ( @methods ) {
51     can_ok( $package, $method );
52 }
53
54 #-----------------------------------------------------------------------------
55
56 my $code = <<'END_PERL';
57 package Foo;
58
59 use My::Module;
60 $this = $that if $condition;
61 sub foo { return @list unless $condition };
62 END_PERL
63
64 #-----------------------------------------------------------------------------
65
66 # Just don't get involved with Perl::Tidy.
67 my $profile = { '-CodeLayout::RequireTidyCode' => {} };
68 my $critic =
69     Perl::Critic->new(
70         -severity => 1,
71         -profile => $profile,
72         -theme => 'core',
73     );
74 my @violations = $critic->critique( \$code );
75
76 #print @violations;
77 #exit;
78
79 my %expected_stats = (
80     average_sub_mccabe            => 2,
81     lines                         => 5,
82     modules                       => 1,
83     statements                    => 6,
84     statements_other_than_subs    => 5,
85     subs                          => 1,
86     total_violations              => 10,
87     violations_per_file           => 10,
88     violations_per_line_of_code   => 2,
89     violations_per_statement      => 2,
90 );
91
92 my $stats = $critic->statistics();
93 isa_ok($stats, $package);
94
95 while ( my($method, $expected) = each %expected_stats) {
96     is( $stats->$method, $expected, "Statistics: $method");
97 }
98
99 #-----------------------------------------------------------------------------
100
101 # ensure we return true if this test is loaded by
102 # t/15_statistics.t_without_optional_dependencies.t
103 1;
104
105 ###############################################################################
106 # Local Variables:
107 #   mode: cperl
108 #   cperl-indent-level: 4
109 #   fill-column: 78
110 #   indent-tabs-mode: nil
111 #   c-indentation-style: bsd
112 # End:
113 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :