Support for anonymous enums: enum [elements]
[gitmo/Mouse.git] / t / 012-predicate.t
CommitLineData
c3398f5b 1#!/usr/bin/env perl
2use strict;
3use warnings;
4use Test::More tests => 15;
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 predicate => 'has_lazy',
17 );
18};
19
20can_ok(Class => 'has_lazy');
21
22my $object = Class->new;
23is($lazy_run, 0, "lazy attribute not yet initialized");
24
25ok(!$object->has_lazy, "no lazy value yet");
26is($lazy_run, 0, "lazy attribute not initialized by predicate");
27
28is($object->lazy, 1, "lazy value");
29is($lazy_run, 1, "lazy coderef invoked once");
30
31ok($object->has_lazy, "lazy value now");
32is($lazy_run, 1, "lazy coderef invoked once");
33
34is($object->lazy, 1, "lazy value is cached");
35is($lazy_run, 1, "lazy coderef invoked once");
36
37my $object2 = Class->new(lazy => 'very');
38is($lazy_run, 1, "lazy attribute not initialized when an argument is passed to the constructor");
39
40ok($object2->has_lazy, "lazy value now");
41is($lazy_run, 1, "lazy attribute not initialized when checked with predicate");
42
43is($object2->lazy, 'very', 'value from the constructor');
44is($lazy_run, 1, "lazy coderef not invoked, we already have a value");
45