Commit | Line | Data |
e185c027 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
a28e50e4 |
6 | use Test::More; |
e185c027 |
7 | use Test::Exception; |
8 | |
7ff56534 |
9 | use Moose::Meta::Role; |
e185c027 |
10 | |
11 | { |
12 | package FooRole; |
d03bd989 |
13 | |
e185c027 |
14 | our $VERSION = '0.01'; |
d03bd989 |
15 | |
e185c027 |
16 | sub foo { 'FooRole::foo' } |
17 | } |
18 | |
68efb014 |
19 | my $foo_role = Moose::Meta::Role->initialize('FooRole'); |
e185c027 |
20 | isa_ok($foo_role, 'Moose::Meta::Role'); |
68efb014 |
21 | isa_ok($foo_role, 'Class::MOP::Module'); |
e185c027 |
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'); |
093b12c2 |
29 | is($foo_role->get_method('foo')->body, \&FooRole::foo, '... FooRole got the foo method'); |
e185c027 |
30 | |
a7d0cd00 |
31 | isa_ok($foo_role->get_method('foo'), 'Moose::Meta::Role::Method'); |
32 | |
e185c027 |
33 | is_deeply( |
34 | [ $foo_role->get_method_list() ], |
35 | [ 'foo' ], |
36 | '... got the right method list'); |
d03bd989 |
37 | |
e185c027 |
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 | |
721b5f29 |
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 | ); |
e185c027 |
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 | |
721b5f29 |
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' ); |
e185c027 |
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 | |
0558683c |
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'); |
a28e50e4 |
110 | |
111 | done_testing; |