6 use Test::More tests => 57;
9 use Scalar::Util 'isweak';
22 ::ok(!$@, '... created the accessor method okay');
26 accessor => 'lazy_foo',
31 ::ok(!$@, '... created the lazy accessor method okay');
35 has 'foo_required' => (
36 accessor => 'foo_required',
40 ::ok(!$@, '... created the required accessor method okay');
44 accessor => 'foo_int',
48 ::ok(!$@, '... created the accessor method with type constraint okay');
52 accessor => 'foo_weak',
56 ::ok(!$@, '... created the accessor method with weak_ref okay');
60 accessor => 'foo_deref',
65 ::ok(!$@, '... created the accessor method with auto_deref okay');
68 has 'foo_deref_ro' => (
69 reader => 'foo_deref_ro',
74 ::ok(!$@, '... created the reader method with auto_deref okay');
77 has 'foo_deref_hash' => (
78 accessor => 'foo_deref_hash',
83 ::ok(!$@, '... created the reader method with auto_deref okay');
87 my $foo = Foo->new(foo_required => 'required');
93 is($foo->foo(), undef, '... got an unset value');
96 } '... foo wrote successfully';
97 is($foo->foo(), 100, '... got the correct set value');
99 ok(!isweak($foo->{foo}), '... it is not a weak reference');
105 } '... cannot create without the required attribute';
107 can_ok($foo, 'foo_required');
108 is($foo->foo_required(), 'required', '... got an unset value');
110 $foo->foo_required(100);
111 } '... foo_required wrote successfully';
112 is($foo->foo_required(), 100, '... got the correct set value');
115 $foo->foo_required(undef);
116 } '... foo_required did not die with undef';
118 is($foo->foo_required, undef, "value is undef");
120 ok(!isweak($foo->{foo_required}), '... it is not a weak reference');
124 ok(!exists($foo->{lazy_foo}), '... no value in lazy_foo slot');
126 can_ok($foo, 'lazy_foo');
127 is($foo->lazy_foo(), 10, '... got an deferred value');
129 # with type constraint
131 can_ok($foo, 'foo_int');
132 is($foo->foo_int(), undef, '... got an unset value');
135 } '... foo_int wrote successfully';
136 is($foo->foo_int(), 100, '... got the correct set value');
139 $foo->foo_int("Foo");
140 } '... foo_int died successfully';
142 ok(!isweak($foo->{foo_int}), '... it is not a weak reference');
148 can_ok($foo, 'foo_weak');
149 is($foo->foo_weak(), undef, '... got an unset value');
151 $foo->foo_weak($test);
152 } '... foo_weak wrote successfully';
153 is($foo->foo_weak(), $test, '... got the correct set value');
155 ok(isweak($foo->{foo_weak}), '... it is a weak reference');
157 can_ok( $foo, 'foo_deref');
158 is_deeply( [$foo->foo_deref()], [], '... default default value');
161 @list = $foo->foo_deref();
162 } "... doesn't deref undef value";
163 is_deeply( \@list, [], "returns empty list in list context");
166 $foo->foo_deref( [ qw/foo bar gorch/ ] );
167 } '... foo_deref wrote successfully';
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" );
172 is( scalar( () = $foo->foo_deref() ), 3, "returns list in list context" );
173 is_deeply( [ $foo->foo_deref() ], [ qw/foo bar gorch/ ], "correct list" );
176 can_ok( $foo, 'foo_deref' );
177 is_deeply( [$foo->foo_deref_ro()], [], "... default default value" );
180 $foo->foo_deref_ro( [] );
183 $foo->{foo_deref_ro} = [qw/la la la/];
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" );
188 can_ok( $foo, 'foo_deref_hash' );
189 is_deeply( { $foo->foo_deref_hash() }, {}, "... default default value" );
193 %hash = $foo->foo_deref_hash();
194 } "... doesn't deref undef value";
195 is_deeply( \%hash, {}, "returns empty list in list context");
198 $foo->foo_deref_hash( { foo => 1, bar => 2 } );
199 } '... foo_deref_hash wrote successfully';
201 is_deeply( scalar($foo->foo_deref_hash), { foo => 1, bar => 2 }, "scalar context" );
203 %hash = $foo->foo_deref_hash;
204 is_deeply( \%hash, { foo => 1, bar => 2 }, "list context");