does
[gitmo/Moose.git] / t / 042_apply_role.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 36;
7 use Test::Exception;
8
9 BEGIN {  
10     use_ok('Moose::Role');               
11 }
12
13 {
14     package FooRole;
15     use strict;
16     use warnings;
17     use Moose::Role;
18     
19     has 'bar' => (is => 'rw', isa => 'FooClass');
20     has 'baz' => (is => 'ro');    
21     
22     sub goo { 'FooRole::goo' }
23     sub foo { 'FooRole::foo' }
24     
25     override 'boo' => sub { 'FooRole::boo -> ' . super() };   
26     
27     around 'blau' => sub {  
28         my $c = shift;
29         'FooRole::blau -> ' . $c->();
30     }; 
31
32     package BarClass;
33     use strict;
34     use warnings;
35     use Moose;
36     
37     sub boo { 'BarClass::boo' }
38     sub foo { 'BarClass::foo' }  # << the role overrides this ...  
39     
40     package FooClass;
41     use strict;
42     use warnings;
43     use Moose;
44     
45     extends 'BarClass';
46        with 'FooRole';
47     
48     sub blau { 'FooClass::blau' }
49
50     sub goo { 'FooClass::goo' }  # << overrides the one from the role ... 
51 }
52
53 my $foo_class_meta = FooClass->meta;
54 isa_ok($foo_class_meta, 'Moose::Meta::Class');
55
56 ok($foo_class_meta->does_role('FooRole'), '... the FooClass->meta does_role FooRole');
57 ok(!$foo_class_meta->does_role('OtherRole'), '... the FooClass->meta !does_role OtherRole');
58
59 foreach my $method_name (qw(bar baz foo boo blau goo)) {
60     ok($foo_class_meta->has_method($method_name), '... FooClass has the method ' . $method_name);    
61 }
62
63 foreach my $attr_name (qw(bar baz)) {
64     ok($foo_class_meta->has_attribute($attr_name), '... FooClass has the attribute ' . $attr_name);    
65 }
66
67 can_ok('FooClass', 'does');
68 ok(FooClass->does('FooRole'), '... the FooClass does FooRole');
69 ok(!FooClass->does('OtherRole'), '... the FooClass does not do OtherRole');
70
71 my $foo = FooClass->new();
72 isa_ok($foo, 'FooClass');
73
74 can_ok($foo, 'does');
75 ok($foo->does('FooRole'), '... an instance of FooClass does FooRole');
76 ok(!$foo->does('OtherRole'), '... and instance of FooClass does not do OtherRole');
77
78 can_ok($foo, 'bar');
79 can_ok($foo, 'baz');
80 can_ok($foo, 'foo');
81 can_ok($foo, 'boo');
82 can_ok($foo, 'goo');
83 can_ok($foo, 'blau');
84
85 is($foo->foo, 'FooRole::foo', '... got the right value of foo');
86 is($foo->goo, 'FooClass::goo', '... got the right value of goo');
87
88 ok(!defined($foo->baz), '... $foo->baz is undefined');
89 ok(!defined($foo->bar), '... $foo->bar is undefined');
90
91 dies_ok {
92     $foo->baz(1)
93 } '... baz is a read-only accessor';
94
95 dies_ok {
96     $foo->bar(1)
97 } '... bar is a read-write accessor with a type constraint';
98
99 my $foo2 = FooClass->new();
100 isa_ok($foo2, 'FooClass');
101
102 lives_ok {
103     $foo->bar($foo2)
104 } '... bar is a read-write accessor with a type constraint';
105
106 is($foo->bar, $foo2, '... got the right value for bar now');
107
108 is($foo->boo, 'FooRole::boo -> BarClass::boo', '... got the right value from ->boo');
109 is($foo->blau, 'FooRole::blau -> FooClass::blau', '... got the right value from ->blau');
110