| 1 | package App::Test::Generator::Planner::Isolation; | ||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||
| 3 | 22 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 4 | 22 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 5 | 22 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 6 | 22 | ▶ | use Readonly; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||
| 8 | # -------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||
| 9 | # Purity levels from Analyzer::SideEffect | ||||||||||||||||||||||||||||||||||||||
| 10 | # -------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||
| 11 | 22 | Readonly my $PURITY_PURE => 'pure'; | |||||||||||||||||||||||||||||||||||||
| 12 | 22 | Readonly my $PURITY_SELF_MUTATING => 'self_mutating'; | |||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||
| 14 | # -------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||
| 15 | # Fixture isolation modes written to the plan output | ||||||||||||||||||||||||||||||||||||||
| 16 | # -------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||
| 17 | 22 | Readonly my $FIXTURE_SHARED => 'shared_fixture'; | |||||||||||||||||||||||||||||||||||||
| 18 | 22 | Readonly my $FIXTURE_FRESH => 'fresh_object'; | |||||||||||||||||||||||||||||||||||||
| 19 | 22 | Readonly my $FIXTURE_ISOLATED => 'isolated_block'; | |||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||
| 21 | 22 | our $VERSION = '0.49'; | |||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||
| 23 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||
| 25 | Version 0.49 | ||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||
| 27 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||
| 29 | Plans isolation strategy for each method under test, based on side | ||||||||||||||||||||||||||||||||||||||
| 30 | effect analysis and dependency metadata from the schema. Determines | ||||||||||||||||||||||||||||||||||||||
| 31 | whether each method needs a shared fixture, a fresh object per test, | ||||||||||||||||||||||||||||||||||||||
| 32 | or a fully isolated block, and records any environmental dependencies | ||||||||||||||||||||||||||||||||||||||
| 33 | that need mocking. | ||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||
| 35 | =head2 new | ||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||
| 37 | Construct a new Isolation planner. | ||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||
| 39 | my $planner = App::Test::Generator::Planner::Isolation->new; | ||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||
| 41 | =head3 Arguments | ||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||
| 43 | None. | ||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||
| 45 | =head3 Returns | ||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||
| 47 | A blessed hashref. | ||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||
| 49 | =head3 API specification | ||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||
| 51 | =head4 input | ||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||
| 53 | {} | ||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||
| 55 | =head4 output | ||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||
| 57 | { | ||||||||||||||||||||||||||||||||||||||
| 58 | type => OBJECT, | ||||||||||||||||||||||||||||||||||||||
| 59 | isa => 'App::Test::Generator::Planner::Isolation', | ||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||
| 62 | =cut | ||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||
| 64 | 76 | ▶ | sub new { bless {}, shift } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||
| 66 | =head2 plan | ||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||
| 68 | Produce an isolation plan for each method based on its side effect | ||||||||||||||||||||||||||||||||||||||
| 69 | analysis and dependency metadata. | ||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||
| 71 | my $planner = App::Test::Generator::Planner::Isolation->new; | ||||||||||||||||||||||||||||||||||||||
| 72 | my $isolation = $planner->plan($schema, $strategy); | ||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||
| 74 | for my $method (keys %{$isolation}) { | ||||||||||||||||||||||||||||||||||||||
| 75 | printf "%s fixture: %s\n", $method, $isolation->{$method}{fixture}; | ||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||
| 78 | =head3 Arguments | ||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||
| 80 | =over 4 | ||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||
| 82 | =item * C<$schema> | ||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||
| 84 | A hashref of method schemas, each optionally containing a C<_analysis> | ||||||||||||||||||||||||||||||||||||||
| 85 | key with C<side_effects> and C<dependencies> sub-keys. | ||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||
| 87 | =item * C<$strategy> | ||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||
| 89 | A hashref whose keys are the method names to plan isolation for. | ||||||||||||||||||||||||||||||||||||||
| 90 | Values are not used directly - the hashref is used only for its keys. | ||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||
| 92 | =back | ||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||
| 94 | =head3 Returns | ||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||
| 96 | A hashref mapping method names to isolation plan hashrefs. Each plan | ||||||||||||||||||||||||||||||||||||||
| 97 | has a C<fixture> key and optionally C<env>, C<filesystem>, C<time>, | ||||||||||||||||||||||||||||||||||||||
| 98 | and C<network> keys where relevant dependencies were detected. | ||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||
| 100 | =head3 API specification | ||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||
| 102 | =head4 input | ||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||
| 104 | { | ||||||||||||||||||||||||||||||||||||||
| 105 | self => { type => OBJECT, isa => 'App::Test::Generator::Planner::Isolation' }, | ||||||||||||||||||||||||||||||||||||||
| 106 | schema => { type => HASHREF, optional => 1 }, | ||||||||||||||||||||||||||||||||||||||
| 107 | strategy => { type => HASHREF, optional => 0 }, | ||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||
| 110 | =head4 output | ||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||
| 112 | { | ||||||||||||||||||||||||||||||||||||||
| 113 | type => HASHREF, | ||||||||||||||||||||||||||||||||||||||
| 114 | keys => { | ||||||||||||||||||||||||||||||||||||||
| 115 | '*' => { | ||||||||||||||||||||||||||||||||||||||
| 116 | type => HASHREF, | ||||||||||||||||||||||||||||||||||||||
| 117 | keys => { | ||||||||||||||||||||||||||||||||||||||
| 118 | fixture => { type => SCALAR }, | ||||||||||||||||||||||||||||||||||||||
| 119 | env => { type => HASHREF, optional => 1 }, | ||||||||||||||||||||||||||||||||||||||
| 120 | filesystem => { type => HASHREF, optional => 1 }, | ||||||||||||||||||||||||||||||||||||||
| 121 | time => { type => SCALAR, optional => 1 }, | ||||||||||||||||||||||||||||||||||||||
| 122 | network => { type => SCALAR, optional => 1 }, | ||||||||||||||||||||||||||||||||||||||
| 123 | }, | ||||||||||||||||||||||||||||||||||||||
| 124 | }, | ||||||||||||||||||||||||||||||||||||||
| 125 | }, | ||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||
| 128 | =cut | ||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||
| 130 | 108 | ▶ | sub plan { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
plan: called CC=17 SCAR=28.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 131 | 108 | my ($self, $schema, $strategy) = @_; | |||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||
| 133 | 108 | ▶ | croak 'schema must be a hashref' if defined($schema) && ref($schema) ne 'HASH'; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $schema and ref $schema ne "HASH"
MC/DC0%0/2
defined $schema and ref $schema ne "HASH"
defined $schema
ref $schema ne "HASH"
Truth table66%2/3
A and B
A= defined $schemaB= ref $schema ne "HASH"
| |||||||||||||||||||||||||||||||||||||||
| 134 | 108 | ▶ | croak 'strategy must be a hashref' unless ref($strategy) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||
| 136 | 96 | my %isolation; | |||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||
| 138 | 96 | 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 | 89 | ▶ | : {}; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%5/5
ref $schema eq "HASH" and ref $schema->{$method} eq "HASH"
$schema->{$method}{"_analysis"} or {}
MC/DC100%2/2
ref $schema eq "HASH" and ref $schema->{$method} eq "HASH"
ref $schema eq "HASH"
ref $schema->{$method} eq "HASH"
MC/DC100%1/1
$schema->{$method}{"_analysis"} or {}
$schema->{$method}{"_analysis"}
Truth table100%3/3
A and B
A= ref $schema eq "HASH"B= ref $schema->{$method} eq "HASH"
Truth table100%2/2
A
A= $schema->{$method}{"_analysis"}
| |||||||||||||||||||||||||||||||||||||||
| 144 | 89 | ▶ | my $effects = $analysis->{side_effects} || {}; | ||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$analysis->{'side_effects'} || {}
MC/DC100%1/1
$analysis->{'side_effects'} || {}
$analysis->{'side_effects'}
Truth table100%2/2
A
A= $analysis->{'side_effects'}
| |||||||||||||||||||||||||||||||||||||||
| 145 | 89 | ▶ | my $deps = $analysis->{dependencies} || {}; | ||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$analysis->{'dependencies'} || {}
MC/DC100%1/1
$analysis->{'dependencies'} || {}
$analysis->{'dependencies'}
Truth table100%2/2
A
A= $analysis->{'dependencies'}
| |||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||
| 147 | 89 | 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 | 89 | ▶ | my $purity = $effects->{purity_level} // ''; | ||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$effects->{"purity_level"} // ""
MC/DC100%1/1
$effects->{"purity_level"} // ""
$effects->{"purity_level"}
Truth table100%2/2
A
A= $effects->{"purity_level"}
| |||||||||||||||||||||||||||||||||||||||
| 157 | $plan{fixture} = | ||||||||||||||||||||||||||||||||||||||
| 158 | 89 | ▶ | $purity eq $PURITY_PURE ? $FIXTURE_SHARED : | ||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||
| 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 | 89 | ▶ | $plan{env} = $deps->{env} if $deps->{env}; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||
| 171 | # Filesystem dependencies — pass through the full | ||||||||||||||||||||||||||||||||||||||
| 172 | # filesystem hashref for the emitter to use | ||||||||||||||||||||||||||||||||||||||
| 173 | 89 | ▶ | $plan{filesystem} = $deps->{filesystem} if $deps->{filesystem}; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||
| 175 | # Time and network are boolean flags — we only care | ||||||||||||||||||||||||||||||||||||||
| 176 | # whether they are present, not their value | ||||||||||||||||||||||||||||||||||||||
| 177 | 89 | ▶ | $plan{time} = 1 if $deps->{time}; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 178 | 89 | ▶ | $plan{network} = 1 if $deps->{network}; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||
| 180 | 89 | $isolation{$method} = \%plan; | |||||||||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||
| 183 | 96 | return \%isolation; | |||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||
| 186 | 22 | 1; | |||||||||||||||||||||||||||||||||||||