6 use Test::More tests => 57;
9 use Scalar::Util 'isweak';
26 ::ok(!$@, '... created the accessor method okay');
30 accessor => 'lazy_foo',
35 ::ok(!$@, '... created the lazy accessor method okay');
39 has 'foo_required' => (
40 accessor => 'foo_required',
44 ::ok(!$@, '... created the required accessor method okay');
48 accessor => 'foo_int',
52 ::ok(!$@, '... created the accessor method with type constraint okay');
56 accessor => 'foo_weak',
60 ::ok(!$@, '... created the accessor method with weak_ref okay');
64 accessor => 'foo_deref',
69 ::ok(!$@, '... created the accessor method with auto_deref okay');
72 has 'foo_deref_ro' => (
73 reader => 'foo_deref_ro',
78 ::ok(!$@, '... created the reader method with auto_deref okay');
81 has 'foo_deref_hash' => (
82 accessor => 'foo_deref_hash',
87 ::ok(!$@, '... created the reader method with auto_deref okay');
91 my $foo = Foo->new(foo_required => 'required');
97 is($foo->foo(), undef, '... got an unset value');
100 } '... foo wrote successfully';
101 is($foo->foo(), 100, '... got the correct set value');
103 ok(!isweak($foo->{foo}), '... it is not a weak reference');
109 } '... cannot create without the required attribute';
111 can_ok($foo, 'foo_required');
112 is($foo->foo_required(), 'required', '... got an unset value');
114 $foo->foo_required(100);
115 } '... foo_required wrote successfully';
116 is($foo->foo_required(), 100, '... got the correct set value');
119 $foo->foo_required(undef);
120 } '... foo_required died successfully';
122 ok(!isweak($foo->{foo_required}), '... it is not a weak reference');
126 ok(!exists($foo->{lazy_foo}), '... no value in lazy_foo slot');
128 can_ok($foo, 'lazy_foo');
129 is($foo->lazy_foo(), 10, '... got an deferred value');
131 # with type constraint
133 can_ok($foo, 'foo_int');
134 is($foo->foo_int(), undef, '... got an unset value');
137 } '... foo_int wrote successfully';
138 is($foo->foo_int(), 100, '... got the correct set value');
141 $foo->foo_int("Foo");
142 } '... foo_int died successfully';
144 ok(!isweak($foo->{foo_int}), '... it is not a weak reference');
150 can_ok($foo, 'foo_weak');
151 is($foo->foo_weak(), undef, '... got an unset value');
153 $foo->foo_weak($test);
154 } '... foo_weak wrote successfully';
155 is($foo->foo_weak(), $test, '... got the correct set value');
157 ok(isweak($foo->{foo_weak}), '... it is a weak reference');
159 can_ok( $foo, 'foo_deref');
160 is( $foo->foo_deref(), undef, '... unset value');
163 @list = $foo->foo_deref();
164 } "... doesn't deref undef value";
165 is_deeply( \@list, [], "returns empty list in list context");
168 $foo->foo_deref( [ qw/foo bar gorch/ ] );
169 } '... foo_deref wrote successfully';
171 is( Scalar::Util::reftype( scalar $foo->foo_deref() ), "ARRAY", "returns an array reference in scalar context" );
172 is_deeply( scalar($foo->foo_deref()), [ qw/foo bar gorch/ ], "correct array" );
174 is( scalar( () = $foo->foo_deref() ), 3, "returns list in list context" );
175 is_deeply( [ $foo->foo_deref() ], [ qw/foo bar gorch/ ], "correct list" );
178 can_ok( $foo, 'foo_deref' );
179 is( $foo->foo_deref_ro(), undef, "... unset value" );
182 $foo->foo_deref_ro( [] );
185 $foo->{foo_deref_ro} = [qw/la la la/];
187 is_deeply( scalar($foo->foo_deref_ro()), [qw/la la la/], "scalar context ro" );
188 is_deeply( [ $foo->foo_deref_ro() ], [qw/la la la/], "list context ro" );
190 can_ok( $foo, 'foo_deref_hash' );
191 is( $foo->foo_deref_hash(), undef, "... unset value" );
195 %hash = $foo->foo_deref_hash();
196 } "... doesn't deref undef value";
197 is_deeply( \%hash, {}, "returns empty list in list context");
200 $foo->foo_deref_hash( { foo => 1, bar => 2 } );
201 } '... foo_deref_hash wrote successfully';
203 is_deeply( scalar($foo->foo_deref_hash), { foo => 1, bar => 2 }, "scalar context" );
205 %hash = $foo->foo_deref_hash;
206 is_deeply( \%hash, { foo => 1, bar => 2 }, "list context");