DEATH TO ALL zionist ELLIPSES
[gitmo/Moose.git] / t / 030_roles / 003_apply_role.t
CommitLineData
78cd1d3b 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
f6bee6fe 6use Test::More tests => 86;
78cd1d3b 7use Test::Exception;
8
78cd1d3b 9{
10 package FooRole;
78cd1d3b 11 use Moose::Role;
0be0b30e 12
13 has 'bar' => ( is => 'rw', isa => 'FooClass' );
14 has 'baz' => ( is => 'ro' );
15
16 sub goo {'FooRole::goo'}
17 sub foo {'FooRole::foo'}
18
19 override 'boo' => sub { 'FooRole::boo -> ' . super() };
20
21 around 'blau' => sub {
0558683c 22 my $c = shift;
23 'FooRole::blau -> ' . $c->();
0be0b30e 24 };
25}
78cd1d3b 26
0be0b30e 27{
3f562dd9 28 package BarRole;
29 use Moose::Role;
0be0b30e 30 sub woot {'BarRole::woot'}
31}
32
33{
78cd1d3b 34 package BarClass;
78cd1d3b 35 use Moose;
8ecb1fa0 36
0be0b30e 37 sub boo {'BarClass::boo'}
38 sub foo {'BarClass::foo'} # << the role overrides this ...
0be0b30e 39}
40
41{
78cd1d3b 42 package FooClass;
78cd1d3b 43 use Moose;
0be0b30e 44
78cd1d3b 45 extends 'BarClass';
f6bee6fe 46 with 'FooRole';
0be0b30e 47
48 sub blau {'FooClass::blau'} # << the role wraps this ...
49
50 sub goo {'FooClass::goo'} # << overrides the one from the role ...
0be0b30e 51}
52
53{
3f562dd9 54 package FooBarClass;
55 use Moose;
0be0b30e 56
3f562dd9 57 extends 'FooClass';
0be0b30e 58 with 'FooRole', 'BarRole';
78cd1d3b 59}
60
61my $foo_class_meta = FooClass->meta;
0be0b30e 62isa_ok( $foo_class_meta, 'Moose::Meta::Class' );
78cd1d3b 63
3f562dd9 64my $foobar_class_meta = FooBarClass->meta;
0be0b30e 65isa_ok( $foobar_class_meta, 'Moose::Meta::Class' );
3f562dd9 66
bbd2fe69 67dies_ok {
0be0b30e 68 $foo_class_meta->does_role();
69}
1808c2da 70'does_role requires a role name';
bbd2fe69 71
72dies_ok {
e016145b 73 $foo_class_meta->add_role();
0be0b30e 74}
1808c2da 75'apply_role requires a role';
bbd2fe69 76
77dies_ok {
e016145b 78 $foo_class_meta->add_role( bless( {} => 'Fail' ) );
0be0b30e 79}
1808c2da 80'apply_role requires a role';
bbd2fe69 81
0be0b30e 82ok( $foo_class_meta->does_role('FooRole'),
1808c2da 83 'the FooClass->meta does_role FooRole' );
0be0b30e 84ok( !$foo_class_meta->does_role('OtherRole'),
1808c2da 85 'the FooClass->meta !does_role OtherRole' );
ef333f17 86
0be0b30e 87ok( $foobar_class_meta->does_role('FooRole'),
1808c2da 88 'the FooBarClass->meta does_role FooRole' );
0be0b30e 89ok( $foobar_class_meta->does_role('BarRole'),
1808c2da 90 'the FooBarClass->meta does_role BarRole' );
0be0b30e 91ok( !$foobar_class_meta->does_role('OtherRole'),
1808c2da 92 'the FooBarClass->meta !does_role OtherRole' );
3f562dd9 93
0558683c 94foreach my $method_name (qw(bar baz foo boo blau goo)) {
0be0b30e 95 ok( $foo_class_meta->has_method($method_name),
1808c2da 96 'FooClass has the method ' . $method_name );
0be0b30e 97 ok( $foobar_class_meta->has_method($method_name),
1808c2da 98 'FooBarClass has the method ' . $method_name );
78cd1d3b 99}
100
0be0b30e 101ok( !$foo_class_meta->has_method('woot'),
1808c2da 102 'FooClass lacks the method woot' );
0be0b30e 103ok( $foobar_class_meta->has_method('woot'),
1808c2da 104 'FooBarClass has the method woot' );
3f562dd9 105
78cd1d3b 106foreach my $attr_name (qw(bar baz)) {
0be0b30e 107 ok( $foo_class_meta->has_attribute($attr_name),
1808c2da 108 'FooClass has the attribute ' . $attr_name );
0be0b30e 109 ok( $foobar_class_meta->has_attribute($attr_name),
1808c2da 110 'FooBarClass has the attribute ' . $attr_name );
78cd1d3b 111}
112
0be0b30e 113can_ok( 'FooClass', 'does' );
1808c2da 114ok( FooClass->does('FooRole'), 'the FooClass does FooRole' );
115ok( !FooClass->does('BarRole'), 'the FooClass does not do BarRole' );
116ok( !FooClass->does('OtherRole'), 'the FooClass does not do OtherRole' );
ef333f17 117
0be0b30e 118can_ok( 'FooBarClass', 'does' );
1808c2da 119ok( FooBarClass->does('FooRole'), 'the FooClass does FooRole' );
120ok( FooBarClass->does('BarRole'), 'the FooBarClass does FooBarRole' );
0be0b30e 121ok( !FooBarClass->does('OtherRole'),
1808c2da 122 'the FooBarClass does not do OtherRole' );
3f562dd9 123
78cd1d3b 124my $foo = FooClass->new();
0be0b30e 125isa_ok( $foo, 'FooClass' );
78cd1d3b 126
3f562dd9 127my $foobar = FooBarClass->new();
0be0b30e 128isa_ok( $foobar, 'FooBarClass' );
129
1808c2da 130is( $foo->goo, 'FooClass::goo', 'got the right value of goo' );
131is( $foobar->goo, 'FooRole::goo', 'got the right value of goo' );
0be0b30e 132
133is( $foo->boo, 'FooRole::boo -> BarClass::boo',
1808c2da 134 'got the right value from ->boo' );
0be0b30e 135is( $foobar->boo, 'FooRole::boo -> FooRole::boo -> BarClass::boo',
1808c2da 136 'got the right value from ->boo (double wrapped)' );
0be0b30e 137
138is( $foo->blau, 'FooRole::blau -> FooClass::blau',
1808c2da 139 'got the right value from ->blau' );
0be0b30e 140is( $foobar->blau, 'FooRole::blau -> FooRole::blau -> FooClass::blau',
1808c2da 141 'got the right value from ->blau' );
0be0b30e 142
143foreach my $foo ( $foo, $foobar ) {
144 can_ok( $foo, 'does' );
1808c2da 145 ok( $foo->does('FooRole'), 'an instance of FooClass does FooRole' );
0be0b30e 146 ok( !$foo->does('OtherRole'),
1808c2da 147 'and instance of FooClass does not do OtherRole' );
0be0b30e 148
149 can_ok( $foobar, 'does' );
150 ok( $foobar->does('FooRole'),
1808c2da 151 'an instance of FooBarClass does FooRole' );
0be0b30e 152 ok( $foobar->does('BarRole'),
1808c2da 153 'an instance of FooBarClass does BarRole' );
0be0b30e 154 ok( !$foobar->does('OtherRole'),
1808c2da 155 'and instance of FooBarClass does not do OtherRole' );
78cd1d3b 156
fb1e11d5 157 for my $method (qw/bar baz foo boo goo blau/) {
0be0b30e 158 can_ok( $foo, $method );
fb1e11d5 159 }
78cd1d3b 160
1808c2da 161 is( $foo->foo, 'FooRole::foo', 'got the right value of foo' );
78cd1d3b 162
1808c2da 163 ok( !defined( $foo->baz ), '$foo->baz is undefined' );
164 ok( !defined( $foo->bar ), '$foo->bar is undefined' );
78cd1d3b 165
fb1e11d5 166 dies_ok {
0be0b30e 167 $foo->baz(1);
168 }
1808c2da 169 'baz is a read-only accessor';
78cd1d3b 170
fb1e11d5 171 dies_ok {
0be0b30e 172 $foo->bar(1);
173 }
1808c2da 174 'bar is a read-write accessor with a type constraint';
78cd1d3b 175
fb1e11d5 176 my $foo2 = FooClass->new();
0be0b30e 177 isa_ok( $foo2, 'FooClass' );
06b30515 178
fb1e11d5 179 lives_ok {
0be0b30e 180 $foo->bar($foo2);
181 }
1808c2da 182 'bar is a read-write accessor with a type constraint';
fb1e11d5 183
1808c2da 184 is( $foo->bar, $foo2, 'got the right value for bar now' );
fb1e11d5 185}