docs-n-attr-refactor
[gitmo/Moose.git] / t / 032_attribute_accessor_generation.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 57;
7 use Test::Exception;
8
9 use Scalar::Util 'isweak';
10
11 BEGIN {
12     use_ok('Moose');           
13 }
14
15 {
16     package Foo;
17     use strict;
18     use warnings;
19     use Moose;
20     
21     eval {
22         has 'foo' => (
23             accessor => 'foo',
24         );
25     };
26     ::ok(!$@, '... created the accessor method okay');
27     
28     eval {
29         has 'lazy_foo' => (
30             accessor => 'lazy_foo', 
31             lazy     => 1, 
32             default  => sub { 10 }
33         );
34     };
35     ::ok(!$@, '... created the lazy accessor method okay');              
36     
37
38     eval {
39         has 'foo_required' => (
40             accessor => 'foo_required',
41             required => 1,
42         );
43     };
44     ::ok(!$@, '... created the required accessor method okay');
45
46     eval {
47         has 'foo_int' => (
48             accessor => 'foo_int',
49             isa      => 'Int',
50         );
51     };
52     ::ok(!$@, '... created the accessor method with type constraint okay');    
53     
54     eval {
55         has 'foo_weak' => (
56             accessor => 'foo_weak',
57             weak_ref => 1
58         );
59     };
60     ::ok(!$@, '... created the accessor method with weak_ref okay');    
61
62     eval {
63         has 'foo_deref' => (
64             accessor => 'foo_deref',
65             isa => 'ArrayRef',
66             auto_deref => 1,
67         );
68     };
69     ::ok(!$@, '... created the accessor method with auto_deref okay');    
70
71     eval {
72         has 'foo_deref_ro' => (
73             reader => 'foo_deref_ro',
74             isa => 'ArrayRef',
75             auto_deref => 1,
76         );
77     };
78     ::ok(!$@, '... created the reader method with auto_deref okay');    
79
80     eval {
81         has 'foo_deref_hash' => (
82             accessor => 'foo_deref_hash',
83             isa => 'HashRef',
84             auto_deref => 1,
85         );
86     };
87     ::ok(!$@, '... created the reader method with auto_deref okay');    
88 }
89
90 {
91     my $foo = Foo->new(foo_required => 'required');
92     isa_ok($foo, 'Foo');
93
94     # regular accessor
95
96     can_ok($foo, 'foo');
97     is($foo->foo(), undef, '... got an unset value');
98     lives_ok {
99         $foo->foo(100);
100     } '... foo wrote successfully';
101     is($foo->foo(), 100, '... got the correct set value');   
102     
103     ok(!isweak($foo->{foo}), '... it is not a weak reference');   
104     
105     # required writer
106     
107     dies_ok {
108         Foo->new;
109     } '... cannot create without the required attribute';
110
111     can_ok($foo, 'foo_required');
112     is($foo->foo_required(), 'required', '... got an unset value');
113     lives_ok {
114         $foo->foo_required(100);
115     } '... foo_required wrote successfully';
116     is($foo->foo_required(), 100, '... got the correct set value');    
117     
118     dies_ok {
119         $foo->foo_required(undef);
120     } '... foo_required died successfully';    
121
122     ok(!isweak($foo->{foo_required}), '... it is not a weak reference'); 
123     
124     # lazy
125     
126     ok(!exists($foo->{lazy_foo}), '... no value in lazy_foo slot');
127     
128     can_ok($foo, 'lazy_foo');
129     is($foo->lazy_foo(), 10, '... got an deferred value');        
130     
131     # with type constraint
132     
133     can_ok($foo, 'foo_int');
134     is($foo->foo_int(), undef, '... got an unset value');
135     lives_ok {
136         $foo->foo_int(100);
137     } '... foo_int wrote successfully';
138     is($foo->foo_int(), 100, '... got the correct set value'); 
139     
140     dies_ok {
141         $foo->foo_int("Foo");
142     } '... foo_int died successfully';   
143         
144     ok(!isweak($foo->{foo_int}), '... it is not a weak reference');        
145         
146     # with weak_ref
147     
148     my $test = [];
149     
150     can_ok($foo, 'foo_weak');
151     is($foo->foo_weak(), undef, '... got an unset value');
152     lives_ok {
153         $foo->foo_weak($test);
154     } '... foo_weak wrote successfully';
155     is($foo->foo_weak(), $test, '... got the correct set value'); 
156     
157     ok(isweak($foo->{foo_weak}), '... it is a weak reference');
158
159     can_ok( $foo, 'foo_deref');
160     is( $foo->foo_deref(), undef, '... unset value');
161     my @list;
162     lives_ok {
163         @list = $foo->foo_deref();
164     } "... doesn't deref undef value";
165     is_deeply( \@list, [], "returns empty list in list context");
166
167     lives_ok {
168         $foo->foo_deref( [ qw/foo bar gorch/ ] );
169     } '... foo_deref wrote successfully';
170
171     is( Scalar::Util::reftype( scalar $foo->foo_deref() ), "ARRAY", "returns an array reference in scalar context" );
172     is_deeply( scalar($foo->foo_deref()), [ qw/foo bar gorch/ ], "correct array" );
173
174     is( scalar( () = $foo->foo_deref() ), 3, "returns list in list context" );
175     is_deeply( [ $foo->foo_deref() ], [ qw/foo bar gorch/ ], "correct list" );
176
177
178     can_ok( $foo, 'foo_deref' );
179     is( $foo->foo_deref_ro(), undef, "... unset value" );
180
181     dies_ok {
182         $foo->foo_deref_ro( [] );
183     } "... read only";
184
185     $foo->{foo_deref_ro} = [qw/la la la/];
186
187     is_deeply( scalar($foo->foo_deref_ro()), [qw/la la la/], "scalar context ro" );
188     is_deeply( [ $foo->foo_deref_ro() ], [qw/la la la/], "list context ro" );
189
190     can_ok( $foo, 'foo_deref_hash' );
191     is( $foo->foo_deref_hash(), undef, "... unset value" );
192
193     my %hash;
194     lives_ok {
195         %hash = $foo->foo_deref_hash();
196     } "... doesn't deref undef value";
197     is_deeply( \%hash, {}, "returns empty list in list context");
198
199     lives_ok {
200         $foo->foo_deref_hash( { foo => 1, bar => 2 } );
201     } '... foo_deref_hash wrote successfully';
202
203     is_deeply( scalar($foo->foo_deref_hash), { foo => 1, bar => 2 }, "scalar context" );
204
205     %hash = $foo->foo_deref_hash;
206     is_deeply( \%hash, { foo => 1, bar => 2 }, "list context");
207 }
208
209
210