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