fix weak lazy attributes (tome)
[gitmo/Moose.git] / t / attributes / attribute_reader_generation.t
CommitLineData
ca01a97b 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
a28e50e4 6use Test::More;
b10dde3a 7use Test::Fatal;
ca01a97b 8
7ff56534 9
ca01a97b 10{
11 package Foo;
ca01a97b 12 use Moose;
d03bd989 13
ca01a97b 14 eval {
15 has 'foo' => (
16 reader => 'get_foo'
17 );
18 };
19 ::ok(!$@, '... created the reader method okay');
d03bd989 20
ca01a97b 21 eval {
22 has 'lazy_foo' => (
d03bd989 23 reader => 'get_lazy_foo',
24 lazy => 1,
ca01a97b 25 default => sub { 10 }
26 );
27 };
d03bd989 28 ::ok(!$@, '... created the lazy reader method okay') or warn $@;
aa4c3a8d 29
12b0a103 30 eval {
31 has 'lazy_weak_foo' => (
32 reader => 'get_lazy_weak_foo',
33 lazy => 1,
34 default => sub { our $AREF = [] },
35 weak_ref => 1,
36 );
37 };
38 ::ok(!$@, '... created the lazy weak reader method okay') or warn $@;
39
aa4c3a8d 40 my $warn;
41
42 eval {
43 local $SIG{__WARN__} = sub { $warn = $_[0] };
44 has 'mtfnpy' => (
45 reder => 'get_mftnpy'
46 );
47 };
48 ::ok($warn, '... got a warning for mispelled attribute argument');
ca01a97b 49}
50
51{
52 my $foo = Foo->new;
53 isa_ok($foo, 'Foo');
54
55 can_ok($foo, 'get_foo');
56 is($foo->get_foo(), undef, '... got an undefined value');
b10dde3a 57 isnt( exception {
ca01a97b 58 $foo->get_foo(100);
b10dde3a 59 }, undef, '... get_foo is a read-only' );
d03bd989 60
ca01a97b 61 ok(!exists($foo->{lazy_foo}), '... no value in get_lazy_foo slot');
d03bd989 62
ca01a97b 63 can_ok($foo, 'get_lazy_foo');
64 is($foo->get_lazy_foo(), 10, '... got an deferred value');
b10dde3a 65 isnt( exception {
ca01a97b 66 $foo->get_lazy_foo(100);
b10dde3a 67 }, undef, '... get_lazy_foo is a read-only' );
12b0a103 68
69 is($foo->get_lazy_weak_foo(), $Foo::AREF, 'got the right value');
70 ok($foo->meta->get_meta_instance->slot_value_is_weak($foo, 'lazy_weak_foo'),
71 '... and it is weak');
ca01a97b 72}
73
74{
312e0f0c 75 my $foo = Foo->new;
76 isa_ok($foo, 'Foo');
77
78 my $attr = $foo->meta->find_attribute_by_name("lazy_foo");
79
80 isa_ok( $attr, "Moose::Meta::Attribute" );
81
82 ok( $attr->is_lazy, "it's lazy" );
83
84 is( $attr->get_raw_value($foo), undef, "raw value" );
85
86 is( $attr->get_value($foo), 10, "lazy value" );
87
88 is( $attr->get_raw_value($foo), 10, "raw value" );
12b0a103 89
90 my $lazy_weak_attr = $foo->meta->find_attribute_by_name("lazy_weak_foo");
91
92 is( $lazy_weak_attr->get_value($foo), $Foo::AREF, "it's the right value" );
93
94 ok( $foo->meta->get_meta_instance->slot_value_is_weak($foo, 'lazy_weak_foo'), "and it is weak");
312e0f0c 95}
96
97{
ca01a97b 98 my $foo = Foo->new(foo => 10, lazy_foo => 100);
99 isa_ok($foo, 'Foo');
100
101 is($foo->get_foo(), 10, '... got the correct value');
d03bd989 102 is($foo->get_lazy_foo(), 100, '... got the correct value');
ca01a97b 103}
104
a28e50e4 105done_testing;