Implementation of auto_deref
[gitmo/Mouse.git] / t / 026-auto-deref.t
CommitLineData
615d5d5f 1#!/usr/bin/env perl
2use strict;
3use warnings;
4use Test::More tests => 13;
5use Test::Exception;
6
7do {
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
24my $obj;
25lives_ok {
26 $obj = Class->new;
27} qr/auto_deref without defaults don't explode on new/;
28
29my ($array, @array, $hash, %hash);
30lives_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
40is($obj->array, undef, "array without value is undef in scalar context");
41is($obj->hash, undef, "hash without value is undef in scalar context");
42
3cf68001 43is(@array, 0, "array without value is empty in list context");
44is(keys %hash, 0, "hash without value is empty in list context");
615d5d5f 45
46@array = $obj->array([1, 2, 3]);
47%hash = $obj->hash({foo => 1, bar => 2});
48
3cf68001 49is_deeply(\@array, [1, 2, 3], "setter returns the dereferenced list");
50is_deeply(\%hash, {foo => 1, bar => 2}, "setter returns the dereferenced hash");
615d5d5f 51
52lives_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
62is_deeply($array, [1, 2, 3], "auto_deref in scalar context gives the reference");
63is_deeply($hash, {foo => 1, bar => 2}, "auto_deref in scalar context gives the reference");
64
3cf68001 65is_deeply(\@array, [1, 2, 3], "auto_deref in list context gives the list");
66is_deeply(\%hash, {foo => 1, bar => 2}, "auto_deref in list context gives the hash");
615d5d5f 67