6 use Test::More tests => 57;
9 use Scalar::Util 'isweak';
24 ::ok(!$@, '... created the accessor method okay');
28 accessor => 'lazy_foo',
33 ::ok(!$@, '... created the lazy accessor method okay');
37 has 'foo_required' => (
38 accessor => 'foo_required',
42 ::ok(!$@, '... created the required accessor method okay');
46 accessor => 'foo_int',
50 ::ok(!$@, '... created the accessor method with type constraint okay');
54 accessor => 'foo_weak',
58 ::ok(!$@, '... created the accessor method with weak_ref okay');
62 accessor => 'foo_deref',
67 ::ok(!$@, '... created the accessor method with auto_deref okay');
70 has 'foo_deref_ro' => (
71 reader => 'foo_deref_ro',
76 ::ok(!$@, '... created the reader method with auto_deref okay');
79 has 'foo_deref_hash' => (
80 accessor => 'foo_deref_hash',
85 ::ok(!$@, '... created the reader method with auto_deref okay');
89 my $foo = Foo->new(foo_required => 'required');
95 is($foo->foo(), undef, '... got an unset value');
98 } '... foo wrote successfully';
99 is($foo->foo(), 100, '... got the correct set value');
101 ok(!isweak($foo->{foo}), '... it is not a weak reference');
107 } '... cannot create without the required attribute';
109 can_ok($foo, 'foo_required');
110 is($foo->foo_required(), 'required', '... got an unset value');
112 $foo->foo_required(100);
113 } '... foo_required wrote successfully';
114 is($foo->foo_required(), 100, '... got the correct set value');
117 $foo->foo_required(undef);
118 } '... foo_required died successfully';
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");