handle trigger with captures
[gitmo/Moo.git] / t / sub-defer.t
CommitLineData
eae82931 1use strictures 1;
2use Test::More;
3use Sub::Defer;
4
5my %made;
6
a165a07f 7my $one_defer = defer_sub 'Foo::one' => sub {
eae82931 8 die "remade - wtf" if $made{'Foo::one'};
9 $made{'Foo::one'} = sub { 'one' }
10};
11
a165a07f 12my $two_defer = defer_sub 'Foo::two' => sub {
eae82931 13 die "remade - wtf" if $made{'Foo::two'};
14 $made{'Foo::two'} = sub { 'two' }
15};
16
17is($one_defer, \&Foo::one, 'one defer installed');
18is($two_defer, \&Foo::two, 'two defer installed');
19
20is($one_defer->(), 'one', 'one defer runs');
21
22is($made{'Foo::one'}, \&Foo::one, 'one made');
23
24is($made{'Foo::two'}, undef, 'two not made');
25
26is($one_defer->(), 'one', 'one (deferred) still runs');
27
28is(Foo->one, 'one', 'one (undeferred) runs');
29
a165a07f 30is(my $two_made = undefer_sub($two_defer), $made{'Foo::two'}, 'make two');
eae82931 31
32is($two_made, \&Foo::two, 'two installed');
33
34is($two_defer->(), 'two', 'two (deferred) still runs');
35
36is($two_made->(), 'two', 'two (undeferred) runs');
37
38my $three = sub { 'three' };
39
a165a07f 40is(undefer_sub($three), $three, 'undefer non-deferred is a no-op');
eae82931 41
42done_testing;