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