use strict;
use warnings;
-use Test::More tests => 33;
+use Test::More tests => 57;
use Test::Exception;
use Scalar::Util 'isweak';
{
package Foo;
- use strict;
- use warnings;
use Moose;
eval {
);
};
::ok(!$@, '... created the accessor method with weak_ref okay');
+
+ eval {
+ has 'foo_deref' => (
+ accessor => 'foo_deref',
+ isa => 'ArrayRef',
+ auto_deref => 1,
+ );
+ };
+ ::ok(!$@, '... created the accessor method with auto_deref okay');
+
+ eval {
+ has 'foo_deref_ro' => (
+ reader => 'foo_deref_ro',
+ isa => 'ArrayRef',
+ auto_deref => 1,
+ );
+ };
+ ::ok(!$@, '... created the reader method with auto_deref okay');
+
+ eval {
+ has 'foo_deref_hash' => (
+ accessor => 'foo_deref_hash',
+ isa => 'HashRef',
+ auto_deref => 1,
+ );
+ };
+ ::ok(!$@, '... created the reader method with auto_deref okay');
}
{
ok(isweak($foo->{foo_weak}), '... it is a weak reference');
+ can_ok( $foo, 'foo_deref');
+ is_deeply( [$foo->foo_deref()], [], '... default default value');
+ my @list;
+ lives_ok {
+ @list = $foo->foo_deref();
+ } "... doesn't deref undef value";
+ is_deeply( \@list, [], "returns empty list in list context");
+
+ lives_ok {
+ $foo->foo_deref( [ qw/foo bar gorch/ ] );
+ } '... foo_deref wrote successfully';
+
+ is( Scalar::Util::reftype( scalar $foo->foo_deref() ), "ARRAY", "returns an array reference in scalar context" );
+ is_deeply( scalar($foo->foo_deref()), [ qw/foo bar gorch/ ], "correct array" );
+
+ is( scalar( () = $foo->foo_deref() ), 3, "returns list in list context" );
+ is_deeply( [ $foo->foo_deref() ], [ qw/foo bar gorch/ ], "correct list" );
+
+
+ can_ok( $foo, 'foo_deref' );
+ is_deeply( [$foo->foo_deref_ro()], [], "... default default value" );
+
+ dies_ok {
+ $foo->foo_deref_ro( [] );
+ } "... read only";
+
+ $foo->{foo_deref_ro} = [qw/la la la/];
+
+ is_deeply( scalar($foo->foo_deref_ro()), [qw/la la la/], "scalar context ro" );
+ is_deeply( [ $foo->foo_deref_ro() ], [qw/la la la/], "list context ro" );
+
+ can_ok( $foo, 'foo_deref_hash' );
+ is_deeply( { $foo->foo_deref_hash() }, {}, "... default default value" );
+
+ my %hash;
+ lives_ok {
+ %hash = $foo->foo_deref_hash();
+ } "... doesn't deref undef value";
+ is_deeply( \%hash, {}, "returns empty list in list context");
+
+ lives_ok {
+ $foo->foo_deref_hash( { foo => 1, bar => 2 } );
+ } '... foo_deref_hash wrote successfully';
+
+ is_deeply( scalar($foo->foo_deref_hash), { foo => 1, bar => 2 }, "scalar context" );
+
+ %hash = $foo->foo_deref_hash;
+ is_deeply( \%hash, { foo => 1, bar => 2 }, "list context");
}