updated dependents test
[gitmo/Moo.git] / t / accessor-default.t
CommitLineData
46389f86 1use strictures 1;
2use Test::More;
3
e6c7abb4 4my $c_ran;
46389f86 5{
6 package Foo;
7
8 use Sub::Quote;
b1eebd55 9 use Moo;
46389f86 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{ {} });
649ac264 15 has four => (is => 'ro', builder => '_build_four');
46389f86 16 sub _build_four { {} }
d02da2bc 17 has five => (is => 'ro', init_arg => undef, default => sub { {} });
e8dc5201 18 has six => (is => 'ro', builder => 1);
19 sub _build_six { {} }
c9a5dcbd 20 has seven => (is => 'ro', required => 1, default => quote_sub q{ {} });
e6c7abb4 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 { {} }
2a894d0c 25 has ten => (is => 'lazy', default => 5 );
2fa823de 26 has eleven => (is => 'ro', default => 5 );
27 has twelve => (is => 'lazy', default => 0 );
28 has thirteen => (is => 'ro', default => 0 );
60cc0a5a 29 has fourteen => (is => 'ro', required => 1, builder => '_build_fourteen');
30 sub _build_fourteen { {} }
46389f86 31}
32
33sub check {
34 my ($attr, @h) = @_;
35
36 is_deeply($h[$_], {}, "${attr}: empty hashref \$h[$_]") for 0..1;
37
38 isnt($h[0],$h[1], "${attr}: not the same hashref");
39}
40
41check one => map Foo->new->one, 1..2;
42
43check two => map Foo->new->two, 1..2;
44
649ac264 45check three => map Foo->new->{three}, 1..2;
46
47check four => map Foo->new->{four}, 1..2;
48
d02da2bc 49check five => map Foo->new->{five}, 1..2;
50
e8dc5201 51check six => map Foo->new->{six}, 1..2;
52
c9a5dcbd 53check seven => map Foo->new->{seven}, 1..2;
54
60cc0a5a 55check fourteen => map Foo->new->{fourteen}, 1..2;
56
e6c7abb4 57check eight => map Foo->new->{eight}, 1..2;
58ok($c_ran, 'coerce defaults');
59
60$c_ran = 0;
61
62check nine => map Foo->new->nine, 1..2;
63ok($c_ran, 'coerce lazy default');
64
2a894d0c 65is(Foo->new->ten, 5, 'non-ref default');
2fa823de 66is(Foo->new->eleven, 5, 'eager non-ref default');
67is(Foo->new->twelve, 0, 'false non-ref default');
68is(Foo->new->thirteen, 0, 'eager false non-ref default');
2a894d0c 69
46389f86 70done_testing;