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