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