| 1 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | package Test::Ratchet; | ||||||||||
| 4 | |||||||||||
| 5 | 2 | ▶ | use Exporter::Easy ( OK => [ qw/ratchet clank/ ] ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 2 | ▶ | use Data::Munge qw(rec); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 2 | ▶ | use Scalar::Util qw(refaddr); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 2 | our $VERSION = '0.005'; | |||||||||
| 10 | |||||||||||
| 11 | # ABSTRACT: Mocking helper that swaps out implementations automatically | ||||||||||
| 12 | |||||||||||
| 13 | |||||||||||
| 14 | 1 | ▶ | sub ratchet { | ||||||||
|
Subroutine
ratchet: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 15 | 1 | my @subrefs = @_; | |||||||||
| 16 | |||||||||||
| 17 | 10 | ▶ | my $ratchet = rec { | ||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||
| 18 | 10 | my $recurse = shift; | |||||||||
| 19 | 10 | ▶ | if (! @subrefs) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 20 | 0 | die "Tried to run a ratchet but there was nothing left to do!"; | |||||||||
| 21 | } | ||||||||||
| 22 | |||||||||||
| 23 | 10 | my $now = $subrefs[0]; | |||||||||
| 24 | |||||||||||
| 25 | # simple scalar should be a number. Run the next item as a subref if | ||||||||||
| 26 | # that number is not 0. If it's reached 0, shift them both off and redo. | ||||||||||
| 27 | # Or it's an asterisk, in which case do the next subref forever. | ||||||||||
| 28 | 10 | ▶ | if (not ref $now) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | 8 | ▶ | if ($now eq '*') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 30 | 4 | return $subrefs[1]->(@_); | |||||||||
| 31 | } | ||||||||||
| 32 | |||||||||||
| 33 | 4 | ▶ | if ($now > 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 34 | 3 | $now = $subrefs[1]; | |||||||||
| 35 | 3 | $subrefs[0]--; | |||||||||
| 36 | 3 | return $now->(@_); | |||||||||
| 37 | } | ||||||||||
| 38 | else { | ||||||||||
| 39 | 1 | shift @subrefs; shift @subrefs; | |||||||||
| 40 | # redo | ||||||||||
| 41 | 1 | return $recurse->(@_); | |||||||||
| 42 | } | ||||||||||
| 43 | } | ||||||||||
| 44 | |||||||||||
| 45 | else { | ||||||||||
| 46 | 2 | shift @subrefs; | |||||||||
| 47 | } | ||||||||||
| 48 | |||||||||||
| 49 | 2 | $now->(@_); | |||||||||
| 50 | 1 | }; | |||||||||
| 51 | } | ||||||||||
| 52 | |||||||||||
| 53 | |||||||||||
| 54 | 0 | ▶ | sub clank($) { | ||||||||
|
Subroutine
clank: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 55 | 0 | my $subref = shift; | |||||||||
| 56 | 0 | my $caller = sprintf "%s, line %s", (caller)[1,2]; | |||||||||
| 57 | 0 | ▶ | my $clank = rec { my $rec = shift; delete $Test::Ratchet::Clank::CLANK{ refaddr $rec }; &$subref }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 58 | 0 | $Test::Ratchet::Clank::CLANK{refaddr $clank} = $caller; | |||||||||
| 59 | 0 | bless $clank, "Test::Ratchet::Clank"; | |||||||||
| 60 | } | ||||||||||
| 61 | |||||||||||
| 62 | package Test::Ratchet::Clank; | ||||||||||
| 63 | |||||||||||
| 64 | 2 | ▶ | use Scalar::Util qw(refaddr); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 65 | |||||||||||
| 66 | 2 | our %CLANK; | |||||||||
| 67 | |||||||||||
| 68 | 0 | ▶ | sub DESTROY { | ||||||||
|
Subroutine
DESTROY: not called CC=2 SCAR=17.9
| |||||||||||
| 69 | 0 | my $self = shift; | |||||||||
| 70 | 0 | require Test::More; | |||||||||
| 71 | 0 | ▶ | Test::More::fail("A Clank was never run! Created at " . $CLANK{refaddr $self}) if $CLANK{ refaddr $self }; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | } | ||||||||||
| 73 | |||||||||||
| 74 | 2 | 1; | |||||||||
| 75 | |||||||||||
| 76 | __END__ | ||||||||||