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