6 use Test::More tests => 87;
14 has 'bar' => ( is => 'rw', isa => 'FooClass' );
15 has 'baz' => ( is => 'ro' );
17 sub goo {'FooRole::goo'}
18 sub foo {'FooRole::foo'}
20 override 'boo' => sub { 'FooRole::boo -> ' . super() };
22 around 'blau' => sub {
24 'FooRole::blau -> ' . $c->();
31 sub woot {'BarRole::woot'}
38 sub boo {'BarClass::boo'}
39 sub foo {'BarClass::foo'} # << the role overrides this ...
50 } qr/The FooClass class has implicitly overridden the method \(goo\) from role FooRole\. If this is intentional, please exclude the method from composition to silence this warning \(see Moose::Cookbook::Roles::Recipe2\)/;
52 sub blau {'FooClass::blau'} # << the role wraps this ...
54 sub goo {'FooClass::goo'} # << overrides the one from the role ...
62 with 'FooRole', 'BarRole';
65 my $foo_class_meta = FooClass->meta;
66 isa_ok( $foo_class_meta, 'Moose::Meta::Class' );
68 my $foobar_class_meta = FooBarClass->meta;
69 isa_ok( $foobar_class_meta, 'Moose::Meta::Class' );
72 $foo_class_meta->does_role();
74 '... does_role requires a role name';
77 $foo_class_meta->add_role();
79 '... apply_role requires a role';
82 $foo_class_meta->add_role( bless( {} => 'Fail' ) );
84 '... apply_role requires a role';
86 ok( $foo_class_meta->does_role('FooRole'),
87 '... the FooClass->meta does_role FooRole' );
88 ok( !$foo_class_meta->does_role('OtherRole'),
89 '... the FooClass->meta !does_role OtherRole' );
91 ok( $foobar_class_meta->does_role('FooRole'),
92 '... the FooBarClass->meta does_role FooRole' );
93 ok( $foobar_class_meta->does_role('BarRole'),
94 '... the FooBarClass->meta does_role BarRole' );
95 ok( !$foobar_class_meta->does_role('OtherRole'),
96 '... the FooBarClass->meta !does_role OtherRole' );
98 foreach my $method_name (qw(bar baz foo boo blau goo)) {
99 ok( $foo_class_meta->has_method($method_name),
100 '... FooClass has the method ' . $method_name );
101 ok( $foobar_class_meta->has_method($method_name),
102 '... FooBarClass has the method ' . $method_name );
105 ok( !$foo_class_meta->has_method('woot'),
106 '... FooClass lacks the method woot' );
107 ok( $foobar_class_meta->has_method('woot'),
108 '... FooBarClass has the method woot' );
110 foreach my $attr_name (qw(bar baz)) {
111 ok( $foo_class_meta->has_attribute($attr_name),
112 '... FooClass has the attribute ' . $attr_name );
113 ok( $foobar_class_meta->has_attribute($attr_name),
114 '... FooBarClass has the attribute ' . $attr_name );
117 can_ok( 'FooClass', 'does' );
118 ok( FooClass->does('FooRole'), '... the FooClass does FooRole' );
119 ok( !FooClass->does('BarRole'), '... the FooClass does not do BarRole' );
120 ok( !FooClass->does('OtherRole'), '... the FooClass does not do OtherRole' );
122 can_ok( 'FooBarClass', 'does' );
123 ok( FooBarClass->does('FooRole'), '... the FooClass does FooRole' );
124 ok( FooBarClass->does('BarRole'), '... the FooBarClass does FooBarRole' );
125 ok( !FooBarClass->does('OtherRole'),
126 '... the FooBarClass does not do OtherRole' );
128 my $foo = FooClass->new();
129 isa_ok( $foo, 'FooClass' );
131 my $foobar = FooBarClass->new();
132 isa_ok( $foobar, 'FooBarClass' );
134 is( $foo->goo, 'FooClass::goo', '... got the right value of goo' );
135 is( $foobar->goo, 'FooRole::goo', '... got the right value of goo' );
137 is( $foo->boo, 'FooRole::boo -> BarClass::boo',
138 '... got the right value from ->boo' );
139 is( $foobar->boo, 'FooRole::boo -> FooRole::boo -> BarClass::boo',
140 '... got the right value from ->boo (double wrapped)' );
142 is( $foo->blau, 'FooRole::blau -> FooClass::blau',
143 '... got the right value from ->blau' );
144 is( $foobar->blau, 'FooRole::blau -> FooRole::blau -> FooClass::blau',
145 '... got the right value from ->blau' );
147 foreach my $foo ( $foo, $foobar ) {
148 can_ok( $foo, 'does' );
149 ok( $foo->does('FooRole'), '... an instance of FooClass does FooRole' );
150 ok( !$foo->does('OtherRole'),
151 '... and instance of FooClass does not do OtherRole' );
153 can_ok( $foobar, 'does' );
154 ok( $foobar->does('FooRole'),
155 '... an instance of FooBarClass does FooRole' );
156 ok( $foobar->does('BarRole'),
157 '... an instance of FooBarClass does BarRole' );
158 ok( !$foobar->does('OtherRole'),
159 '... and instance of FooBarClass does not do OtherRole' );
161 for my $method (qw/bar baz foo boo goo blau/) {
162 can_ok( $foo, $method );
165 is( $foo->foo, 'FooRole::foo', '... got the right value of foo' );
167 ok( !defined( $foo->baz ), '... $foo->baz is undefined' );
168 ok( !defined( $foo->bar ), '... $foo->bar is undefined' );
173 '... baz is a read-only accessor';
178 '... bar is a read-write accessor with a type constraint';
180 my $foo2 = FooClass->new();
181 isa_ok( $foo2, 'FooClass' );
186 '... bar is a read-write accessor with a type constraint';
188 is( $foo->bar, $foo2, '... got the right value for bar now' );