update repo to point to github
[gitmo/Moo.git] / t / sub-defer.t
CommitLineData
eae82931 1use strictures 1;
2use Test::More;
9e858450 3use Test::Fatal;
eae82931 4use Sub::Defer;
5
6my %made;
7
a165a07f 8my $one_defer = defer_sub 'Foo::one' => sub {
eae82931 9 die "remade - wtf" if $made{'Foo::one'};
10 $made{'Foo::one'} = sub { 'one' }
11};
12
a165a07f 13my $two_defer = defer_sub 'Foo::two' => sub {
eae82931 14 die "remade - wtf" if $made{'Foo::two'};
15 $made{'Foo::two'} = sub { 'two' }
16};
17
18is($one_defer, \&Foo::one, 'one defer installed');
19is($two_defer, \&Foo::two, 'two defer installed');
20
21is($one_defer->(), 'one', 'one defer runs');
22
23is($made{'Foo::one'}, \&Foo::one, 'one made');
24
25is($made{'Foo::two'}, undef, 'two not made');
26
27is($one_defer->(), 'one', 'one (deferred) still runs');
28
29is(Foo->one, 'one', 'one (undeferred) runs');
30
a165a07f 31is(my $two_made = undefer_sub($two_defer), $made{'Foo::two'}, 'make two');
eae82931 32
9e858450 33is exception { undefer_sub($two_defer) }, undef,
34 "repeated undefer doesn't regenerate";
35
eae82931 36is($two_made, \&Foo::two, 'two installed');
37
38is($two_defer->(), 'two', 'two (deferred) still runs');
39
40is($two_made->(), 'two', 'two (undeferred) runs');
41
42my $three = sub { 'three' };
43
a165a07f 44is(undefer_sub($three), $three, 'undefer non-deferred is a no-op');
eae82931 45
ba37527b 46my $four_defer = defer_sub 'Foo::four' => sub {
47 sub { 'four' }
48};
49is($four_defer, \&Foo::four, 'four defer installed');
50
51# somebody somewhere wraps up around the deferred installer
52no warnings qw/redefine/;
53my $orig = Foo->can('four');
54*Foo::four = sub {
55 $orig->() . ' with a twist';
56};
57
58is(Foo->four, 'four with a twist', 'around works');
59is(Foo->four, 'four with a twist', 'around has not been destroyed by first invocation');
60
eae82931 61done_testing;