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