summaryrefslogtreecommitdiffstats
path: root/gnu/usr.bin/perl/cpan/Test-Simple/t/Test2/modules/API/Stack.t
blob: c2016e292ed1227f68d897f73b68d9a14faad70f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use strict;
use warnings;
use Test2::IPC;
use Test2::Tools::Tiny;
use Test2::API::Stack;
use Test2::API qw/test2_ipc/;

ok(my $stack = Test2::API::Stack->new, "Create a stack");

ok(!@$stack, "Empty stack");
ok(!$stack->peek, "Nothing to peek at");

ok(!exception { $stack->cull },  "cull lives when stack is empty");
ok(!exception { $stack->all },   "all lives when stack is empty");
ok(!exception { $stack->clear }, "clear lives when stack is empty");

like(
    exception { $stack->pop(Test2::Hub->new) },
    qr/No hubs on the stack/,
    "No hub to pop"
);

my $hub = Test2::Hub->new;
ok($stack->push($hub), "pushed a hub");

like(
    exception { $stack->pop($hub) },
    qr/You cannot pop the root hub/,
    "Root hub cannot be popped"
);

$stack->push($hub);
like(
    exception { $stack->pop(Test2::Hub->new) },
    qr/Hub stack mismatch, attempted to pop incorrect hub/,
    "Must specify correct hub to pop"
);

is_deeply(
    [ $stack->all ],
    [ $hub, $hub ],
    "Got all hubs"
);

ok(!exception { $stack->pop($hub) }, "Popped the correct hub");

is_deeply(
    [ $stack->all ],
    [ $hub ],
    "Got all hubs"
);

is($stack->peek, $hub, "got the hub");
is($stack->top, $hub, "got the hub");

$stack->clear;

is_deeply(
    [ $stack->all ],
    [ ],
    "no hubs"
);

ok(my $top = $stack->top, "Generated a top hub");
is($top->ipc, test2_ipc, "Used sync's ipc");
ok($top->format, 'Got formatter');

is($stack->top, $stack->top, "do not generate a new top if there is already a top");

ok(my $new = $stack->new_hub(), "Add a new hub");
is($stack->top, $new, "new one is on top");
is($new->ipc, $top->ipc, "inherited ipc");
is($new->format, $top->format, "inherited formatter");

my $new2 = $stack->new_hub(formatter => undef, ipc => undef);
ok(!$new2->ipc, "built with no ipc");
ok(!$new2->format, "built with no formatter");

done_testing;