ROLES
[gitmo/Moose.git] / t / 041_role.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 17;
7 use Test::Exception;
8
9 BEGIN {  
10     use_ok('Moose::Role');               
11 }
12
13 {
14     package FooRole;
15     
16     use strict;
17     use warnings;
18     use Moose::Role;
19     
20     our $VERSION = '0.01';
21     
22     has 'bar' => (is => 'rw', isa => 'Foo');
23     has 'baz' => (is => 'ro');    
24     
25     sub foo { 'FooRole::foo' }
26     
27     before 'boo' => sub { "FooRole::boo:before" };
28 }
29
30 my $foo_role = FooRole->meta;
31 isa_ok($foo_role, 'Moose::Meta::Role');
32
33 isa_ok($foo_role->role_meta, 'Class::MOP::Class');
34
35 is($foo_role->name, 'FooRole', '... got the right name of FooRole');
36 is($foo_role->version, '0.01', '... got the right version of FooRole');
37
38 # methods ...
39
40 ok($foo_role->has_method('foo'), '... FooRole has the foo method');
41 is($foo_role->get_method('foo'), \&FooRole::foo, '... FooRole got the foo method');
42
43 isa_ok($foo_role->get_method('foo'), 'Moose::Meta::Role::Method');
44
45 is_deeply(
46     [ $foo_role->get_method_list() ],
47     [ 'foo' ],
48     '... got the right method list');
49     
50 # attributes ...
51
52 is_deeply(
53     [ sort $foo_role->get_attribute_list() ],
54     [ 'bar', 'baz' ],
55     '... got the right attribute list');
56
57 ok($foo_role->has_attribute('bar'), '... FooRole does have the bar attribute');
58
59 is_deeply(
60     $foo_role->get_attribute('bar'),
61     { is => 'rw', isa => 'Foo' },
62     '... got the correct description of the bar attribute');
63
64 ok($foo_role->has_attribute('baz'), '... FooRole does have the baz attribute');
65
66 is_deeply(
67     $foo_role->get_attribute('baz'),
68     { is => 'ro' },
69     '... got the correct description of the baz attribute');
70
71 # method modifiers
72
73 ok($foo_role->has_method_modifier('before' => 'boo'), '... now we have a boo:before modifier');
74 is($foo_role->get_method_modifier('before' => 'boo')->(), 
75     "FooRole::boo:before", 
76     '... got the right method back');
77
78 is_deeply(
79     [ $foo_role->get_method_modifier_list('before') ],
80     [ 'boo' ],
81     '... got the right list of before method modifiers');
82