6 use Test::More tests => 87;
10 use_ok('Moose::Role');
17 has 'bar' => (is => 'rw', isa => 'FooClass');
18 has 'baz' => (is => 'ro');
20 sub goo { 'FooRole::goo' }
21 sub foo { 'FooRole::foo' }
23 override 'boo' => sub { 'FooRole::boo -> ' . super() };
25 around 'blau' => sub {
27 'FooRole::blau -> ' . $c->();
33 sub woot { 'BarRole::woot' }
39 sub boo { 'BarClass::boo' }
40 sub foo { 'BarClass::foo' } # << the role overrides this ...
42 metaclass->make_immutable(debug => 0);
51 sub blau { 'FooClass::blau' } # << the role wraps this ...
53 sub goo { 'FooClass::goo' } # << overrides the one from the role ...
55 metaclass->make_immutable(debug => 0);
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()
73 } '... does_role requires a role name';
76 $foo_class_meta->apply_role()
77 } '... apply_role requires a role';
80 $foo_class_meta->apply_role(bless({} => 'Fail'))
81 } '... apply_role requires a role';
83 ok($foo_class_meta->does_role('FooRole'), '... the FooClass->meta does_role FooRole');
84 ok(!$foo_class_meta->does_role('OtherRole'), '... the FooClass->meta !does_role OtherRole');
86 ok($foobar_class_meta->does_role('FooRole'), '... the FooBarClass->meta does_role FooRole');
87 ok($foobar_class_meta->does_role('BarRole'), '... the FooBarClass->meta does_role BarRole');
88 ok(!$foobar_class_meta->does_role('OtherRole'), '... the FooBarClass->meta !does_role OtherRole');
90 foreach my $method_name (qw(bar baz foo boo blau goo)) {
91 ok($foo_class_meta->has_method($method_name), '... FooClass has the method ' . $method_name);
92 ok($foobar_class_meta->has_method($method_name), '... FooBarClass has the method ' . $method_name);
95 ok(!$foo_class_meta->has_method('woot'), '... FooClass lacks the method woot');
96 ok($foobar_class_meta->has_method('woot'), '... FooBarClass has the method woot');
98 foreach my $attr_name (qw(bar baz)) {
99 ok($foo_class_meta->has_attribute($attr_name), '... FooClass has the attribute ' . $attr_name);
100 ok($foobar_class_meta->has_attribute($attr_name), '... FooBarClass has the attribute ' . $attr_name);
103 can_ok('FooClass', 'does');
104 ok(FooClass->does('FooRole'), '... the FooClass does FooRole');
105 ok(!FooClass->does('BarRole'), '... the FooClass does not do BarRole');
106 ok(!FooClass->does('OtherRole'), '... the FooClass does not do OtherRole');
108 can_ok('FooBarClass', 'does');
109 ok(FooBarClass->does('FooRole'), '... the FooClass does FooRole');
110 ok(FooBarClass->does('BarRole'), '... the FooBarClass does FooBarRole');
111 ok(!FooBarClass->does('OtherRole'), '... the FooBarClass does not do OtherRole');
113 my $foo = FooClass->new();
114 isa_ok($foo, 'FooClass');
116 my $foobar = FooBarClass->new();
117 isa_ok($foobar, 'FooBarClass');
119 is($foo->goo, 'FooClass::goo', '... got the right value of goo');
120 is($foobar->goo, 'FooRole::goo', '... got the right value of goo');
122 is($foo->boo, 'FooRole::boo -> BarClass::boo', '... got the right value from ->boo');
123 is($foobar->boo, 'FooRole::boo -> FooRole::boo -> BarClass::boo', '... got the right value from ->boo (double wrapped)');
125 is($foo->blau, 'FooRole::blau -> FooClass::blau', '... got the right value from ->blau');
126 is($foobar->blau, 'FooRole::blau -> FooRole::blau -> FooClass::blau', '... got the right value from ->blau');
128 foreach my $foo ($foo, $foobar) {
129 can_ok($foo, 'does');
130 ok($foo->does('FooRole'), '... an instance of FooClass does FooRole');
131 ok(!$foo->does('OtherRole'), '... and instance of FooClass does not do OtherRole');
133 can_ok($foobar, 'does');
134 ok($foobar->does('FooRole'), '... an instance of FooBarClass does FooRole');
135 ok($foobar->does('BarRole'), '... an instance of FooBarClass does BarRole');
136 ok(!$foobar->does('OtherRole'), '... and instance of FooBarClass does not do OtherRole');
138 for my $method (qw/bar baz foo boo goo blau/) {
139 can_ok($foo, $method);
142 is($foo->foo, 'FooRole::foo', '... got the right value of foo');
144 ok(!defined($foo->baz), '... $foo->baz is undefined');
145 ok(!defined($foo->bar), '... $foo->bar is undefined');
149 } '... baz is a read-only accessor';
153 } '... bar is a read-write accessor with a type constraint';
155 my $foo2 = FooClass->new();
156 isa_ok($foo2, 'FooClass');
160 } '... bar is a read-write accessor with a type constraint';
162 is($foo->bar, $foo2, '... got the right value for bar now');