Commit | Line | Data |
c3398f5b |
1 | #!/usr/bin/env perl |
2 | use strict; |
3 | use warnings; |
4 | use Test::More tests => 15; |
5 | |
6 | my $lazy_run = 0; |
7 | |
8 | do { |
9 | package Class; |
10 | use Mouse; |
11 | |
12 | has lazy => ( |
13 | is => 'rw', |
14 | lazy => 1, |
15 | default => sub { ++$lazy_run }, |
16 | predicate => 'has_lazy', |
17 | ); |
18 | }; |
19 | |
20 | can_ok(Class => 'has_lazy'); |
21 | |
22 | my $object = Class->new; |
23 | is($lazy_run, 0, "lazy attribute not yet initialized"); |
24 | |
25 | ok(!$object->has_lazy, "no lazy value yet"); |
26 | is($lazy_run, 0, "lazy attribute not initialized by predicate"); |
27 | |
28 | is($object->lazy, 1, "lazy value"); |
29 | is($lazy_run, 1, "lazy coderef invoked once"); |
30 | |
31 | ok($object->has_lazy, "lazy value now"); |
32 | is($lazy_run, 1, "lazy coderef invoked once"); |
33 | |
34 | is($object->lazy, 1, "lazy value is cached"); |
35 | is($lazy_run, 1, "lazy coderef invoked once"); |
36 | |
37 | my $object2 = Class->new(lazy => 'very'); |
38 | is($lazy_run, 1, "lazy attribute not initialized when an argument is passed to the constructor"); |
39 | |
40 | ok($object2->has_lazy, "lazy value now"); |
41 | is($lazy_run, 1, "lazy attribute not initialized when checked with predicate"); |
42 | |
43 | is($object2->lazy, 'very', 'value from the constructor'); |
44 | is($lazy_run, 1, "lazy coderef not invoked, we already have a value"); |
45 | |