lib/App/Test/Generator/Planner/Grouping.pm

filter: Statementstmt 100% 32 / 32 Branchbran 100% 8 / 8 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 6 / 6 Podpod 100% 2 / 2 totaltotal 100% 51 / 51 CC 7 SCAR 15.0
CC
7
Cov
100%
plan
7.0
BEGIN
1.0
BEGIN
1.0
CRAP 4.5
« lib/App/Test/Generator/Planner.pm ↑ Summary blib/lib/App/Test/Generator/LCSAJ.pm »
1 package App::Test::Generator::Planner::Grouping;
2  
3 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 19 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 19 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # --------------------------------------------------
9 # Purity level strings from Analyzer::SideEffect
10 # --------------------------------------------------
11 19 Readonly my $PURITY_PURE => 'pure';
12 19 Readonly my $PURITY_SELF_MUTATING => 'self_mutating';
13  
14 # --------------------------------------------------
15 # Group keys in the output plan.
16 # Note: self_mutating maps to 'mutating' in the
17 # group key to keep the output API concise.
18 # --------------------------------------------------
19 19 Readonly my $GROUP_PURE => 'pure';
20 19 Readonly my $GROUP_MUTATING => 'mutating';
21 19 Readonly my $GROUP_IMPURE => 'impure';
22  
23 19 our $VERSION = '0.50';
24  
25 =head1 VERSION
26  
27 Version 0.50
28  
29 =head1 DESCRIPTION
30  
31 Groups methods by their purity level for more efficient test file
32 organisation. Pure methods can share setup; mutating methods need
33 per-test objects; impure methods need full isolation blocks.
34  
35 =head2 new
36  
37 Construct a new Grouping planner.
38  
39 my $planner = App::Test::Generator::Planner::Grouping->new;
40  
41 =head3 Arguments
42  
43 None.
44  
45 =head3 Returns
46  
47 A blessed hashref.
48  
49 =head3 API specification
50  
51 =head4 input
52  
53 {}
54  
55 =head4 output
56  
57 {
58 type => OBJECT,
59 isa => 'App::Test::Generator::Planner::Grouping',
60 }
61  
62 =cut
63  
64 34 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
65 34 my $class = $_[0];
66 34 return bless {}, $class;
67 }
68  
69 =head2 plan
70  
71 Group all methods in the schema by their purity level.
72  
73 my $planner = App::Test::Generator::Planner::Grouping->new;
74 my $groups = $planner->plan($schema);
75  
76 printf "Pure methods: %s\n", join(', ', @{ $groups->{pure} });
77 printf "Mutating methods:%s\n", join(', ', @{ $groups->{mutating} });
78 printf "Impure methods: %s\n", join(', ', @{ $groups->{impure} });
79  
80 =head3 Arguments
81  
82 =over 4
83  
84 =item * C<$schema>
85  
86 A hashref of method schemas each optionally containing a C<_analysis>
87 key with a C<side_effects> sub-key as produced by
88 L<App::Test::Generator::Analyzer::SideEffect>.
89  
90 =back
91  
92 =head3 Returns
93  
94 A hashref with three keys - C<pure>, C<mutating>, and C<impure> -
95 each containing an arrayref of method names assigned to that group.
96 Methods without purity metadata are placed in C<impure> by default.
97  
98 Note: the purity level C<self_mutating> from
99 L<App::Test::Generator::Analyzer::SideEffect> is mapped to the
100 C<mutating> group key in this output.
101  
102 =head3 API specification
103  
104 =head4 input
105  
106 {
107 self => { type => OBJECT, isa => 'App::Test::Generator::Planner::Grouping' },
108 schema => { type => HASHREF },
109 }
110  
111 =head4 output
112  
113 {
114 type => HASHREF,
115 keys => {
116 pure => { type => ARRAYREF },
117 mutating => { type => ARRAYREF },
118 impure => { type => ARRAYREF },
119 },
120 }
121  
122 =cut
123  
124 34 ▶sub plan {
Subroutine plan: called CC=7 SCAR=19.5
Pod documented
125 34 my ($self, $schema) = @_;
126  
127 # Validate that schema is a hashref before iterating
128 34 ▶ croak 'schema must be a hashref'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $schema eq "HASH" 7 27 34
129 unless ref($schema) eq 'HASH';
130  
131 # Initialise all three groups so the output always
132 # has all three keys even if some groups are empty
133 27 my %groups = (
134 $GROUP_PURE => [],
135 $GROUP_MUTATING => [],
136 $GROUP_IMPURE => [],
137 );
138  
139 27 for my $method (keys %{$schema}) {
140 30 ▶ next unless ref($schema->{$method}) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $schema->{$method} eq "HASH" 2 28 30
141 # Default to empty string if purity_level is absent —
142 # missing metadata falls through to the impure group
143 28 ▶ my $level = $schema->{$method}{_analysis}{side_effects}{purity_level} // '';
Condition100%2/2
$schema->{$method}{"_analysis"}{"side_effects"}{"purity_level"} // ""
l!l
217
MC/DC100%1/1
$schema->{$method}{"_analysis"}{"side_effects"}{"purity_level"} // ""
$schema->{$method}{"_analysis"}{"side_effects"}{"purity_level"}
Truth table100%2/2
A
A= $schema->{$method}{"_analysis"}{"side_effects"}{"purity_level"}
Aresult
11
00
144  
145 # Map purity level to group key — self_mutating becomes
146 # 'mutating' to keep the output API concise
147 28 ▶ my $group =
Branch: 2 branches
BranchTrueFalseTotal
$level eq $PURITY_PURE ? : 12 16 28
$level eq $PURITY_SELF_MUTATING ? : 5 11 16
148 $level eq $PURITY_PURE ? $GROUP_PURE :
149 $level eq $PURITY_SELF_MUTATING ? $GROUP_MUTATING :
150 $GROUP_IMPURE;
151  
152 28 push @{ $groups{$group} }, $method;
153 }
154  
155 27 return \%groups;
156 }
157  
158 19 1;
« lib/App/Test/Generator/Planner.pm ↑ Summary blib/lib/App/Test/Generator/LCSAJ.pm »