16 default => sub { ++$lazy_run },
17 predicate => 'has_lazy',
18 clearer => 'clear_lazy',
22 can_ok(Class => 'clear_lazy');
27 my $object = Class->new;
28 is($lazy_run, 0, "lazy attribute not yet initialized");
30 ok(!$object->has_lazy, "no lazy value yet");
31 is($lazy_run, 0, "lazy attribute not initialized by predicate");
34 is($lazy_run, 0, "lazy attribute not initialized by clearer");
36 ok(!$object->has_lazy, "no lazy value yet");
37 is($lazy_run, 0, "lazy attribute not initialized by predicate");
39 is($object->lazy, 1, "lazy value");
40 is($lazy_run, 1, "lazy coderef invoked once");
42 ok($object->has_lazy, "lazy value now");
43 is($lazy_run, 1, "lazy coderef invoked once");
45 is($object->lazy, 1, "lazy value is cached");
46 is($lazy_run, 1, "lazy coderef invoked once");
49 is($lazy_run, 1, "lazy coderef not invoked by clearer");
51 ok(!$object->has_lazy, "no value now, clearer removed it");
52 is($lazy_run, 1, "lazy attribute not initialized by predicate");
54 is($object->lazy, 2, "new lazy value; previous was cleared");
55 is($lazy_run, 2, "lazy coderef invoked twice");
57 my $object2 = Class->new(lazy => 'very');
58 is($lazy_run, 2, "lazy attribute not initialized when an argument is passed to the constructor");
60 ok($object2->has_lazy, "lazy value now");
61 is($lazy_run, 2, "lazy attribute not initialized when checked with predicate");
63 is($object2->lazy, 'very', 'value from the constructor');
64 is($lazy_run, 2, "lazy coderef not invoked, we already have a value");
67 is($lazy_run, 2, "lazy attribute not initialized by clearer");
69 ok(!$object2->has_lazy, "no more lazy value");
70 is($lazy_run, 2, "lazy attribute not initialized by predicate");
72 is($object2->lazy, 3, 'new lazy value');
73 is($lazy_run, 3, "lazy value re-created");