lib/App/Test/Generator/Analyzer/ReturnMeta.pm

filter: Statementstmt 100% 54 / 54 Branchbran 86.3% 19 / 22 Conditioncond 100% 13 / 13 MC/DCmcdc 100% 7 / 7 Subroutinesub 100% 6 / 6 Podpod 100% 2 / 2 totaltotal 97.1% 101 / 104 CC 17 SCAR 25.9
CC
17
Cov
97%
analyze
17.0
BEGIN
1.0
BEGIN
1.0
CRAP 13.4
« lib/App/Test/Generator/Planner/Isolation.pm ↑ Summary blib/lib/App/Test/Generator/BenchmarkGenerator.pm »
1 package App::Test::Generator::Analyzer::ReturnMeta;
2  
3 161 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 161 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 161 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 161 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # --------------------------------------------------
9 # Scoring penalties and bonuses applied to stability
10 # and consistency based on detected return patterns.
11 # Scores are clamped to [0, 100] after all adjustments.
12 # --------------------------------------------------
13 161 Readonly my $PENALTY_CONTEXT_SENSITIVE_STABILITY => 25;
14 161 Readonly my $PENALTY_CONTEXT_SENSITIVE_CONSISTENCY => 15;
15 161 Readonly my $PENALTY_MIXED_RETURN_CONSISTENCY => 30;
16 161 Readonly my $PENALTY_IMPLICIT_UNDEF_STABILITY => 20;
17 161 Readonly my $PENALTY_EXPLICIT_UNDEF_STABILITY => 10;
18 161 Readonly my $PENALTY_EMPTY_LIST_CONSISTENCY => 15;
19 161 Readonly my $PENALTY_EXCEPTION_SWALLOW_STABILITY => 20;
20 161 Readonly my $BONUS_BOOLEAN_STABILITY => 5;
21  
22 161 our $VERSION = '0.50';
23  
24 =head1 VERSION
25  
26 Version 0.50
27  
28 =head1 DESCRIPTION
29  
30 Analyses the return metadata of a schema's output section and produces
31 stability and consistency scores along with a list of risk flags. This
32 is used by L<App::Test::Generator> to assess how reliably a function's
33 return value can be tested.
34  
35 =head2 new
36  
37 Construct a new ReturnMeta analyser.
38  
39 my $analyser = App::Test::Generator::Analyzer::ReturnMeta->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::Analyzer::ReturnMeta',
60 }
61  
62 =cut
63  
64 801 ▶sub new { bless {}, shift }
Subroutine new: called CC=1 SCAR=0.0
Pod documented
65  
66 =head2 analyze
67  
68 Analyse the C<output> section of a schema hashref and return a scoring
69 report covering stability, consistency, and risk flags.
70  
71 my $analyser = App::Test::Generator::Analyzer::ReturnMeta->new;
72 my $report = $analyser->analyze($schema);
73  
74 printf "Stability: %d\n", $report->{stability_score};
75 printf "Consistency: %d\n", $report->{consistency_score};
76 printf "Risks: %s\n", join(', ', @{ $report->{risk_flags} });
77  
78 =head3 Arguments
79  
80 =over 4
81  
82 =item * C<$schema>
83  
84 A hashref that may include an C<output> key containing return metadata.
85 The C<output> hashref may include any of the following keys:
86  
87 =over 4
88  
89 =item C<_context_aware> - true if the function returns differently in
90 list vs scalar context.
91  
92 =item C<_returns_self> - true if the function returns C<$self>.
93  
94 =item C<type> - the declared return type string e.g. C<object>,
95 C<boolean>, C<string>.
96  
97 =item C<_error_handling> - a hashref with boolean keys C<implicit_undef>,
98 C<empty_list>, and C<exception_handling>.
99  
100 =item C<_error_return> - the value returned on error e.g. C<undef>.
101  
102 =back
103  
104 =back
105  
106 =head3 Returns
107  
108 A hashref with three keys:
109  
110 =over 4
111  
112 =item * C<stability_score> - integer in [0, 100]. Higher is more stable.
113  
114 =item * C<consistency_score> - integer in [0, 100]. Higher is more
115 consistent.
116  
117 =item * C<risk_flags> - arrayref of string risk identifiers detected
118 during analysis.
119  
120 =back
121  
122 =head3 Notes
123  
124 Both scores start at 100 and are reduced by penalties for each detected
125 risk pattern. A small bonus is applied to stability for boolean return
126 types, but since C<stability_score> starts at 100 and is clamped to
127 [0, 100] after all adjustments, this bonus is a no-op unless an earlier
128 penalty has already reduced the score below 100 - for a boolean-returning
129 function with no other detected risk, C<stability_score> is unaffected
130 by C<$BONUS_BOOLEAN_STABILITY> and remains 100.
131  
132 =head3 API specification
133  
134 =head4 input
135  
136 {
137 self => { type => OBJECT, isa => 'App::Test::Generator::Analyzer::ReturnMeta' },
138 schema => { type => HASHREF },
139 }
140  
141 =head4 output
142  
143 {
144 type => HASHREF,
145 keys => {
146 stability_score => { type => SCALAR },
147 consistency_score => { type => SCALAR },
148 risk_flags => { type => ARRAYREF },
149 },
150 }
151  
152 =cut
153  
154 806 ▶sub analyze {
Subroutine analyze: called CC=17 SCAR=28.3
Pod documented
155 806 my ($self, $schema) = @_;
156  
157 806 ▶ my $output = $schema->{output} || {};
Condition100%2/2
$schema->{'output'} || {}
l!l
7988
MC/DC100%1/1
$schema->{'output'} || {}
$schema->{'output'}
Truth table100%2/2
A
A= $schema->{'output'}
Aresult
11
00
158 806 my @risk;
159 806 my $stability = 100;
160 806 my $consistency = 100;
161  
162 # --------------------------------------------------
163 # Context sensitivity — function returns differently
164 # in list vs scalar context, making it harder to test
165 # predictably
166 # --------------------------------------------------
167 806 ▶ if($output->{_context_aware}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output->{"_context_aware"}) 12 794 806
168 12 push @risk, 'context_sensitive';
169 12 $stability -= $PENALTY_CONTEXT_SENSITIVE_STABILITY;
170 12 $consistency -= $PENALTY_CONTEXT_SENSITIVE_CONSISTENCY;
171 }
172  
173 # --------------------------------------------------
174 # Mixed return types — function claims to return self
175 # but is not typed as object, suggesting inconsistent
176 # return paths
177 # --------------------------------------------------
178 806 ▶ if($output->{_returns_self} && ($output->{type} // '') ne 'object') {
Branch: 1 branch
BranchTrueFalseTotal
if ($output->{"_returns_self"} and ($output->{"type"} // "") ne "object") 4 802 806
Condition100%5/5
$output->{"_returns_self"} and ($output->{"type"} // "") ne "object"
!ll&&!rl&&r
790124
$output->{"type"} // ""
l!l
133
MC/DC100%2/2
$output->{"_returns_self"} and ($output->{"type"} // "") ne "object"
$output->{"_returns_self"} $output->{"type"}
Truth table100%3/3
A and B
A= $output->{"_returns_self"}B= $output->{"type"}
ABresult
0X0
100
111
179 4 push @risk, 'mixed_return_types';
180 4 $consistency -= $PENALTY_MIXED_RETURN_CONSISTENCY;
181 }
182  
183 # --------------------------------------------------
184 # Implicit undef returns — function falls off the end
185 # without an explicit return, making error paths hard
186 # to distinguish from successful empty returns
187 # --------------------------------------------------
188 806 ▶ if($output->{_error_handling}{implicit_undef}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output->{"_error_handling"}{"implicit_undef"}) 15 791 806
189 15 push @risk, 'implicit_error_return';
190 15 $stability -= $PENALTY_IMPLICIT_UNDEF_STABILITY;
191 }
192  
193 # --------------------------------------------------
194 # Explicit undef on error — function explicitly returns
195 # undef on failure; lower penalty than implicit since
196 # the intent is at least documented in the code
197 # --------------------------------------------------
198 806 ▶ if($output->{_error_return} && $output->{_error_return} eq 'undef') {
Branch: 1 branch
BranchTrueFalseTotal
if ($output->{"_error_return"} and $output->{"_error_return"} eq "undef") 19 787 806
Condition100%3/3
$output->{"_error_return"} and $output->{"_error_return"} eq "undef"
!ll&&!rl&&r
7681919
MC/DC100%2/2
$output->{"_error_return"} and $output->{"_error_return"} eq "undef"
$output->{"_error_return"} $output->{"_error_return"} eq "undef"
Truth table100%3/3
A and B
A= $output->{"_error_return"}B= $output->{"_error_return"} eq "undef"
ABresult
0X0
100
111
199 19 push @risk, 'undef_on_error';
200 19 $stability -= $PENALTY_EXPLICIT_UNDEF_STABILITY;
201 }
202  
203 # --------------------------------------------------
204 # Empty list error pattern — function returns () on
205 # error, which is indistinguishable from a successful
206 # call that found no results
207 # --------------------------------------------------
208 806 ▶ if($output->{_error_handling}{empty_list}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output->{"_error_handling"}{"empty_list"}) 5 801 806
209 5 push @risk, 'empty_list_error';
210 5 $consistency -= $PENALTY_EMPTY_LIST_CONSISTENCY;
211 }
212  
213 # --------------------------------------------------
214 # Exception swallowing — function catches exceptions
215 # without rethrowing, hiding failures from the caller
216 # --------------------------------------------------
217 806 ▶ if($output->{_error_handling}{exception_handling}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output->{"_error_handling"}{"exception_handling"}) 22 784 806
218 22 push @risk, 'exception_swallowing';
219 22 $stability -= $PENALTY_EXCEPTION_SWALLOW_STABILITY;
220 }
221  
222 # --------------------------------------------------
223 # Boolean return bonus — boolean returns are the most
224 # predictable and easiest to assert, so a small boost
225 # is applied. Only has effect if stability was already
226 # reduced below 95 by earlier penalties.
227 # --------------------------------------------------
228 806 ▶ if($output->{type} && $output->{type} eq 'boolean') {
Branch: 1 branch
BranchTrueFalseTotal
if ($output->{"type"} and $output->{"type"} eq "boolean") 65 741 806
Condition100%3/3
$output->{"type"} and $output->{"type"} eq "boolean"
!ll&&!rl&&r
5668565
MC/DC100%2/2
$output->{"type"} and $output->{"type"} eq "boolean"
$output->{"type"} $output->{"type"} eq "boolean"
Truth table100%3/3
A and B
A= $output->{"type"}B= $output->{"type"} eq "boolean"
ABresult
0X0
100
111
229 65 $stability += $BONUS_BOOLEAN_STABILITY;
230 }
231  
232 # Clamp both scores to the valid [0, 100] range
233 806 ▶ $stability = 0 if $stability < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $stability < 0 0 806 806
234 806 ▶ $stability = 100 if $stability > 100;
Branch: 1 branch
BranchTrueFalseTotal
if $stability > 100 59 747 806
235 806 ▶ $consistency = 0 if $consistency < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $consistency < 0 0 806 806
236 806 ▶ $consistency = 100 if $consistency > 100;
Branch: 1 branch
BranchTrueFalseTotal
if $consistency > 100 0 806 806
237  
238 return {
239 806 stability_score => $stability,
240 consistency_score => $consistency,
241 risk_flags => \@risk,
242 };
243 }
244  
245 161 1;
« lib/App/Test/Generator/Planner/Isolation.pm ↑ Summary blib/lib/App/Test/Generator/BenchmarkGenerator.pm »