Make strict_constructor public
[gitmo/Mouse.git] / t / 001_mouse / 011-lazy.t
CommitLineData
c3398f5b 1#!/usr/bin/env perl
2use strict;
3use warnings;
8ab2c6ab 4use Test::More;
eab81545 5use Test::Exception;
c3398f5b 6
7my $lazy_run = 0;
8
9do {
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
33my $object = Class->new;
34is($lazy_run, 0, "lazy attribute not yet initialized");
35
36is($object->lazy, 1, "lazy coderef");
37is($lazy_run, 1, "lazy coderef invoked once");
38
39is($object->lazy, 1, "lazy coderef is cached");
40is($lazy_run, 1, "lazy coderef invoked once");
41
42is($object->lazy_value, 'welp', "lazy value");
43is($lazy_run, 1, "lazy coderef invoked once");
44
45is($object->lazy_value("newp"), "newp", "set new value");
46is($lazy_run, 1, "lazy coderef invoked once");
47
48is($object->lazy_value, "newp", "got new value");
49is($lazy_run, 1, "lazy coderef invoked once");
50
8ab2c6ab 51is($object->lazy(42), 42);
52is($object->lazy_value(3.14), 3.14);
53
c3398f5b 54my $object2 = Class->new(lazy => 'very', lazy_value => "heh");
55is($lazy_run, 1, "lazy attribute not initialized when an argument is passed to the constructor");
56
57is($object2->lazy, 'very', 'value from the constructor');
58is($object2->lazy_value, 'heh', 'value from the constructor');
59is($lazy_run, 1, "lazy coderef not invoked, we already have a value");
60
8ab2c6ab 61done_testing;