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