lib/App/Test/Generator/Emitter/Perl.pm

filter: Statementstmt 100% 100 / 100 Branchbran 100% 38 / 38 Conditioncond 87.5% 14 / 16 MC/DCmcdc 75.0% 6 / 8 Subroutinesub 100% 19 / 19 Podpod 100% 2 / 2 totaltotal 97.8% 179 / 183 CC 29 SCAR 18.9
CC
29
Cov
99%
_emit_method_tests
14.0
_emit_boundary_test
6.0
_emit_getset_test
6.0
CRAP 6.6
« lib/App/Test/Generator/Mutator.pm ↑ Summary lib/App/Test/Generator/Mutation/ReturnUndef.pm »
1 package App::Test::Generator::Emitter::Perl;
2  
3 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # --------------------------------------------------
9 # Plan key names — must match those emitted by
10 # App::Test::Generator::TestStrategy and
11 # App::Test::Generator::Planner
12 # --------------------------------------------------
13 10 Readonly my $TEST_BASIC => 'basic_test';
14 10 Readonly my $TEST_GETTER => 'getter_test';
15 10 Readonly my $TEST_SETTER => 'setter_test';
16 10 Readonly my $TEST_GETSET => 'getset_test';
17 10 Readonly my $TEST_CHAINING => 'chaining_test';
18 10 Readonly my $TEST_ERROR_HANDLING => 'error_handling_test';
19 10 Readonly my $TEST_CONTEXT => 'context_tests';
20 10 Readonly my $TEST_OBJECT_INJECT => 'object_injection_test';
21 10 Readonly my $TEST_PREDICATE => 'predicate_test';
22 10 Readonly my $TEST_BOOLEAN => 'boolean_test';
23 10 Readonly my $TEST_VOID => 'void_context_test';
24 10 Readonly my $TEST_BOUNDARY => 'boundary_tests';
25  
26 # --------------------------------------------------
27 # Input/output type strings from the schema
28 # --------------------------------------------------
29 10 Readonly my $TYPE_OBJECT => 'object';
30 10 Readonly my $TYPE_BOOLEAN => 'boolean';
31  
32 10 our $VERSION = '0.50';
33  
34 =head1 VERSION
35  
36 Version 0.50
37  
38 =head1 DESCRIPTION
39  
40 Emits Perl test code for a set of method schemas and their associated
41 test plans. Each method plan is translated into one or more test blocks
42 using L<Test::Most>. The emitted code is returned as a string ready to
43 be written to a C<.t> file.
44  
45 =head2 new
46  
47 Construct a new Perl emitter.
48  
49 my $emitter = App::Test::Generator::Emitter::Perl->new(
50 schema => \%schemas,
51 plans => \%plans,
52 package => 'My::Module',
53 );
54  
55 =head3 Arguments
56  
57 =over 4
58  
59 =item * C<schema>
60  
61 A hashref of method name to schema hashref. Required.
62  
63 =item * C<plans>
64  
65 A hashref of method name to test plan hashref, as produced by
66 L<App::Test::Generator::TestStrategy> or
67 L<App::Test::Generator::Planner>. Required.
68  
69 =item * C<package>
70  
71 The Perl package name of the module under test. Required.
72  
73 =back
74  
75 =head3 Returns
76  
77 A blessed hashref. Croaks if any required argument is missing.
78  
79 =head3 API specification
80  
81 =head4 input
82  
83 {
84 schema => { type => HASHREF },
85 plans => { type => HASHREF },
86 package => { type => SCALAR },
87 }
88  
89 =head4 output
90  
91 {
92 type => OBJECT,
93 isa => 'App::Test::Generator::Emitter::Perl',
94 }
95  
96 =cut
97  
98 1513 ▶sub new {
Subroutine new: called CC=5 SCAR=16.1
Pod documented
99 1513 my ($class, %args) = @_;
100  
101 # All three arguments are required for meaningful emission
102 1513 ▶ croak 'schema required' unless defined $args{schema};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"schema"} 643 870 1513
103 870 ▶ croak 'plans required' unless defined $args{plans};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"plans"} 3 867 870
104 867 ▶ croak 'package required' unless defined $args{package};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"package"} 3 864 867
105  
106 # $args{package} is spliced unescaped into use_ok()/new_ok() calls
107 # in _emit_header() — reject anything that isn't a valid Perl
108 # package name now, rather than generating broken or injected code.
109 croak "package '$args{package}' is not a valid Perl package name"
110 864 ▶ unless $args{package} =~ /^[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"package"} =~ /^[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/ 376 488 864
111  
112 return bless {
113 schema => $args{schema},
114 plans => $args{plans},
115 package => $args{package},
116 488 }, $class;
117 }
118  
119 =head2 emit
120  
121 Generate and return the complete Perl test file source as a string,
122 including the file header, one test block per method, and the
123 C<done_testing()> footer.
124  
125 my $emitter = App::Test::Generator::Emitter::Perl->new(
126 schema => \%schemas,
127 plans => \%plans,
128 package => 'My::Module',
129 );
130 my $test_code = $emitter->emit;
131 write_file('t/generated.t', $test_code);
132  
133 =head3 Arguments
134  
135 None beyond C<$self>.
136  
137 =head3 Returns
138  
139 A string containing the complete Perl test file source.
140  
141 =head3 Notes
142  
143 A method whose plan has the C<boundary_tests> flag set (because its
144 schema carries non-empty C<_yamltest_hints>) gets one smoke-test block
145 per hint value in C<boundary_values> and C<invalid_inputs>, calling
146 the method with that value and asserting only that the call does not
147 crash the test process.
148  
149 =head3 API specification
150  
151 =head4 input
152  
153 {
154 self => { type => OBJECT, isa => 'App::Test::Generator::Emitter::Perl' },
155 }
156  
157 =head4 output
158  
159 { type => SCALAR }
160  
161 =cut
162  
163 65 ▶sub emit {
Subroutine emit: called CC=2 SCAR=6.9
Pod documented
164 65 my $self = $_[0];
165  
166 # Start with the file header then append per-method test blocks
167 65 my $code = $self->_emit_header();
168  
169 # Sort methods for deterministic output order
170 65 for my $method (sort keys %{ $self->{plans} }) {
171 118 $code .= $self->_emit_method_tests($method);
172 }
173  
174 # TAP footer required by Test::More / Test::Most
175 64 $code .= "\ndone_testing();\n";
176  
177 64 return $code;
178 }
179  
180 # --------------------------------------------------
181 # _emit_header
182 #
183 # Purpose: Generate the standard test file header
184 # including strict/warnings, use_ok and
185 # a default object construction.
186 #
187 # Entry: None beyond $self.
188 # Exit: Returns a string of Perl code.
189 # Side effects: None.
190 # Notes: The generated $obj is used by all
191 # subsequent test blocks.
192 # --------------------------------------------------
193 72 ▶sub _emit_header {
Subroutine _emit_header: called CC=1 SCAR=0.0
194 72 my $self = $_[0];
195  
196 72 return <<"END_HEADER";
197 use strict;
198 use warnings;
199 use Test::Most;
200  
201 use_ok('$self->{package}');
202  
203 my \$obj = new_ok('$self->{package}');
204  
205 END_HEADER
206 }
207  
208 # --------------------------------------------------
209 # _emit_method_tests
210 #
211 # Purpose: Dispatch to the appropriate emit method
212 # for each test type flagged in the plan
213 # for a given method.
214 #
215 # Entry: $method - the method name string.
216 # Plan and schema are read from $self.
217 # Exit: Returns a string of Perl test code.
218 # Side effects: None.
219 # Notes: Test types are emitted in a fixed order
220 # for deterministic output. Methods with
221 # no recognised plan flags produce no
222 # output beyond the section comment.
223 # --------------------------------------------------
224 138 ▶sub _emit_method_tests {
Subroutine _emit_method_tests: called CC=14 SCAR=26.4
225 138 my ($self, $method) = @_;
226  
227 # $method is spliced unescaped as a bareword method name
228 # (->$method(...)) by every _emit_*_test sub below — reject
229 # anything that isn't a valid Perl identifier before any of them run.
230 138 ▶ croak "method '$method' is not a valid Perl identifier"
Branch: 1 branch
BranchTrueFalseTotal
unless $method =~ /^[A-Za-z_]\w*\z/ 2 136 138
231 unless $method =~ /^[A-Za-z_]\w*\z/;
232  
233 136 my $plan = $self->{plans}{$method};
234 136 my $code = "\n# --- Tests for $method ---\n";
235  
236 # Emit each test type in a consistent fixed order
237 136 ▶ $code .= $self->_emit_basic_test($method) if $plan->{$TEST_BASIC};
Branch: 1 branch
BranchTrueFalseTotal
if $plan->{$TEST_BASIC} 64 72 136
238  
239 136 ▶ $code .= $self->_emit_getter_test($method) if $plan->{$TEST_GETTER};
Branch: 1 branch
BranchTrueFalseTotal
if $plan->{$TEST_GETTER} 13 123 136
240  
241 136 ▶ $code .= $self->_emit_setter_test($method) if $plan->{$TEST_SETTER};
Branch: 1 branch
BranchTrueFalseTotal
if $plan->{$TEST_SETTER} 10 126 136
242  
243 136 ▶ $code .= $self->_emit_getset_test($method) if $plan->{$TEST_GETSET};
Branch: 1 branch
BranchTrueFalseTotal
if $plan->{$TEST_GETSET} 15 121 136
244  
245 136 ▶ $code .= $self->_emit_chaining_test($method) if $plan->{$TEST_CHAINING};
Branch: 1 branch
BranchTrueFalseTotal
if $plan->{$TEST_CHAINING} 6 130 136
246  
247 136 ▶ $code .= $self->_emit_error_test($method) if $plan->{$TEST_ERROR_HANDLING};
Branch: 1 branch
BranchTrueFalseTotal
if $plan->{$TEST_ERROR_HANDLING} 5 131 136
248  
249 136 ▶ $code .= $self->_emit_context_test($method) if $plan->{$TEST_CONTEXT};
Branch: 1 branch
BranchTrueFalseTotal
if $plan->{$TEST_CONTEXT} 2 134 136
250  
251 136 ▶ $code .= $self->_emit_object_injection_test($method) if $plan->{$TEST_OBJECT_INJECT};
Branch: 1 branch
BranchTrueFalseTotal
if $plan->{$TEST_OBJECT_INJECT} 4 132 136
252  
253 136 ▶ $code .= $self->_emit_boolean_test($method) if $plan->{$TEST_PREDICATE} || $plan->{$TEST_BOOLEAN};
Branch: 1 branch
BranchTrueFalseTotal
if $plan->{$TEST_PREDICATE} or $plan->{$TEST_BOOLEAN} 7 129 136
Condition100%3/3
$plan->{$TEST_PREDICATE} or $plan->{$TEST_BOOLEAN}
l!l&&r!l&&!r
16129
MC/DC100%2/2
$plan->{$TEST_PREDICATE} or $plan->{$TEST_BOOLEAN}
$plan->{$TEST_PREDICATE} $plan->{$TEST_BOOLEAN}
Truth table100%3/3
A or B
A= $plan->{$TEST_PREDICATE}B= $plan->{$TEST_BOOLEAN}
ABresult
1X1
011
000
254  
255 136 ▶ $code .= $self->_emit_void_test($method) if $plan->{$TEST_VOID};
Branch: 1 branch
BranchTrueFalseTotal
if $plan->{$TEST_VOID} 3 133 136
256  
257 136 ▶ $code .= $self->_emit_boundary_test($method) if $plan->{$TEST_BOUNDARY};
Branch: 1 branch
BranchTrueFalseTotal
if $plan->{$TEST_BOUNDARY} 2 134 136
258  
259 136 return $code;
260 }
261  
262 # --------------------------------------------------
263 # _emit_basic_test
264 #
265 # Purpose: Emit a minimal test that calls the
266 # method and verifies it does not die.
267 #
268 # Entry: $method - method name string.
269 # Exit: Returns a string of Perl test code.
270 # Side effects: None.
271 # --------------------------------------------------
272 66 ▶sub _emit_basic_test {
Subroutine _emit_basic_test: called CC=1 SCAR=0.0
273 66 my ($self, $method) = @_;
274  
275 66 return <<"END_TEST";
276 {
277 my \$result = eval { \$obj->$method() };
278 ok(!\$@, '$method does not die');
279 }
280 END_TEST
281 }
282  
283 # --------------------------------------------------
284 # _emit_getter_test
285 #
286 # Purpose: Emit a test that calls the getter and
287 # verifies it returns a defined value.
288 #
289 # Entry: $method - method name string.
290 # Exit: Returns a string of Perl test code.
291 # Side effects: None.
292 # --------------------------------------------------
293 14 ▶sub _emit_getter_test {
Subroutine _emit_getter_test: called CC=1 SCAR=0.0
294 14 my ($self, $method) = @_;
295  
296 14 return <<"END_TEST";
297 {
298 my \$value = \$obj->$method();
299 ok(defined \$value, '$method returns a value');
300 }
301 END_TEST
302 }
303  
304 # --------------------------------------------------
305 # _emit_setter_test
306 #
307 # Purpose: Emit a test that calls the setter with
308 # a string argument and verifies it
309 # accepts the input without dying.
310 #
311 # Entry: $method - method name string.
312 # Exit: Returns a string of Perl test code.
313 # Side effects: None.
314 # --------------------------------------------------
315 12 ▶sub _emit_setter_test {
Subroutine _emit_setter_test: called CC=1 SCAR=0.0
316 12 my ($self, $method) = @_;
317  
318 12 return <<"END_TEST";
319 {
320 ok(\$obj->$method('test'), '$method accepts input');
321 }
322 END_TEST
323 }
324  
325 # --------------------------------------------------
326 # _emit_getset_test
327 #
328 # Purpose: Emit a round-trip get/set test. The
329 # test type (object, boolean, or string)
330 # is determined from the schema input
331 # parameter type.
332 #
333 # Entry: $method - method name string.
334 # Schema is read from $self.
335 # Exit: Returns a string of Perl test code.
336 # Side effects: None.
337 # Notes: Falls back to string round-trip if the
338 # parameter type is unrecognised.
339 # --------------------------------------------------
340 22 ▶sub _emit_getset_test {
Subroutine _emit_getset_test: called CC=6 SCAR=17.9
341 22 my ($self, $method) = @_;
342  
343 22 my $schema = $self->{schema}{$method};
344  
345 # Find the first non-internal input parameter
346 22 ▶ my ($param) = grep { !/^_/ } keys %{ $schema->{input} || {} };
Condition100%2/2
$schema->{'input'} || {}
l!l
202
MC/DC100%1/1
$schema->{'input'} || {}
$schema->{'input'}
Truth table100%2/2
A
A= $schema->{'input'}
Aresult
11
00
347 22 ▶ my $type = ($param && $schema->{input}{$param}{type}) // '';
Condition80%4/5
$param && $schema->{"input"}{$param}{"type"} // ""
l!l
148
$param and $schema->{"input"}{$param}{"type"}
!ll&&!rl&&r
8014
MC/DC50%1/2
$param && $schema->{"input"}{$param}{"type"} // ""
$param $schema->{"input"}{$param}{"type"}
Truth table66%2/3
A and B
A= $paramB= $schema->{"input"}{$param}{"type"}
ABresult
111
0X0
100
348  
349 # Object injection round-trip
350 22 ▶ if($type eq $TYPE_OBJECT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq $TYPE_OBJECT) 4 18 22
351 4 return <<"END_TEST";
352 {
353 my \$mock = bless {}, 'Test::MockObject';
354 \$obj->$method(\$mock);
355 isa_ok(\$obj->$method(), ref(\$mock), '$method get/set works');
356 }
357 END_TEST
358 }
359  
360 # Boolean round-trip
361 18 ▶ if($type eq $TYPE_BOOLEAN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq $TYPE_BOOLEAN) 4 14 18
362 4 return <<"END_TEST";
363 {
364 \$obj->$method(1);
365 ok(\$obj->$method(), '$method get/set boolean true works');
366 \$obj->$method(0);
367 ok(!\$obj->$method(), '$method get/set boolean false works');
368 }
369 END_TEST
370 }
371  
372 # Default string round-trip
373 14 return <<"END_TEST";
374 {
375 \$obj->$method('value');
376 is(\$obj->$method(), 'value', '$method get/set works');
377 }
378 END_TEST
379 }
380  
381 # --------------------------------------------------
382 # _emit_chaining_test
383 #
384 # Purpose: Emit a test that verifies the method
385 # returns $self for method chaining.
386 #
387 # Entry: $method - method name string.
388 # Exit: Returns a string of Perl test code.
389 # Side effects: None.
390 # --------------------------------------------------
391 7 ▶sub _emit_chaining_test {
Subroutine _emit_chaining_test: called CC=1 SCAR=0.0
392 7 my ($self, $method) = @_;
393  
394 7 return <<"END_TEST";
395 {
396 my \$ret = \$obj->$method();
397 isa_ok(\$ret, ref(\$obj), '$method returns self for chaining');
398 }
399 END_TEST
400 }
401  
402 # --------------------------------------------------
403 # _emit_error_test
404 #
405 # Purpose: Emit a test that calls the method with
406 # undef input and verifies it handles the
407 # error gracefully.
408 #
409 # Entry: $method - method name string.
410 # Exit: Returns a string of Perl test code.
411 # Side effects: None.
412 # --------------------------------------------------
413 7 ▶sub _emit_error_test {
Subroutine _emit_error_test: called CC=1 SCAR=0.0
414 7 my ($self, $method) = @_;
415  
416 7 return <<"END_TEST";
417 {
418 my \$result = eval { \$obj->$method(undef) };
419 ok(!\$result || \$@, '$method handles invalid input');
420 }
421 END_TEST
422 }
423  
424 # --------------------------------------------------
425 # _emit_context_test
426 #
427 # Purpose: Emit tests that call the method in
428 # both scalar and list context to verify
429 # context-aware return behaviour.
430 #
431 # Entry: $method - method name string.
432 # Exit: Returns a string of Perl test code.
433 # Side effects: None.
434 # Notes: Uses eval to verify the calls survive
435 # rather than checking return values,
436 # since context-aware return values vary.
437 # --------------------------------------------------
438 4 ▶sub _emit_context_test {
Subroutine _emit_context_test: called CC=1 SCAR=0.0
439 4 my ($self, $method) = @_;
440  
441 4 return <<"END_TEST";
442 {
443 my \$scalar = eval { \$obj->$method() };
444 ok(!\$@, '$method survives in scalar context');
445  
446 my \@list = eval { \$obj->$method() };
447 ok(!\$@, '$method survives in list context');
448 }
449 END_TEST
450 }
451  
452 # --------------------------------------------------
453 # _emit_object_injection_test
454 #
455 # Purpose: Emit a test that injects a mock object
456 # and verifies the same object is returned
457 # by the getter.
458 #
459 # Entry: $method - method name string.
460 # Exit: Returns a string of Perl test code.
461 # Side effects: None.
462 # --------------------------------------------------
463 6 ▶sub _emit_object_injection_test {
Subroutine _emit_object_injection_test: called CC=1 SCAR=0.0
464 6 my ($self, $method) = @_;
465  
466 6 return <<"END_TEST";
467 {
468 my \$mock = bless {}, 'Mock::Object';
469 \$obj->$method(\$mock);
470 isa_ok(\$obj->$method(), 'Mock::Object',
471 '$method stores injected object instance');
472 }
473 END_TEST
474 }
475  
476 # --------------------------------------------------
477 # _emit_boolean_test
478 #
479 # Purpose: Emit a test that verifies the method
480 # returns a defined scalar boolean value.
481 #
482 # Entry: $method - method name string.
483 # Exit: Returns a string of Perl test code.
484 # Side effects: None.
485 # Notes: Checks that the return value is defined,
486 # is not a reference, and is boolean-like
487 # without using numeric comparison which
488 # would warn on string returns.
489 # --------------------------------------------------
490 8 ▶sub _emit_boolean_test {
Subroutine _emit_boolean_test: called CC=1 SCAR=0.0
491 8 my ($self, $method) = @_;
492  
493 8 return <<"END_TEST";
494 {
495 my \$result = \$obj->$method();
496 ok(defined \$result, '$method returns a defined value');
497 ok(!ref \$result, '$method returns a scalar');
498 ok(\$result ? 1 : 0, '$method returns a boolean-like value');
499 }
500 END_TEST
501 }
502  
503 # --------------------------------------------------
504 # _emit_void_test
505 #
506 # Emit a test that verifies the method
507 # does not return a meaningful value,
508 # consistent with a void return type.
509 #
510 # Entry: $method - method name string.
511 # Exit: Returns a string of Perl test code.
512 # Side effects: None.
513 # --------------------------------------------------
514 5 ▶sub _emit_void_test {
Subroutine _emit_void_test: called CC=1 SCAR=0.0
515 5 my ($self, $method) = @_;
516  
517 5 return <<"END_TEST";
518 {
519 my \$result = eval { \$obj->$method() };
520 ok(!\$@, '$method does not die');
521 ok(!defined \$result, '$method returns nothing (void)');
522 }
523 END_TEST
524 }
525  
526 # --------------------------------------------------
527 # _emit_boundary_test
528 #
529 # Purpose: Emit one smoke-test block per boundary
530 # or invalid-input value detected by
531 # SchemaExtractor's _yamltest_hints, calling
532 # the method with each and confirming the
533 # call does not crash the test process.
534 #
535 # Entry: $method - method name string.
536 # Schema is read from $self.
537 # Exit: Returns a string of Perl test code, or
538 # the empty string if no hint values exist.
539 # Side effects: None.
540 # Notes: boundary_values and invalid_inputs are
541 # deliberately tested the same way: these
542 # are smoke tests proving resilience, not
543 # assertions on whether the call should
544 # succeed or die, since invalid_inputs are
545 # expected to be rejected while boundary_values
546 # are expected to be accepted.
547 # --------------------------------------------------
548 2 ▶sub _emit_boundary_test {
Subroutine _emit_boundary_test: called CC=6 SCAR=17.9
549 2 my ($self, $method) = @_;
550  
551 2 require App::Test::Generator;
552  
553 2 ▶ my $hints = $self->{schema}{$method}{_yamltest_hints} || {};
Condition50%1/2
$self->{'schema'}{$method}{'_yamltest_hints'} || {}
l!l
20
MC/DC0%0/1
$self->{'schema'}{$method}{'_yamltest_hints'} || {}
$self->{'schema'}{$method}{'_yamltest_hints'}
Truth table50%1/2
A
A= $self->{'schema'}{$method}{'_yamltest_hints'}
Aresult
11
00
554 my @values = (
555 2 ▶ @{ $hints->{boundary_values} || [] },
Condition100%2/2
$hints->{'boundary_values'} || []
l!l
11
MC/DC100%1/1
$hints->{'boundary_values'} || []
$hints->{'boundary_values'}
Truth table100%2/2
A
A= $hints->{'boundary_values'}
Aresult
11
00
556 2 ▶ @{ $hints->{invalid_inputs} || [] },
Condition100%2/2
$hints->{'invalid_inputs'} || []
l!l
11
MC/DC100%1/1
$hints->{'invalid_inputs'} || []
$hints->{'invalid_inputs'}
Truth table100%2/2
A
A= $hints->{'invalid_inputs'}
Aresult
11
00
557 );
558  
559 2 ▶ return '' unless @values;
Branch: 1 branch
BranchTrueFalseTotal
unless @values 1 1 2
560  
561 1 my $code = '';
562 1 for my $value (@values) {
563 3 my $literal = App::Test::Generator::perl_quote($value);
564 3 $code .= <<"END_TEST";
565 {
566 eval { \$obj->$method($literal) };
567 pass('$method survives boundary input $literal');
568 }
569 END_TEST
570 }
571  
572 1 return $code;
573 }
574  
575 10 1;
« lib/App/Test/Generator/Mutator.pm ↑ Summary lib/App/Test/Generator/Mutation/ReturnUndef.pm »