ROLES
[gitmo/Moose.git] / t / 040_meta_role.t
CommitLineData
e185c027 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More tests => 27;
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
36is_deeply(
37 [ $foo_role->get_method_list() ],
38 [ 'foo' ],
39 '... got the right method list');
40
41# attributes ...
42
43is_deeply(
44 [ $foo_role->get_attribute_list() ],
45 [],
46 '... got the right attribute list');
47
48ok(!$foo_role->has_attribute('bar'), '... FooRole does not have the bar attribute');
49
50lives_ok {
51 $foo_role->add_attribute('bar' => (is => 'rw', isa => 'Foo'));
52} '... added the bar attribute okay';
53
54is_deeply(
55 [ $foo_role->get_attribute_list() ],
56 [ 'bar' ],
57 '... got the right attribute list');
58
59ok($foo_role->has_attribute('bar'), '... FooRole does have the bar attribute');
60
61is_deeply(
62 $foo_role->get_attribute('bar'),
63 { is => 'rw', isa => 'Foo' },
64 '... got the correct description of the bar attribute');
65
66lives_ok {
67 $foo_role->add_attribute('baz' => (is => 'ro'));
68} '... added the baz attribute okay';
69
70is_deeply(
71 [ sort $foo_role->get_attribute_list() ],
72 [ 'bar', 'baz' ],
73 '... got the right attribute list');
74
75ok($foo_role->has_attribute('baz'), '... FooRole does have the baz attribute');
76
77is_deeply(
78 $foo_role->get_attribute('baz'),
79 { is => 'ro' },
80 '... got the correct description of the baz attribute');
81
82lives_ok {
83 $foo_role->remove_attribute('bar');
84} '... removed the bar attribute okay';
85
86is_deeply(
87 [ $foo_role->get_attribute_list() ],
88 [ 'baz' ],
89 '... got the right attribute list');
90
91ok(!$foo_role->has_attribute('bar'), '... FooRole does not have the bar attribute');
92ok($foo_role->has_attribute('baz'), '... FooRole does still have the baz attribute');
93
94# method modifiers
95
96ok(!$foo_role->has_method_modifier('before' => 'boo'), '... no boo:before modifier');
97
98my $method = sub { "FooRole::boo:before" };
99lives_ok {
100 $foo_role->add_method_modifier('before' => (
101 'boo' => $method
102 ));
103} '... added a method modifier okay';
104
105ok($foo_role->has_method_modifier('before' => 'boo'), '... now we have a boo:before modifier');
106is($foo_role->get_method_modifier('before' => 'boo'), $method, '... got the right method back');
107
108is_deeply(
109 [ $foo_role->get_method_modifier_list('before') ],
110 [ 'boo' ],
111 '... got the right list of before method modifiers');