Commit | Line | Data |
---|---|---|
c07af9d2 | 1 | #!/usr/bin/perl |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
a28e50e4 | 6 | use Test::More; |
53a4d826 | 7 | use Test::Exception; |
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 | ||
17 | my $foo = Foo->new; | |
18 | isa_ok($foo, 'Foo'); | |
19 | ||
53a4d826 | 20 | lives_ok { |
c07af9d2 | 21 | $foo->bar([]) |
53a4d826 | 22 | } '... set bar successfully with an ARRAY ref'; |
c07af9d2 | 23 | |
53a4d826 | 24 | lives_ok { |
c07af9d2 | 25 | $foo->bar({}) |
53a4d826 | 26 | } '... set bar successfully with a HASH ref'; |
c07af9d2 | 27 | |
53a4d826 | 28 | dies_ok { |
c07af9d2 | 29 | $foo->bar(100) |
53a4d826 | 30 | } '... couldnt set bar successfully with a number'; |
c07af9d2 | 31 | |
53a4d826 | 32 | dies_ok { |
c07af9d2 | 33 | $foo->bar(sub {}) |
53a4d826 | 34 | } '... couldnt set bar successfully with a CODE ref'; |
c07af9d2 | 35 | |
36 | # check the constructor | |
37 | ||
53a4d826 | 38 | lives_ok { |
c07af9d2 | 39 | Foo->new(bar => []) |
53a4d826 | 40 | } '... created new Foo with bar successfully set with an ARRAY ref'; |
c07af9d2 | 41 | |
53a4d826 | 42 | lives_ok { |
c07af9d2 | 43 | Foo->new(bar => {}) |
53a4d826 | 44 | } '... created new Foo with bar successfully set with a HASH ref'; |
c07af9d2 | 45 | |
53a4d826 | 46 | dies_ok { |
c07af9d2 | 47 | Foo->new(bar => 50) |
53a4d826 | 48 | } '... didnt create a new Foo with bar as a number'; |
c07af9d2 | 49 | |
53a4d826 | 50 | dies_ok { |
c07af9d2 | 51 | Foo->new(bar => sub {}) |
53a4d826 | 52 | } '... 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 | ||
61 | my $bar = Bar->new; | |
62 | isa_ok($bar, 'Bar'); | |
63 | ||
53a4d826 | 64 | lives_ok { |
2d2b92e5 | 65 | $bar->baz('a string') |
53a4d826 | 66 | } '... set baz successfully with a string'; |
2d2b92e5 | 67 | |
53a4d826 | 68 | lives_ok { |
2d2b92e5 | 69 | $bar->baz(sub { 'a sub' }) |
53a4d826 | 70 | } '... set baz successfully with a CODE ref'; |
2d2b92e5 | 71 | |
53a4d826 | 72 | dies_ok { |
2d2b92e5 | 73 | $bar->baz(\(my $var1)) |
53a4d826 | 74 | } '... couldnt set baz successfully with a SCALAR ref'; |
2d2b92e5 | 75 | |
53a4d826 | 76 | dies_ok { |
2d2b92e5 | 77 | $bar->baz({}) |
53a4d826 | 78 | } '... couldnt set bar successfully with a HASH ref'; |
2d2b92e5 | 79 | |
80 | # check the constructor | |
81 | ||
53a4d826 | 82 | lives_ok { |
2d2b92e5 | 83 | Bar->new(baz => 'a string') |
53a4d826 | 84 | } '... created new Bar with baz successfully set with a string'; |
2d2b92e5 | 85 | |
53a4d826 | 86 | lives_ok { |
2d2b92e5 | 87 | Bar->new(baz => sub { 'a sub' }) |
53a4d826 | 88 | } '... created new Bar with baz successfully set with a CODE ref'; |
2d2b92e5 | 89 | |
53a4d826 | 90 | dies_ok { |
2d2b92e5 | 91 | Bar->new(baz => \(my $var2)) |
53a4d826 | 92 | } '... didnt create a new Bar with baz as a number'; |
2d2b92e5 | 93 | |
53a4d826 | 94 | dies_ok { |
2d2b92e5 | 95 | Bar->new(baz => {}) |
53a4d826 | 96 | } '... didnt create a new Bar with baz as a HASH ref'; |
c07af9d2 | 97 | |
a28e50e4 | 98 | done_testing; |