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