Reorganize t/020_attributes/
[gitmo/Mouse.git] / t / 020_attributes / 008_attribute_type_unions.t
CommitLineData
4060c871 1#!/usr/bin/perl
1f5ce14a 2# This is automatically generated by author/import-moose-test.pl.
3# DO NOT EDIT THIS FILE. ANY CHANGES WILL BE LOST!!!
4use t::lib::MooseCompat;
4060c871 5
6use strict;
7use warnings;
8
1f5ce14a 9use Test::More;
4060c871 10use Test::Exception;
11
12
4060c871 13{
14 package Foo;
15 use Mouse;
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 {})
55} '... didnt create a new Foo with bar as a CODE ref';
56
57{
58 package Bar;
59 use Mouse;
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';
100
1f5ce14a 101done_testing;