Commit | Line | Data |
78cd1d3b |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
a28e50e4 |
6 | use Test::More; |
be0ed157 |
7 | use Test::Fatal; |
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 | |
be0ed157 |
49 | ::like ::exception { with 'FooRole' => { -version => 42 } }, |
2e7f6cf4 |
50 | qr/FooRole version 42 required--this is only version 23/, |
51 | 'applying role with unsatisfied version requirement'; |
52 | |
be0ed157 |
53 | ::ok ! ::exception { 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 | |
69 | my $foo_class_meta = FooClass->meta; |
0be0b30e |
70 | isa_ok( $foo_class_meta, 'Moose::Meta::Class' ); |
78cd1d3b |
71 | |
3f562dd9 |
72 | my $foobar_class_meta = FooBarClass->meta; |
0be0b30e |
73 | isa_ok( $foobar_class_meta, 'Moose::Meta::Class' ); |
3f562dd9 |
74 | |
be0ed157 |
75 | ok exception { |
0be0b30e |
76 | $foo_class_meta->does_role(); |
be0ed157 |
77 | }, |
0be0b30e |
78 | '... does_role requires a role name'; |
bbd2fe69 |
79 | |
be0ed157 |
80 | ok exception { |
e016145b |
81 | $foo_class_meta->add_role(); |
be0ed157 |
82 | }, |
0be0b30e |
83 | '... apply_role requires a role'; |
bbd2fe69 |
84 | |
be0ed157 |
85 | ok exception { |
e016145b |
86 | $foo_class_meta->add_role( bless( {} => 'Fail' ) ); |
be0ed157 |
87 | }, |
0be0b30e |
88 | '... apply_role requires a role'; |
bbd2fe69 |
89 | |
0be0b30e |
90 | ok( $foo_class_meta->does_role('FooRole'), |
91 | '... the FooClass->meta does_role FooRole' ); |
92 | ok( !$foo_class_meta->does_role('OtherRole'), |
93 | '... the FooClass->meta !does_role OtherRole' ); |
ef333f17 |
94 | |
0be0b30e |
95 | ok( $foobar_class_meta->does_role('FooRole'), |
96 | '... the FooBarClass->meta does_role FooRole' ); |
97 | ok( $foobar_class_meta->does_role('BarRole'), |
98 | '... the FooBarClass->meta does_role BarRole' ); |
99 | ok( !$foobar_class_meta->does_role('OtherRole'), |
100 | '... the FooBarClass->meta !does_role OtherRole' ); |
3f562dd9 |
101 | |
0558683c |
102 | foreach 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 |
109 | ok( !$foo_class_meta->has_method('woot'), |
110 | '... FooClass lacks the method woot' ); |
111 | ok( $foobar_class_meta->has_method('woot'), |
112 | '... FooBarClass has the method woot' ); |
3f562dd9 |
113 | |
78cd1d3b |
114 | foreach 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 |
121 | can_ok( 'FooClass', 'does' ); |
122 | ok( FooClass->does('FooRole'), '... the FooClass does FooRole' ); |
123 | ok( !FooClass->does('BarRole'), '... the FooClass does not do BarRole' ); |
124 | ok( !FooClass->does('OtherRole'), '... the FooClass does not do OtherRole' ); |
ef333f17 |
125 | |
0be0b30e |
126 | can_ok( 'FooBarClass', 'does' ); |
127 | ok( FooBarClass->does('FooRole'), '... the FooClass does FooRole' ); |
128 | ok( FooBarClass->does('BarRole'), '... the FooBarClass does FooBarRole' ); |
129 | ok( !FooBarClass->does('OtherRole'), |
130 | '... the FooBarClass does not do OtherRole' ); |
3f562dd9 |
131 | |
78cd1d3b |
132 | my $foo = FooClass->new(); |
0be0b30e |
133 | isa_ok( $foo, 'FooClass' ); |
78cd1d3b |
134 | |
3f562dd9 |
135 | my $foobar = FooBarClass->new(); |
0be0b30e |
136 | isa_ok( $foobar, 'FooBarClass' ); |
137 | |
138 | is( $foo->goo, 'FooClass::goo', '... got the right value of goo' ); |
139 | is( $foobar->goo, 'FooRole::goo', '... got the right value of goo' ); |
140 | |
141 | is( $foo->boo, 'FooRole::boo -> BarClass::boo', |
142 | '... got the right value from ->boo' ); |
143 | is( $foobar->boo, 'FooRole::boo -> FooRole::boo -> BarClass::boo', |
144 | '... got the right value from ->boo (double wrapped)' ); |
145 | |
146 | is( $foo->blau, 'FooRole::blau -> FooClass::blau', |
147 | '... got the right value from ->blau' ); |
148 | is( $foobar->blau, 'FooRole::blau -> FooRole::blau -> FooClass::blau', |
149 | '... got the right value from ->blau' ); |
150 | |
151 | foreach 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 | |
be0ed157 |
174 | ok exception { |
0be0b30e |
175 | $foo->baz(1); |
be0ed157 |
176 | }, |
0be0b30e |
177 | '... baz is a read-only accessor'; |
78cd1d3b |
178 | |
be0ed157 |
179 | ok exception { |
0be0b30e |
180 | $foo->bar(1); |
be0ed157 |
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 | |
be0ed157 |
187 | ok ! exception { |
0be0b30e |
188 | $foo->bar($foo2); |
be0ed157 |
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 | |
195 | done_testing; |