Test that lazy defaults get coerced the same as non-lazy ones
[gitmo/Role-Tiny.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
ba37527b 42my $four_defer = defer_sub 'Foo::four' => sub {
43 sub { 'four' }
44};
45is($four_defer, \&Foo::four, 'four defer installed');
46
47# somebody somewhere wraps up around the deferred installer
48no warnings qw/redefine/;
49my $orig = Foo->can('four');
50*Foo::four = sub {
51 $orig->() . ' with a twist';
52};
53
54is(Foo->four, 'four with a twist', 'around works');
55is(Foo->four, 'four with a twist', 'around has not been destroyed by first invocation');
56
eae82931 57done_testing;