1use strict;
2use warnings;
3
4use Test::More;
5use Test::Fatal;
6
7
8{
9    package Customer;
10    use Moose;
11
12    package Firm;
13    use Moose;
14    use Moose::Util::TypeConstraints;
15
16    ::is( ::exception {
17        has 'customers' => (
18            is         => 'ro',
19            isa        => subtype('ArrayRef' => where {
20                            (blessed($_) && $_->isa('Customer') || return) for @$_; 1 }),
21            auto_deref => 1,
22        );
23    }, undef, '... successfully created attr' );
24}
25
26{
27    my $customer = Customer->new;
28    isa_ok($customer, 'Customer');
29
30    my $firm = Firm->new(customers => [ $customer ]);
31    isa_ok($firm, 'Firm');
32
33    can_ok($firm, 'customers');
34
35    is_deeply(
36        [ $firm->customers ],
37        [ $customer ],
38        '... got the right dereferenced value'
39    );
40}
41
42{
43    my $firm = Firm->new();
44    isa_ok($firm, 'Firm');
45
46    can_ok($firm, 'customers');
47
48    is_deeply(
49        [ $firm->customers ],
50        [],
51        '... got the right dereferenced value'
52    );
53}
54
55{
56    package AutoDeref;
57    use Moose;
58
59    has 'bar' => (
60        is         => 'rw',
61        isa        => 'ArrayRef[Int]',
62        auto_deref => 1,
63    );
64}
65
66{
67    my $autoderef = AutoDeref->new;
68
69    isnt( exception {
70        $autoderef->bar(1, 2, 3);
71    }, undef, '... its auto-de-ref-ing, not auto-en-ref-ing' );
72
73    is( exception {
74        $autoderef->bar([ 1, 2, 3 ])
75    }, undef, '... set the results of bar correctly' );
76
77    is_deeply [ $autoderef->bar ], [ 1, 2, 3 ], '... auto-dereffed correctly';
78}
79
80done_testing;
81