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