1 package Test2::API::Stack;
5 our $VERSION = '1.302073';
14 return bless [], $class;
21 my $class = delete $params{class} || 'Test2::Hub';
23 my $hub = $class->new(%params);
26 $hub->inherit($self->[-1], %params);
30 $hub->format(Test2::API::test2_formatter()->new)
31 unless $hub->format || exists($params{formatter});
33 my $ipc = Test2::API::test2_ipc();
34 if ($ipc && !$hub->ipc && !exists($params{ipc})) {
36 $ipc->add_hub($hub->hid);
47 return $self->new_hub unless @$self;
53 return @$self ? $self->[-1] : undef;
58 $_->cull for reverse @$self;
71 # Do these last without keywords in order to prevent them from getting used
72 # when we want the real push/pop.
80 $hub->inherit($self->[-1]) if @$self;
87 confess "No hubs on the stack"
89 confess "You cannot pop the root hub"
91 confess "Hub stack mismatch, attempted to pop incorrect hub"
92 unless $self->[-1] == $hub;
107 Test2::API::Stack - Object to manage a stack of L<Test2::Hub>
110 =head1 ***INTERNALS NOTE***
112 B<The internals of this package are subject to change at any time!> The public
113 methods provided will not change in backwards incompatible ways, but the
114 underlying implementation details might. B<Do not break encapsulation here!>
118 This module is used to represent and manage a stack of L<Test2::Hub>
119 objects. Hubs are usually in a stack so that you can push a new hub into place
120 that can intercept and handle events differently than the primary hub.
124 my $stack = Test2::API::Stack->new;
125 my $hub = $stack->top;
131 =item $stack = Test2::API::Stack->new()
133 This will create a new empty stack instance. All arguments are ignored.
135 =item $hub = $stack->new_hub()
137 =item $hub = $stack->new_hub(%params)
139 =item $hub = $stack->new_hub(%params, class => $class)
141 This will generate a new hub and push it to the top of the stack. Optionally
142 you can provide arguments that will be passed into the constructor for the
143 L<Test2::Hub> object.
145 If you specify the C<< 'class' => $class >> argument, the new hub will be an
146 instance of the specified class.
148 Unless your parameters specify C<'formatter'> or C<'ipc'> arguments, the
149 formatter and IPC instance will be inherited from the current top hub. You can
150 set the parameters to C<undef> to avoid having a formatter or IPC instance.
152 If there is no top hub, and you do not ask to leave IPC and formatter undef,
153 then a new formatter will be created, and the IPC instance from
154 L<Test2::API> will be used.
156 =item $hub = $stack->top()
158 This will return the top hub from the stack. If there is no top hub yet this
161 =item $hub = $stack->peek()
163 This will return the top hub from the stack. If there is no top hub yet this
168 This will call C<< $hub->cull >> on all hubs in the stack.
170 =item @hubs = $stack->all
172 This will return all the hubs in the stack as a list.
176 This will completely remove all hubs from the stack. Normally you do not want
177 to do this, but there are a few valid reasons for it.
179 =item $stack->push($hub)
181 This will push the new hub onto the stack.
183 =item $stack->pop($hub)
185 This will pop a hub from the stack, if the hub at the top of the stack does not
186 match the hub you expect (passed in as an argument) it will throw an exception.
192 The source code repository for Test2 can be found at
193 F<http://github.com/Test-More/test-more/>.
199 =item Chad Granum E<lt>exodist@cpan.orgE<gt>
207 =item Chad Granum E<lt>exodist@cpan.orgE<gt>
213 Copyright 2016 Chad Granum E<lt>exodist@cpan.orgE<gt>.
215 This program is free software; you can redistribute it and/or
216 modify it under the same terms as Perl itself.
218 See F<http://dev.perl.org/licenses/>