Fix an error message to compatible with Moose
[gitmo/Mouse.git] / t / 001_mouse / 011-lazy.t
CommitLineData
c3398f5b 1#!/usr/bin/env perl
2use strict;
3use warnings;
8ab2c6ab 4use Test::More;
c3398f5b 5
6my $lazy_run = 0;
7
8do {
9 package Class;
10 use Mouse;
11
12 has lazy => (
13 is => 'rw',
14 lazy => 1,
15 default => sub { ++$lazy_run },
16 );
17
18 has lazy_value => (
19 is => 'rw',
20 lazy => 1,
21 default => "welp",
22 );
23
2c689df4 24 eval {
c3398f5b 25 has lazy_no_default => (
26 is => 'rw',
27 lazy => 1,
28 );
2c689df4 29 };
30 ::like $@, qr/You cannot have a 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;