9 use Scalar::Util 'isweak';
21 ::ok(!$@, '... created the accessor method okay');
25 accessor => 'lazy_foo',
30 ::ok(!$@, '... created the lazy accessor method okay');
34 has 'foo_required' => (
35 accessor => 'foo_required',
39 ::ok(!$@, '... created the required accessor method okay');
43 accessor => 'foo_int',
47 ::ok(!$@, '... created the accessor method with type constraint okay');
51 accessor => 'foo_weak',
55 ::ok(!$@, '... created the accessor method with weak_ref okay');
59 accessor => 'foo_deref',
64 ::ok(!$@, '... created the accessor method with auto_deref okay');
67 has 'foo_deref_ro' => (
68 reader => 'foo_deref_ro',
73 ::ok(!$@, '... created the reader method with auto_deref okay');
76 has 'foo_deref_hash' => (
77 accessor => 'foo_deref_hash',
82 ::ok(!$@, '... created the reader method with auto_deref okay');
86 my $foo = Foo->new(foo_required => 'required');
92 is($foo->foo(), undef, '... got an unset value');
95 }, undef, '... foo wrote successfully' );
96 is($foo->foo(), 100, '... got the correct set value');
98 ok(!isweak($foo->{foo}), '... it is not a weak reference');
104 }, undef, '... cannot create without the required attribute' );
106 can_ok($foo, 'foo_required');
107 is($foo->foo_required(), 'required', '... got an unset value');
109 $foo->foo_required(100);
110 }, undef, '... foo_required wrote successfully' );
111 is($foo->foo_required(), 100, '... got the correct set value');
114 $foo->foo_required(undef);
115 }, undef, '... foo_required did not die with undef' );
117 is($foo->foo_required, undef, "value is undef");
119 ok(!isweak($foo->{foo_required}), '... it is not a weak reference');
123 ok(!exists($foo->{lazy_foo}), '... no value in lazy_foo slot');
125 can_ok($foo, 'lazy_foo');
126 is($foo->lazy_foo(), 10, '... got an deferred value');
128 # with type constraint
130 can_ok($foo, 'foo_int');
131 is($foo->foo_int(), undef, '... got an unset value');
134 }, undef, '... foo_int wrote successfully' );
135 is($foo->foo_int(), 100, '... got the correct set value');
138 $foo->foo_int("Foo");
139 }, undef, '... foo_int died successfully' );
141 ok(!isweak($foo->{foo_int}), '... it is not a weak reference');
147 can_ok($foo, 'foo_weak');
148 is($foo->foo_weak(), undef, '... got an unset value');
150 $foo->foo_weak($test);
151 }, undef, '... foo_weak wrote successfully' );
152 is($foo->foo_weak(), $test, '... got the correct set value');
154 ok(isweak($foo->{foo_weak}), '... it is a weak reference');
156 can_ok( $foo, 'foo_deref');
157 is_deeply( [$foo->foo_deref()], [], '... default default value');
160 @list = $foo->foo_deref();
161 }, undef, "... doesn't deref undef value" );
162 is_deeply( \@list, [], "returns empty list in list context");
165 $foo->foo_deref( [ qw/foo bar gorch/ ] );
166 }, undef, '... foo_deref wrote successfully' );
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" );
171 is( scalar( () = $foo->foo_deref() ), 3, "returns list in list context" );
172 is_deeply( [ $foo->foo_deref() ], [ qw/foo bar gorch/ ], "correct list" );
175 can_ok( $foo, 'foo_deref' );
176 is_deeply( [$foo->foo_deref_ro()], [], "... default default value" );
179 $foo->foo_deref_ro( [] );
180 }, undef, "... read only" );
182 $foo->{foo_deref_ro} = [qw/la la la/];
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" );
187 can_ok( $foo, 'foo_deref_hash' );
188 is_deeply( { $foo->foo_deref_hash() }, {}, "... default default value" );
192 %hash = $foo->foo_deref_hash();
193 }, undef, "... doesn't deref undef value" );
194 is_deeply( \%hash, {}, "returns empty list in list context");
197 $foo->foo_deref_hash( { foo => 1, bar => 2 } );
198 }, undef, '... foo_deref_hash wrote successfully' );
200 is_deeply( scalar($foo->foo_deref_hash), { foo => 1, bar => 2 }, "scalar context" );
202 %hash = $foo->foo_deref_hash;
203 is_deeply( \%hash, { foo => 1, bar => 2 }, "list context");