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