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