inlining for overloaded object isa/coerce
[gitmo/Moo.git] / t / accessor-default.t
1 use strictures 1;
2 use Test::More;
3
4 my $c_ran;
5 {
6   package Foo;
7
8   use Sub::Quote;
9   use Moo;
10
11   has one => (is => 'ro', lazy => 1, default => quote_sub q{ {} });
12   has two => (is => 'ro', lazy => 1, builder => '_build_two');
13   sub _build_two { {} }
14   has three => (is => 'ro', default => quote_sub q{ {} });
15   has four => (is => 'ro', builder => '_build_four');
16   sub _build_four { {} }
17   has five => (is => 'ro', init_arg => undef, default => sub { {} });
18   has six => (is => 'ro', builder => 1);
19   sub _build_six { {} }
20   has seven => (is => 'ro', required => 1, default => quote_sub q{ {} });
21   has eight => (is => 'ro', builder => '_build_eight', coerce => sub { $c_ran = 1; $_[0] });
22   sub _build_eight { {} }
23   has nine => (is => 'lazy', coerce => sub { $c_ran = 1; $_[0] });
24   sub _build_nine { {} }
25   has ten => (is => 'lazy', default => 5 );
26 }
27
28 sub check {
29   my ($attr, @h) = @_;
30
31   is_deeply($h[$_], {}, "${attr}: empty hashref \$h[$_]") for 0..1;
32
33   isnt($h[0],$h[1], "${attr}: not the same hashref");
34 }
35
36 check one => map Foo->new->one, 1..2;
37
38 check two => map Foo->new->two, 1..2;
39
40 check three => map Foo->new->{three}, 1..2;
41
42 check four => map Foo->new->{four}, 1..2;
43
44 check five => map Foo->new->{five}, 1..2;
45
46 check six => map Foo->new->{six}, 1..2;
47
48 check seven => map Foo->new->{seven}, 1..2;
49
50 check eight => map Foo->new->{eight}, 1..2;
51 ok($c_ran, 'coerce defaults');
52
53 $c_ran = 0;
54
55 check nine => map Foo->new->nine, 1..2;
56 ok($c_ran, 'coerce lazy default');
57
58 is(Foo->new->ten, 5, 'non-ref default');
59
60 done_testing;