Commit | Line | Data |
c3398f5b |
1 | #!/usr/bin/env perl |
2 | use strict; |
3 | use warnings; |
4 | use Test::More tests => 16; |
8c831d08 |
5 | use Mouse::Util ':test'; |
c3398f5b |
6 | |
7 | my $lazy_run = 0; |
8 | |
9 | do { |
10 | package Class; |
11 | use Mouse; |
12 | |
13 | has lazy => ( |
14 | is => 'rw', |
15 | lazy => 1, |
16 | default => sub { ++$lazy_run }, |
17 | ); |
18 | |
19 | has lazy_value => ( |
20 | is => 'rw', |
21 | lazy => 1, |
22 | default => "welp", |
23 | ); |
24 | |
25 | ::throws_ok { |
26 | has lazy_no_default => ( |
27 | is => 'rw', |
28 | lazy => 1, |
29 | ); |
4eb1339a |
30 | } qr/You cannot have lazy attribute \(lazy_no_default\) without specifying a default value for it/; |
c3398f5b |
31 | }; |
32 | |
33 | my $object = Class->new; |
34 | is($lazy_run, 0, "lazy attribute not yet initialized"); |
35 | |
36 | is($object->lazy, 1, "lazy coderef"); |
37 | is($lazy_run, 1, "lazy coderef invoked once"); |
38 | |
39 | is($object->lazy, 1, "lazy coderef is cached"); |
40 | is($lazy_run, 1, "lazy coderef invoked once"); |
41 | |
42 | is($object->lazy_value, 'welp', "lazy value"); |
43 | is($lazy_run, 1, "lazy coderef invoked once"); |
44 | |
45 | is($object->lazy_value("newp"), "newp", "set new value"); |
46 | is($lazy_run, 1, "lazy coderef invoked once"); |
47 | |
48 | is($object->lazy_value, "newp", "got new value"); |
49 | is($lazy_run, 1, "lazy coderef invoked once"); |
50 | |
51 | my $object2 = Class->new(lazy => 'very', lazy_value => "heh"); |
52 | is($lazy_run, 1, "lazy attribute not initialized when an argument is passed to the constructor"); |
53 | |
54 | is($object2->lazy, 'very', 'value from the constructor'); |
55 | is($object2->lazy_value, 'heh', 'value from the constructor'); |
56 | is($lazy_run, 1, "lazy coderef not invoked, we already have a value"); |
57 | |