lib/App/Test/Generator/TestStrategy.pm

filter: Statementstmt 100% 73 / 73 Branchbran 96.4% 27 / 28 Conditioncond 79.4% 27 / 34 MC/DCmcdc 58.8% 10 / 17 Subroutinesub 100% 6 / 6 Podpod 100% 2 / 2 totaltotal 90.6% 145 / 160 CC 31 SCAR 31.2
CC
31
Cov
94%
_plan_for_method
28.5
new
3.0
generate_plan
2.0
CRAP 22.6
« blib/lib/App/Test/Generator/SchemaExtractor.pm ↑ Summary lib/App/Test/Generator/Planner/Isolation.pm »
1 package App::Test::Generator::TestStrategy;
2  
3 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 17 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # --------------------------------------------------
8 # Accessor type strings from the schema
9 # --------------------------------------------------
10 17 Readonly my $ACCESSOR_GETTER => 'getter';
11 17 Readonly my $ACCESSOR_SETTER => 'setter';
12 17 Readonly my $ACCESSOR_GETSET => 'getset';
13  
14 # --------------------------------------------------
15 # Output type strings from the schema
16 # --------------------------------------------------
17 17 Readonly my $TYPE_BOOLEAN => 'boolean';
18 17 Readonly my $TYPE_OBJECT => 'object';
19 17 Readonly my $TYPE_VOID => 'void';
20  
21 # --------------------------------------------------
22 # Default confidence threshold for plan generation
23 # --------------------------------------------------
24 17 Readonly my $DEFAULT_CONFIDENCE => 'medium';
25  
26 # --------------------------------------------------
27 # Test plan flag keys written to the method plan
28 # --------------------------------------------------
29 17 Readonly my $TEST_CONTEXT => 'context_tests';
30 17 Readonly my $TEST_PREDICATE => 'predicate_test';
31 17 Readonly my $TEST_GETTER => 'getter_test';
32 17 Readonly my $TEST_SETTER => 'setter_test';
33 17 Readonly my $TEST_GETSET => 'getset_test';
34 17 Readonly my $TEST_OBJECT_INJECT => 'object_injection_test';
35 17 Readonly my $TEST_BOOLEAN_SET => 'boolean_set_test';
36 17 Readonly my $TEST_VOID => 'void_context_test';
37 17 Readonly my $TEST_ERROR_HANDLING => 'error_handling_test';
38 17 Readonly my $TEST_BOUNDARY => 'boundary_tests';
39 17 Readonly my $TEST_CHAINING => 'chaining_test';
40 17 Readonly my $TEST_BASIC => 'basic_test';
41  
42 17 our $VERSION = '0.50';
43  
44 =head1 VERSION
45  
46 Version 0.50
47  
48 =head1 DESCRIPTION
49  
50 Generates a test strategy plan for all methods in a schema, determining
51 which test types should be produced for each method based on its
52 accessor classification, output type, and other metadata. Side-effect
53 and dependency-driven planning (mocking, isolation) is handled
54 separately by L<App::Test::Generator::Planner::Mock> and
55 L<App::Test::Generator::Planner::Isolation>.
56  
57 This way a package's API can be automatically tested.
58  
59 =head2 new
60  
61 Construct a new TestStrategy.
62  
63 my $strategy = App::Test::Generator::TestStrategy->new(
64 schema => \%schemas,
65 thresholds => { confidence => 'high' },
66 );
67  
68 =head3 Arguments
69  
70 =over 4
71  
72 =item * C<schema>
73  
74 A hashref of method name to schema hashref
75 Optional - defaults to
76 an empty hashref.
77  
78 =item * C<thresholds>
79  
80 A hashref of threshold configuration.
81 Optional - defaults to
82 C<< { confidence => 'medium' } >>.
83  
84 =back
85  
86 =head3 Returns
87  
88 A blessed hashref.
89  
90 =head3 API specification
91  
92 =head4 input
93  
94 {
95 schema => { type => 'hashref', optional => 1 },
96 thresholds => { type => 'hashref', optional => 1 },
97 }
98  
99 =head4 output
100  
101 {
102 type => OBJECT,
103 isa => 'App::Test::Generator::TestStrategy',
104 }
105  
106 =cut
107  
108 38 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
109 38 my ($class, %args) = @_;
110 return bless {
111 schema => $args{schema} || {},
112 38 ▶ thresholds => $args{thresholds} || { confidence => $DEFAULT_CONFIDENCE },
Condition100%4/4
$args{'schema'} || {}
l!l
2810
$args{'thresholds'} || {'confidence', $DEFAULT_CONFIDENCE}
l!l
335
MC/DC100%1/1
$args{'schema'} || {}
$args{'schema'}
MC/DC100%1/1
$args{'thresholds'} || {'confidence', $DEFAULT_CONFIDENCE}
$args{'thresholds'}
Truth table100%2/2
A
A= $args{'schema'}
Aresult
11
00
Truth table100%2/2
A
A= $args{'thresholds'}
Aresult
11
00
113 plans => {},
114 }, $class;
115 }
116  
117 =head2 generate_plan
118  
119 Generate a test plan for all methods in the schema and return it as
120 a hashref mapping method names to plan hashrefs.
121  
122 my $strategy = App::Test::Generator::TestStrategy->new(
123 schema => \%schemas,
124 );
125 my $plan = $strategy->generate_plan;
126  
127 for my $method (keys %{$plan}) {
128 print "$method: ", join(', ', keys %{ $plan->{$method} }), "\n";
129 }
130  
131 =head3 Arguments
132  
133 None beyond C<$self>.
134  
135 =head3 Returns
136  
137 A hashref mapping method names to test plan hashrefs, each containing
138 boolean flags for the test types that should be generated.
139  
140 =head3 API specification
141  
142 =head4 input
143  
144 {
145 self => { type => OBJECT, isa => 'App::Test::Generator::TestStrategy' },
146 }
147  
148 =head4 output
149  
150 {
151 type => 'hashref',
152 keys => {
153 '*' => { type => 'hashref' },
154 },
155 }
156  
157 =cut
158  
159 28 ▶sub generate_plan {
Subroutine generate_plan: called CC=2 SCAR=6.9
Pod documented
160 28 my $self = $_[0];
161  
162 28 for my $method (keys %{ $self->{schema} }) {
163 24 my $schema = $self->{schema}{$method};
164  
165 # Generate and store the plan for this method
166 24 $self->{plans}{$method} = $self->_plan_for_method($schema);
167 }
168  
169 27 return $self->{plans};
170 }
171  
172 # --------------------------------------------------
173 # _plan_for_method
174 #
175 # Determine which test types should be
176 # generated for a single method based on
177 # its schema metadata.
178 #
179 # Entry: $schema - the per-method schema hashref
180 #
181 # Exit: Returns a hashref of test type flags.
182 # Always contains at least basic_test => 1.
183 #
184 # Side effects: None.
185 #
186 # Notes: All string comparisons use // '' guards
187 # to avoid uninitialized value warnings
188 # when schema fields are absent.
189 # --------------------------------------------------
190 25 ▶sub _plan_for_method {
Subroutine _plan_for_method: called CC=28 SCAR=33.5
191 25 my ($self, $schema) = @_;
192  
193 25 my %plan;
194  
195 # --------------------------------------------------
196 # Context-aware returns need both scalar and list
197 # context tests to verify correct behaviour in each
198 # --------------------------------------------------
199 25 ▶ if($schema->{output}{_context_aware}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"output"}{"_context_aware"}) 1 23 24
200 1 $plan{$TEST_CONTEXT} = 1;
201 }
202  
203 # --------------------------------------------------
204 # Accessor detection — choose test types based on
205 # whether the method is a getter, setter, or both
206 # --------------------------------------------------
207 24 ▶ if($schema->{accessor} && scalar keys %{ $schema->{accessor} }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"accessor"} and scalar keys %{$$schema{"accessor"};}) 10 14 24
Condition100%2/2
$schema->{"accessor"} and scalar keys %{$$schema{"accessor"};}
!ll
1410
MC/DC100%1/1
$schema->{"accessor"} and scalar keys %{$$schema{"accessor"};}
$schema->{"accessor"}
Truth table100%2/2
A
A= $schema->{"accessor"}
Aresult
00
11
208 10 ▶ my $acc_type = $schema->{accessor}{type} // '';
Condition50%1/2
$schema->{"accessor"}{"type"} // ""
l!l
100
MC/DC0%0/1
$schema->{"accessor"}{"type"} // ""
$schema->{"accessor"}{"type"}
Truth table50%1/2
A
A= $schema->{"accessor"}{"type"}
Aresult
11
00
209  
210 10 ▶ if($acc_type eq $ACCESSOR_GETTER) {
Branch: 3 branches
BranchTrueFalseTotal
if ($acc_type eq $ACCESSOR_GETTER) { } 3 7 10
elsif ($acc_type eq $ACCESSOR_SETTER) { } 1 6 7
elsif ($acc_type eq $ACCESSOR_GETSET) { } 6 0 6
211 # Boolean getters are predicates and need
212 # truthy/falsy tests in addition to getter tests
213 3 ▶ if(($schema->{output}{type} // '') eq $TYPE_BOOLEAN) {
Branch: 1 branch
BranchTrueFalseTotal
if (($schema->{"output"}{"type"} // "") eq $TYPE_BOOLEAN) 1 2 3
Condition100%2/2
$schema->{"output"}{"type"} // ""
l!l
21
MC/DC100%1/1
$schema->{"output"}{"type"} // ""
$schema->{"output"}{"type"}
Truth table100%2/2
A
A= $schema->{"output"}{"type"}
Aresult
11
00
214 1 $plan{$TEST_PREDICATE} = 1;
215 }
216 3 $plan{$TEST_GETTER} = 1;
217  
218 } elsif($acc_type eq $ACCESSOR_SETTER) {
219 1 $plan{$TEST_SETTER} = 1;
220  
221 } elsif($acc_type eq $ACCESSOR_GETSET) {
222 # For getset accessors, check the input parameter
223 # type to determine if object injection or boolean
224 # set tests are more appropriate. Sort by position (keys
225 # %hash has no defined order) so the choice is deterministic
226 # if more than one candidate is present.
227 6 ▶ my $input = $schema->{input} || {};
Condition50%1/2
$schema->{'input'} || {}
l!l
60
MC/DC0%0/1
$schema->{'input'} || {}
$schema->{'input'}
Truth table50%1/2
A
A= $schema->{'input'}
Aresult
11
00
228 my ($param) = sort {
229 2 ▶ ($input->{$a}{position} // 9999) <=> ($input->{$b}{position} // 9999)
Condition85%6/7
($input->{$a}{'position'} // 9999) <=> ($input->{$b}{'position'} // 9999) || $a cmp $b
l!l&&r!l&&!r
110
$input->{$a}{"position"} // 9999
l!l
11
$input->{$b}{"position"} // 9999
l!l
11
MC/DC0%0/2
($input->{$a}{'position'} // 9999) <=> ($input->{$b}{'position'} // 9999) || $a cmp $b
$input->{$a}{"position"} $a cmp $b
MC/DC100%1/1
$input->{$b}{"position"} // 9999
$input->{$b}{"position"}
Truth table66%2/3
A || B
A= $input->{$a}{"position"}B= $a cmp $b
ABresult
1X1
011
000
Truth table100%2/2
A
A= $input->{$b}{"position"}
Aresult
11
00
230 || $a cmp $b
231 6 } grep { !/^_/ } keys %{ $input };
232 6 ▶ my $param_type = ($param && $input->{$param}{type}) // '';
Condition40%2/5
$param && $input->{$param}{"type"} // ""
l!l
60
$param and $input->{$param}{"type"}
!ll&&!rl&&r
006
MC/DC0%0/2
$param && $input->{$param}{"type"} // ""
$param $input->{$param}{"type"}
Truth table33%1/3
A and B
A= $paramB= $input->{$param}{"type"}
ABresult
111
0X0
100
233  
234 6 ▶ if($param_type eq $TYPE_OBJECT) {
Branch: 2 branches
BranchTrueFalseTotal
if ($param_type eq $TYPE_OBJECT) { } 1 5 6
elsif ($param_type eq $TYPE_BOOLEAN) { } 4 1 5
235 1 $plan{$TEST_OBJECT_INJECT} = 1;
236 } elsif($param_type eq $TYPE_BOOLEAN) {
237 4 $plan{$TEST_BOOLEAN_SET} = 1;
238 }
239 6 $plan{$TEST_GETSET} = 1;
240 }
241 }
242  
243 # --------------------------------------------------
244 # Void return type — verify the method returns nothing
245 # and does not accidentally return a useful value
246 # --------------------------------------------------
247 24 ▶ if(($schema->{output}{type} // '') eq $TYPE_VOID) {
Branch: 1 branch
BranchTrueFalseTotal
if (($schema->{"output"}{"type"} // "") eq $TYPE_VOID) 2 22 24
Condition100%2/2
$schema->{"output"}{"type"} // ""
l!l
717
MC/DC100%1/1
$schema->{"output"}{"type"} // ""
$schema->{"output"}{"type"}
Truth table100%2/2
A
A= $schema->{"output"}{"type"}
Aresult
11
00
248 2 $plan{$TEST_VOID} = 1;
249 }
250  
251 # --------------------------------------------------
252 # Error handling — verify error return conventions
253 # are tested explicitly
254 # --------------------------------------------------
255 24 ▶ if($schema->{output}{_error_return}
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"output"}{"_error_return"} or $schema->{"output"}{"success_failure_pattern"}) 2 22 24
Condition100%3/3
$schema->{"output"}{"_error_return"} or $schema->{"output"}{"success_failure_pattern"}
l!l&&r!l&&!r
1122
MC/DC100%2/2
$schema->{"output"}{"_error_return"} or $schema->{"output"}{"success_failure_pattern"}
$schema->{"output"}{"_error_return"} $schema->{"output"}{"success_failure_pattern"}
Truth table100%3/3
A or B
A= $schema->{"output"}{"_error_return"}B= $schema->{"output"}{"success_failure_pattern"}
ABresult
1X1
011
000
256 || $schema->{output}{success_failure_pattern}) {
257 2 $plan{$TEST_ERROR_HANDLING} = 1;
258 }
259  
260 # --------------------------------------------------
261 # Boundary hints from YAML test configuration —
262 # generate boundary/equivalence class tests
263 # --------------------------------------------------
264 24 ▶ if($schema->{_yamltest_hints} && keys %{ $schema->{_yamltest_hints} }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"_yamltest_hints"} and keys %{$$schema{"_yamltest_hints"};}) 1 23 24
Condition66%2/3
$schema->{"_yamltest_hints"} and keys %{$$schema{"_yamltest_hints"};}
!ll&&!rl&&r
2301
MC/DC50%1/2
$schema->{"_yamltest_hints"} and keys %{$$schema{"_yamltest_hints"};}
$schema->{"_yamltest_hints"} keys %{$$schema{"_yamltest_hints"};}
Truth table66%2/3
A and B
A= $schema->{"_yamltest_hints"}B= keys %{$$schema{"_yamltest_hints"};}
ABresult
0X0
100
111
265 1 $plan{$TEST_BOUNDARY} = 1;
266 }
267  
268 # --------------------------------------------------
269 # Method chaining — verify that $self is returned
270 # and that calls can be chained
271 # --------------------------------------------------
272 24 ▶ if($schema->{output}{_returns_self}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"output"}{"_returns_self"}) 1 23 24
273 1 $plan{$TEST_CHAINING} = 1;
274 }
275  
276 # --------------------------------------------------
277 # Boolean output — needs predicate tests regardless
278 # of whether an accessor was detected
279 # --------------------------------------------------
280 24 ▶ if(($schema->{output}{type} // '') eq $TYPE_BOOLEAN) {
Branch: 1 branch
BranchTrueFalseTotal
if (($schema->{"output"}{"type"} // "") eq $TYPE_BOOLEAN) 2 22 24
Condition100%2/2
$schema->{"output"}{"type"} // ""
l!l
717
MC/DC100%1/1
$schema->{"output"}{"type"} // ""
$schema->{"output"}{"type"}
Truth table100%2/2
A
A= $schema->{"output"}{"type"}
Aresult
11
00
281 2 $plan{$TEST_PREDICATE} = 1;
282 }
283  
284 # --------------------------------------------------
285 # Always generate at least a basic call test even
286 # if no other test types were identified
287 # --------------------------------------------------
288 24 ▶ $plan{$TEST_BASIC} = 1 unless %plan;
Branch: 1 branch
BranchTrueFalseTotal
unless %plan 6 18 24
289  
290 24 return \%plan;
291 }
292  
293 =head1 SUPPORT
294  
295 This module is provided as-is without any warranty.
296  
297 =head1 LICENCE AND COPYRIGHT
298  
299 Copyright 2025-2026 Nigel Horne.
300  
301 Usage is subject to the terms of GPL2.
302 If you use it,
303 please let me know.
304  
305 =cut
306  
307 17 1;
« blib/lib/App/Test/Generator/SchemaExtractor.pm ↑ Summary lib/App/Test/Generator/Planner/Isolation.pm »