| File: | blib/lib/App/Test/Generator/Planner/Mock.pm |
| Coverage: | 97.8% |
| line | stmt | bran | cond | sub | time | code |
|---|---|---|---|---|---|---|
| 1 | package App::Test::Generator::Planner::Mock; | |||||
| 2 | ||||||
| 3 | 13 13 13 | 50149 8 92 | use strict; | |||
| 4 | 13 13 13 | 13 7 176 | use warnings; | |||
| 5 | 13 13 13 | 14 7 156 | use Carp qw(croak); | |||
| 6 | 13 13 13 | 168 1204 1233 | use Readonly; | |||
| 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 | Readonly my $MOCK_SYSTEM => 'mock_system'; | |||||
| 16 | Readonly my $MOCK_CAPTURE_IO => 'capture_io'; | |||||
| 17 | ||||||
| 18 | our $VERSION = '0.52'; | |||||
| 19 | ||||||
| 20 - 60 | =head1 VERSION
Version 0.52
=head1 DESCRIPTION
Plans mock strategy for each method that has external side effects,
based on side effect analysis metadata in the schema. Methods that
call external commands are assigned a system mock; methods that
perform IO are assigned IO capture. Used by
L<App::Test::Generator::Emitter::Perl> to generate appropriate mock
setup code in the test output.
=head2 new
Construct a new Mock planner.
my $planner = App::Test::Generator::Planner::Mock->new;
=head3 Arguments
None.
=head3 Returns
A blessed hashref.
=head3 API specification
=head4 input
{}
=head4 output
{
type => OBJECT,
isa => 'App::Test::Generator::Planner::Mock',
}
=cut | |||||
| 61 | ||||||
| 62 | 35 | 91639 | sub new { bless {}, shift } | |||
| 63 | ||||||
| 64 - 117 | =head2 plan
Produce a mock plan for each method that requires external mocking,
based on side effect analysis metadata in the schema.
my $planner = App::Test::Generator::Planner::Mock->new;
my $mock_plan = $planner->plan($schema);
for my $method (keys %{$mock_plan}) {
printf "%s: %s\n", $method, $mock_plan->{$method};
}
=head3 Arguments
=over 4
=item * C<$schema>
A hashref of method schemas, each optionally containing a
C<_analysis> key with a C<side_effects> sub-key as produced by
L<App::Test::Generator::Analyzer::SideEffect>.
=back
=head3 Returns
A hashref mapping method names to a mock strategy. Only methods that
require mocking appear in the output - pure methods are omitted.
Currently supported strategy values are C<mock_system> and
C<capture_io>. A method needing only one of these gets that value as
a plain scalar string. A method that both calls external commands and
performs IO needs both mocks applied, so it gets an arrayref
C<[mock_system, capture_io]> instead of a single string.
=head3 API specification
=head4 input
{
self => { type => OBJECT, isa => 'App::Test::Generator::Planner::Mock' },
schema => { type => HASHREF },
}
=head4 output
{
type => HASHREF,
keys => {
'*' => { type => 'scalar | arrayref' },
},
}
=cut | |||||
| 118 | ||||||
| 119 | sub plan { | |||||
| 120 | 36 | 1601 | my ($self, $schema) = @_; | |||
| 121 | ||||||
| 122 | # Validate that schema is a hashref before iterating | |||||
| 123 | 36 | 74 | croak 'schema must be a hashref' unless ref($schema) eq 'HASH'; | |||
| 124 | ||||||
| 125 | 31 | 17 | my %mock_plan; | |||
| 126 | ||||||
| 127 | 31 31 | 22 38 | for my $method (keys %{$schema}) { | |||
| 128 | 33 | 35 | next unless ref($schema->{$method}) eq 'HASH'; | |||
| 129 | # Extract side effect analysis if present â | |||||
| 130 | # default to empty hashref if not available | |||||
| 131 | 33 | 44 | my $effects = $schema->{$method}{_analysis}{side_effects} || {}; | |||
| 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 | 85 | if($effects->{calls_external} && $effects->{performs_io}) { | |||
| 139 | 7 | 12 | $mock_plan{$method} = [$MOCK_SYSTEM, $MOCK_CAPTURE_IO]; | |||
| 140 | } elsif($effects->{calls_external}) { | |||||
| 141 | 7 | 11 | $mock_plan{$method} = $MOCK_SYSTEM; | |||
| 142 | } elsif($effects->{performs_io}) { | |||||
| 143 | 7 | 11 | $mock_plan{$method} = $MOCK_CAPTURE_IO; | |||
| 144 | } | |||||
| 145 | # Pure methods require no mocking â omit from plan | |||||
| 146 | } | |||||
| 147 | ||||||
| 148 | 31 | 108 | return \%mock_plan; | |||
| 149 | } | |||||
| 150 | ||||||
| 151 | 1; | |||||