f18af27db4de52ceb43b17dd17d293e4890080c9
[gitmo/Mouse.git] / t / 030_roles / 001_meta_role.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 26;
7 use Test::Exception;
8
9 use lib 't/lib';
10 use Test::Mouse; # Mouse::Meta::Module->version
11 use Mouse::Meta::Role;
12
13 {
14     package FooRole;
15
16     our $VERSION = '0.01';
17
18     sub foo { 'FooRole::foo' }
19 }
20
21 my $foo_role = Mouse::Meta::Role->initialize('FooRole');
22 isa_ok($foo_role, 'Mouse::Meta::Role');
23 #isa_ok($foo_role, 'Class::MOP::Module');
24
25 is($foo_role->name, 'FooRole', '... got the right name of FooRole');
26 is($foo_role->version, '0.01', '... got the right version of FooRole');
27
28 # methods ...
29
30 ok($foo_role->has_method('foo'), '... FooRole has the foo method');
31 is($foo_role->get_method('foo')->body, \&FooRole::foo, '... FooRole got the foo method');
32
33 isa_ok($foo_role->get_method('foo'), 'Mouse::Meta::Role::Method');
34
35 is_deeply(
36     [ $foo_role->get_method_list() ],
37     [ 'foo' ],
38     '... got the right method list');
39
40 # attributes ...
41
42 is_deeply(
43     [ $foo_role->get_attribute_list() ],
44     [],
45     '... got the right attribute list');
46
47 ok(!$foo_role->has_attribute('bar'), '... FooRole does not have the bar attribute');
48
49 lives_ok {
50     $foo_role->add_attribute('bar' => (is => 'rw', isa => 'Foo'));
51 } '... added the bar attribute okay';
52
53 is_deeply(
54     [ $foo_role->get_attribute_list() ],
55     [ 'bar' ],
56     '... got the right attribute list');
57
58 ok($foo_role->has_attribute('bar'), '... FooRole does have the bar attribute');
59
60 is_deeply(
61     join('|', %{$foo_role->get_attribute('bar')}),
62     join('|', %{+{ is => 'rw', isa => 'Foo' }}),
63     '... got the correct description of the bar attribute');
64
65 lives_ok {
66     $foo_role->add_attribute('baz' => (is => 'ro'));
67 } '... added the baz attribute okay';
68
69 is_deeply(
70     [ sort $foo_role->get_attribute_list() ],
71     [ 'bar', 'baz' ],
72     '... got the right attribute list');
73
74 ok($foo_role->has_attribute('baz'), '... FooRole does have the baz attribute');
75
76 is_deeply(
77     $foo_role->get_attribute('baz'),
78     { is => 'ro' },
79     '... got the correct description of the baz attribute');
80
81 lives_ok {
82     $foo_role->remove_attribute('bar');
83 } '... removed the bar attribute okay';
84
85 is_deeply(
86     [ $foo_role->get_attribute_list() ],
87     [ 'baz' ],
88     '... got the right attribute list');
89
90 ok(!$foo_role->has_attribute('bar'), '... FooRole does not have the bar attribute');
91 ok($foo_role->has_attribute('baz'), '... FooRole does still have the baz attribute');
92
93 # method modifiers
94
95 ok(!$foo_role->has_before_method_modifiers('boo'), '... no boo:before modifier');
96
97 my $method = sub { "FooRole::boo:before" };
98 lives_ok {
99     $foo_role->add_before_method_modifier('boo' => $method);
100 } '... added a method modifier okay';
101
102 ok($foo_role->has_before_method_modifiers('boo'), '... now we have a boo:before modifier');
103 is(($foo_role->get_before_method_modifiers('boo'))[0], $method, '... got the right method back');
104
105 is_deeply(
106     [ $foo_role->get_method_modifier_list('before') ],
107     [ 'boo' ],
108     '... got the right list of before method modifiers');