File: TestData.pm

package info (click to toggle)
libperl-metrics-simple-perl 0.18-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 280 kB
  • ctags: 103
  • sloc: perl: 1,255; makefile: 2
file content (204 lines) | stat: -rw-r--r-- 6,620 bytes parent folder | download | duplicates (2)
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package Perl::Metrics::Simple::TestData;
use strict;
use warnings;

use Carp qw(confess);
use English qw(-no_match_vars);
use Readonly;

our $VERSION = '0.01';

# Bad hack. Do this in the data instead!
our @ORDER_OF_FILES = qw(
  Module.pm
  Moose.pm
  empty_file.pl
  no_packages_nor_subs
  package_no_subs.pl
  subs_no_package.pl
);

my %TestData = ();

sub new {
    my ( $class, %parameters ) = @_;
    my $self = {};
    bless $self, ref $class || $class;
    $TestData{$self} = $self->make_test_data( $parameters{test_directory} );
    return $self;
}

sub get_test_data {
    my $self = shift;
    return $TestData{$self};
}

sub get_main_stats {
    my $self       = shift;
    my $test_data  = $self->get_test_data;
    my $main_stats = {};

    foreach my $file_name (@ORDER_OF_FILES) {
        my $hash = $test_data->{$file_name};
        $main_stats->{lines}             += $hash->{main_stats}->{lines};
        $main_stats->{mccabe_complexity} +=
          $hash->{main_stats}->{mccabe_complexity};
    }
    return $main_stats;
}

sub get_file_stats {
    my $self       = shift;
    my $test_data  = $self->get_test_data;
    my @file_stats = ();
    foreach my $file_name (@ORDER_OF_FILES) {
        my $hash                    = $test_data->{$file_name};
        my $stats_hash_for_one_file = {
            path       => $hash->{path},
            main_stats => $hash->{main_stats},
        };
        push @file_stats, $stats_hash_for_one_file;
    }
    return \@file_stats;
}

sub make_test_data {
    my $self           = shift;
    my $test_directory = shift;
    if ( !-d $test_directory ) {
        confess "test_directory '$test_directory' not found! ";
    }
    my $test_data = bless {
        'no_packages_nor_subs' => {
            path       => "$test_directory/no_packages_nor_subs",
            lines      => 4,
            main_stats => {
                lines             => 4,
                mccabe_complexity => 1,
                name              => '{code not in named subroutines}',
                path              => "$test_directory/no_packages_nor_subs",
            },
            subs     => [],
            packages => [],
        },
        'empty_file.pl' => {
            path       => "$test_directory/empty_file.pl",
            lines      => 0,
            main_stats => {
                lines             => 0,
                mccabe_complexity => 0,
                name              => '{code not in named subroutines}',
                path              => "$test_directory/empty_file.pl",
            },
            subs     => [],
            packages => [],
        },
        'package_no_subs.pl' => {
            path       => "$test_directory/package_no_subs.pl",
            lines      => 12,
            main_stats => {
                lines             => 12,
                mccabe_complexity => 3,
                name              => '{code not in named subroutines}',
                path              => "$test_directory/package_no_subs.pl",
            },
            subs => [

            ],
            packages => ['Hello::Dolly'],
        },
        'subs_no_package.pl' => {
            path       => "$test_directory/subs_no_package.pl",
            lines      => 8,
            main_stats => {
                lines             => 5,
                mccabe_complexity => 2,
                name              => '{code not in named subroutines}',
                path              => "$test_directory/subs_no_package.pl",
            },
            subs => [
                {
                    name              => 'foo',
                    lines             => 1,
                    mccabe_complexity => 1,
                    path              => "$test_directory/subs_no_package.pl",
                },
                {
                    name              => 'bar',
                    lines             => 2,
                    mccabe_complexity => 1,
                    path              => "$test_directory/subs_no_package.pl",
                }
            ],
            packages => [],
        },
        'Module.pm' => {
            path       => "$test_directory/Perl/Code/Analyze/Test/Module.pm",
            lines      => 29,
            main_stats => {
                lines             => 6,
                mccabe_complexity => 1,
                name              => '{code not in named subroutines}',
                path => "$test_directory/Perl/Code/Analyze/Test/Module.pm",
            },
            subs => [
                {
                    name              => 'new',
                    lines             => 5,
                    mccabe_complexity => 1,
                    path => "$test_directory/Perl/Code/Analyze/Test/Module.pm",
                },
                {
                    name              => 'foo',
                    lines             => 9,
                    mccabe_complexity => 8,
                    path => "$test_directory/Perl/Code/Analyze/Test/Module.pm",
                },
                {
                    name              => 'say_hello',
                    lines             => 9,
                    mccabe_complexity => 5,
                    path => "$test_directory/Perl/Code/Analyze/Test/Module.pm",
                },
            ],
            packages => [
                'Perl::Metrics::Simple::Test::Module',
                'Perl::Metrics::Simple::Test::Module::InnerClass'
            ],
        },
        'Moose.pm' => {
            path       => "$test_directory/Perl/Code/Analyze/Test/Moose.pm",
            lines      => 9,
            main_stats => {
                lines             => 3,
                mccabe_complexity => 1,
                name              => '{code not in named subroutines}',
                path => "$test_directory/Perl/Code/Analyze/Test/Moose.pm",
            },
            subs => [
                {
                    name              => 'foo',
                    lines             => 3,
                    mccabe_complexity => 1,
                    path => "$test_directory/Perl/Code/Analyze/Test/Moose.pm",
                },
                {
                    name              => '_after_bar',
                    lines             => 3,
                    mccabe_complexity => 1,
                    path => "$test_directory/Perl/Code/Analyze/Test/Moose.pm",
                },
            ],
            packages => [
                'Perl::Metrics::Simple::Test::Moose',
            ],
        },
      },
      'Perl::Metrics::Simple::Analysis';
    return $test_data;
}
1;
__END__