File Coverage

lib/Test/Mock/LWP/Conditional/Stubs.pm
Criterion Covered Total %
statement 24 24 100.0
branch 4 6 66.6
condition 5 8 62.5
subroutine 8 8 100.0
pod 0 5 0.0
total 41 51 80.3


line stmt bran cond sub pod time code
1             package Test::Mock::LWP::Conditional::Stubs;
2              
3 7     7   27453 use strict;
  7         10  
  7         226  
4 7     7   45 use warnings;
  7         8  
  7         156  
5 7     7   25 use Scalar::Util qw(blessed);
  7         10  
  7         1559  
6              
7             sub new {
8 11     11 0 278 my $class = shift;
9 11         141 return bless +{ count => 0, stubs => [] }, $class;
10             }
11              
12             sub is_http_res {
13 42     42 0 45 my ($self, $res) = @_;
14 42   66     405 return blessed($res) && $res->isa('HTTP::Response');
15             }
16              
17             sub is_stub {
18 15     15 0 77 my ($self, $stub) = @_;
19 15   66     37 return $self->is_http_res($stub) || ref($stub) eq 'CODE';
20             }
21              
22             sub add {
23 15     15 0 71 my ($self, $stub) = @_;
24 15 50       37 push @{$self->{stubs}}, $stub if $self->is_stub($stub);
  15         60  
25             }
26              
27             sub execute {
28 27     27 0 40 my ($self, $req) = @_;
29              
30 27         86 my $i = $self->{count}++;
31 27   50     281 my $stub = $self->{stubs}->[$i] || $self->{stubs}->[-1] || return;
32              
33 27 100       57 if ($self->is_http_res($stub)) {
    50          
34 24         73 return $stub;
35             }
36             elsif (ref($stub) eq 'CODE') {
37 3         9 return $stub->($req);
38             }
39             }
40              
41             1;