Commit | Line | Data |
51c107ef |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
a28e50e4 |
6 | use Test::More; |
bad76b8e |
7 | |
51c107ef |
8 | |
9 | { |
10 | package Foo; |
11 | use Moose; |
12 | |
13 | our $foo_default_called = 0; |
14 | |
15 | has foo => ( |
16 | is => 'rw', |
17 | isa => 'Str', |
bad76b8e |
18 | default => sub { $foo_default_called++; 'foo' }, |
51c107ef |
19 | ); |
20 | |
21 | our $bar_default_called = 0; |
22 | |
23 | has bar => ( |
24 | is => 'rw', |
25 | isa => 'Str', |
26 | lazy => 1, |
27 | default => sub { $bar_default_called++; 'bar' }, |
28 | ); |
29 | |
30 | __PACKAGE__->meta->make_immutable; |
31 | } |
32 | |
33 | my $foo = Foo->new(); |
34 | |
35 | is($Foo::foo_default_called, 1, "foo default was only called once during constructor"); |
36 | |
37 | $foo->bar(); |
38 | |
39 | is($Foo::bar_default_called, 1, "bar default was only called once when lazy attribute is accessed"); |
a28e50e4 |
40 | |
41 | done_testing; |