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