lib/App/Test/Generator/Sample/Module.pm

filter: Statementstmt 100% 67 / 67 Branchbran 95.6% 44 / 46 Conditioncond 90.9% 10 / 11 MC/DCmcdc 85.7% 6 / 7 Subroutinesub 100% 13 / 13 Podpod 100% 9 / 9 totaltotal 97.3% 149 / 153 CC 29 SCAR 16.1
CC
29
Cov
98%
greet
8.0
validate_score
6.0
process_names
6.0
CRAP 5.0
« lib/App/Test/Generator/Planner/Fixture.pm ↑ Summary bin/test-generator-mutate »
1 package App::Test::Generator::Sample::Module;
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 Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 17 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 17 our $VERSION = '0.49';
9  
10 # --------------------------------------------------
11 # Validation constants — centralised so that changes
12 # to limits only need to be made in one place
13 # --------------------------------------------------
14 17 Readonly my $MIN_EMAIL_LEN => 5;
15 17 Readonly my $MAX_EMAIL_LEN => 254;
16 17 Readonly my $MIN_BIRTH_YEAR => 1900;
17 17 Readonly my $MIN_NAME_LEN => 1;
18 17 Readonly my $MAX_NAME_LEN => 50;
19 17 Readonly my $MIN_SCORE => 0.0;
20 17 Readonly my $MAX_SCORE => 100.0;
21 17 Readonly my $PASS_THRESHOLD => 60.0;
22  
23 =head1 NAME
24  
25 App::Test::Generator::Sample::Module - Example module for schema extraction testing
26  
27 =head1 VERSION
28  
29 Version 0.49
30  
31 =head1 SYNOPSIS
32  
33 use App::Test::Generator::Sample::Module;
34  
35 my $obj = App::Test::Generator::Sample::Module->new();
36 my $result = $obj->validate_email('user@example.com');
37  
38 =head1 DESCRIPTION
39  
40 A sample module with a variety of well and poorly documented methods,
41 used to exercise L<App::Test::Generator::SchemaExtractor>. The methods
42 cover common parameter types, validation patterns, and confidence levels
43 so that the extractor's heuristics can be tested against known inputs.
44  
45 =head2 new
46  
47 Constructor. Returns a new instance.
48  
49 my $obj = App::Test::Generator::Sample::Module->new();
50  
51 =head3 Returns
52  
53 A blessed hashref.
54  
55 =head3 API specification
56  
57 =head4 input
58  
59 { class => { type => SCALAR } }
60  
61 =head4 output
62  
63 { type => OBJECT, isa => 'App::Test::Generator::Sample::Module' }
64  
65 =cut
66  
67 32 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
68 32 my $class = $_[0];
69  
70 # Bless an empty hashref into the calling class
71 32 return bless {}, $class;
72 }
73  
74 =head2 validate_email
75  
76 Validate an email address against basic structural rules.
77  
78 my $ok = $obj->validate_email('user@example.com');
79  
80 =head3 Arguments
81  
82 =over 4
83  
84 =item * C<$email>
85  
86 String (C<$MIN_EMAIL_LEN>-C<$MAX_EMAIL_LEN> chars). Required.
87  
88 =back
89  
90 =head3 Returns
91  
92 1 if the address is valid. Croaks on any validation failure.
93  
94 =head3 API specification
95  
96 =head4 input
97  
98 {
99 self => { type => OBJECT, isa => 'App::Test::Generator::Sample::Module' },
100 email => { type => SCALAR, min => 5, max => 254 },
101 }
102  
103 =head4 output
104  
105 { type => SCALAR }
106  
107 =cut
108  
109 1243 ▶sub validate_email {
Subroutine validate_email: called CC=5 SCAR=16.1
Pod documented
110 1243 my ($self, $email) = @_;
111  
112 # Presence check before length checks to give a clear error
113 1243 ▶ croak 'Email is required' unless defined $email;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $email 1 1242 1243
114 1242 ▶ croak 'Email too short' unless length($email) >= $MIN_EMAIL_LEN;
Branch: 1 branch
BranchTrueFalseTotal
unless length $email >= $MIN_EMAIL_LEN 633 609 1242
115 609 ▶ croak 'Email too long' unless length($email) <= $MAX_EMAIL_LEN;
Branch: 1 branch
BranchTrueFalseTotal
unless length $email <= $MAX_EMAIL_LEN 122 487 609
116  
117 # Basic structural check — one @ with non-empty local and domain parts
118 487 ▶ croak 'Invalid email format'
Branch: 1 branch
BranchTrueFalseTotal
unless $email =~ /^[^\@]+\@[^\@]+\.[^\@]+$/ 421 66 487
119 unless $email =~ /^[^@]+\@[^@]+\.[^@]+$/;
120  
121 66 return 1;
122 }
123  
124 =head2 calculate_age
125  
126 Calculate age in years from a birth year.
127  
128 my $age = $obj->calculate_age(1985);
129  
130 =head3 Arguments
131  
132 =over 4
133  
134 =item * C<$birth_year>
135  
136 A birth year value (C<$MIN_BIRTH_YEAR> to current year). Required.
137  
138 =back
139  
140 =head3 Returns
141  
142 Age in years as an integer.
143  
144 =head3 API specification
145  
146 =head4 input
147  
148 {
149 self => { type => OBJECT, isa => 'App::Test::Generator::Sample::Module' },
150 birth_year => { type => SCALAR, min => 1900 },
151 }
152  
153 =head4 output
154  
155 { type => SCALAR }
156  
157 =cut
158  
159 123 ▶sub calculate_age {
Subroutine calculate_age: called CC=5 SCAR=16.1
Pod documented
160 123 my ($self, $birth_year) = @_;
161  
162 # Get the current year from the system clock rather than using
163 # a hardcoded value that would become stale each year
164 123 my $current_year = (localtime)[5] + 1900;
165  
166 123 ▶ croak 'Birth year required' unless defined $birth_year;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $birth_year 1 122 123
167 122 ▶ croak 'Birth year must be a number' unless $birth_year =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $birth_year =~ /^\d+$/ 111 11 122
168  
169 # Upper bound is the current year — you cannot be born in the future
170 11 ▶ croak 'Birth year out of range'
Branch: 1 branch
BranchTrueFalseTotal
unless $birth_year >= $MIN_BIRTH_YEAR and $birth_year <= $current_year 4 7 11
Condition100%3/3
$birth_year >= $MIN_BIRTH_YEAR and $birth_year <= $current_year
!ll&&!rl&&r
227
MC/DC100%2/2
$birth_year >= $MIN_BIRTH_YEAR and $birth_year <= $current_year
$birth_year >= $MIN_BIRTH_YEAR $birth_year <= $current_year
Truth table100%3/3
A and B
A= $birth_year >= $MIN_BIRTH_YEARB= $birth_year <= $current_year
ABresult
0X0
100
111
171 unless $birth_year >= $MIN_BIRTH_YEAR && $birth_year <= $current_year;
172  
173 7 return $current_year - $birth_year;
174 }
175  
176 =head2 process_names
177  
178 Process a list of names and return the count of non-empty entries.
179  
180 my $count = $obj->process_names(['Alice', 'Bob', '']);
181  
182 =head3 Arguments
183  
184 =over 4
185  
186 =item * C<$names>
187  
188 Arrayref of name strings. Required.
189  
190 =back
191  
192 =head3 Returns
193  
194 Count of non-empty name strings as an integer.
195  
196 =head3 API specification
197  
198 =head4 input
199  
200 {
201 self => { type => OBJECT, isa => 'App::Test::Generator::Sample::Module' },
202 names => { type => ARRAYREF },
203 }
204  
205 =head4 output
206  
207 { type => SCALAR, min => 0 }
208  
209 =cut
210  
211 15 ▶sub process_names {
Subroutine process_names: called CC=6 SCAR=17.9
Pod documented
212 15 my ($self, $names) = @_;
213  
214 15 ▶ croak 'Names required' unless defined $names;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $names 1 14 15
215 14 ▶ croak 'Names must be an array reference' unless ref($names) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $names eq "ARRAY" 2 12 14
216  
217 # Count only non-empty name strings — undef and '' are skipped
218 12 my $count = 0;
219 12 for my $name (@{$names}) {
220 # Increment only for defined, non-empty entries
221 14 ▶ $count++ if defined($name) && length($name) > 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $name and length $name > 0 9 5 14
Condition100%3/3
defined $name and length $name > 0
!ll&&!rl&&r
239
MC/DC100%2/2
defined $name and length $name > 0
defined $name length $name > 0
Truth table100%3/3
A and B
A= defined $nameB= length $name > 0
ABresult
0X0
100
111
222 }
223  
224 12 return $count;
225 }
226  
227 =head2 set_config
228  
229 Store a configuration hashref on the object.
230  
231 $obj->set_config({ timeout => 30, retries => 3 });
232  
233 =head3 Arguments
234  
235 =over 4
236  
237 =item * C<$config>
238  
239 Hashref of configuration options. Required.
240  
241 =back
242  
243 =head3 Returns
244  
245 1 on success. Croaks if C<$config> is absent or not a hashref.
246  
247 =head3 API specification
248  
249 =head4 input
250  
251 {
252 self => { type => OBJECT, isa => 'App::Test::Generator::Sample::Module' },
253 config => { type => HASHREF },
254 }
255  
256 =head4 output
257  
258 { type => SCALAR }
259  
260 =cut
261  
262 8 ▶sub set_config {
Subroutine set_config: called CC=3 SCAR=11.0
Pod documented
263 8 my ($self, $config) = @_;
264  
265 8 ▶ croak 'Config required' unless defined $config;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $config 1 7 8
266 7 ▶ croak 'Config must be a hash reference' unless ref($config) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $config eq "HASH" 2 5 7
267  
268 # Store the config hashref directly — callers own the data
269 5 $self->{config} = $config;
270  
271 5 return 1;
272 }
273  
274 =head2 greet
275  
276 Generate a greeting message for a named person.
277  
278 my $msg = $obj->greet('Alice');
279 my $msg = $obj->greet('Alice', 'Good morning');
280  
281 =head3 Arguments
282  
283 =over 4
284  
285 =item * C<$name>
286  
287 String (C<$MIN_NAME_LEN>-C<$MAX_NAME_LEN> chars). Required.
288  
289 =item * C<$greeting>
290  
291 String. Optional - defaults to C<"Hello">.
292  
293 =back
294  
295 =head3 Returns
296  
297 Greeting string of the form C<"$greeting, $name!">.
298  
299 =head3 API specification
300  
301 =head4 input
302  
303 {
304 self => { type => OBJECT, isa => 'App::Test::Generator::Sample::Module' },
305 name => { type => SCALAR, min => 1, max => 50 },
306 greeting => { type => SCALAR, optional => 1 },
307 }
308  
309 =head4 output
310  
311 { type => SCALAR }
312  
313 =cut
314  
315 928 ▶sub greet {
Subroutine greet: called CC=8 SCAR=20.8
Pod documented
316 928 my ($self, $name, $greeting) = @_;
317  
318 928 ▶ croak 'Name is required' unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 1 927 928
319 927 ▶ croak 'Name must be a string' if ref($name);
Branch: 1 branch
BranchTrueFalseTotal
if ref $name 0 927 927
320 927 ▶ croak 'Name too short' unless length($name) >= $MIN_NAME_LEN;
Branch: 1 branch
BranchTrueFalseTotal
unless length $name >= $MIN_NAME_LEN 3 924 927
321 924 ▶ croak 'Name too long' unless length($name) <= $MAX_NAME_LEN;
Branch: 1 branch
BranchTrueFalseTotal
unless length $name <= $MAX_NAME_LEN 4 920 924
322  
323 920 ▶ if(defined $greeting) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $greeting) 505 415 920
324 505 ▶ croak 'Greeting must be a string' if ref($greeting);
Branch: 1 branch
BranchTrueFalseTotal
if ref $greeting 0 505 505
325 }
326  
327 # Apply default greeting when caller does not supply one
328 920 ▶ $greeting ||= 'Hello';
Condition100%2/2
$greeting ||= "Hello"
l!l
495425
MC/DC100%1/1
$greeting ||= "Hello"
$greeting
Truth table100%2/2
A
A= $greeting
Aresult
11
00
329  
330 920 return "$greeting, $name!";
331 }
332  
333 =head2 check_flag
334  
335 Return a normalised boolean for a flag value.
336  
337 my $result = $obj->check_flag(1); # returns 1
338 my $result = $obj->check_flag(0); # returns 0
339  
340 =head3 Arguments
341  
342 =over 4
343  
344 =item * C<$enabled>
345  
346 Boolean scalar.
347  
348 =back
349  
350 =head3 Returns
351  
352 1 if C<$enabled> is true, 0 otherwise.
353  
354 =head3 API specification
355  
356 =head4 input
357  
358 {
359 self => { type => OBJECT, isa => 'App::Test::Generator::Sample::Module' },
360 enabled => { type => 'any', optional => 1 },
361 }
362  
363 =head4 output
364  
365 { type => SCALAR }
366  
367 =cut
368  
369 15 ▶sub check_flag {
Subroutine check_flag: called CC=2 SCAR=6.9
Pod documented
370 15 my ($self, $enabled) = @_;
371  
372 # Normalise any truthy/falsy value to a strict 1 or 0
373 15 ▶ return $enabled ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$enabled ? : 12 3 15
374 }
375  
376 =head2 validate_score
377  
378 Validate a numeric test score and return a pass/fail string.
379  
380 my $status = $obj->validate_score(75.5); # returns 'Pass'
381 my $status = $obj->validate_score(45.0); # returns 'Fail'
382  
383 =head3 Arguments
384  
385 =over 4
386  
387 =item * C<$score>
388  
389 Number (C<$MIN_SCORE>-C<$MAX_SCORE>). Required.
390  
391 =back
392  
393 =head3 Returns
394  
395 The string C<'Pass'> if the score meets or exceeds C<$PASS_THRESHOLD>,
396 C<'Fail'> otherwise. Croaks on invalid input.
397  
398 =head3 API specification
399  
400 =head4 input
401  
402 {
403 self => { type => OBJECT, isa => 'App::Test::Generator::Sample::Module' },
404 score => { type => 'number', min => 0.0, max => 100.0 },
405 }
406  
407 =head4 output
408  
409 { type => SCALAR }
410  
411 =cut
412  
413 155 ▶sub validate_score {
Subroutine validate_score: called CC=6 SCAR=17.9
Pod documented
414 155 my ($self, $score) = @_;
415  
416 155 ▶ croak 'Score is required' unless defined $score;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $score 1 154 155
417  
418 # Accept integers, decimals, and values like '.5' but not '1.2.3'
419 154 ▶ croak 'Score must be numeric'
Branch: 1 branch
BranchTrueFalseTotal
unless $score =~ /^(?:\d+\.?\d*|\.\d+)$/ 5 149 154
420 unless $score =~ /^(?:\d+\.?\d*|\.\d+)$/;
421  
422 149 ▶ croak 'Score out of range'
Branch: 1 branch
BranchTrueFalseTotal
unless $score >= $MIN_SCORE and $score <= $MAX_SCORE 4 145 149
Condition66%2/3
$score >= $MIN_SCORE and $score <= $MAX_SCORE
!ll&&!rl&&r
04145
MC/DC50%1/2
$score >= $MIN_SCORE and $score <= $MAX_SCORE
$score >= $MIN_SCORE $score <= $MAX_SCORE
Truth table66%2/3
A and B
A= $score >= $MIN_SCOREB= $score <= $MAX_SCORE
ABresult
0X0
100
111
423 unless $score >= $MIN_SCORE && $score <= $MAX_SCORE;
424  
425 # Compare against the pass threshold constant
426 145 ▶ return $score >= $PASS_THRESHOLD ? 'Pass' : 'Fail';
Branch: 1 branch
BranchTrueFalseTotal
$score >= $PASS_THRESHOLD ? : 57 88 145
427 }
428  
429 =head2 mysterious_method
430  
431 A deliberately under-validated method used to test that
432 L<App::Test::Generator::SchemaExtractor> correctly assigns low
433 confidence when validation is absent.
434  
435 =head3 Arguments
436  
437 =over 4
438  
439 =item * C<$thing>
440  
441 A value to double. No type validation is performed intentionally.
442  
443 =back
444  
445 =head3 Returns
446  
447 C<$thing * 2>.
448  
449 =head3 API specification
450  
451 =head4 input
452  
453 {
454 self => { type => OBJECT },
455 thing => { type => 'any' },
456 }
457  
458 =head4 output
459  
460 { type => 'number' }
461  
462 =cut
463  
464 6 ▶sub mysterious_method {
Subroutine mysterious_method: called CC=1 SCAR=0.0
Pod documented
465 6 my ($self, $thing) = @_;
466  
467 # Intentionally unvalidated — used to verify that SchemaExtractor
468 # flags low-confidence schemas when no validation logic is present.
469 # Callers passing non-numeric values will trigger a Perl warning;
470 # this is expected behaviour for this test fixture.
471 6 return $thing * 2;
472 }
473  
474 =head1 AUTHOR
475  
476 Example Author
477  
478 =head1 LICENSE
479  
480 This is free software.
481  
482 =cut
483  
484 17 1;
« lib/App/Test/Generator/Planner/Fixture.pm ↑ Summary bin/test-generator-mutate »