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