Add predicates to the informational Mouse::Attribute attributes
[gitmo/Mouse.git] / t / 013-clearer.t
CommitLineData
c3398f5b 1#!/usr/bin/env perl
2use strict;
3use warnings;
4use Test::More tests => 28;
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 clearer => 'clear_lazy',
18 );
19};
20
21can_ok(Class => 'clear_lazy');
22
23my $object = Class->new;
24is($lazy_run, 0, "lazy attribute not yet initialized");
25
26ok(!$object->has_lazy, "no lazy value yet");
27is($lazy_run, 0, "lazy attribute not initialized by predicate");
28
29$object->clear_lazy;
30is($lazy_run, 0, "lazy attribute not initialized by clearer");
31
32ok(!$object->has_lazy, "no lazy value yet");
33is($lazy_run, 0, "lazy attribute not initialized by predicate");
34
35is($object->lazy, 1, "lazy value");
36is($lazy_run, 1, "lazy coderef invoked once");
37
38ok($object->has_lazy, "lazy value now");
39is($lazy_run, 1, "lazy coderef invoked once");
40
41is($object->lazy, 1, "lazy value is cached");
42is($lazy_run, 1, "lazy coderef invoked once");
43
44$object->clear_lazy;
45is($lazy_run, 1, "lazy coderef not invoked by clearer");
46
47ok(!$object->has_lazy, "no value now, clearer removed it");
48is($lazy_run, 1, "lazy attribute not initialized by predicate");
49
50is($object->lazy, 2, "new lazy value; previous was cleared");
51is($lazy_run, 2, "lazy coderef invoked twice");
52
53my $object2 = Class->new(lazy => 'very');
54is($lazy_run, 2, "lazy attribute not initialized when an argument is passed to the constructor");
55
56ok($object2->has_lazy, "lazy value now");
57is($lazy_run, 2, "lazy attribute not initialized when checked with predicate");
58
59is($object2->lazy, 'very', 'value from the constructor');
60is($lazy_run, 2, "lazy coderef not invoked, we already have a value");
61
62$object2->clear_lazy;
63is($lazy_run, 2, "lazy attribute not initialized by clearer");
64
65ok(!$object2->has_lazy, "no more lazy value");
66is($lazy_run, 2, "lazy attribute not initialized by predicate");
67
68is($object2->lazy, 3, 'new lazy value');
69is($lazy_run, 3, "lazy value re-created");
70