Move is_valid_class_name into XS
[gitmo/Mouse.git] / t / 001_mouse / 011-lazy.t
CommitLineData
c3398f5b 1#!/usr/bin/env perl
2use strict;
3use warnings;
4use Test::More tests => 16;
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
51my $object2 = Class->new(lazy => 'very', lazy_value => "heh");
52is($lazy_run, 1, "lazy attribute not initialized when an argument is passed to the constructor");
53
54is($object2->lazy, 'very', 'value from the constructor');
55is($object2->lazy_value, 'heh', 'value from the constructor');
56is($lazy_run, 1, "lazy coderef not invoked, we already have a value");
57