Commit | Line | Data |
615d5d5f |
1 | #!/usr/bin/env perl |
2 | use strict; |
3 | use warnings; |
45ea8620 |
4 | use Test::More tests => 15; |
eab81545 |
5 | use Test::Exception; |
615d5d5f |
6 | |
7 | do { |
8 | package Class; |
9 | use Mouse; |
10 | |
11 | has array => ( |
12 | is => 'rw', |
13 | isa => 'ArrayRef', |
14 | auto_deref => 1, |
15 | ); |
16 | |
17 | has hash => ( |
18 | is => 'rw', |
19 | isa => 'HashRef', |
20 | auto_deref => 1, |
21 | ); |
45ea8620 |
22 | |
23 | ::throws_ok { |
24 | has any => ( |
25 | is => 'rw', |
26 | auto_deref => 1, |
27 | ); |
97661b77 |
28 | } qr/You cannot auto-dereference without specifying a type constraint on attribute \(any\)/; |
45ea8620 |
29 | |
30 | ::throws_ok { |
31 | has scalar => ( |
32 | is => 'rw', |
33 | isa => 'Value', |
34 | auto_deref => 1, |
35 | ); |
97661b77 |
36 | } qr/You cannot auto-dereference anything other than a ArrayRef or HashRef on attribute \(scalar\)/; |
615d5d5f |
37 | }; |
38 | |
39 | my $obj; |
40 | lives_ok { |
41 | $obj = Class->new; |
42 | } qr/auto_deref without defaults don't explode on new/; |
43 | |
44 | my ($array, @array, $hash, %hash); |
45 | lives_ok { |
46 | @array = $obj->array; |
47 | %hash = $obj->hash; |
48 | $array = $obj->array; |
49 | $hash = $obj->hash; |
50 | |
51 | $obj->array; |
52 | $obj->hash; |
53 | } qr/auto_deref without default doesn't explode on get/; |
54 | |
55 | is($obj->array, undef, "array without value is undef in scalar context"); |
56 | is($obj->hash, undef, "hash without value is undef in scalar context"); |
57 | |
3cf68001 |
58 | is(@array, 0, "array without value is empty in list context"); |
59 | is(keys %hash, 0, "hash without value is empty in list context"); |
615d5d5f |
60 | |
61 | @array = $obj->array([1, 2, 3]); |
62 | %hash = $obj->hash({foo => 1, bar => 2}); |
63 | |
3cf68001 |
64 | is_deeply(\@array, [1, 2, 3], "setter returns the dereferenced list"); |
65 | is_deeply(\%hash, {foo => 1, bar => 2}, "setter returns the dereferenced hash"); |
615d5d5f |
66 | |
67 | lives_ok { |
68 | @array = $obj->array; |
69 | %hash = $obj->hash; |
70 | $array = $obj->array; |
71 | $hash = $obj->hash; |
72 | |
73 | $obj->array; |
74 | $obj->hash; |
75 | } qr/auto_deref without default doesn't explode on get/; |
76 | |
77 | is_deeply($array, [1, 2, 3], "auto_deref in scalar context gives the reference"); |
78 | is_deeply($hash, {foo => 1, bar => 2}, "auto_deref in scalar context gives the reference"); |
79 | |
3cf68001 |
80 | is_deeply(\@array, [1, 2, 3], "auto_deref in list context gives the list"); |
81 | is_deeply(\%hash, {foo => 1, bar => 2}, "auto_deref in list context gives the hash"); |
615d5d5f |
82 | |