1use strict;
2use warnings;
3use Test2::IPC;
4use Test2::Tools::Tiny;
5use Test2::API::Stack;
6use Test2::API qw/test2_ipc/;
7
8ok(my $stack = Test2::API::Stack->new, "Create a stack");
9
10ok(!@$stack, "Empty stack");
11ok(!$stack->peek, "Nothing to peek at");
12
13ok(!exception { $stack->cull },  "cull lives when stack is empty");
14ok(!exception { $stack->all },   "all lives when stack is empty");
15ok(!exception { $stack->clear }, "clear lives when stack is empty");
16
17like(
18    exception { $stack->pop(Test2::Hub->new) },
19    qr/No hubs on the stack/,
20    "No hub to pop"
21);
22
23my $hub = Test2::Hub->new;
24ok($stack->push($hub), "pushed a hub");
25
26like(
27    exception { $stack->pop($hub) },
28    qr/You cannot pop the root hub/,
29    "Root hub cannot be popped"
30);
31
32$stack->push($hub);
33like(
34    exception { $stack->pop(Test2::Hub->new) },
35    qr/Hub stack mismatch, attempted to pop incorrect hub/,
36    "Must specify correct hub to pop"
37);
38
39is_deeply(
40    [ $stack->all ],
41    [ $hub, $hub ],
42    "Got all hubs"
43);
44
45ok(!exception { $stack->pop($hub) }, "Popped the correct hub");
46
47is_deeply(
48    [ $stack->all ],
49    [ $hub ],
50    "Got all hubs"
51);
52
53is($stack->peek, $hub, "got the hub");
54is($stack->top, $hub, "got the hub");
55
56$stack->clear;
57
58is_deeply(
59    [ $stack->all ],
60    [ ],
61    "no hubs"
62);
63
64ok(my $top = $stack->top, "Generated a top hub");
65is($top->ipc, test2_ipc, "Used sync's ipc");
66ok($top->format, 'Got formatter');
67
68is($stack->top, $stack->top, "do not generate a new top if there is already a top");
69
70ok(my $new = $stack->new_hub(), "Add a new hub");
71is($stack->top, $new, "new one is on top");
72is($new->ipc, $top->ipc, "inherited ipc");
73is($new->format, $top->format, "inherited formatter");
74
75my $new2 = $stack->new_hub(formatter => undef, ipc => undef);
76ok(!$new2->ipc, "built with no ipc");
77ok(!$new2->format, "built with no formatter");
78
79done_testing;
80