File Coverage

inc/Test2/API/Stack.pm
Criterion Covered Total %
statement 40 52 76.9
branch 8 18 44.4
condition 5 11 45.4
subroutine 10 14 71.4
pod 7 7 100.0
total 70 102 68.6


line stmt bran cond sub pod time code
1             #line 1
2 6     6   40 package Test2::API::Stack;
  6         13  
  6         169  
3 6     6   28 use strict;
  6         13  
  6         216  
4             use warnings;
5              
6             our $VERSION = '1.302073';
7              
8 6     6   2759  
  6         16  
  6         148  
9             use Test2::Hub();
10 6     6   50  
  6         12  
  6         2320  
11             use Carp qw/confess/;
12              
13 6     6 1 12 sub new {
14 6         21 my $class = shift;
15             return bless [], $class;
16             }
17              
18 15     15 1 39 sub new_hub {
19 15         50 my $self = shift;
20             my %params = @_;
21 15   100     98  
22             my $class = delete $params{class} || 'Test2::Hub';
23 15         120  
24             my $hub = $class->new(%params);
25 15 100       52  
26 9         74 if (@$self) {
27             $hub->inherit($self->[-1], %params);
28             }
29 6         45 else {
30             require Test2::API;
31 6 50 33     31 $hub->format(Test2::API::test2_formatter()->new)
32             unless $hub->format || exists($params{formatter});
33 6         29  
34 6 0 33     30 my $ipc = Test2::API::test2_ipc();
      33        
35 0         0 if ($ipc && !$hub->ipc && !exists($params{ipc})) {
36 0         0 $hub->set_ipc($ipc);
37             $ipc->add_hub($hub->hid);
38             }
39             }
40 15         57  
41             push @$self => $hub;
42 15         107  
43             $hub;
44             }
45              
46 21     21 1 42 sub top {
47 21 100       71 my $self = shift;
48 15         44 return $self->new_hub unless @$self;
49             return $self->[-1];
50             }
51              
52 0     0 1 0 sub peek {
53 0 0       0 my $self = shift;
54             return @$self ? $self->[-1] : undef;
55             }
56              
57 0     0 1 0 sub cull {
58 0         0 my $self = shift;
59             $_->cull for reverse @$self;
60             }
61              
62 6     6 1 18 sub all {
63 6         54 my $self = shift;
64             return @$self;
65             }
66              
67 0     0 1 0 sub clear {
68 0         0 my $self = shift;
69             @$self = ();
70             }
71              
72             # Do these last without keywords in order to prevent them from getting used
73             # when we want the real push/pop.
74              
75 6     6   49 {
  6         12  
  6         1181  
76             no warnings 'once';
77              
78 0     0   0 *push = sub {
79 0         0 my $self = shift;
80 0 0       0 my ($hub) = @_;
81 0         0 $hub->inherit($self->[-1]) if @$self;
82             push @$self => $hub;
83             };
84              
85 9     9   21 *pop = sub {
86 9         32 my $self = shift;
87 9 50       34 my ($hub) = @_;
88             confess "No hubs on the stack"
89 9 50       30 unless @$self;
90             confess "You cannot pop the root hub"
91 9 50       31 if 1 == @$self;
92             confess "Hub stack mismatch, attempted to pop incorrect hub"
93 9         37 unless $self->[-1] == $hub;
94             pop @$self;
95             };
96             }
97              
98             1;
99              
100             __END__