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