Remove the redefine type tests for now, they'll go in a separate file
[gitmo/Mouse.git] / t / 026-auto-deref.t
CommitLineData
615d5d5f 1#!/usr/bin/env perl
2use strict;
3use warnings;
45ea8620 4use Test::More tests => 15;
eab81545 5use Test::Exception;
615d5d5f 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 );
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
39my $obj;
40lives_ok {
41 $obj = Class->new;
42} qr/auto_deref without defaults don't explode on new/;
43
44my ($array, @array, $hash, %hash);
45lives_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
55is($obj->array, undef, "array without value is undef in scalar context");
56is($obj->hash, undef, "hash without value is undef in scalar context");
57
3cf68001 58is(@array, 0, "array without value is empty in list context");
59is(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 64is_deeply(\@array, [1, 2, 3], "setter returns the dereferenced list");
65is_deeply(\%hash, {foo => 1, bar => 2}, "setter returns the dereferenced hash");
615d5d5f 66
67lives_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
77is_deeply($array, [1, 2, 3], "auto_deref in scalar context gives the reference");
78is_deeply($hash, {foo => 1, bar => 2}, "auto_deref in scalar context gives the reference");
79
3cf68001 80is_deeply(\@array, [1, 2, 3], "auto_deref in list context gives the list");
81is_deeply(\%hash, {foo => 1, bar => 2}, "auto_deref in list context gives the hash");
615d5d5f 82