0edffeda456de15eac6bb7f6933db1f56c43a4ae
[gitmo/Mouse.git] / t / 026-auto-deref.t
1 #!/usr/bin/env perl
2 use strict;
3 use warnings;
4 use Test::More tests => 13;
5 use Test::Exception;
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     );
22 };
23
24 my $obj;
25 lives_ok {
26     $obj = Class->new;
27 } qr/auto_deref without defaults don't explode on new/;
28
29 my ($array, @array, $hash, %hash);
30 lives_ok {
31     @array = $obj->array;
32     %hash  = $obj->hash;
33     $array = $obj->array;
34     $hash  = $obj->hash;
35
36     $obj->array;
37     $obj->hash;
38 } qr/auto_deref without default doesn't explode on get/;
39
40 is($obj->array, undef, "array without value is undef in scalar context");
41 is($obj->hash, undef, "hash without value is undef in scalar context");
42
43 is(@array, 0, "array without value is empty in list context");
44 is(keys %hash, 0, "hash without value is empty in list context");
45
46 @array = $obj->array([1, 2, 3]);
47 %hash  = $obj->hash({foo => 1, bar => 2});
48
49 is_deeply(\@array, [1, 2, 3], "setter returns the dereferenced list");
50 is_deeply(\%hash, {foo => 1, bar => 2}, "setter returns the dereferenced hash");
51
52 lives_ok {
53     @array = $obj->array;
54     %hash  = $obj->hash;
55     $array = $obj->array;
56     $hash  = $obj->hash;
57
58     $obj->array;
59     $obj->hash;
60 } qr/auto_deref without default doesn't explode on get/;
61
62 is_deeply($array, [1, 2, 3], "auto_deref in scalar context gives the reference");
63 is_deeply($hash, {foo => 1, bar => 2}, "auto_deref in scalar context gives the reference");
64
65 is_deeply(\@array, [1, 2, 3], "auto_deref in list context gives the list");
66 is_deeply(\%hash, {foo => 1, bar => 2}, "auto_deref in list context gives the hash");
67