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