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

filter: Statementstmt 100% 29 / 29 Branchbran 100% 10 / 10 Conditioncond 100% 5 / 5 MC/DCmcdc 100% 3 / 3 Subroutinesub 100% 6 / 6 Podpod 100% 2 / 2 totaltotal 100% 55 / 55 CC 9 SCAR 18.2
CC
9
Cov
100%
plan
9.0
BEGIN
1.0
BEGIN
1.0
CRAP 6.1
« lib/App/Test/Generator/Model/Method.pm ↑ Summary blib/lib/App/Test/Generator/Analyzer/Return.pm »
1 package App::Test::Generator::Planner::Mock;
2  
3 21 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 21 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 21 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 21 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # --------------------------------------------------
9 # Mock strategy labels written to the plan output.
10 # mock_system is used when a method calls external
11 # commands; capture_io when it performs IO operations.
12 # A method needing both gets an arrayref of both labels
13 # — see note in plan() below.
14 # --------------------------------------------------
15 21 Readonly my $MOCK_SYSTEM => 'mock_system';
16 21 Readonly my $MOCK_CAPTURE_IO => 'capture_io';
17  
18 21 our $VERSION = '0.49';
19  
20 =head1 VERSION
21  
22 Version 0.49
23  
24 =head1 DESCRIPTION
25  
26 Plans mock strategy for each method that has external side effects,
27 based on side effect analysis metadata in the schema. Methods that
28 call external commands are assigned a system mock; methods that
29 perform IO are assigned IO capture. Used by
30 L<App::Test::Generator::Emitter::Perl> to generate appropriate mock
31 setup code in the test output.
32  
33 =head2 new
34  
35 Construct a new Mock planner.
36  
37 my $planner = App::Test::Generator::Planner::Mock->new;
38  
39 =head3 Arguments
40  
41 None.
42  
43 =head3 Returns
44  
45 A blessed hashref.
46  
47 =head3 API specification
48  
49 =head4 input
50  
51 {}
52  
53 =head4 output
54  
55 {
56 type => OBJECT,
57 isa => 'App::Test::Generator::Planner::Mock',
58 }
59  
60 =cut
61  
62 40 ▶sub new { bless {}, shift }
Subroutine new: called CC=1 SCAR=0.0
Pod documented
63  
64 =head2 plan
65  
66 Produce a mock plan for each method that requires external mocking,
67 based on side effect analysis metadata in the schema.
68  
69 my $planner = App::Test::Generator::Planner::Mock->new;
70 my $mock_plan = $planner->plan($schema);
71  
72 for my $method (keys %{$mock_plan}) {
73 printf "%s: %s\n", $method, $mock_plan->{$method};
74 }
75  
76 =head3 Arguments
77  
78 =over 4
79  
80 =item * C<$schema>
81  
82 A hashref of method schemas, each optionally containing a
83 C<_analysis> key with a C<side_effects> sub-key as produced by
84 L<App::Test::Generator::Analyzer::SideEffect>.
85  
86 =back
87  
88 =head3 Returns
89  
90 A hashref mapping method names to a mock strategy. Only methods that
91 require mocking appear in the output - pure methods are omitted.
92  
93 Currently supported strategy values are C<mock_system> and
94 C<capture_io>. A method needing only one of these gets that value as
95 a plain scalar string. A method that both calls external commands and
96 performs IO needs both mocks applied, so it gets an arrayref
97 C<[mock_system, capture_io]> instead of a single string.
98  
99 =head3 API specification
100  
101 =head4 input
102  
103 {
104 self => { type => OBJECT, isa => 'App::Test::Generator::Planner::Mock' },
105 schema => { type => HASHREF },
106 }
107  
108 =head4 output
109  
110 {
111 type => HASHREF,
112 keys => {
113 '*' => { type => 'scalar | arrayref' },
114 },
115 }
116  
117 =cut
118  
119 42 ▶sub plan {
Subroutine plan: called CC=9 SCAR=22.0
Pod documented
120 42 my ($self, $schema) = @_;
121  
122 # Validate that schema is a hashref before iterating
123 42 ▶ croak 'schema must be a hashref' unless ref($schema) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $schema eq "HASH" 5 37 42
124  
125 37 my %mock_plan;
126  
127 37 for my $method (keys %{$schema}) {
128 35 ▶ next unless ref($schema->{$method}) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $schema->{$method} eq "HASH" 2 33 35
129 # Extract side effect analysis if present —
130 # default to empty hashref if not available
131 33 ▶ my $effects = $schema->{$method}{_analysis}{side_effects} || {};
Condition100%2/2
$schema->{$method}{'_analysis'}{'side_effects'} || {}
l!l
285
MC/DC100%1/1
$schema->{$method}{'_analysis'}{'side_effects'} || {}
$schema->{$method}{'_analysis'}{'side_effects'}
Truth table100%2/2
A
A= $schema->{$method}{'_analysis'}{'side_effects'}
Aresult
11
00
132  
133 # --------------------------------------------------
134 # Assign mock strategy based on detected side effects.
135 # A method needing both gets an arrayref of both labels
136 # rather than silently dropping one of them.
137 # --------------------------------------------------
138 33 ▶ if($effects->{calls_external} && $effects->{performs_io}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($effects->{'calls_external'} and $effects->{'performs_io'}) { } 7 26 33
elsif ($effects->{'calls_external'}) { } 7 19 26
elsif ($effects->{'performs_io'}) { } 7 12 19
Condition100%3/3
$effects->{"calls_external"} and $effects->{"performs_io"}
!ll&&!rl&&r
1977
MC/DC100%2/2
$effects->{"calls_external"} and $effects->{"performs_io"}
$effects->{"calls_external"} $effects->{"performs_io"}
Truth table100%3/3
A and B
A= $effects->{"calls_external"}B= $effects->{"performs_io"}
ABresult
0X0
100
111
139 7 $mock_plan{$method} = [$MOCK_SYSTEM, $MOCK_CAPTURE_IO];
140 } elsif($effects->{calls_external}) {
141 7 $mock_plan{$method} = $MOCK_SYSTEM;
142 } elsif($effects->{performs_io}) {
143 7 $mock_plan{$method} = $MOCK_CAPTURE_IO;
144 }
145 # Pure methods require no mocking — omit from plan
146 }
147  
148 37 return \%mock_plan;
149 }
150  
151 21 1;
« lib/App/Test/Generator/Model/Method.pm ↑ Summary blib/lib/App/Test/Generator/Analyzer/Return.pm »