summaryrefslogtreecommitdiffstats
path: root/t/31-auto-deref.t
blob: 06762f6555c9ae16b107c10d90c16e426cb9d1f2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;
use Test::Moose;

{
    package Foo;
    use Moose;
    use Bread::Board::Declare;

    has foo => (
        is         => 'ro',
        isa        => 'ArrayRef',
        auto_deref => 1,
        block      => sub { ['foo', 'bar'] },
    );

    has bar => (
        is         => 'ro',
        isa        => 'HashRef',
        auto_deref => 1,
        block      => sub { {'foo' => 'bar'} },
    );
}

with_immutable {
{
    my $foo = Foo->new;

    is_deeply(scalar($foo->foo), ['foo', 'bar'], "scalar array");
    is_deeply([$foo->foo], ['foo', 'bar'], "list array");
    is_deeply(scalar($foo->bar), {'foo', 'bar'}, "scalar hash");
    is_deeply({$foo->foo}, {'foo', 'bar'}, "list hash");
}

{
    my $foo = Foo->new(foo => ['foo', 'bar'], bar => {'foo' => 'bar'});

    is_deeply(scalar($foo->foo), ['foo', 'bar'], "scalar array");
    is_deeply([$foo->foo], ['foo', 'bar'], "list array");
    is_deeply(scalar($foo->bar), {'foo', 'bar'}, "scalar hash");
    is_deeply({$foo->foo}, {'foo', 'bar'}, "list hash");
}
} 'Foo';

done_testing;