4 use Test::More tests => 28;
15 default => sub { ++$lazy_run },
16 predicate => 'has_lazy',
17 clearer => 'clear_lazy',
21 can_ok(Class => 'clear_lazy');
23 my $object = Class->new;
24 is($lazy_run, 0, "lazy attribute not yet initialized");
26 ok(!$object->has_lazy, "no lazy value yet");
27 is($lazy_run, 0, "lazy attribute not initialized by predicate");
30 is($lazy_run, 0, "lazy attribute not initialized by clearer");
32 ok(!$object->has_lazy, "no lazy value yet");
33 is($lazy_run, 0, "lazy attribute not initialized by predicate");
35 is($object->lazy, 1, "lazy value");
36 is($lazy_run, 1, "lazy coderef invoked once");
38 ok($object->has_lazy, "lazy value now");
39 is($lazy_run, 1, "lazy coderef invoked once");
41 is($object->lazy, 1, "lazy value is cached");
42 is($lazy_run, 1, "lazy coderef invoked once");
45 is($lazy_run, 1, "lazy coderef not invoked by clearer");
47 ok(!$object->has_lazy, "no value now, clearer removed it");
48 is($lazy_run, 1, "lazy attribute not initialized by predicate");
50 is($object->lazy, 2, "new lazy value; previous was cleared");
51 is($lazy_run, 2, "lazy coderef invoked twice");
53 my $object2 = Class->new(lazy => 'very');
54 is($lazy_run, 2, "lazy attribute not initialized when an argument is passed to the constructor");
56 ok($object2->has_lazy, "lazy value now");
57 is($lazy_run, 2, "lazy attribute not initialized when checked with predicate");
59 is($object2->lazy, 'very', 'value from the constructor');
60 is($lazy_run, 2, "lazy coderef not invoked, we already have a value");
63 is($lazy_run, 2, "lazy attribute not initialized by clearer");
65 ok(!$object2->has_lazy, "no more lazy value");
66 is($lazy_run, 2, "lazy attribute not initialized by predicate");
68 is($object2->lazy, 3, 'new lazy value');
69 is($lazy_run, 3, "lazy value re-created");