Resolve a todo
[gitmo/Mouse.git] / t / 020_attributes / 008_attribute_type_unions.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 18;
7 use Test::Exception;
8
9
10
11 {
12     package Foo;
13     use Mouse;
14
15     has 'bar' => (is => 'rw', isa => 'ArrayRef | HashRef');
16 }
17
18 my $foo = Foo->new;
19 isa_ok($foo, 'Foo');
20
21 lives_ok {
22     $foo->bar([])
23 } '... set bar successfully with an ARRAY ref';
24
25 lives_ok {
26     $foo->bar({})
27 } '... set bar successfully with a HASH ref';
28
29 dies_ok {
30     $foo->bar(100)
31 } '... couldnt set bar successfully with a number';
32
33 dies_ok {
34     $foo->bar(sub {})
35 } '... couldnt set bar successfully with a CODE ref';
36
37 # check the constructor
38
39 lives_ok {
40     Foo->new(bar => [])
41 } '... created new Foo with bar successfully set with an ARRAY ref';
42
43 lives_ok {
44     Foo->new(bar => {})
45 } '... created new Foo with bar successfully set with a HASH ref';
46
47 dies_ok {
48     Foo->new(bar => 50)
49 } '... didnt create a new Foo with bar as a number';
50
51 dies_ok {
52     Foo->new(bar => sub {})
53 } '... didnt create a new Foo with bar as a CODE ref';
54
55 {
56     package Bar;
57     use Mouse;
58
59     has 'baz' => (is => 'rw', isa => 'Str | CodeRef');
60 }
61
62 my $bar = Bar->new;
63 isa_ok($bar, 'Bar');
64
65 lives_ok {
66     $bar->baz('a string')
67 } '... set baz successfully with a string';
68
69 lives_ok {
70     $bar->baz(sub { 'a sub' })
71 } '... set baz successfully with a CODE ref';
72
73 dies_ok {
74     $bar->baz(\(my $var1))
75 } '... couldnt set baz successfully with a SCALAR ref';
76
77 dies_ok {
78     $bar->baz({})
79 } '... couldnt set bar successfully with a HASH ref';
80
81 # check the constructor
82
83 lives_ok {
84     Bar->new(baz => 'a string')
85 } '... created new Bar with baz successfully set with a string';
86
87 lives_ok {
88     Bar->new(baz => sub { 'a sub' })
89 } '... created new Bar with baz successfully set with a CODE ref';
90
91 dies_ok {
92     Bar->new(baz => \(my $var2))
93 } '... didnt create a new Bar with baz as a number';
94
95 dies_ok {
96     Bar->new(baz => {})
97 } '... didnt create a new Bar with baz as a HASH ref';
98
99