basic Role::Tiny code
[gitmo/Moo.git] / t / simple.t
CommitLineData
ab3370e7 1use strictures 1;
2use Test::More qw(no_plan);
3use Test::Fatal;
4
5BEGIN {
6 package MyRole;
7
8 use Role::Tiny;
9
10 requires qw(req1 req2);
11
12 around foo => sub { my $orig = shift; join ' ', 'role foo', $orig->(@_) };
13
14 sub bar { 'role bar' }
15
16 sub baz { 'role baz' }
17}
18
19BEGIN {
20 package MyClass;
21
22 sub req1 { }
23 sub req2 { }
24 sub foo { 'class foo' }
25 sub baz { 'class baz' }
26
27}
28
29BEGIN {
30 package NoMethods;
31
32 package OneMethod;
33
34 sub req1 { }
35}
36
37sub try_apply_to {
38 my $to = shift;
39 exception { Role::Tiny->apply_role_to_package('MyRole', $to) }
40}
41
42is(try_apply_to('MyClass'), undef, 'role applies cleanly');
43is(MyClass->foo, 'role foo class foo', 'method modifier');
44is(MyClass->bar, 'role bar', 'method from role');
45is(MyClass->baz, 'class baz', 'method from class');
46
47like(try_apply_to('NoMethods'), qr/req1, req2/, 'error for both methods');
48like(try_apply_to('OneMethod'), qr/req2/, 'error for one method');