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