File Coverage

File:blib/lib/App/Test/Generator/Planner/Isolation.pm
Coverage:97.1%

linestmtbrancondsubtimecode
1package App::Test::Generator::Planner::Isolation;
2
3
14
14
14
49262
14
104
use strict;
4
14
14
14
15
9
207
use warnings;
5
14
14
14
14
10
176
use Carp    qw(croak);
6
14
14
14
173
1187
1822
use Readonly;
7
8# --------------------------------------------------
9# Purity levels from Analyzer::SideEffect
10# --------------------------------------------------
11Readonly my $PURITY_PURE          => 'pure';
12Readonly my $PURITY_SELF_MUTATING => 'self_mutating';
13
14# --------------------------------------------------
15# Fixture isolation modes written to the plan output
16# --------------------------------------------------
17Readonly my $FIXTURE_SHARED   => 'shared_fixture';
18Readonly my $FIXTURE_FRESH    => 'fresh_object';
19Readonly my $FIXTURE_ISOLATED => 'isolated_block';
20
21our $VERSION = '0.52';
22
23 - 62
=head1 VERSION

Version 0.52

=head1 DESCRIPTION

Plans isolation strategy for each method under test, based on side
effect analysis and dependency metadata from the schema. Determines
whether each method needs a shared fixture, a fresh object per test,
or a fully isolated block, and records any environmental dependencies
that need mocking.

=head2 new

Construct a new Isolation planner.

    my $planner = App::Test::Generator::Planner::Isolation->new;

=head3 Arguments

None.

=head3 Returns

A blessed hashref.

=head3 API specification

=head4 input

    {}

=head4 output

    {
        type => OBJECT,
        isa  => 'App::Test::Generator::Planner::Isolation',
    }

=cut
63
64
71
121216
sub new { bless {}, shift }
65
66 - 128
=head2 plan

Produce an isolation plan for each method based on its side effect
analysis and dependency metadata.

    my $planner   = App::Test::Generator::Planner::Isolation->new;
    my $isolation = $planner->plan($schema, $strategy);

    for my $method (keys %{$isolation}) {
        printf "%s fixture: %s\n", $method, $isolation->{$method}{fixture};
    }

=head3 Arguments

=over 4

=item * C<$schema>

A hashref of method schemas, each optionally containing a C<_analysis>
key with C<side_effects> and C<dependencies> sub-keys.

=item * C<$strategy>

A hashref whose keys are the method names to plan isolation for.
Values are not used directly - the hashref is used only for its keys.

=back

=head3 Returns

A hashref mapping method names to isolation plan hashrefs. Each plan
has a C<fixture> key and optionally C<env>, C<filesystem>, C<time>,
and C<network> keys where relevant dependencies were detected.

=head3 API specification

=head4 input

    {
        self     => { type => OBJECT,  isa  => 'App::Test::Generator::Planner::Isolation' },
        schema   => { type => HASHREF, optional => 1 },
        strategy => { type => HASHREF, optional => 0 },
    }

=head4 output

    {
        type => HASHREF,
        keys => {
            '*' => {
                type => HASHREF,
                keys => {
                    fixture    => { type => SCALAR },
                    env        => { type => HASHREF,  optional => 1 },
                    filesystem => { type => HASHREF,  optional => 1 },
                    time       => { type => SCALAR,   optional => 1 },
                    network    => { type => SCALAR,   optional => 1 },
                },
            },
        },
    }

=cut
129
130sub plan {
131
82
4017
        my ($self, $schema, $strategy) = @_;
132
133
82
166
        croak 'schema must be a hashref'   if defined($schema) && ref($schema) ne 'HASH';
134
82
123
        croak 'strategy must be a hashref' unless ref($strategy) eq 'HASH';
135
136
70
42
        my %isolation;
137
138
70
70
40
76
        for my $method (keys %{$strategy}) {
139                # Extract side effect and dependency analysis from schema
140                # if present — degrade to empty analysis (most defensive) when schema is absent
141                my $analysis = (ref($schema) eq 'HASH' && ref($schema->{$method}) eq 'HASH')
142                        ? ($schema->{$method}{_analysis} || {})
143
69
155
                        : {};
144
69
69
                my $effects  = $analysis->{side_effects}     || {};
145
69
79
                my $deps     = $analysis->{dependencies}     || {};
146
147
69
41
                my %plan;
148
149                # --------------------------------------------------
150                # Choose fixture isolation mode based on purity level
151                # as determined by Analyzer::SideEffect:
152                #   pure          -> shared fixture safe to reuse
153                #   self_mutating -> fresh object needed per test
154                #   impure        -> full isolation block required
155                # --------------------------------------------------
156
69
70
                my $purity = $effects->{purity_level} // '';
157                $plan{fixture} =
158
69
98
                        $purity eq $PURITY_PURE          ? $FIXTURE_SHARED   :
159                        $purity eq $PURITY_SELF_MUTATING ? $FIXTURE_FRESH     :
160                                                           $FIXTURE_ISOLATED;
161
162                # --------------------------------------------------
163                # Record dependency isolation requirements so the
164                # test emitter knows what to mock or stub out
165                # --------------------------------------------------
166
167                # Environment variable dependencies — pass through
168                # the full env hashref for the emitter to use
169
69
474
                $plan{env}        = $deps->{env}        if $deps->{env};
170
171                # Filesystem dependencies — pass through the full
172                # filesystem hashref for the emitter to use
173
69
63
                $plan{filesystem} = $deps->{filesystem} if $deps->{filesystem};
174
175                # Time and network are boolean flags — we only care
176                # whether they are present, not their value
177
69
62
                $plan{time}    = 1 if $deps->{time};
178
69
58
                $plan{network} = 1 if $deps->{network};
179
180
69
72
                $isolation{$method} = \%plan;
181        }
182
183
70
62
        return \%isolation;
184}
185
1861;