adding-basic-role-support
[gitmo/Moose.git] / t / 041_role.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 16;
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 is_deeply(
44     [ $foo_role->get_method_list() ],
45     [ 'foo' ],
46     '... got the right method list');
47     
48 # attributes ...
49
50 is_deeply(
51     [ sort $foo_role->get_attribute_list() ],
52     [ 'bar', 'baz' ],
53     '... got the right attribute list');
54
55 ok($foo_role->has_attribute('bar'), '... FooRole does have the bar attribute');
56
57 is_deeply(
58     $foo_role->get_attribute('bar'),
59     { is => 'rw', isa => 'Foo' },
60     '... got the correct description of the bar attribute');
61
62 ok($foo_role->has_attribute('baz'), '... FooRole does have the baz attribute');
63
64 is_deeply(
65     $foo_role->get_attribute('baz'),
66     { is => 'ro' },
67     '... got the correct description of the baz attribute');
68
69 # method modifiers
70
71 ok($foo_role->has_method_modifier('before' => 'boo'), '... now we have a boo:before modifier');
72 is($foo_role->get_method_modifier('before' => 'boo')->(), 
73     "FooRole::boo:before", 
74     '... got the right method back');
75
76 is_deeply(
77     [ $foo_role->get_method_modifier_list('before') ],
78     [ 'boo' ],
79     '... got the right list of before method modifiers');
80