Commit | Line | Data |
46389f86 |
1 | use strictures 1; |
2 | use Test::More; |
3 | |
e6c7abb4 |
4 | my $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 { {} } |
46389f86 |
25 | } |
26 | |
27 | sub check { |
28 | my ($attr, @h) = @_; |
29 | |
30 | is_deeply($h[$_], {}, "${attr}: empty hashref \$h[$_]") for 0..1; |
31 | |
32 | isnt($h[0],$h[1], "${attr}: not the same hashref"); |
33 | } |
34 | |
35 | check one => map Foo->new->one, 1..2; |
36 | |
37 | check two => map Foo->new->two, 1..2; |
38 | |
649ac264 |
39 | check three => map Foo->new->{three}, 1..2; |
40 | |
41 | check four => map Foo->new->{four}, 1..2; |
42 | |
d02da2bc |
43 | check five => map Foo->new->{five}, 1..2; |
44 | |
e8dc5201 |
45 | check six => map Foo->new->{six}, 1..2; |
46 | |
c9a5dcbd |
47 | check seven => map Foo->new->{seven}, 1..2; |
48 | |
e6c7abb4 |
49 | check eight => map Foo->new->{eight}, 1..2; |
50 | ok($c_ran, 'coerce defaults'); |
51 | |
52 | $c_ran = 0; |
53 | |
54 | check nine => map Foo->new->nine, 1..2; |
55 | ok($c_ran, 'coerce lazy default'); |
56 | |
46389f86 |
57 | done_testing; |